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.list;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
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.SaxDefaultHandler;
24  import org.qedeq.kernel.xml.handler.common.SimpleAttributes;
25  
26  /**
27   * Parse unknown number of elements.
28   *
29   * @author  Michael Meyling
30   */
31  public class BasicHandler extends AbstractSimpleHandler {
32  
33      /** Our parsed elements. */
34      private List elements;
35  
36      /** Handles {@link org.qedeq.kernel.se.base.list.Element}s. */
37      private final ElementHandler elementHandler;
38  
39  
40      /**
41       * Deals with elements.
42       *
43       * @param   handler Parent handler.
44       */
45      public BasicHandler(final SaxDefaultHandler handler) {
46          super(handler, "basic");
47          elements = new ArrayList();
48          elementHandler = new ElementHandler(this);
49      }
50  
51      /**
52       * Handles formulas.
53       *
54       * @param   handler     Parent handler.
55       */
56      public BasicHandler(final AbstractSimpleHandler handler) {
57          super(handler, "basic");
58          elements = new ArrayList();
59          elementHandler = new ElementHandler(this);
60      }
61  
62      public final void init() {
63          elements.clear();
64      }
65  
66      /**
67       * Get parsed result.
68       *
69       * @return  List of elements.
70       */
71      public final List getElements() {
72          return elements;
73      }
74  
75      public final void startElement(final String name, final SimpleAttributes attributes)
76              throws XmlSyntaxException {
77          if (getStartTag().equals(name)) {
78              // nothing to do
79          } else if (getLevel() > 1) {
80              changeHandler(elementHandler, name, attributes);
81          } else {
82              throw XmlSyntaxException.createUnexpectedTagException(name);
83          }
84      }
85  
86      public final void endElement(final String name) throws XmlSyntaxException {
87          if (getStartTag().equals(name)) {
88              // nothing to do, we are ready!
89          } else if (getLevel() <= 1) {
90              throw XmlSyntaxException.createUnexpectedTagException(name);
91          } else {
92              elements.add(elementHandler.getElement());
93          }
94      }
95  }