| 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.common; |
| 17 | |
| 18 | import org.qedeq.kernel.se.common.ModuleService; |
| 19 | import org.qedeq.kernel.xml.common.XmlSyntaxException; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Simple handler that gets SAX parser events. These events were received by the |
| 24 | * {@link org.qedeq.kernel.xml.handler.common.SaxDefaultHandler} and are delegated to the |
| 25 | * current {@link AbstractSimpleHandler}. |
| 26 | * |
| 27 | * @author Michael Meyling |
| 28 | */ |
| 29 | public abstract class AbstractSimpleHandler { |
| 30 | |
| 31 | /** This handler gets the original SAX events. */ |
| 32 | private final SaxDefaultHandler defaultHandler; |
| 33 | |
| 34 | /** Start tag for this handler .*/ |
| 35 | private final String startTag; |
| 36 | |
| 37 | /** |
| 38 | * Constructor. |
| 39 | * |
| 40 | * @param defaultHandler Original SAX event handler. |
| 41 | * @param startTag Start tag for this handler. |
| 42 | */ |
| 43 | public AbstractSimpleHandler(final SaxDefaultHandler defaultHandler, final String startTag) { |
| 44 | this.defaultHandler = defaultHandler; |
| 45 | this.startTag = startTag; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Constructor. |
| 50 | * |
| 51 | * @param defaultHandler Original SAX event handler. |
| 52 | */ |
| 53 | public AbstractSimpleHandler(final SaxDefaultHandler defaultHandler) { |
| 54 | this.defaultHandler = defaultHandler; |
| 55 | this.startTag = null; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Constructor, should be used for creating handlers within handlers. |
| 60 | * |
| 61 | * @param handler Already existing simple handler. |
| 62 | * @param startTag Start tag for this handler. |
| 63 | */ |
| 64 | public AbstractSimpleHandler(final AbstractSimpleHandler handler, final String startTag) { |
| 65 | this.defaultHandler = handler.defaultHandler; |
| 66 | this.startTag = startTag; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Constructor, should be used for creating handlers within handlers. |
| 71 | * |
| 72 | * @param handler Already existing simple handler. |
| 73 | */ |
| 74 | public AbstractSimpleHandler(final AbstractSimpleHandler handler) { |
| 75 | this.defaultHandler = handler.defaultHandler; |
| 76 | this.startTag = null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Must be called before a handler should parse a new section. |
| 81 | */ |
| 82 | public abstract void init(); |
| 83 | |
| 84 | /** |
| 85 | * Called at begin of element <code>elementName</code>. Must be overwritten. |
| 86 | * |
| 87 | * @param elementName Tag name. |
| 88 | * @param attributes Tag attributes. |
| 89 | * @throws XmlSyntaxException There is a semantic error in this event occurrence. |
| 90 | */ |
| 91 | public abstract void startElement(final String elementName, final SimpleAttributes attributes) |
| 92 | throws XmlSyntaxException; |
| 93 | |
| 94 | /** |
| 95 | * Called at end of element <code>elementName</code>. Must be overwritten. |
| 96 | * |
| 97 | * @param elementName Tag name. |
| 98 | * @throws XmlSyntaxException There is a semantic error in this event occurrence. |
| 99 | */ |
| 100 | public abstract void endElement(final String elementName) throws XmlSyntaxException; |
| 101 | |
| 102 | /** |
| 103 | * Called at end of element <code>elementName</code>. Must be overwritten if you expect |
| 104 | * character data. |
| 105 | * |
| 106 | * @param elementName Tag name. |
| 107 | * @param value String value. |
| 108 | * @throws XmlSyntaxException There is a semantic error in this event occurrence. |
| 109 | */ |
| 110 | public void characters(final String elementName, final String value) throws XmlSyntaxException { |
| 111 | // default implementation |
| 112 | throw XmlSyntaxException.createUnexpectedTextDataException(elementName, value); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Change current handler to new one. The new handler gets automatically a |
| 117 | * <code>beginElement</code> event. |
| 118 | * |
| 119 | * @param newHandler Handler that gets all the events now. |
| 120 | * @param elementName Current element name. |
| 121 | * @param attributes Current element attributes. |
| 122 | * @throws XmlSyntaxException New handler detected semantical problems. |
| 123 | */ |
| 124 | public final void changeHandler(final AbstractSimpleHandler newHandler, |
| 125 | final String elementName, final SimpleAttributes attributes) |
| 126 | throws XmlSyntaxException { |
| 127 | if (newHandler.getStartTag() != null && !newHandler.getStartTag().equals(elementName)) { |
| 128 | throw new RuntimeException(newHandler.getClass().getName() + " has start tag \"" |
| 129 | + newHandler.getStartTag() + "\", but should start with tag \"" |
| 130 | + elementName + "\""); |
| 131 | } |
| 132 | defaultHandler.changeHandler(newHandler, elementName, attributes); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get current plugin we work for. |
| 137 | * |
| 138 | * @return Plugin in use. |
| 139 | */ |
| 140 | public final ModuleService getPlugin() { |
| 141 | return defaultHandler.getPlugin(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get current tag level. |
| 146 | * |
| 147 | * @return Current level. |
| 148 | */ |
| 149 | public final int getLevel() { |
| 150 | return defaultHandler.getLevel(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get start tag for this handler. Could be <code>null</code> if there is no specific start tag. |
| 155 | * |
| 156 | * @return Start tag. |
| 157 | */ |
| 158 | public final String getStartTag() { |
| 159 | return startTag; |
| 160 | } |
| 161 | |
| 162 | } |