src/java.xml/share/classes/com/sun/org/apache/xml/internal/resolver/tools/ResolvingParser.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 // ResolvingParser.java - An interface for reading catalog files
   6 
   7 /*
   8  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
   9  * as applicable.
  10  *
  11  * Licensed under the Apache License, Version 2.0 (the "License");
  12  * you may not use this file except in compliance with the License.
  13  * You may obtain a copy of the License at
  14  *
  15  *      http://www.apache.org/licenses/LICENSE-2.0
  16  *
  17  * Unless required by applicable law or agreed to in writing, software
  18  * distributed under the License is distributed on an "AS IS" BASIS,
  19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20  * See the License for the specific language governing permissions and
  21  * limitations under the License.
  22  */
  23 
  24 package com.sun.org.apache.xml.internal.resolver.tools;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.net.URL;
  29 import java.net.MalformedURLException;
  30 import java.util.Locale;
  31 
  32 import org.xml.sax.Parser;
  33 import org.xml.sax.InputSource;
  34 import org.xml.sax.Locator;


  50 /**
  51  * A SAX Parser that performs catalog-based entity resolution.
  52  *
  53  * <p>This class implements a SAX Parser that performs entity resolution
  54  * using the CatalogResolver. The actual, underlying parser is obtained
  55  * from a SAXParserFactory.</p>
  56  * </p>
  57  *
  58  * @deprecated This interface has been replaced by the
  59  *             {@link com.sun.org.apache.xml.internal.resolver.tools.ResolvingXMLReader} for SAX2.
  60  * @see CatalogResolver
  61  * @see org.xml.sax.Parser
  62  *
  63  * @author Norman Walsh
  64  * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
  65  *
  66  * @version 1.0
  67  */
  68 public class ResolvingParser
  69   implements Parser, DTDHandler, DocumentHandler, EntityResolver {
  70   /** Make the parser Namespace aware? */
  71   public static boolean namespaceAware = true;
  72 
  73   /** Make the parser validating? */
  74   public static boolean validating = false;
  75 
  76   /** Suppress explanatory message?
  77    *
  78    * @see #parse(InputSource)
  79    */
  80   public static boolean suppressExplanation = false;
  81 
  82   /** The underlying parser. */
  83   private SAXParser saxParser = null;
  84 
  85   /** The underlying reader. */
  86   private Parser parser = null;
  87 
  88   /** The underlying DocumentHandler. */
  89   private DocumentHandler documentHandler = null;
  90 
  91   /** The underlying DTDHandler. */
  92   private DTDHandler dtdHandler = null;
  93 
  94   /** The manager for the underlying resolver. */
  95   private CatalogManager catalogManager = CatalogManager.getStaticManager();
  96 
  97   /** The underlying catalog resolver. */
  98   private CatalogResolver catalogResolver = null;
  99 
 100   /** A separate resolver for oasis-xml-pi catalogs. */
 101   private CatalogResolver piCatalogResolver = null;
 102 
 103   /** Are we in the prolog? Is an oasis-xml-catalog PI valid now? */
 104   private boolean allowXMLCatalogPI = false;
 105 
 106   /** Has an oasis-xml-catalog PI been seen? */
 107   private boolean oasisXMLCatalogPI = false;
 108 
 109   /** The base URI of the input document, if known. */
 110   private URL baseURL = null;
 111 
 112   /** Constructor. */
 113   public ResolvingParser() {
 114     initParser();
 115   }
 116 
 117   /** Constructor. */
 118   public ResolvingParser(CatalogManager manager) {
 119     catalogManager = manager;
 120     initParser();
 121   }
 122 
 123   /** Initialize the parser. */
 124   private void initParser() {
 125     catalogResolver = new CatalogResolver(catalogManager);
 126     SAXParserFactory spf = catalogManager.useServicesMechanism() ?
 127                     SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
 128     spf.setNamespaceAware(namespaceAware);
 129     spf.setValidating(validating);
 130 
 131     try {
 132       saxParser = spf.newSAXParser();
 133       parser = saxParser.getParser();
 134       documentHandler = null;
 135       dtdHandler = null;
 136     } catch (Exception ex) {
 137       ex.printStackTrace();
 138     }
 139   }
 140 
 141   /** Return the Catalog being used. */
 142   public Catalog getCatalog() {
 143     return catalogResolver.getCatalog();
 144   }
 145 
 146   /**
 147    * SAX Parser API.
 148    *
 149    * <p>Note that the JAXP 1.1ea2 parser crashes with an InternalError if


 272             try {
 273               if (baseURL != null) {
 274                 catalog = new URL(baseURL, data);
 275               } else {
 276                 catalog = new URL(data);
 277               }
 278             } catch (MalformedURLException mue) {
 279               // nevermind
 280             }
 281           }
 282         }
 283       }
 284 
 285       if (allowXMLCatalogPI) {
 286         if (catalogManager.getAllowOasisXMLCatalogPI()) {
 287           catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata);
 288 
 289           if (catalog != null) {
 290             try {
 291               catalogManager.debug.message(4,"oasis-xml-catalog", catalog.toString());
 292               oasisXMLCatalogPI = true;
 293 
 294               if (piCatalogResolver == null) {
 295                 piCatalogResolver = new CatalogResolver(true);
 296               }
 297 
 298               piCatalogResolver.getCatalog().parseCatalog(catalog.toString());
 299             } catch (Exception e) {
 300               catalogManager.debug.message(3, "Exception parsing oasis-xml-catalog: "
 301                             + catalog.toString());
 302             }
 303           } else {
 304             catalogManager.debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata);
 305           }
 306         } else {
 307           catalogManager.debug.message(4,"PI oasis-xml-catalog ignored: " + pidata);
 308         }
 309       } else {
 310         catalogManager.debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: "
 311                       + pidata);
 312       }


 379         InputSource iSource = new InputSource(resolved);
 380         iSource.setPublicId(publicId);
 381 
 382         // Ideally this method would not attempt to open the
 383         // InputStream, but there is a bug (in Xerces, at least)
 384         // that causes the parser to mistakenly open the wrong
 385         // system identifier if the returned InputSource does
 386         // not have a byteStream.
 387         //
 388         // It could be argued that we still shouldn't do this here,
 389         // but since the purpose of calling the entityResolver is
 390         // almost certainly to open the input stream, it seems to
 391         // do little harm.
 392         //
 393         URL url = new URL(resolved);
 394         InputStream iStream = url.openStream();
 395         iSource.setByteStream(iStream);
 396 
 397         return iSource;
 398       } catch (Exception e) {
 399         catalogManager.debug.message(1, "Failed to create InputSource", resolved);



 400         return null;
 401       }
 402     } else {
 403       return null;
 404     }
 405   }
 406 
 407   /** Setup for parsing. */
 408   private void setupParse(String systemId) {
 409     allowXMLCatalogPI = true;
 410     parser.setEntityResolver(this);
 411     parser.setDocumentHandler(this);
 412     parser.setDTDHandler(this);
 413 
 414     URL cwd = null;
 415 
 416     try {
 417       cwd = FileURL.makeURL("basename");
 418     } catch (MalformedURLException mue) {
 419       cwd = null;


   1 /*
   2  * Licensed to the Apache Software Foundation (ASF) under one or more
   3  * contributor license agreements.  See the NOTICE file distributed with
   4  * this work for additional information regarding copyright ownership.
   5  * The ASF licenses this file to You under the Apache License, Version 2.0
   6  * (the "License"); you may not use this file except in compliance with
   7  * the License.  You may obtain a copy of the License at


   8  *




   9  *      http://www.apache.org/licenses/LICENSE-2.0
  10  *
  11  * Unless required by applicable law or agreed to in writing, software
  12  * distributed under the License is distributed on an "AS IS" BASIS,
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14  * See the License for the specific language governing permissions and
  15  * limitations under the License.
  16  */
  17 
  18 package com.sun.org.apache.xml.internal.resolver.tools;
  19 
  20 import java.io.IOException;
  21 import java.io.InputStream;
  22 import java.net.URL;
  23 import java.net.MalformedURLException;
  24 import java.util.Locale;
  25 
  26 import org.xml.sax.Parser;
  27 import org.xml.sax.InputSource;
  28 import org.xml.sax.Locator;


  44 /**
  45  * A SAX Parser that performs catalog-based entity resolution.
  46  *
  47  * <p>This class implements a SAX Parser that performs entity resolution
  48  * using the CatalogResolver. The actual, underlying parser is obtained
  49  * from a SAXParserFactory.</p>
  50  * </p>
  51  *
  52  * @deprecated This interface has been replaced by the
  53  *             {@link com.sun.org.apache.xml.internal.resolver.tools.ResolvingXMLReader} for SAX2.
  54  * @see CatalogResolver
  55  * @see org.xml.sax.Parser
  56  *
  57  * @author Norman Walsh
  58  * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
  59  *
  60  * @version 1.0
  61  */
  62 public class ResolvingParser
  63   implements Parser, DTDHandler, DocumentHandler, EntityResolver {


  64 



  65   /** Suppress explanatory message?
  66    *
  67    * @see #parse(InputSource)
  68    */
  69   private static final boolean suppressExplanation = false;
  70 
  71   /** The underlying parser. */
  72   private SAXParser saxParser = null;
  73 
  74   /** The underlying reader. */
  75   private Parser parser = null;
  76 
  77   /** The underlying DocumentHandler. */
  78   private DocumentHandler documentHandler = null;
  79 
  80   /** The underlying DTDHandler. */
  81   private DTDHandler dtdHandler = null;
  82 
  83   /** The manager for the underlying resolver. */
  84   private CatalogManager catalogManager = CatalogManager.getStaticManager();
  85 
  86   /** The underlying catalog resolver. */
  87   private CatalogResolver catalogResolver = null;
  88 
  89   /** A separate resolver for oasis-xml-pi catalogs. */
  90   private CatalogResolver piCatalogResolver = null;
  91 
  92   /** Are we in the prolog? Is an oasis-xml-catalog PI valid now? */
  93   private boolean allowXMLCatalogPI = false;
  94 



  95   /** The base URI of the input document, if known. */
  96   private URL baseURL = null;
  97 
  98   /** Constructor. */
  99   public ResolvingParser() {
 100     initParser();
 101   }
 102 
 103   /** Constructor. */
 104   public ResolvingParser(CatalogManager manager) {
 105     catalogManager = manager;
 106     initParser();
 107   }
 108 
 109   /** Initialize the parser. */
 110   private void initParser() {
 111     catalogResolver = new CatalogResolver(catalogManager);
 112     SAXParserFactory spf = catalogManager.useServicesMechanism() ?
 113                     SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
 114     spf.setNamespaceAware(true);
 115     spf.setValidating(false);
 116 
 117     try {
 118       saxParser = spf.newSAXParser();
 119       parser = saxParser.getParser();
 120       documentHandler = null;
 121       dtdHandler = null;
 122     } catch (Exception ex) {
 123       ex.printStackTrace();
 124     }
 125   }
 126 
 127   /** Return the Catalog being used. */
 128   public Catalog getCatalog() {
 129     return catalogResolver.getCatalog();
 130   }
 131 
 132   /**
 133    * SAX Parser API.
 134    *
 135    * <p>Note that the JAXP 1.1ea2 parser crashes with an InternalError if


 258             try {
 259               if (baseURL != null) {
 260                 catalog = new URL(baseURL, data);
 261               } else {
 262                 catalog = new URL(data);
 263               }
 264             } catch (MalformedURLException mue) {
 265               // nevermind
 266             }
 267           }
 268         }
 269       }
 270 
 271       if (allowXMLCatalogPI) {
 272         if (catalogManager.getAllowOasisXMLCatalogPI()) {
 273           catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata);
 274 
 275           if (catalog != null) {
 276             try {
 277               catalogManager.debug.message(4,"oasis-xml-catalog", catalog.toString());

 278 
 279               if (piCatalogResolver == null) {
 280                 piCatalogResolver = new CatalogResolver(true);
 281               }
 282 
 283               piCatalogResolver.getCatalog().parseCatalog(catalog.toString());
 284             } catch (Exception e) {
 285               catalogManager.debug.message(3, "Exception parsing oasis-xml-catalog: "
 286                             + catalog.toString());
 287             }
 288           } else {
 289             catalogManager.debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata);
 290           }
 291         } else {
 292           catalogManager.debug.message(4,"PI oasis-xml-catalog ignored: " + pidata);
 293         }
 294       } else {
 295         catalogManager.debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: "
 296                       + pidata);
 297       }


 364         InputSource iSource = new InputSource(resolved);
 365         iSource.setPublicId(publicId);
 366 
 367         // Ideally this method would not attempt to open the
 368         // InputStream, but there is a bug (in Xerces, at least)
 369         // that causes the parser to mistakenly open the wrong
 370         // system identifier if the returned InputSource does
 371         // not have a byteStream.
 372         //
 373         // It could be argued that we still shouldn't do this here,
 374         // but since the purpose of calling the entityResolver is
 375         // almost certainly to open the input stream, it seems to
 376         // do little harm.
 377         //
 378         URL url = new URL(resolved);
 379         InputStream iStream = url.openStream();
 380         iSource.setByteStream(iStream);
 381 
 382         return iSource;
 383       } catch (Exception e) {
 384         catalogManager.debug.message(1,
 385                                      "Failed to create InputSource ("
 386                                      + e.toString()
 387                                      + ")", resolved);
 388         return null;
 389       }
 390     } else {
 391       return null;
 392     }
 393   }
 394 
 395   /** Setup for parsing. */
 396   private void setupParse(String systemId) {
 397     allowXMLCatalogPI = true;
 398     parser.setEntityResolver(this);
 399     parser.setDocumentHandler(this);
 400     parser.setDTDHandler(this);
 401 
 402     URL cwd = null;
 403 
 404     try {
 405       cwd = FileURL.makeURL("basename");
 406     } catch (MalformedURLException mue) {
 407       cwd = null;