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.dto.module.RenameVo;
19  import org.qedeq.kernel.xml.common.XmlSyntaxException;
20  import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler;
21  import org.qedeq.kernel.xml.handler.common.SimpleAttributes;
22  import org.qedeq.kernel.xml.handler.list.ElementHandler;
23  
24  
25  /**
26   * Parse a Rename Bound Subject Variable Rule usage.
27   *
28   * @author  Michael Meyling
29   */
30  public class RenameHandler extends AbstractSimpleHandler {
31  
32      /** Rule value object. */
33      private RenameVo rename;
34  
35      /** Handle subject variables. */
36      private final ElementHandler subjectVariableHandler;
37  
38      /**
39       * Deals with definitions.
40       *
41       * @param   handler Parent handler.
42       */
43      public RenameHandler(final AbstractSimpleHandler handler) {
44          super(handler, "RENAME");
45          subjectVariableHandler = new ElementHandler(this);
46      }
47  
48      public final void init() {
49          rename = null;
50      }
51  
52      /**
53       * Get Rename Bound Subject Variable Rule usage.
54       *
55       * @return  Substitute Free Variable usage.
56       */
57      public final RenameVo getRenameVo() {
58          return rename;
59      }
60  
61      public final void startElement(final String name, final SimpleAttributes attributes)
62              throws XmlSyntaxException {
63          if (getStartTag().equals(name)) {
64              rename = new RenameVo();
65              final String ref = attributes.getString("ref");
66              if (ref != null) {
67                  rename.setReference(ref);
68              }
69              final Integer occurrence = attributes.getInteger("occurrence");
70              if (occurrence != null) {
71                  rename.setOccurrence(occurrence.intValue());
72              }
73          } else if ("VAR".equals(name)) {
74              changeHandler(subjectVariableHandler, name, attributes);
75          } else {
76              throw XmlSyntaxException.createUnexpectedTagException(name);
77          }
78      }
79  
80      public final void endElement(final String name) throws XmlSyntaxException {
81          if (getStartTag().equals(name)) {
82              // nothing to do
83          } else if ("VAR".equals(name)) {
84              if (rename != null && rename.getOriginalSubjectVariable() == null) {
85                  rename.setOriginalSubjectVariable(subjectVariableHandler.getElement());
86              } else if (rename != null) {
87                  rename.setReplacementSubjectVariable(subjectVariableHandler.getElement());
88              }
89          } else {
90              throw XmlSyntaxException.createUnexpectedTagException(name);
91          }
92      }
93  
94  }