1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38 public class ManualContentHandlerHelper {
39
40
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
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
85
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
95
96 public void endDocument() throws SAXException {
97 this.handler.endDocument();
98 }
99
100
101
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
110
111 public void endPrefixMapping(final String prefix) throws SAXException {
112 this.handler.endPrefixMapping(prefix);
113 }
114
115
116
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
124
125 public void processingInstruction(final String target, final String data) throws SAXException {
126 this.handler.processingInstruction(target, data);
127 }
128
129
130
131
132 public void setDocumentLocator(final Locator locator) {
133 this.handler.setDocumentLocator(locator);
134 }
135
136
137
138
139 public void skippedEntity(final String name) throws SAXException {
140 this.handler.skippedEntity(name);
141 }
142
143
144
145
146 public void startDocument() throws SAXException {
147 this.handler.startDocument();
148 }
149
150
151
152
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
161
162
163
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
171
172
173
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
181
182
183
184 public void startElement(final String uri, final String localName) throws SAXException {
185 startElement(uri, localName, localName, new AttributesImpl());
186 }
187
188
189
190
191
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
205
206
207 public void startElement(final QName qName) throws SAXException {
208 startElement(qName, new AttributesImpl());
209 }
210
211
212
213
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
223
224
225
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
235
236
237
238 public void putElement(final QName qName, final String value) throws SAXException {
239 putElement(qName, new AttributesImpl(), value);
240 }
241
242
243
244
245
246
247 public void putElement(final QName qName, final Attributes atts) throws SAXException {
248 startElement(qName, atts);
249 endElement();
250 }
251
252
253
254
255
256 public void putElement(final QName qName) throws SAXException {
257 startElement(qName, new AttributesImpl());
258 endElement();
259 }
260
261
262
263
264 public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
265 this.handler.startPrefixMapping(prefix, uri);
266 }
267
268 }