View Javadoc

1   /*
2    * File: ForkingHandler.java
3    * Created: 14-dub-06 10:52:33 
4    *
5    * Copyright 2006 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (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   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  package net.sf.webmancer.util.xml.sax;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.xml.namespace.QName;
27  
28  import org.xml.sax.Attributes;
29  import org.xml.sax.ContentHandler;
30  import org.xml.sax.SAXException;
31  
32  /**
33   * @author michal
34   */
35  public class ForkingHandler extends DelegateContentHandler {
36      private Map<QName, ContentHandler> handlers;
37  
38      private ContentHandler defaultHandler;
39  
40      private QName inside;
41  
42      /**
43       * @param parent
44       */
45      public ForkingHandler(final ContentHandler defaultHandler) {
46          super(defaultHandler);
47          this.handlers = new HashMap<QName, ContentHandler>();
48          this.defaultHandler = defaultHandler;
49          this.inside = null;
50      }
51  
52      /**
53       * 
54       */
55      public ForkingHandler() {
56          this(null);
57      }
58  
59      /**
60       * @param qname
61       * @param contentHandler
62       */
63      public void addContentHandler(final QName qname, final ContentHandler contentHandler) {
64          this.handlers.put(qname, contentHandler);
65      }
66  
67      /**
68       * @param uri
69       * @param localName
70       * @param contentHandler
71       */
72      public void addContentHandler(final String uri, final String localName, final ContentHandler contentHandler) {
73          addContentHandler(new QName(uri, localName), contentHandler);
74      }
75  
76      /**
77       * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String,
78       *      org.xml.sax.Attributes)
79       */
80      @Override
81      public void startElement(final String uri, final String localName, final String qName, final Attributes atts) throws SAXException {
82          QName qname = new QName(uri, localName);
83          ContentHandler newHandler = this.handlers.get(qname);
84          if (newHandler != null) {
85              setTargetContentHandler(newHandler);
86              this.inside = qname;
87          }
88          super.startElement(uri, localName, qName, atts);
89      }
90  
91      /**
92       * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
93       */
94      @Override
95      public void endElement(final String uri, final String localName, final String qName) throws SAXException {
96          super.endElement(uri, localName, qName);
97          QName qname = new QName(uri, localName);
98          if (qname.equals(this.inside)) {
99              setTargetContentHandler(this.defaultHandler);
100             this.inside = null;
101         }
102     }
103 
104 }