| 1 | /* This file is part of the project "Hilbert II" - 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.se.base.list.Element; |
| 22 | import org.qedeq.kernel.se.base.list.ElementList; |
| 23 | import org.qedeq.kernel.se.dto.list.DefaultAtom; |
| 24 | import org.qedeq.kernel.se.dto.list.DefaultElementList; |
| 25 | import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler; |
| 26 | import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler; |
| 27 | import org.qedeq.kernel.xml.handler.common.SimpleAttributes; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Parse elements. For example formulas and terms are build of |
| 32 | * {@link org.qedeq.kernel.se.base.list.Element}s. |
| 33 | * <P> |
| 34 | * This handler knows nothing about special forms. It doesn't do any |
| 35 | * validating. It simply puts all attributes into string atoms and |
| 36 | * adds all sub elements. The element name is taken for the operator name. |
| 37 | * |
| 38 | * @author Michael Meyling |
| 39 | */ |
| 40 | public class ElementHandler extends AbstractSimpleHandler { |
| 41 | |
| 42 | /** Value object element. */ |
| 43 | private Element result; |
| 44 | |
| 45 | /** Element stack. */ |
| 46 | private final List elements; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Deals with elements. |
| 51 | * |
| 52 | * @param handler Parent handler. |
| 53 | */ |
| 54 | public ElementHandler(final AbstractSimpleHandler handler) { |
| 55 | super(handler); |
| 56 | elements = new ArrayList(20); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Deals with elements. |
| 61 | * |
| 62 | * @param handler Parent handler. |
| 63 | */ |
| 64 | public ElementHandler(final SaxDefaultHandler handler) { |
| 65 | super(handler); |
| 66 | elements = new ArrayList(20); |
| 67 | } |
| 68 | |
| 69 | public final void init() { |
| 70 | result = null; |
| 71 | elements.clear(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get parsed element. |
| 76 | * |
| 77 | * @return Parsed element. |
| 78 | */ |
| 79 | public final Element getElement() { |
| 80 | return result; |
| 81 | } |
| 82 | |
| 83 | public final void startElement(final String name, final SimpleAttributes attributes) { |
| 84 | final String[] values = attributes.getKeySortedStringValues(); |
| 85 | final ElementList element = new DefaultElementList(name); |
| 86 | for (int i = 0; i < values.length; i++) { |
| 87 | element.add(new DefaultAtom(values[i])); |
| 88 | } |
| 89 | elements.add(element); |
| 90 | } |
| 91 | |
| 92 | public final void endElement(final String name) { |
| 93 | ElementList last = (ElementList) elements.get(elements.size() - 1); |
| 94 | elements.remove(elements.size() - 1); |
| 95 | if (elements.size() > 0) { |
| 96 | ((ElementList) elements.get(elements.size() - 1)).add(last); |
| 97 | } else { |
| 98 | result = last; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public final void characters(final String name, final String data) { |
| 103 | ElementList last = (ElementList) elements.get(elements.size() - 1); |
| 104 | last.add(new DefaultAtom(data)); |
| 105 | } |
| 106 | |
| 107 | } |