1 16 17 package org.apache.xerces.impl.xs.traversers; 18 19 import org.apache.xerces.impl.xs.opti.SchemaDOMParser; 20 import org.apache.xerces.util.NamespaceSupport; 21 import org.apache.xerces.util.SAXLocatorWrapper; 22 import org.apache.xerces.util.SymbolTable; 23 import org.apache.xerces.util.XMLAttributesImpl; 24 import org.apache.xerces.util.XMLSymbols; 25 import org.apache.xerces.xni.NamespaceContext; 26 import org.apache.xerces.xni.QName; 27 import org.apache.xerces.xni.XMLString; 28 import org.apache.xerces.xni.XNIException; 29 import org.apache.xerces.xni.parser.XMLParseException; 30 import org.w3c.dom.Document ; 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.Locator ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.SAXParseException ; 36 import org.xml.sax.helpers.LocatorImpl ; 37 38 49 final class SchemaContentHandler implements ContentHandler { 50 51 52 private SymbolTable fSymbolTable; 53 54 55 private SchemaDOMParser fSchemaDOMParser; 56 57 58 private final SAXLocatorWrapper fSAXLocatorWrapper = new SAXLocatorWrapper(); 59 60 61 private NamespaceSupport fNamespaceContext = new NamespaceSupport(); 62 63 64 private boolean fNeedPushNSContext; 65 66 67 private boolean fNamespacePrefixes = false; 68 69 70 private boolean fStringsInternalized = false; 71 72 73 private final QName fElementQName = new QName(); 74 private final QName fAttributeQName = new QName(); 75 private final XMLAttributesImpl fAttributes = new XMLAttributesImpl(); 76 private final XMLString fTempString = new XMLString(); 77 78 81 public SchemaContentHandler() {} 82 83 86 public Document getDocument() { 87 return fSchemaDOMParser.getDocument(); 88 } 89 90 93 public void setDocumentLocator(Locator locator) { 94 fSAXLocatorWrapper.setLocator(locator); 95 } 96 97 100 public void startDocument() throws SAXException { 101 fNeedPushNSContext = true; 102 try { 103 fSchemaDOMParser.startDocument(fSAXLocatorWrapper, null, fNamespaceContext, null); 104 } 105 catch (XMLParseException e) { 106 convertToSAXParseException(e); 107 } 108 catch (XNIException e) { 109 convertToSAXException(e); 110 } 111 } 112 113 116 public void endDocument() throws SAXException { 117 fSAXLocatorWrapper.setLocator(null); 118 try { 119 fSchemaDOMParser.endDocument(null); 120 } 121 catch (XMLParseException e) { 122 convertToSAXParseException(e); 123 } 124 catch (XNIException e) { 125 convertToSAXException(e); 126 } 127 } 128 129 132 public void startPrefixMapping(String prefix, String uri) throws SAXException { 133 if (fNeedPushNSContext) { 134 fNeedPushNSContext = false; 135 fNamespaceContext.pushContext(); 136 } 137 if (!fStringsInternalized) { 138 prefix = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING; 139 uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null; 140 } 141 else { 142 if (prefix == null) { 143 prefix = XMLSymbols.EMPTY_STRING; 144 } 145 if (uri != null && uri.length() == 0) { 146 uri = null; 147 } 148 } 149 fNamespaceContext.declarePrefix(prefix, uri); 150 } 151 152 155 public void endPrefixMapping(String prefix) throws SAXException { 156 } 158 159 162 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 163 if (fNeedPushNSContext) { 164 fNamespaceContext.pushContext(); 165 } 166 fNeedPushNSContext = true; 167 168 fillQName(fElementQName, uri, localName, qName); 170 fillXMLAttributes(atts); 171 172 if (!fNamespacePrefixes) { 174 final int prefixCount = fNamespaceContext.getDeclaredPrefixCount(); 175 if (prefixCount > 0) { 176 addNamespaceDeclarations(prefixCount); 177 } 178 } 179 180 try { 181 fSchemaDOMParser.startElement(fElementQName, fAttributes, null); 182 } 183 catch (XMLParseException e) { 184 convertToSAXParseException(e); 185 } 186 catch (XNIException e) { 187 convertToSAXException(e); 188 } 189 } 190 191 194 public void endElement(String uri, String localName, String qName) throws SAXException { 195 fillQName(fElementQName, uri, localName, qName); 196 try { 197 fSchemaDOMParser.endElement(fElementQName, null); 198 } 199 catch (XMLParseException e) { 200 convertToSAXParseException(e); 201 } 202 catch (XNIException e) { 203 convertToSAXException(e); 204 } 205 finally { 206 fNamespaceContext.popContext(); 207 } 208 } 209 210 213 public void characters(char[] ch, int start, int length) throws SAXException { 214 try { 215 fTempString.setValues(ch, start, length); 216 fSchemaDOMParser.characters(fTempString, null); 217 } 218 catch (XMLParseException e) { 219 convertToSAXParseException(e); 220 } 221 catch (XNIException e) { 222 convertToSAXException(e); 223 } 224 } 225 226 229 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 230 try { 231 fTempString.setValues(ch, start, length); 232 fSchemaDOMParser.ignorableWhitespace(fTempString, null); 233 } 234 catch (XMLParseException e) { 235 convertToSAXParseException(e); 236 } 237 catch (XNIException e) { 238 convertToSAXException(e); 239 } 240 } 241 242 245 public void processingInstruction(String target, String data) throws SAXException { 246 try { 247 fTempString.setValues(data.toCharArray(), 0, data.length()); 248 fSchemaDOMParser.processingInstruction(target, fTempString, null); 249 } 250 catch (XMLParseException e) { 251 convertToSAXParseException(e); 252 } 253 catch (XNIException e) { 254 convertToSAXException(e); 255 } 256 } 257 258 261 public void skippedEntity(String arg) throws SAXException { 262 } 264 265 268 269 private void fillQName(QName toFill, String uri, String localpart, String rawname) { 270 if (!fStringsInternalized) { 271 uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null; 272 localpart = (localpart != null) ? fSymbolTable.addSymbol(localpart) : XMLSymbols.EMPTY_STRING; 273 rawname = (rawname != null) ? fSymbolTable.addSymbol(rawname) : XMLSymbols.EMPTY_STRING; 274 } 275 else { 276 if (uri != null && uri.length() == 0) { 277 uri = null; 278 } 279 if (localpart == null) { 280 localpart = XMLSymbols.EMPTY_STRING; 281 } 282 if (rawname == null) { 283 rawname = XMLSymbols.EMPTY_STRING; 284 } 285 } 286 String prefix = XMLSymbols.EMPTY_STRING; 287 int prefixIdx = rawname.indexOf(':'); 288 if (prefixIdx != -1) { 289 prefix = fSymbolTable.addSymbol(rawname.substring(0, prefixIdx)); 290 if (localpart == XMLSymbols.EMPTY_STRING) { 292 localpart = fSymbolTable.addSymbol(rawname.substring(prefixIdx + 1)); 293 } 294 } 295 else if (localpart == XMLSymbols.EMPTY_STRING) { 297 localpart = rawname; 298 } 299 toFill.setValues(prefix, localpart, rawname, uri); 300 } 301 302 private void fillXMLAttributes(Attributes atts) { 303 fAttributes.removeAllAttributes(); 304 final int attrCount = atts.getLength(); 305 for (int i = 0; i < attrCount; ++i) { 306 fillQName(fAttributeQName, atts.getURI(i), atts.getLocalName(i), atts.getQName(i)); 307 String type = atts.getType(i); 308 fAttributes.addAttributeNS(fAttributeQName, (type != null) ? type : XMLSymbols.fCDATASymbol, atts.getValue(i)); 309 fAttributes.setSpecified(i, true); 310 } 311 } 312 313 private void addNamespaceDeclarations(final int prefixCount) { 314 String prefix = null; 315 String localpart = null; 316 String rawname = null; 317 String nsPrefix = null; 318 String nsURI = null; 319 for (int i = 0; i < prefixCount; ++i) { 320 nsPrefix = fNamespaceContext.getDeclaredPrefixAt(i); 321 nsURI = fNamespaceContext.getURI(nsPrefix); 322 if (nsPrefix.length() > 0) { 323 prefix = XMLSymbols.PREFIX_XMLNS; 324 localpart = nsPrefix; 325 rawname = fSymbolTable.addSymbol(prefix + ":" + localpart); 326 } 327 else { 328 prefix = XMLSymbols.EMPTY_STRING; 329 localpart = XMLSymbols.PREFIX_XMLNS; 330 rawname = XMLSymbols.PREFIX_XMLNS; 331 } 332 fAttributeQName.setValues(prefix, localpart, rawname, NamespaceContext.XMLNS_URI); 333 fAttributes.addAttribute(fAttributeQName, XMLSymbols.fCDATASymbol, nsURI); 334 } 335 } 336 337 public void reset(SchemaDOMParser schemaDOMParser, SymbolTable symbolTable, 338 boolean namespacePrefixes, boolean stringsInternalized) { 339 fSchemaDOMParser = schemaDOMParser; 340 fSymbolTable = symbolTable; 341 fNamespacePrefixes = namespacePrefixes; 342 fStringsInternalized = stringsInternalized; 343 } 344 345 348 349 static void convertToSAXParseException(XMLParseException e) throws SAXException { 350 Exception ex = e.getException(); 351 if (ex == null) { 352 LocatorImpl locatorImpl = new LocatorImpl (); 355 locatorImpl.setPublicId(e.getPublicId()); 356 locatorImpl.setSystemId(e.getExpandedSystemId()); 357 locatorImpl.setLineNumber(e.getLineNumber()); 358 locatorImpl.setColumnNumber(e.getColumnNumber()); 359 throw new SAXParseException (e.getMessage(), locatorImpl); 360 } 361 if (ex instanceof SAXException ) { 362 throw (SAXException ) ex; 364 } 365 throw new SAXException (ex); 366 } 367 368 static void convertToSAXException(XNIException e) throws SAXException { 369 Exception ex = e.getException(); 370 if (ex == null) { 371 throw new SAXException (e.getMessage()); 372 } 373 if (ex instanceof SAXException ) { 374 throw (SAXException ) ex; 375 } 376 throw new SAXException (ex); 377 } 378 379 } | Popular Tags |