src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/internalizer/DOMForest.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.xjc.reader.internalizer;
ohair@286 27
ohair@286 28 import java.io.IOException;
ohair@286 29 import java.io.OutputStream;
ohair@286 30 import java.io.OutputStreamWriter;
ohair@286 31 import java.util.ArrayList;
ohair@286 32 import java.util.Collections;
ohair@286 33 import java.util.HashMap;
ohair@286 34 import java.util.HashSet;
ohair@286 35 import java.util.List;
ohair@286 36 import java.util.Map;
ohair@286 37 import java.util.Set;
ohair@286 38
ohair@286 39 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
ohair@286 40 import javax.xml.parsers.DocumentBuilder;
ohair@286 41 import javax.xml.parsers.DocumentBuilderFactory;
ohair@286 42 import javax.xml.parsers.ParserConfigurationException;
ohair@286 43 import javax.xml.parsers.SAXParserFactory;
ohair@286 44 import javax.xml.stream.XMLStreamException;
ohair@286 45 import javax.xml.stream.XMLStreamReader;
ohair@286 46 import javax.xml.transform.Source;
ohair@286 47 import javax.xml.transform.Transformer;
ohair@286 48 import javax.xml.transform.TransformerException;
ohair@286 49 import javax.xml.transform.TransformerFactory;
ohair@286 50 import javax.xml.transform.dom.DOMSource;
ohair@286 51 import javax.xml.transform.sax.SAXResult;
ohair@286 52 import javax.xml.transform.sax.SAXSource;
ohair@286 53 import javax.xml.validation.SchemaFactory;
ohair@286 54
ohair@286 55 import com.sun.istack.internal.NotNull;
ohair@286 56 import com.sun.istack.internal.XMLStreamReaderToContentHandler;
ohair@286 57 import com.sun.tools.internal.xjc.ErrorReceiver;
ohair@286 58 import com.sun.tools.internal.xjc.Options;
ohair@286 59 import com.sun.tools.internal.xjc.reader.Const;
ohair@286 60 import com.sun.tools.internal.xjc.reader.xmlschema.parser.SchemaConstraintChecker;
ohair@286 61 import com.sun.tools.internal.xjc.util.ErrorReceiverFilter;
ohair@286 62 import com.sun.xml.internal.bind.marshaller.DataWriter;
ohair@286 63 import com.sun.xml.internal.xsom.parser.JAXPParser;
ohair@286 64 import com.sun.xml.internal.xsom.parser.XMLParser;
ohair@286 65
ohair@286 66 import org.w3c.dom.Document;
ohair@286 67 import org.w3c.dom.Element;
ohair@286 68 import org.xml.sax.ContentHandler;
ohair@286 69 import org.xml.sax.EntityResolver;
ohair@286 70 import org.xml.sax.InputSource;
ohair@286 71 import org.xml.sax.SAXException;
ohair@286 72 import org.xml.sax.SAXParseException;
ohair@286 73 import org.xml.sax.XMLReader;
ohair@286 74 import org.xml.sax.helpers.XMLFilterImpl;
ohair@286 75
ohair@286 76
ohair@286 77 /**
ohair@286 78 * Builds a DOM forest and maintains association from
ohair@286 79 * system IDs to DOM trees.
ohair@286 80 *
ohair@286 81 * <p>
ohair@286 82 * A forest is a transitive reflexive closure of referenced documents.
ohair@286 83 * IOW, if a document is in a forest, all the documents referenced from
ohair@286 84 * it is in a forest, too. To support this semantics, {@link DOMForest}
ohair@286 85 * uses {@link InternalizationLogic} to find referenced documents.
ohair@286 86 *
ohair@286 87 * <p>
ohair@286 88 * Some documents are marked as "root"s, meaning those documents were
ohair@286 89 * put into a forest explicitly, not because it is referenced from another
ohair@286 90 * document. (However, a root document can be referenced from other
ohair@286 91 * documents, too.)
ohair@286 92 *
ohair@286 93 * @author
ohair@286 94 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 95 */
ohair@286 96 public final class DOMForest {
ohair@286 97 /** actual data storage map&lt;SystemId,Document>. */
ohair@286 98 private final Map<String,Document> core = new HashMap<String,Document>();
ohair@286 99
ohair@286 100 /**
ohair@286 101 * To correctly feed documents to a schema parser, we need to remember
ohair@286 102 * which documents (of the forest) were given as the root
ohair@286 103 * documents, and which of them are read as included/imported
ohair@286 104 * documents.
ohair@286 105 *
ohair@286 106 * <p>
ohair@286 107 * Set of system ids as strings.
ohair@286 108 */
ohair@286 109 private final Set<String> rootDocuments = new HashSet<String>();
ohair@286 110
ohair@286 111 /** Stores location information for all the trees in this forest. */
ohair@286 112 public final LocatorTable locatorTable = new LocatorTable();
ohair@286 113
ohair@286 114 /** Stores all the outer-most &lt;jaxb:bindings> customizations. */
ohair@286 115 public final Set<Element> outerMostBindings = new HashSet<Element>();
ohair@286 116
ohair@286 117 /** Used to resolve references to other schema documents. */
ohair@286 118 private EntityResolver entityResolver = null;
ohair@286 119
ohair@286 120 /** Errors encountered during the parsing will be sent to this object. */
ohair@286 121 private ErrorReceiver errorReceiver = null;
ohair@286 122
ohair@286 123 /** Schema language dependent part of the processing. */
ohair@286 124 protected final InternalizationLogic logic;
ohair@286 125
ohair@286 126 private final SAXParserFactory parserFactory;
ohair@286 127 private final DocumentBuilder documentBuilder;
ohair@286 128
ohair@286 129
ohair@286 130 public DOMForest(
ohair@286 131 SAXParserFactory parserFactory, DocumentBuilder documentBuilder,
ohair@286 132 InternalizationLogic logic ) {
ohair@286 133
ohair@286 134 this.parserFactory = parserFactory;
ohair@286 135 this.documentBuilder = documentBuilder;
ohair@286 136 this.logic = logic;
ohair@286 137 }
ohair@286 138
ohair@286 139 public DOMForest( InternalizationLogic logic ) {
ohair@286 140 try {
ohair@286 141 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
ohair@286 142 dbf.setNamespaceAware(true);
ohair@286 143 this.documentBuilder = dbf.newDocumentBuilder();
ohair@286 144
ohair@286 145 this.parserFactory = SAXParserFactory.newInstance();
ohair@286 146 this.parserFactory.setNamespaceAware(true);
ohair@286 147 } catch( ParserConfigurationException e ) {
ohair@286 148 throw new AssertionError(e);
ohair@286 149 }
ohair@286 150
ohair@286 151 this.logic = logic;
ohair@286 152 }
ohair@286 153
ohair@286 154 /**
ohair@286 155 * Gets the DOM tree associated with the specified system ID,
ohair@286 156 * or null if none is found.
ohair@286 157 */
ohair@286 158 public Document get( String systemId ) {
ohair@286 159 Document doc = core.get(systemId);
ohair@286 160
ohair@286 161 if( doc==null && systemId.startsWith("file:/") && !systemId.startsWith("file://") ) {
ohair@286 162 // As of JDK1.4, java.net.URL.toExternal method returns URLs like
ohair@286 163 // "file:/abc/def/ghi" which is an incorrect file protocol URL according to RFC1738.
ohair@286 164 // Some other correctly functioning parts return the correct URLs ("file:///abc/def/ghi"),
ohair@286 165 // and this descripancy breaks DOM look up by system ID.
ohair@286 166
ohair@286 167 // this extra check solves this problem.
ohair@286 168 doc = core.get( "file://"+systemId.substring(5) );
ohair@286 169 }
ohair@286 170
ohair@286 171 if( doc==null && systemId.startsWith("file:") ) {
ohair@286 172 // on Windows, filenames are case insensitive.
ohair@286 173 // perform case-insensitive search for improved user experience
ohair@286 174 String systemPath = getPath(systemId);
ohair@286 175 for (String key : core.keySet()) {
ohair@286 176 if(key.startsWith("file:") && getPath(key).equalsIgnoreCase(systemPath)) {
ohair@286 177 doc = core.get(key);
ohair@286 178 break;
ohair@286 179 }
ohair@286 180 }
ohair@286 181 }
ohair@286 182
ohair@286 183 return doc;
ohair@286 184 }
ohair@286 185
ohair@286 186 /**
ohair@286 187 * Strips off the leading 'file:///' portion from an URL.
ohair@286 188 */
ohair@286 189 private String getPath(String key) {
ohair@286 190 key = key.substring(5); // skip 'file:'
ohair@286 191 while(key.length()>0 && key.charAt(0)=='/')
ohair@286 192 key = key.substring(1);
ohair@286 193 return key;
ohair@286 194 }
ohair@286 195
ohair@286 196 /**
ohair@286 197 * Returns a read-only set of root document system IDs.
ohair@286 198 */
ohair@286 199 public Set<String> getRootDocuments() {
ohair@286 200 return Collections.unmodifiableSet(rootDocuments);
ohair@286 201 }
ohair@286 202
ohair@286 203 /**
ohair@286 204 * Picks one document at random and returns it.
ohair@286 205 */
ohair@286 206 public Document getOneDocument() {
ohair@286 207 for (Document dom : core.values()) {
ohair@286 208 if (!dom.getDocumentElement().getNamespaceURI().equals(Const.JAXB_NSURI))
ohair@286 209 return dom;
ohair@286 210 }
ohair@286 211 // we should have caught this error very early on
ohair@286 212 throw new AssertionError();
ohair@286 213 }
ohair@286 214
ohair@286 215 /**
ohair@286 216 * Checks the correctness of the XML Schema documents and return true
ohair@286 217 * if it's OK.
ohair@286 218 *
ohair@286 219 * <p>
ohair@286 220 * This method performs a weaker version of the tests where error messages
ohair@286 221 * are provided without line number information. So whenever possible
ohair@286 222 * use {@link SchemaConstraintChecker}.
ohair@286 223 *
ohair@286 224 * @see SchemaConstraintChecker
ohair@286 225 */
ohair@286 226 public boolean checkSchemaCorrectness(ErrorReceiver errorHandler) {
ohair@286 227 try {
ohair@286 228 SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
ohair@286 229 ErrorReceiverFilter filter = new ErrorReceiverFilter(errorHandler);
ohair@286 230 sf.setErrorHandler(filter);
ohair@286 231 Set<String> roots = getRootDocuments();
ohair@286 232 Source[] sources = new Source[roots.size()];
ohair@286 233 int i=0;
ohair@286 234 for (String root : roots) {
ohair@286 235 sources[i++] = new DOMSource(get(root),root);
ohair@286 236 }
ohair@286 237 sf.newSchema(sources);
ohair@286 238 return !filter.hadError();
ohair@286 239 } catch (SAXException e) {
ohair@286 240 // the errors should have been reported
ohair@286 241 return false;
ohair@286 242 }
ohair@286 243 }
ohair@286 244
ohair@286 245 /**
ohair@286 246 * Gets the system ID from which the given DOM is parsed.
ohair@286 247 * <p>
ohair@286 248 * Poor-man's base URI.
ohair@286 249 */
ohair@286 250 public String getSystemId( Document dom ) {
ohair@286 251 for (Map.Entry<String,Document> e : core.entrySet()) {
ohair@286 252 if (e.getValue() == dom)
ohair@286 253 return e.getKey();
ohair@286 254 }
ohair@286 255 return null;
ohair@286 256 }
ohair@286 257
ohair@286 258 public Document parse( InputSource source, boolean root ) throws SAXException {
ohair@286 259 if( source.getSystemId()==null )
ohair@286 260 throw new IllegalArgumentException();
ohair@286 261
ohair@286 262 return parse( source.getSystemId(), source, root );
ohair@286 263 }
ohair@286 264
ohair@286 265 /**
ohair@286 266 * Parses an XML at the given location (
ohair@286 267 * and XMLs referenced by it) into DOM trees
ohair@286 268 * and stores them to this forest.
ohair@286 269 *
ohair@286 270 * @return the parsed DOM document object.
ohair@286 271 */
ohair@286 272 public Document parse( String systemId, boolean root ) throws SAXException, IOException {
ohair@286 273
ohair@286 274 systemId = Options.normalizeSystemId(systemId);
ohair@286 275
ohair@286 276 if( core.containsKey(systemId) )
ohair@286 277 // this document has already been parsed. Just ignore.
ohair@286 278 return core.get(systemId);
ohair@286 279
ohair@286 280 InputSource is=null;
ohair@286 281
ohair@286 282 // allow entity resolver to find the actual byte stream.
ohair@286 283 if( entityResolver!=null )
ohair@286 284 is = entityResolver.resolveEntity(null,systemId);
ohair@286 285 if( is==null )
ohair@286 286 is = new InputSource(systemId);
ohair@286 287
ohair@286 288 // but we still use the original system Id as the key.
ohair@286 289 return parse( systemId, is, root );
ohair@286 290 }
ohair@286 291
ohair@286 292 /**
ohair@286 293 * Returns a {@link ContentHandler} to feed SAX events into.
ohair@286 294 *
ohair@286 295 * <p>
ohair@286 296 * The client of this class can feed SAX events into the handler
ohair@286 297 * to parse a document into this DOM forest.
ohair@286 298 *
ohair@286 299 * This version requires that the DOM object to be created and registered
ohair@286 300 * to the map beforehand.
ohair@286 301 */
ohair@286 302 private ContentHandler getParserHandler( Document dom ) {
ohair@286 303 ContentHandler handler = new DOMBuilder(dom,locatorTable,outerMostBindings);
ohair@286 304 handler = new WhitespaceStripper(handler,errorReceiver,entityResolver);
ohair@286 305 handler = new VersionChecker(handler,errorReceiver,entityResolver);
ohair@286 306
ohair@286 307 // insert the reference finder so that
ohair@286 308 // included/imported schemas will be also parsed
ohair@286 309 XMLFilterImpl f = logic.createExternalReferenceFinder(this);
ohair@286 310 f.setContentHandler(handler);
ohair@286 311
ohair@286 312 if(errorReceiver!=null)
ohair@286 313 f.setErrorHandler(errorReceiver);
ohair@286 314 if(entityResolver!=null)
ohair@286 315 f.setEntityResolver(entityResolver);
ohair@286 316
ohair@286 317 return f;
ohair@286 318 }
ohair@286 319
ohair@286 320 public interface Handler extends ContentHandler {
ohair@286 321 /**
ohair@286 322 * Gets the DOM that was built.
ohair@286 323 */
ohair@286 324 public Document getDocument();
ohair@286 325 }
ohair@286 326
ohair@286 327 private static abstract class HandlerImpl extends XMLFilterImpl implements Handler {
ohair@286 328 }
ohair@286 329
ohair@286 330 /**
ohair@286 331 * Returns a {@link ContentHandler} to feed SAX events into.
ohair@286 332 *
ohair@286 333 * <p>
ohair@286 334 * The client of this class can feed SAX events into the handler
ohair@286 335 * to parse a document into this DOM forest.
ohair@286 336 */
ohair@286 337 public Handler getParserHandler( String systemId, boolean root ) {
ohair@286 338 final Document dom = documentBuilder.newDocument();
ohair@286 339 core.put( systemId, dom );
ohair@286 340 if(root)
ohair@286 341 rootDocuments.add(systemId);
ohair@286 342
ohair@286 343 ContentHandler handler = getParserHandler(dom);
ohair@286 344
ohair@286 345 // we will register the DOM to the map once the system ID becomes available.
ohair@286 346 // but the SAX allows the event source to not to provide that information,
ohair@286 347 // so be prepared for such case.
ohair@286 348 HandlerImpl x = new HandlerImpl() {
ohair@286 349 public Document getDocument() {
ohair@286 350 return dom;
ohair@286 351 }
ohair@286 352 };
ohair@286 353 x.setContentHandler(handler);
ohair@286 354
ohair@286 355 return x;
ohair@286 356 }
ohair@286 357
ohair@286 358 /**
ohair@286 359 * Parses the given document and add it to the DOM forest.
ohair@286 360 *
ohair@286 361 * @return
ohair@286 362 * null if there was a parse error. otherwise non-null.
ohair@286 363 */
ohair@286 364 public Document parse( String systemId, InputSource inputSource, boolean root ) throws SAXException {
ohair@286 365 Document dom = documentBuilder.newDocument();
ohair@286 366
ohair@286 367 systemId = Options.normalizeSystemId(systemId);
ohair@286 368
ohair@286 369 // put into the map before growing a tree, to
ohair@286 370 // prevent recursive reference from causing infinite loop.
ohair@286 371 core.put( systemId, dom );
ohair@286 372 if(root)
ohair@286 373 rootDocuments.add(systemId);
ohair@286 374
ohair@286 375 try {
ohair@286 376 XMLReader reader = parserFactory.newSAXParser().getXMLReader();
ohair@286 377 reader.setContentHandler(getParserHandler(dom));
ohair@286 378 if(errorReceiver!=null)
ohair@286 379 reader.setErrorHandler(errorReceiver);
ohair@286 380 if(entityResolver!=null)
ohair@286 381 reader.setEntityResolver(entityResolver);
ohair@286 382 reader.parse(inputSource);
ohair@286 383 } catch( ParserConfigurationException e ) {
ohair@286 384 // in practice, this exception won't happen.
ohair@286 385 errorReceiver.error(e.getMessage(),e);
ohair@286 386 core.remove(systemId);
ohair@286 387 rootDocuments.remove(systemId);
ohair@286 388 return null;
ohair@286 389 } catch( IOException e ) {
ohair@286 390 errorReceiver.error(Messages.format(Messages.DOMFOREST_INPUTSOURCE_IOEXCEPTION, systemId, e.toString()),e);
ohair@286 391 core.remove(systemId);
ohair@286 392 rootDocuments.remove(systemId);
ohair@286 393 return null;
ohair@286 394 }
ohair@286 395
ohair@286 396 return dom;
ohair@286 397 }
ohair@286 398
ohair@286 399 public Document parse( String systemId, XMLStreamReader parser, boolean root ) throws XMLStreamException {
ohair@286 400 Document dom = documentBuilder.newDocument();
ohair@286 401
ohair@286 402 systemId = Options.normalizeSystemId(systemId);
ohair@286 403
ohair@286 404 if(root)
ohair@286 405 rootDocuments.add(systemId);
ohair@286 406
ohair@286 407 if(systemId==null)
ohair@286 408 throw new IllegalArgumentException("system id cannot be null");
ohair@286 409 core.put( systemId, dom );
ohair@286 410
ohair@286 411 new XMLStreamReaderToContentHandler(parser,getParserHandler(dom),false,false).bridge();
ohair@286 412
ohair@286 413 return dom;
ohair@286 414 }
ohair@286 415
ohair@286 416 /**
ohair@286 417 * Performs internalization.
ohair@286 418 *
ohair@286 419 * This method should be called only once, only after all the
ohair@286 420 * schemas are parsed.
ohair@286 421 *
ohair@286 422 * @return
ohair@286 423 * the returned bindings need to be applied after schema
ohair@286 424 * components are built.
ohair@286 425 */
ohair@286 426 public SCDBasedBindingSet transform(boolean enableSCD) {
ohair@286 427 return Internalizer.transform(this,enableSCD);
ohair@286 428 }
ohair@286 429
ohair@286 430 /**
ohair@286 431 * Performs the schema correctness check by using JAXP 1.3.
ohair@286 432 *
ohair@286 433 * <p>
ohair@286 434 * This is "weak", because {@link SchemaFactory#newSchema(Source[])}
ohair@286 435 * doesn't handle inclusions very correctly (it ends up parsing it
ohair@286 436 * from its original source, not in this tree), and because
ohair@286 437 * it doesn't handle two documents for the same namespace very
ohair@286 438 * well.
ohair@286 439 *
ohair@286 440 * <p>
ohair@286 441 * We should eventually fix JAXP (and Xerces), but meanwhile
ohair@286 442 * this weaker and potentially wrong correctness check is still
ohair@286 443 * better than nothing when used inside JAX-WS (JAXB CLI and Ant
ohair@286 444 * does a better job of checking this.)
ohair@286 445 *
ohair@286 446 * <p>
ohair@286 447 * To receive errors, use {@link SchemaFactory#setErrorHandler(ErrorHandler)}.
ohair@286 448 */
ohair@286 449 public void weakSchemaCorrectnessCheck(SchemaFactory sf) {
ohair@286 450 List<SAXSource> sources = new ArrayList<SAXSource>();
ohair@286 451 for( String systemId : getRootDocuments() ) {
ohair@286 452 Document dom = get(systemId);
ohair@286 453 if (dom.getDocumentElement().getNamespaceURI().equals(Const.JAXB_NSURI))
ohair@286 454 continue; // this isn't a schema. we have to do a negative check because if we see completely unrelated ns, we want to report that as an error
ohair@286 455
ohair@286 456 SAXSource ss = createSAXSource(systemId);
ohair@286 457 try {
ohair@286 458 ss.getXMLReader().setFeature("http://xml.org/sax/features/namespace-prefixes",true);
ohair@286 459 } catch (SAXException e) {
ohair@286 460 throw new AssertionError(e); // Xerces wants this. See 6395322.
ohair@286 461 }
ohair@286 462 sources.add(ss);
ohair@286 463 }
ohair@286 464
ohair@286 465 try {
ohair@286 466 sf.newSchema(sources.toArray(new SAXSource[0]));
ohair@286 467 } catch (SAXException e) {
ohair@286 468 // error should have been reported.
ohair@286 469 } catch (RuntimeException e) {
ohair@286 470 // JAXP RI isn't very trustworthy when it comes to schema error check,
ohair@286 471 // and we know some cases where it just dies with NPE. So handle it gracefully.
ohair@286 472 // this masks a bug in the JAXP RI, but we need a release that we have to make.
ohair@286 473 try {
ohair@286 474 sf.getErrorHandler().warning(
ohair@286 475 new SAXParseException(Messages.format(
ohair@286 476 Messages.ERR_GENERAL_SCHEMA_CORRECTNESS_ERROR,e.getMessage()),
ohair@286 477 null,null,-1,-1,e));
ohair@286 478 } catch (SAXException _) {
ohair@286 479 // ignore
ohair@286 480 }
ohair@286 481 }
ohair@286 482 }
ohair@286 483
ohair@286 484 /**
ohair@286 485 * Creates a {@link SAXSource} that, when parsed, reads from this {@link DOMForest}
ohair@286 486 * (instead of parsing the original source identified by the system ID.)
ohair@286 487 */
ohair@286 488 public @NotNull SAXSource createSAXSource(String systemId) {
ohair@286 489 ContentHandlerNamespacePrefixAdapter reader = new ContentHandlerNamespacePrefixAdapter(new XMLFilterImpl() {
ohair@286 490 // XMLReader that uses XMLParser to parse. We need to use XMLFilter to indrect
ohair@286 491 // handlers, since SAX allows handlers to be changed while parsing.
ohair@286 492 public void parse(InputSource input) throws SAXException, IOException {
ohair@286 493 createParser().parse(input, this, this, this);
ohair@286 494 }
ohair@286 495
ohair@286 496 public void parse(String systemId) throws SAXException, IOException {
ohair@286 497 parse(new InputSource(systemId));
ohair@286 498 }
ohair@286 499 });
ohair@286 500
ohair@286 501 return new SAXSource(reader,new InputSource(systemId));
ohair@286 502 }
ohair@286 503
ohair@286 504 /**
ohair@286 505 * Creates {@link XMLParser} for XSOM which reads documents from
ohair@286 506 * this DOMForest rather than doing a fresh parse.
ohair@286 507 *
ohair@286 508 * The net effect is that XSOM will read transformed XML Schemas
ohair@286 509 * instead of the original documents.
ohair@286 510 */
ohair@286 511 public XMLParser createParser() {
ohair@286 512 return new DOMForestParser(this,new JAXPParser());
ohair@286 513 }
ohair@286 514
ohair@286 515
ohair@286 516
ohair@286 517 public EntityResolver getEntityResolver() {
ohair@286 518 return entityResolver;
ohair@286 519 }
ohair@286 520
ohair@286 521 public void setEntityResolver(EntityResolver entityResolver) {
ohair@286 522 this.entityResolver = entityResolver;
ohair@286 523 }
ohair@286 524
ohair@286 525 public ErrorReceiver getErrorHandler() {
ohair@286 526 return errorReceiver;
ohair@286 527 }
ohair@286 528
ohair@286 529 public void setErrorHandler(ErrorReceiver errorHandler) {
ohair@286 530 this.errorReceiver = errorHandler;
ohair@286 531 }
ohair@286 532
ohair@286 533 /**
ohair@286 534 * Gets all the parsed documents.
ohair@286 535 */
ohair@286 536 public Document[] listDocuments() {
ohair@286 537 return core.values().toArray(new Document[core.size()]);
ohair@286 538 }
ohair@286 539
ohair@286 540 /**
ohair@286 541 * Gets all the system IDs of the documents.
ohair@286 542 */
ohair@286 543 public String[] listSystemIDs() {
ohair@286 544 return core.keySet().toArray(new String[core.keySet().size()]);
ohair@286 545 }
ohair@286 546
ohair@286 547 /**
ohair@286 548 * Dumps the contents of the forest to the specified stream.
ohair@286 549 *
ohair@286 550 * This is a debug method. As such, error handling is sloppy.
ohair@286 551 */
ohair@286 552 public void dump( OutputStream out ) throws IOException {
ohair@286 553 try {
ohair@286 554 // create identity transformer
ohair@286 555 Transformer it = TransformerFactory.newInstance().newTransformer();
ohair@286 556
ohair@286 557 for (Map.Entry<String, Document> e : core.entrySet()) {
ohair@286 558 out.write( ("---<< "+e.getKey()+'\n').getBytes() );
ohair@286 559
ohair@286 560 DataWriter dw = new DataWriter(new OutputStreamWriter(out),null);
ohair@286 561 dw.setIndentStep(" ");
ohair@286 562 it.transform( new DOMSource(e.getValue()),
ohair@286 563 new SAXResult(dw));
ohair@286 564
ohair@286 565 out.write( "\n\n\n".getBytes() );
ohair@286 566 }
ohair@286 567 } catch( TransformerException e ) {
ohair@286 568 e.printStackTrace();
ohair@286 569 }
ohair@286 570 }
ohair@286 571 }

mercurial