View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.kernel.xml.handler.module;
17  
18  import org.qedeq.kernel.se.base.module.InitialPredicateDefinition;
19  import org.qedeq.kernel.se.dto.module.InitialPredicateDefinitionVo;
20  import org.qedeq.kernel.xml.common.XmlSyntaxException;
21  import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler;
22  import org.qedeq.kernel.xml.handler.common.SimpleAttributes;
23  import org.qedeq.kernel.xml.handler.list.ElementHandler;
24  
25  
26  /**
27   * Parse an initial predicate definition.
28   *
29   * @author  Michael Meyling
30   */
31  public class InitialPredicateDefinitionHandler extends AbstractSimpleHandler {
32  
33      /** Handler for an element. */
34      private final ElementHandler elementHandler;
35  
36      /** Handler for predicate description. */
37      private final LatexListHandler descriptionHandler;
38  
39      /** Definition value object. */
40      private InitialPredicateDefinitionVo definition;
41  
42      /** LaTeX pattern for the definition. */
43      private String latexPattern;
44  
45  
46      /**
47       * Deals with definitions.
48       *
49       * @param   handler Parent handler.
50       */
51      public InitialPredicateDefinitionHandler(final AbstractSimpleHandler handler) {
52          super(handler, "DEFINITION_PREDICATE_INITIAL");
53          elementHandler = new ElementHandler(this);
54          descriptionHandler = new LatexListHandler(this, "DESCRIPTION");
55      }
56  
57      public final void init() {
58          definition = null;
59          latexPattern = null;
60      }
61  
62      /**
63       * Get definition.
64       *
65       * @return  Definition.
66       */
67      public final InitialPredicateDefinition getInitialDefinition() {
68          return definition;
69      }
70  
71      public final void startElement(final String name, final SimpleAttributes attributes)
72              throws XmlSyntaxException {
73          if (getStartTag().equals(name)) {
74              definition = new InitialPredicateDefinitionVo();
75              definition.setArgumentNumber(attributes.getString("arguments"));
76              definition.setName(attributes.getString("name"));
77          } else if ("LATEXPATTERN".equals(name)) {
78              // nothing to do yet
79          } else if ("PREDCON".equals(name)) {
80              changeHandler(elementHandler, name, attributes);
81          } else if (descriptionHandler.getStartTag().equals(name)) {
82              changeHandler(descriptionHandler, name, attributes);
83          } else {
84              throw XmlSyntaxException.createUnexpectedTagException(name);
85          }
86      }
87  
88      public final void endElement(final String name) throws XmlSyntaxException {
89          if (getStartTag().equals(name)) {
90              // nothing to do
91          } else if ("LATEXPATTERN".equals(name)) {
92              definition.setLatexPattern(latexPattern);
93          } else if ("PREDCON".equals(name)) {
94              definition.setPredCon(elementHandler.getElement());
95          } else if (descriptionHandler.getStartTag().equals(name)) {
96              definition.setDescription(descriptionHandler.getLatexList());
97          } else {
98              throw XmlSyntaxException.createUnexpectedTagException(name);
99          }
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 }