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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 397
b99d7e355d4b
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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

mercurial