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