KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > testsuite > impl > runtime > ContentHandlerAdaptor


1 //
2
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.3-b18-fcs
3
// See <a HREF="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
4
// Any modifications to this file will be lost upon recompilation of the source schema.
5
// Generated on: 2005.06.14 at 12:02:17 PDT
6
//
7

8 package com.sun.japex.testsuite.impl.runtime;
9
10 import java.util.ArrayList JavaDoc;
11
12 import org.xml.sax.Attributes JavaDoc;
13 import org.xml.sax.ContentHandler JavaDoc;
14 import org.xml.sax.Locator JavaDoc;
15 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc {
25
26     /** Stores newly declared prefix-URI mapping. */
27     private final ArrayList JavaDoc prefixMap = new ArrayList JavaDoc();
28     
29     /** Events will be sent to this object. */
30     private final XMLSerializer serializer;
31     
32     private final StringBuffer JavaDoc text = new StringBuffer JavaDoc();
33     
34     
35     public ContentHandlerAdaptor( XMLSerializer _serializer ) {
36         this.serializer = _serializer;
37     }
38     
39     
40
41     public void startDocument() throws SAXException JavaDoc {
42         prefixMap.clear();
43     }
44
45     public void endDocument() throws SAXException JavaDoc {
46     }
47
48     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
49         prefixMap.add(prefix);
50         prefixMap.add(uri);
51     }
52
53     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
54     }
55
56     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
57         throws SAXException JavaDoc {
58         
59         flushText();
60
61         int len = atts.getLength();
62         
63         serializer.startElement(namespaceURI,localName);
64         // declare namespace events
65
for( int i=0; i<len; i++ ) {
66             String JavaDoc qname = atts.getQName(i);
67             int idx = qname.indexOf(':');
68             String JavaDoc 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 JavaDoc prefix = (String JavaDoc)prefixMap.get(i);
75             serializer.getNamespaceContext().declareNamespace(
76                 (String JavaDoc)prefixMap.get(i+1),
77                 prefix,
78                 prefix.length()!=0 );
79         }
80         
81         serializer.endNamespaceDecls();
82         // fire attribute events
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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
93         flushText();
94         serializer.endElement();
95     }
96     
97     private void flushText() throws SAXException JavaDoc {
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 JavaDoc {
105         text.append(ch,start,length);
106     }
107
108     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc {
109         text.append(ch,start,length);
110     }
111
112
113
114     public void setDocumentLocator(Locator JavaDoc locator) {
115     }
116     
117     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
118     }
119
120     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
121     }
122
123 }
124
Popular Tags