1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.xml.sax; |
21 |
| |
22 |
| import org.apache.commons.logging.Log; |
23 |
| import org.apache.commons.logging.LogFactory; |
24 |
| import org.apache.xindice.util.XMLUtilities; |
25 |
| import org.apache.xindice.util.XindiceRuntimeException; |
26 |
| import org.xml.sax.Attributes; |
27 |
| import org.xml.sax.SAXException; |
28 |
| import org.xml.sax.helpers.DefaultHandler; |
29 |
| import org.xmldb.api.modules.XMLResource; |
30 |
| |
31 |
| import java.util.HashMap; |
32 |
| import java.util.Iterator; |
33 |
| import java.util.Map; |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| public class SetContentHandler extends DefaultHandler { |
48 |
| |
49 |
| private static final Log log = LogFactory.getLog(SetContentHandler.class); |
50 |
| |
51 |
| protected XMLResource resource = null; |
52 |
| protected StringBuffer newContent = null; |
53 |
| protected Map namespaces = null; |
54 |
| |
55 |
0
| public SetContentHandler(XMLResource resource) {
|
56 |
0
| this.resource = resource;
|
57 |
0
| namespaces = new HashMap();
|
58 |
| } |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
0
| public void startDocument()
|
67 |
| throws SAXException { |
68 |
0
| newContent = new StringBuffer();
|
69 |
0
| newContent.append("<?xml version=\"1.0\"?>");
|
70 |
| } |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
0
| public void endDocument()
|
80 |
| throws SAXException { |
81 |
0
| try {
|
82 |
0
| resource.setContent(newContent.toString());
|
83 |
| } catch (Exception e) { |
84 |
0
| log.warn("Exception in endDocument", e);
|
85 |
0
| throw new SAXException(e);
|
86 |
| } |
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
0
| public void startPrefixMapping(String prefix, String uri)
|
99 |
| throws SAXException { |
100 |
0
| namespaces.put(prefix, uri);
|
101 |
| } |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
0
| public void endPrefixMapping(String prefix)
|
112 |
| throws SAXException { |
113 |
0
| namespaces.remove(prefix);
|
114 |
| } |
115 |
| |
116 |
| |
117 |
0
| private String getLocalName(String qn) {
|
118 |
0
| if (qn.indexOf(':') != -1) {
|
119 |
0
| return qn.substring(qn.indexOf(':') + 1);
|
120 |
| } else { |
121 |
0
| return qn;
|
122 |
| } |
123 |
| } |
124 |
| |
125 |
0
| private String getQNameAtt(String uri, String localName) throws SAXException {
|
126 |
0
| if ("".equals(uri)) {
|
127 |
0
| return localName;
|
128 |
| } |
129 |
| |
130 |
| |
131 |
0
| String prefix = null;
|
132 |
0
| Iterator prefixes = namespaces.keySet().iterator();
|
133 |
0
| while (prefixes.hasNext()) {
|
134 |
0
| String key = (String) prefixes.next();
|
135 |
0
| if ((!("".equals(key))) && namespaces.get(key).equals(uri)) {
|
136 |
0
| prefix = key;
|
137 |
0
| break;
|
138 |
| } |
139 |
| } |
140 |
| |
141 |
0
| if (prefix == null) {
|
142 |
0
| throw new SAXException("No declared prefix for namespace '" +
|
143 |
| uri + "'."); |
144 |
| } |
145 |
| |
146 |
0
| return prefix + ":" + localName;
|
147 |
| } |
148 |
| |
149 |
| |
150 |
0
| private String getQNameElement(String uri, String localName) throws SAXException {
|
151 |
0
| if ("".equals(uri)) {
|
152 |
0
| if (namespaces.get("") != null) {
|
153 |
0
| throw new SAXException("default namespace is declared here!");
|
154 |
| } else { |
155 |
| |
156 |
0
| return localName;
|
157 |
| } |
158 |
| } |
159 |
| |
160 |
| |
161 |
0
| String prefix = null;
|
162 |
0
| Iterator prefixes = namespaces.keySet().iterator();
|
163 |
0
| while (prefixes.hasNext()) {
|
164 |
0
| String key = (String) prefixes.next();
|
165 |
0
| if (namespaces.get(key).equals(uri)) {
|
166 |
0
| prefix = key;
|
167 |
0
| break;
|
168 |
| } |
169 |
| } |
170 |
| |
171 |
0
| if (prefix == null) {
|
172 |
0
| throw new SAXException("No declared prefix for namespace '"
|
173 |
| + uri + "'."); |
174 |
| } |
175 |
| |
176 |
0
| return ("".equals(prefix) ? localName : prefix + ":" + localName);
|
177 |
| } |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
0
| public void startElement(String uri, String localName, String qName, Attributes attributes)
|
191 |
| throws SAXException { |
192 |
| |
193 |
0
| newContent.append("<");
|
194 |
| |
195 |
| |
196 |
0
| if ("".equals(qName)) {
|
197 |
0
| newContent.append(getQNameElement(uri, localName));
|
198 |
| } else { |
199 |
0
| newContent.append(qName);
|
200 |
| } |
201 |
| |
202 |
0
| for (int i = 0; i < attributes.getLength(); i++) {
|
203 |
0
| String qn = attributes.getQName(i);
|
204 |
| |
205 |
| |
206 |
0
| if ("".equals(qn)) {
|
207 |
0
| qn = getQNameAtt(attributes.getURI(i), attributes.getLocalName(i));
|
208 |
| } |
209 |
0
| newContent.append(" ");
|
210 |
0
| newContent.append(qn);
|
211 |
0
| newContent.append("=");
|
212 |
0
| newContent.append("\"");
|
213 |
0
| try {
|
214 |
0
| newContent.append(XMLUtilities.escape(attributes.getValue(i), true));
|
215 |
| } catch (XindiceRuntimeException e) { |
216 |
0
| throw new SAXException(e);
|
217 |
| } |
218 |
0
| newContent.append("\"");
|
219 |
| |
220 |
| |
221 |
0
| if (qn.equals("xmlns")) {
|
222 |
0
| namespaces.remove("");
|
223 |
| } |
224 |
| |
225 |
0
| if (qn.startsWith("xmlns:")) {
|
226 |
0
| String ln = getLocalName(qn);
|
227 |
0
| namespaces.remove(ln);
|
228 |
| } |
229 |
| } |
230 |
| |
231 |
0
| Iterator iter = namespaces.keySet().iterator();
|
232 |
0
| while (iter.hasNext()) {
|
233 |
0
| String key = (String) iter.next();
|
234 |
0
| newContent.append(" xmlns");
|
235 |
0
| if (key.length() > 0) {
|
236 |
0
| newContent.append(":");
|
237 |
0
| newContent.append(key);
|
238 |
| } |
239 |
0
| newContent.append("=");
|
240 |
0
| newContent.append("\"");
|
241 |
0
| newContent.append(namespaces.get(key));
|
242 |
0
| newContent.append("\"");
|
243 |
0
| namespaces.remove(key);
|
244 |
| } |
245 |
| |
246 |
0
| newContent.append(">");
|
247 |
| } |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
0
| public void endElement(String uri, String localName, String qName)
|
260 |
| throws SAXException { |
261 |
0
| newContent.append("</");
|
262 |
| |
263 |
0
| if ("".equals(qName)) {
|
264 |
0
| qName = getQNameElement(uri, localName);
|
265 |
| } |
266 |
0
| newContent.append(qName);
|
267 |
0
| newContent.append(">");
|
268 |
| } |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
0
| public void characters(char ch[], int start, int length) throws SAXException {
|
282 |
0
| try {
|
283 |
0
| newContent.append(XMLUtilities.escape(ch, start, length, true));
|
284 |
| } catch (XindiceRuntimeException e) { |
285 |
0
| throw new SAXException(e);
|
286 |
| } |
287 |
| } |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
0
| public void ignorableWhitespace(char ch[], int start, int length)
|
301 |
| throws SAXException { |
302 |
0
| int i = 0;
|
303 |
0
| while (i < length) {
|
304 |
0
| newContent.append(ch[start + i]);
|
305 |
0
| i++;
|
306 |
| } |
307 |
| } |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
0
| public void processingInstruction(String target, String data)
|
320 |
| throws SAXException { |
321 |
0
| newContent.append("<?");
|
322 |
0
| newContent.append(target);
|
323 |
0
| newContent.append(" ");
|
324 |
| |
325 |
0
| if (data != null) {
|
326 |
0
| newContent.append(data);
|
327 |
| } |
328 |
| |
329 |
0
| newContent.append("?>");
|
330 |
| } |
331 |
| |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
0
| public void skippedEntity(String name)
|
341 |
| throws SAXException { |
342 |
| |
343 |
| } |
344 |
| } |