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