AbstractSimpleHandler.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.xml.handler.common;
017 
018 import org.qedeq.kernel.se.common.Plugin;
019 import org.qedeq.kernel.xml.common.XmlSyntaxException;
020 
021 
022 /**
023  * Simple handler that gets SAX parser events. These events were received by the
024  {@link org.qedeq.kernel.xml.handler.common.SaxDefaultHandler} and are delegated to the
025  * current {@link AbstractSimpleHandler}.
026  *
027  @author  Michael Meyling
028  */
029 public abstract class AbstractSimpleHandler {
030 
031     /** This handler gets the original SAX events. */
032     private final SaxDefaultHandler defaultHandler;
033 
034     /** Start tag for this handler .*/
035     private final String startTag;
036 
037     /**
038      * Constructor.
039      *
040      @param   defaultHandler  Original SAX event handler.
041      @param   startTag        Start tag for this handler.
042      */
043     public AbstractSimpleHandler(final SaxDefaultHandler defaultHandler, final String startTag) {
044         this.defaultHandler = defaultHandler;
045         this.startTag = startTag;
046     }
047 
048     /**
049      * Constructor.
050      *
051      @param   defaultHandler  Original SAX event handler.
052      */
053     public AbstractSimpleHandler(final SaxDefaultHandler defaultHandler) {
054         this.defaultHandler = defaultHandler;
055         this.startTag = null;
056     }
057 
058     /**
059      * Constructor, should be used for creating handlers within handlers.
060      *
061      @param   handler     Already existing simple handler.
062      @param   startTag    Start tag for this handler.
063      */
064     public AbstractSimpleHandler(final AbstractSimpleHandler handler, final String startTag) {
065         this.defaultHandler = handler.defaultHandler;
066         this.startTag = startTag;
067     }
068 
069     /**
070      * Constructor, should be used for creating handlers within handlers.
071      *
072      @param   handler Already existing simple handler.
073      */
074     public AbstractSimpleHandler(final AbstractSimpleHandler handler) {
075         this.defaultHandler = handler.defaultHandler;
076         this.startTag = null;
077     }
078 
079     /**
080      * Must be called before a handler should parse a new section.
081      */
082     public abstract void init();
083 
084     /**
085      * Called at begin of element <code>elementName</code>. Must be overwritten.
086      *
087      @param   elementName Tag name.
088      @param   attributes  Tag attributes.
089      @throws  XmlSyntaxException   There is a semantic error in this event occurrence.
090      */
091     public abstract void startElement(final String elementName, final SimpleAttributes attributes)
092         throws XmlSyntaxException;
093 
094     /**
095      * Called at end of element <code>elementName</code>. Must be overwritten.
096      *
097      @param   elementName Tag name.
098      @throws  XmlSyntaxException   There is a semantic error in this event occurrence.
099      */
100     public abstract void endElement(final String elementNamethrows 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 valuethrows 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 Plugin 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 }