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.Rule;
19  import org.qedeq.kernel.se.dto.module.RuleVo;
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  
24  
25  /**
26   * Parse a rule.
27   *
28   * @author  Michael Meyling
29   */
30  public class RuleHandler extends AbstractSimpleHandler {
31  
32      /** Handler for rule description. */
33      private final LatexListHandler descriptionHandler;
34  
35      /** Handle proofs. */
36      private final ProofHandler proofHandler;
37  
38      /** Handle rule changes. */
39      private final ChangedRuleHandler changedRuleHandler;
40  
41      /** Rule value object. */
42      private RuleVo rule;
43  
44      /**
45       * Deals with definitions.
46       *
47       * @param   handler Parent handler.
48       */
49      public RuleHandler(final AbstractSimpleHandler handler) {
50          super(handler, "RULE");
51          descriptionHandler = new LatexListHandler(this, "DESCRIPTION");
52          changedRuleHandler = new ChangedRuleHandler(this);
53          proofHandler = new ProofHandler(this);
54      }
55  
56      public final void init() {
57          rule = null;
58      }
59  
60      /**
61       * Get Rule.
62       *
63       * @return  Rule.
64       */
65      public final Rule getRule() {
66          return rule;
67      }
68  
69      public final void startElement(final String name, final SimpleAttributes attributes)
70              throws XmlSyntaxException {
71          if (getStartTag().equals(name)) {
72              rule = new RuleVo();
73              if (null != attributes.getString("name")) {
74                  rule.setName(attributes.getString("name"));
75              } else {
76                  throw XmlSyntaxException.createMissingAttributeException(name, "name");
77              }
78              if (null != attributes.getString("version")) {
79                  rule.setVersion(attributes.getString("version"));
80              } else {
81                  throw XmlSyntaxException.createMissingAttributeException(name, "version");
82              }
83          } else if ("LINK".equals(name)) {
84              if (null != attributes.getString("id")) {
85                  rule.addLink(attributes.getString("id"));
86              } else {
87                  throw XmlSyntaxException.createMissingAttributeException(name, "id");
88              }
89          } else if (descriptionHandler.getStartTag().equals(name)) {
90              changeHandler(descriptionHandler, name, attributes);
91          } else if (proofHandler.getStartTag().equals(name)) {
92              changeHandler(proofHandler, name, attributes);
93          } else if (changedRuleHandler.getStartTag().equals(name)) {
94              changeHandler(changedRuleHandler, name, attributes);
95          } else {
96              throw XmlSyntaxException.createUnexpectedTagException(name);
97          }
98      }
99  
100     public final void endElement(final String name) throws XmlSyntaxException {
101         if (getStartTag().equals(name)) {
102             // nothing to do
103         } else if ("LINK".equals(name)) {
104             // nothing to do
105         } else if (descriptionHandler.getStartTag().equals(name)) {
106             rule.setDescription(descriptionHandler.getLatexList());
107         } else if (proofHandler.getStartTag().equals(name)) {
108             rule.addProof(proofHandler.getProof());
109         } else if (changedRuleHandler.getStartTag().equals(name)) {
110             rule.addChangedRule(changedRuleHandler.getChangedRule());
111         } else {
112             throw XmlSyntaxException.createUnexpectedTagException(name);
113         }
114     }
115 
116 
117 }