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.list.Element;
19  import org.qedeq.kernel.se.base.module.Term;
20  import org.qedeq.kernel.se.dto.module.SubstFreeVo;
21  import org.qedeq.kernel.xml.common.XmlSyntaxException;
22  import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler;
23  import org.qedeq.kernel.xml.handler.common.SimpleAttributes;
24  import org.qedeq.kernel.xml.handler.list.ElementHandler;
25  
26  
27  /**
28   * Parse a Substitute Free Variable Rule usage.
29   *
30   * @author  Michael Meyling
31   */
32  public class SubstFreevarHandler extends AbstractSimpleHandler {
33  
34      /** Rule value object. */
35      private SubstFreeVo substFreevar;
36  
37      /** Reference to previously proved formula. */
38      private String ref;
39  
40      /** Subject variable we want to substitute. */
41      private Element subjectVariable;
42  
43      /** Replacement term. */
44      private Term substituteTerm;
45  
46      /** Handle terms. */
47      private final TermHandler termHandler;
48  
49      /** Handle elements. */
50      private final ElementHandler elementHandler;
51  
52      /**
53       * Deals with definitions.
54       *
55       * @param   handler Parent handler.
56       */
57      public SubstFreevarHandler(final AbstractSimpleHandler handler) {
58          super(handler, "SUBST_FREE");
59          termHandler = new TermHandler(this);
60          elementHandler = new ElementHandler(this);
61      }
62  
63      public final void init() {
64          substFreevar = null;
65          subjectVariable = null;
66          substituteTerm = null;
67          ref = null;
68      }
69  
70      /**
71       * Get Substitute Free Variable Rule usage.
72       *
73       * @return  Substitute Free Variable usage.
74       */
75      public final SubstFreeVo getSubstFreeVo() {
76          return substFreevar;
77      }
78  
79      public final void startElement(final String name, final SimpleAttributes attributes)
80              throws XmlSyntaxException {
81          if (getStartTag().equals(name)) {
82              ref = attributes.getString("ref");
83          } else if ("VAR".equals(name)) {
84              changeHandler(elementHandler, name, attributes);
85          } else if (termHandler.getStartTag().equals(name)) {
86              changeHandler(termHandler, name, attributes);
87          } else {
88              throw XmlSyntaxException.createUnexpectedTagException(name);
89          }
90      }
91  
92      public final void endElement(final String name) throws XmlSyntaxException {
93          if (getStartTag().equals(name)) {
94              substFreevar = new SubstFreeVo(ref, subjectVariable,
95                  (substituteTerm != null ? substituteTerm.getElement() : null));
96          } else if ("VAR".equals(name)) {
97              subjectVariable = elementHandler.getElement();
98          } else if (termHandler.getStartTag().equals(name)) {
99              substituteTerm = termHandler.getTerm();
100         } else {
101             throw XmlSyntaxException.createUnexpectedTagException(name);
102         }
103     }
104 
105 }