InitialPredicateDefinitionHandler.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.xml.handler.module;
017 
018 import org.qedeq.kernel.se.base.module.InitialPredicateDefinition;
019 import org.qedeq.kernel.se.dto.module.InitialPredicateDefinitionVo;
020 import org.qedeq.kernel.xml.common.XmlSyntaxException;
021 import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler;
022 import org.qedeq.kernel.xml.handler.common.SimpleAttributes;
023 import org.qedeq.kernel.xml.handler.list.ElementHandler;
024 
025 
026 /**
027  * Parse an initial predicate definition.
028  *
029  @author  Michael Meyling
030  */
031 public class InitialPredicateDefinitionHandler extends AbstractSimpleHandler {
032 
033     /** Handler for an element. */
034     private final ElementHandler elementHandler;
035 
036     /** Handler for predicate description. */
037     private final LatexListHandler descriptionHandler;
038 
039     /** Definition value object. */
040     private InitialPredicateDefinitionVo definition;
041 
042     /** LaTeX pattern for the definition. */
043     private String latexPattern;
044 
045 
046     /**
047      * Deals with definitions.
048      *
049      @param   handler Parent handler.
050      */
051     public InitialPredicateDefinitionHandler(final AbstractSimpleHandler handler) {
052         super(handler, "DEFINITION_PREDICATE_INITIAL");
053         elementHandler = new ElementHandler(this);
054         descriptionHandler = new LatexListHandler(this, "DESCRIPTION");
055     }
056 
057     public final void init() {
058         definition = null;
059         latexPattern = null;
060     }
061 
062     /**
063      * Get definition.
064      *
065      @return  Definition.
066      */
067     public final InitialPredicateDefinition getInitialDefinition() {
068         return definition;
069     }
070 
071     public final void startElement(final String name, final SimpleAttributes attributes)
072             throws XmlSyntaxException {
073         if (getStartTag().equals(name)) {
074             definition = new InitialPredicateDefinitionVo();
075             definition.setArgumentNumber(attributes.getString("arguments"));
076             definition.setName(attributes.getString("name"));
077         else if ("LATEXPATTERN".equals(name)) {
078             // nothing to do yet
079         else if ("PREDCON".equals(name)) {
080             changeHandler(elementHandler, name, attributes);
081         else if (descriptionHandler.getStartTag().equals(name)) {
082             changeHandler(descriptionHandler, name, attributes);
083         else {
084             throw XmlSyntaxException.createUnexpectedTagException(name);
085         }
086     }
087 
088     public final void endElement(final String namethrows XmlSyntaxException {
089         if (getStartTag().equals(name)) {
090             // nothing to do
091         else if ("LATEXPATTERN".equals(name)) {
092             definition.setLatexPattern(latexPattern);
093         else if ("PREDCON".equals(name)) {
094             definition.setPredCon(elementHandler.getElement());
095         else if (descriptionHandler.getStartTag().equals(name)) {
096             definition.setDescription(descriptionHandler.getLatexList());
097         else {
098             throw XmlSyntaxException.createUnexpectedTagException(name);
099         }
100     }
101 
102     public final void characters(final String name, final String data) {
103         if ("LATEXPATTERN".equals(name)) {
104             latexPattern = data;
105         else {
106             throw new RuntimeException("Unexpected character data in tag: " + name);
107         }
108     }
109 
110 
111 }