|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractSimpleHandler.java | 50% | 85,7% | 87,5% | 83,3% |
|
||||||||||||||
| 1 | /* $Id: AbstractSimpleHandler.java,v 1.10 2008/03/27 05:16:29 m31 Exp $ | |
| 2 | * | |
| 3 | * This file is part of the project "Hilbert II" - http://www.qedeq.org | |
| 4 | * | |
| 5 | * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>. | |
| 6 | * | |
| 7 | * "Hilbert II" is free software; you can redistribute | |
| 8 | * it and/or modify it under the terms of the GNU General Public | |
| 9 | * License as published by the Free Software Foundation; either | |
| 10 | * version 2 of the License, or (at your option) any later version. | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | */ | |
| 17 | ||
| 18 | package org.qedeq.kernel.xml.parser; | |
| 19 | ||
| 20 | import org.qedeq.kernel.xml.common.XmlSyntaxException; | |
| 21 | ||
| 22 | ||
| 23 | /** | |
| 24 | * Simple handler that gets SAX parser events. These events were received by the | |
| 25 | * {@link org.qedeq.kernel.xml.parser.SaxDefaultHandler} and are delegated to the | |
| 26 | * current {@link AbstractSimpleHandler}. | |
| 27 | * | |
| 28 | * @version $Revision: 1.10 $ | |
| 29 | * @author Michael Meyling | |
| 30 | */ | |
| 31 | public abstract class AbstractSimpleHandler { | |
| 32 | ||
| 33 | /** This handler gets the original SAX events. */ | |
| 34 | private final SaxDefaultHandler defaultHandler; | |
| 35 | ||
| 36 | /** Start tag for this handler .*/ | |
| 37 | private final String startTag; | |
| 38 | ||
| 39 | /** | |
| 40 | * Constructor. | |
| 41 | * | |
| 42 | * @param defaultHandler Original SAX event handler. | |
| 43 | * @param startTag Start tag for this handler. | |
| 44 | */ | |
| 45 | 280 | public AbstractSimpleHandler(final SaxDefaultHandler defaultHandler, final String startTag) { |
| 46 | 280 | this.defaultHandler = defaultHandler; |
| 47 | 280 | this.startTag = startTag; |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * Constructor. | |
| 52 | * | |
| 53 | * @param defaultHandler Original SAX event handler. | |
| 54 | */ | |
| 55 | 137 | public AbstractSimpleHandler(final SaxDefaultHandler defaultHandler) { |
| 56 | 137 | this.defaultHandler = defaultHandler; |
| 57 | 137 | this.startTag = null; |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Constructor, should be used for creating handlers within handlers. | |
| 62 | * | |
| 63 | * @param handler Already existing simple handler. | |
| 64 | * @param startTag Start tag for this handler. | |
| 65 | */ | |
| 66 | 9065 | public AbstractSimpleHandler(final AbstractSimpleHandler handler, final String startTag) { |
| 67 | 9065 | this.defaultHandler = handler.defaultHandler; |
| 68 | 9065 | this.startTag = startTag; |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Constructor, should be used for creating handlers within handlers. | |
| 73 | * | |
| 74 | * @param handler Already existing simple handler. | |
| 75 | */ | |
| 76 | 1110 | public AbstractSimpleHandler(final AbstractSimpleHandler handler) { |
| 77 | 1110 | this.defaultHandler = handler.defaultHandler; |
| 78 | 1110 | this.startTag = null; |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Must be called before a handler should parse a new section. | |
| 83 | */ | |
| 84 | public abstract void init(); | |
| 85 | ||
| 86 | /** | |
| 87 | * Called at begin of element <code>elementName</code>. Must be overwritten. | |
| 88 | * | |
| 89 | * @param elementName Tag name. | |
| 90 | * @param attributes Tag attributes. | |
| 91 | * @throws XmlSyntaxException There is a semantic error in this event occurrence. | |
| 92 | */ | |
| 93 | public abstract void startElement(final String elementName, final SimpleAttributes attributes) | |
| 94 | throws XmlSyntaxException; | |
| 95 | ||
| 96 | /** | |
| 97 | * Called at end of element <code>elementName</code>. Must be overwritten. | |
| 98 | * | |
| 99 | * @param elementName Tag name. | |
| 100 | * @throws XmlSyntaxException There is a semantic error in this event occurrence. | |
| 101 | */ | |
| 102 | public abstract void endElement(final String elementName) throws XmlSyntaxException; | |
| 103 | ||
| 104 | /** | |
| 105 | * Called at end of element <code>elementName</code>. Must be overwritten if you expect | |
| 106 | * character data. | |
| 107 | * | |
| 108 | * @param elementName Tag name. | |
| 109 | * @param value String value. | |
| 110 | * @throws XmlSyntaxException There is a semantic error in this event occurrence. | |
| 111 | */ | |
| 112 | 0 | public void characters(final String elementName, final String value) throws XmlSyntaxException { |
| 113 | // default implementation | |
| 114 | 0 | throw XmlSyntaxException.createUnexpectedTextDataException(elementName, value); |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Change current handler to new one. The new handler gets automatically a | |
| 119 | * <code>beginElement</code> event. | |
| 120 | * | |
| 121 | * @param newHandler Handler that gets all the events now. | |
| 122 | * @param elementName Current element name. | |
| 123 | * @param attributes Current element attributes. | |
| 124 | * @throws XmlSyntaxException New handler detected semantical problems. | |
| 125 | */ | |
| 126 | 15424 | public final void changeHandler(final AbstractSimpleHandler newHandler, |
| 127 | final String elementName, final SimpleAttributes attributes) | |
| 128 | throws XmlSyntaxException { | |
| 129 | 15424 | if (newHandler.getStartTag() != null && !newHandler.getStartTag().equals(elementName)) { |
| 130 | 0 | throw new RuntimeException(newHandler.getClass().getName() + " has start tag \"" |
| 131 | + newHandler.getStartTag() + "\", but should start with tag \"" | |
| 132 | + elementName + "\""); | |
| 133 | } | |
| 134 | 15424 | defaultHandler.changeHandler(newHandler, elementName, attributes); |
| 135 | } | |
| 136 | ||
| 137 | /** | |
| 138 | * Get current tag level. | |
| 139 | * | |
| 140 | * @return Current level. | |
| 141 | */ | |
| 142 | 3800 | public final int getLevel() { |
| 143 | 3800 | return defaultHandler.getLevel(); |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * Get start tag for this handler. Could be <code>null</code> if there is no specific start tag. | |
| 148 | * | |
| 149 | * @return Start tag. | |
| 150 | */ | |
| 151 | 177890 | public final String getStartTag() { |
| 152 | 177890 | return startTag; |
| 153 | } | |
| 154 | ||
| 155 | } |
|
||||||||||