1 57 package com.sun.org.apache.xerces.internal.util; 58 59 import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator; 60 import com.sun.org.apache.xerces.internal.jaxp.validation.WrappedSAXException; 61 import com.sun.org.apache.xerces.internal.xni.QName; 62 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 63 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 64 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 65 import com.sun.org.apache.xerces.internal.xni.XMLString; 66 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 67 import org.xml.sax.Attributes ; 68 import org.xml.sax.ContentHandler ; 69 import org.xml.sax.Locator ; 70 import org.xml.sax.SAXException ; 71 72 79 public class SAX2XNI implements ContentHandler , XMLDocumentSource { 80 public SAX2XNI( XMLDocumentHandler core ) { 81 this.fCore = core; 82 } 83 84 private XMLDocumentHandler fCore; 85 86 private final NamespaceSupport nsContext = new NamespaceSupport(); 87 private final SymbolTable symbolTable = new SymbolTable(); 88 89 90 public void setDocumentHandler(XMLDocumentHandler handler) { 91 fCore = handler; 92 } 93 94 public XMLDocumentHandler getDocumentHandler() { 95 return fCore; 96 } 97 98 99 public void startDocument() throws SAXException { 105 try { 106 nsContext.reset(); 107 108 XMLLocator xmlLocator; 109 if(locator==null) 110 xmlLocator=new SimpleLocator(null,null,-1,-1); 115 else 116 xmlLocator=new LocatorWrapper(locator); 117 118 fCore.startDocument( 119 xmlLocator, 120 null, 121 nsContext, 122 null); 123 } catch( WrappedSAXException e ) { 124 throw e.exception; 125 } 126 } 127 128 public void endDocument() throws SAXException { 129 try { 130 fCore.endDocument(null); 131 } catch( WrappedSAXException e ) { 132 throw e.exception; 133 } 134 } 135 136 public void startElement( String uri, String local, String qname, Attributes att ) throws SAXException { 137 try { 138 fCore.startElement(createQName(uri,local,qname),createAttributes(att),null); 139 } catch( WrappedSAXException e ) { 140 throw e.exception; 141 } 142 } 143 144 public void endElement( String uri, String local, String qname ) throws SAXException { 145 try { 146 fCore.endElement(createQName(uri,local,qname),null); 147 } catch( WrappedSAXException e ) { 148 throw e.exception; 149 } 150 } 151 152 public void characters( char[] buf, int offset, int len ) throws SAXException { 153 try { 154 fCore.characters(new XMLString(buf,offset,len),null); 155 } catch( WrappedSAXException e ) { 156 throw e.exception; 157 } 158 } 159 160 public void ignorableWhitespace( char[] buf, int offset, int len ) throws SAXException { 161 try { 162 fCore.ignorableWhitespace(new XMLString(buf,offset,len),null); 163 } catch( WrappedSAXException e ) { 164 throw e.exception; 165 } 166 } 167 168 public void startPrefixMapping( String prefix, String uri ) { 169 nsContext.pushContext(); 170 nsContext.declarePrefix(prefix,uri); 171 } 172 173 public void endPrefixMapping( String prefix ) { 174 nsContext.popContext(); 175 } 176 177 public void processingInstruction( String target, String data ) throws SAXException { 178 try { 179 fCore.processingInstruction( 180 symbolize(target),createXMLString(data),null); 181 } catch( WrappedSAXException e ) { 182 throw e.exception; 183 } 184 } 185 186 public void skippedEntity( String name ) { 187 } 188 189 private Locator locator; 190 public void setDocumentLocator( Locator _loc ) { 191 this.locator = _loc; 192 } 193 194 195 private QName createQName(String uri, String local, String raw) { 196 197 int idx = raw.indexOf(':'); 198 199 if( local.length()==0 ) { 200 uri = ""; 203 if(idx<0) 204 local = raw; 205 else 206 local = raw.substring(idx+1); 207 } 208 209 String prefix; 210 if (idx < 0) 211 prefix = null; 212 else 213 prefix = raw.substring(0, idx); 214 215 if (uri != null && uri.length() == 0) 216 uri = null; 218 return new QName(symbolize(prefix), symbolize(local), symbolize(raw), symbolize(uri)); 219 } 220 221 222 private String symbolize(String s) { 223 if (s == null) 224 return null; 225 else 226 return symbolTable.addSymbol(s); 227 } 228 229 private XMLString createXMLString(String str) { 230 233 return new XMLString(str.toCharArray(), 0, str.length()); 235 } 236 237 238 239 private final XMLAttributes xa = new XMLAttributesImpl(); 240 241 242 private XMLAttributes createAttributes(Attributes att) { 243 xa.removeAllAttributes(); 244 int len = att.getLength(); 245 for (int i = 0; i < len; i++) 246 xa.addAttribute( 247 createQName(att.getURI(i), att.getLocalName(i), att.getQName(i)), 248 att.getType(i), 249 att.getValue(i)); 250 return xa; 251 } 252 } 253 | Popular Tags |