View Javadoc

1   /*
2    * File: ManualContentHandlerHelper.java
3    * Created: 3-čec-06 7:04:55 
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.Stack;
24  
25  import javax.xml.namespace.QName;
26  
27  import net.sf.webmancer.util.ContractChecker;
28  
29  import org.xml.sax.Attributes;
30  import org.xml.sax.ContentHandler;
31  import org.xml.sax.Locator;
32  import org.xml.sax.SAXException;
33  import org.xml.sax.helpers.AttributesImpl;
34  
35  /**
36   * @author Michal Burda
37   */
38  public class ManualContentHandlerHelper {
39      /**
40       * @author Michal Burda
41       */
42      private static class StackItem {
43          String uri;
44  
45          String localName;
46  
47          String qName;
48  
49          StackItem(final String uri, final String localName, final String qName) {
50              this.uri = uri;
51              this.localName = localName;
52              this.qName = qName;
53          }
54      }
55  
56      /**
57       * 
58       */
59      private ContentHandler handler;
60  
61      /**
62       * 
63       */
64      private Stack<StackItem> openElements;
65  
66      /**
67       * 
68       */
69      public ManualContentHandlerHelper(final ContentHandler handler) {
70          super();
71          ContractChecker.mustNotBeNull(handler, "handler");
72          this.handler = handler;
73          this.openElements = new Stack<StackItem>();
74      }
75  
76      /**
77       * @see org.xml.sax.ContentHandler#characters(char[], int, int)
78       */
79      public void characters(final char[] ch, final int start, final int length) throws SAXException {
80          this.handler.characters(ch, start, length);
81      }
82  
83      /**
84       * @param str
85       * @throws SAXException
86       */
87      public void characters(final String str) throws SAXException {
88          if (str != null) {
89              this.handler.characters(str.toCharArray(), 0, str.length());
90          }
91      }
92  
93      /**
94       * @see org.xml.sax.ContentHandler#endDocument()
95       */
96      public void endDocument() throws SAXException {
97          this.handler.endDocument();
98      }
99  
100     /**
101      * @throws SAXException
102      */
103     public void endElement() throws SAXException {
104         StackItem item = this.openElements.pop();
105         this.handler.endElement(item.uri, item.localName, item.qName);
106     }
107 
108     /**
109      * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
110      */
111     public void endPrefixMapping(final String prefix) throws SAXException {
112         this.handler.endPrefixMapping(prefix);
113     }
114 
115     /**
116      * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
117      */
118     public void ignorableWhitespace(final char[] ch, final int start, final int length) throws SAXException {
119         this.handler.ignorableWhitespace(ch, start, length);
120     }
121 
122     /**
123      * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
124      */
125     public void processingInstruction(final String target, final String data) throws SAXException {
126         this.handler.processingInstruction(target, data);
127     }
128 
129     /**
130      * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
131      */
132     public void setDocumentLocator(final Locator locator) {
133         this.handler.setDocumentLocator(locator);
134     }
135 
136     /**
137      * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
138      */
139     public void skippedEntity(final String name) throws SAXException {
140         this.handler.skippedEntity(name);
141     }
142 
143     /**
144      * @see org.xml.sax.ContentHandler#startDocument()
145      */
146     public void startDocument() throws SAXException {
147         this.handler.startDocument();
148     }
149 
150     /**
151      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String,
152      *      org.xml.sax.Attributes)
153      */
154     public void startElement(final String uri, final String localName, final String qName, final Attributes atts) throws SAXException {
155         this.handler.startElement(uri, localName, qName, atts);
156         this.openElements.push(new StackItem(uri, localName, qName));
157     }
158 
159     /**
160      * @param uri
161      * @param localName
162      * @param qName
163      * @throws SAXException
164      */
165     public void startElement(final String uri, final String localName, final String qName) throws SAXException {
166         startElement(uri, localName, qName, new AttributesImpl());
167     }
168 
169     /**
170      * @param uri
171      * @param localName
172      * @param atts
173      * @throws SAXException
174      */
175     public void startElement(final String uri, final String localName, final Attributes atts) throws SAXException {
176         startElement(uri, localName, localName, atts);
177     }
178 
179     /**
180      * @param uri
181      * @param localName
182      * @throws SAXException
183      */
184     public void startElement(final String uri, final String localName) throws SAXException {
185         startElement(uri, localName, localName, new AttributesImpl());
186     }
187 
188     /**
189      * @param qName
190      * @param atts
191      * @throws SAXException
192      */
193     public void startElement(final QName qName, final Attributes atts) throws SAXException {
194         StringBuffer qualified = new StringBuffer();
195         String prefix = qName.getPrefix();
196         if (prefix != null && prefix.length() > 0) {
197             qualified.append(prefix).append(':');
198         }
199         qualified.append(qName.getLocalPart());
200         startElement(qName.getNamespaceURI(), qName.getLocalPart(), qualified.toString(), atts);
201     }
202 
203     /**
204      * @param qName
205      * @throws SAXException
206      */
207     public void startElement(final QName qName) throws SAXException {
208         startElement(qName, new AttributesImpl());
209     }
210 
211     /**
212      * @param qName
213      * @throws SAXException
214      */
215     public void startElementWithNsDeclaration(final QName qName) throws SAXException {
216         AttributesImpl attrs = new AttributesImpl();
217         attrs.addAttribute("http://www.w3.org/2000/xmlns/", "xmlns", "xmlns", "CDATA", qName.getNamespaceURI());
218         startElement(qName, attrs);
219     }
220 
221     /**
222      * @param qName
223      * @param atts
224      * @param value
225      * @throws SAXException
226      */
227     public void putElement(final QName qName, final Attributes atts, final String value) throws SAXException {
228         startElement(qName, atts);
229         characters(value);
230         endElement();
231     }
232 
233     /**
234      * @param qName
235      * @param value
236      * @throws SAXException
237      */
238     public void putElement(final QName qName, final String value) throws SAXException {
239         putElement(qName, new AttributesImpl(), value);
240     }
241 
242     /**
243      * @param qName
244      * @param atts
245      * @throws SAXException
246      */
247     public void putElement(final QName qName, final Attributes atts) throws SAXException {
248         startElement(qName, atts);
249         endElement();
250     }
251 
252     /**
253      * @param qName
254      * @throws SAXException
255      */
256     public void putElement(final QName qName) throws SAXException {
257         startElement(qName, new AttributesImpl());
258         endElement();
259     }
260 
261     /**
262      * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
263      */
264     public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
265         this.handler.startPrefixMapping(prefix, uri);
266     }
267 
268 }