1 57 package com.sun.org.apache.xerces.internal.jaxp; 58 59 import com.sun.org.apache.xerces.internal.impl.xs.opti.DefaultXMLDocumentHandler; 60 import com.sun.org.apache.xerces.internal.util.AttributesProxy; 61 import com.sun.org.apache.xerces.internal.util.LocatorProxy; 62 import com.sun.org.apache.xerces.internal.xni.Augmentations; 63 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 64 import com.sun.org.apache.xerces.internal.xni.QName; 65 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 66 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 67 import com.sun.org.apache.xerces.internal.xni.XMLString; 68 import com.sun.org.apache.xerces.internal.xni.XNIException; 69 import org.xml.sax.ContentHandler ; 70 import org.xml.sax.SAXException ; 71 72 83 public class XNI2SAX extends DefaultXMLDocumentHandler { 84 85 private ContentHandler fContentHandler; 86 87 private String fVersion; 88 89 90 protected NamespaceContext fNamespaceContext; 91 92 95 private final AttributesProxy fAttributesProxy = new AttributesProxy(); 96 97 public void setContentHandler( ContentHandler handler ) { 98 this.fContentHandler = handler; 99 } 100 101 public ContentHandler getContentHandler() { 102 return fContentHandler; 103 } 104 105 106 public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) throws XNIException { 107 this.fVersion = version; 108 } 109 110 public void startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs) throws XNIException { 111 fNamespaceContext = namespaceContext; 112 fContentHandler.setDocumentLocator(new LocatorProxy(locator,fVersion)); 113 try { 114 fContentHandler.startDocument(); 115 } catch (SAXException e) { 116 throw new XNIException(e); 117 } 118 } 119 120 public void endDocument(Augmentations augs) throws XNIException { 121 try { 122 fContentHandler.endDocument(); 123 } catch (SAXException e) { 124 throw new XNIException(e); 125 } 126 } 127 128 public void processingInstruction(String target, XMLString data, Augmentations augs) throws XNIException { 129 try { 130 fContentHandler.processingInstruction(target,data.toString()); 131 } catch (SAXException e) { 132 throw new XNIException(e); 133 } 134 } 135 136 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException { 137 try { 138 int count = fNamespaceContext.getDeclaredPrefixCount(); 140 if (count > 0) { 141 String prefix = null; 142 String uri = null; 143 for (int i = 0; i < count; i++) { 144 prefix = fNamespaceContext.getDeclaredPrefixAt(i); 145 uri = fNamespaceContext.getURI(prefix); 146 fContentHandler.startPrefixMapping(prefix, (uri == null)?"":uri); 147 } 148 } 149 150 String uri = element.uri != null ? element.uri : ""; 151 String localpart = element.localpart; 152 fAttributesProxy.setAttributes(attributes); 153 fContentHandler.startElement(uri, localpart, element.rawname, fAttributesProxy); 154 } catch( SAXException e ) { 155 throw new XNIException(e); 156 } 157 } 158 159 public void endElement(QName element, Augmentations augs) throws XNIException { 160 try { 161 String uri = element.uri != null ? element.uri : ""; 162 String localpart = element.localpart; 163 fContentHandler.endElement(uri, localpart, element.rawname); 164 165 int count = fNamespaceContext.getDeclaredPrefixCount(); 167 if (count > 0) { 168 for (int i = 0; i < count; i++) { 169 fContentHandler.endPrefixMapping(fNamespaceContext.getDeclaredPrefixAt(i)); 170 } 171 } 172 } catch( SAXException e ) { 173 throw new XNIException(e); 174 } 175 } 176 177 178 179 180 181 public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException { 182 startElement(element,attributes,augs); 183 endElement(element,augs); 184 } 185 186 public void characters(XMLString text, Augmentations augs) throws XNIException { 187 try { 188 fContentHandler.characters(text.ch,text.offset,text.length); 189 } catch (SAXException e) { 190 throw new XNIException(e); 191 } 192 } 193 194 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 195 try { 196 fContentHandler.ignorableWhitespace(text.ch,text.offset,text.length); 197 } catch (SAXException e) { 198 throw new XNIException(e); 199 } 200 } 201 202 203 } 204 | Popular Tags |