1
2
3
4
5
6
7
8 package de.nierbeck.timeTrack.model.impl.runtime;
9
10 import java.util.ArrayList;
11
12 import org.xml.sax.Attributes;
13 import org.xml.sax.ContentHandler;
14 import org.xml.sax.Locator;
15 import org.xml.sax.SAXException;
16
17 /***
18 * Receives SAX2 events and send the equivalent events to
19 * {@link com.sun.xml.bind.serializer.XMLSerializer}
20 *
21 * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
22 */
23 public class ContentHandlerAdaptor implements ContentHandler {
24
25 /*** Stores newly declared prefix-URI mapping. */
26 private final ArrayList prefixMap = new ArrayList();
27
28 /*** Events will be sent to this object. */
29 private final XMLSerializer serializer;
30
31 private final StringBuffer text = new StringBuffer();
32
33 public ContentHandlerAdaptor(XMLSerializer _serializer) {
34 this.serializer = _serializer;
35 }
36
37 public void startDocument() throws SAXException {
38 prefixMap.clear();
39 }
40
41 public void endDocument() throws SAXException {
42 }
43
44 public void startPrefixMapping(String prefix, String uri)
45 throws SAXException {
46 prefixMap.add(prefix);
47 prefixMap.add(uri);
48 }
49
50 public void endPrefixMapping(String prefix) throws SAXException {
51 }
52
53 public void startElement(String namespaceURI, String localName,
54 String qName, Attributes atts) throws SAXException {
55
56 flushText();
57
58 int len = atts.getLength();
59
60 serializer.startElement(namespaceURI, localName);
61
62 for (int i = 0; i < len; i++) {
63 String qname = atts.getQName(i);
64 int idx = qname.indexOf(':');
65 String prefix = (idx == -1) ? qname : qname.substring(0, idx);
66
67 serializer.getNamespaceContext().declareNamespace(atts.getURI(i),
68 prefix, true);
69 }
70 for (int i = 0; i < prefixMap.size(); i += 2) {
71 String prefix = (String) prefixMap.get(i);
72 serializer.getNamespaceContext()
73 .declareNamespace((String) prefixMap.get(i + 1), prefix,
74 prefix.length() != 0);
75 }
76
77 serializer.endNamespaceDecls();
78
79 for (int i = 0; i < len; i++) {
80 serializer.startAttribute(atts.getURI(i), atts.getLocalName(i));
81 serializer.text(atts.getValue(i), null);
82 serializer.endAttribute();
83 }
84 prefixMap.clear();
85 serializer.endAttributes();
86 }
87
88 public void endElement(String namespaceURI, String localName, String qName)
89 throws SAXException {
90 flushText();
91 serializer.endElement();
92 }
93
94 private void flushText() throws SAXException {
95 if (text.length() != 0) {
96 serializer.text(text.toString(), null);
97 text.setLength(0);
98 }
99 }
100
101 public void characters(char[] ch, int start, int length)
102 throws SAXException {
103 text.append(ch, start, length);
104 }
105
106 public void ignorableWhitespace(char[] ch, int start, int length)
107 throws SAXException {
108 text.append(ch, start, length);
109 }
110
111 public void setDocumentLocator(Locator locator) {
112 }
113
114 public void processingInstruction(String target, String data)
115 throws SAXException {
116 }
117
118 public void skippedEntity(String name) throws SAXException {
119 }
120
121 }