Home » xml-commons-external-1.4.01-src » javax » xml » parsers » [javadoc | source]

    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   // $Id: SAXParser.java 584483 2007-10-14 02:54:48Z mrglavas $
   19   
   20   package javax.xml.parsers;
   21   
   22   import java.io.File;
   23   import java.io.IOException;
   24   import java.io.InputStream;
   25   
   26   import javax.xml.validation.Schema;
   27   
   28   import org.xml.sax.HandlerBase;
   29   import org.xml.sax.InputSource;
   30   import org.xml.sax.Parser;
   31   import org.xml.sax.SAXException;
   32   import org.xml.sax.SAXNotRecognizedException;
   33   import org.xml.sax.SAXNotSupportedException;
   34   import org.xml.sax.XMLReader;
   35   import org.xml.sax.helpers.DefaultHandler;
   36   
   37   
   38   /**
   39    * Defines the API that wraps an {@link org.xml.sax.XMLReader}
   40    * implementation class. In JAXP 1.0, this class wrapped the
   41    * {@link org.xml.sax.Parser} interface, however this interface was
   42    * replaced by the {@link org.xml.sax.XMLReader}. For ease
   43    * of transition, this class continues to support the same name
   44    * and interface as well as supporting new methods.
   45    *
   46    * An instance of this class can be obtained from the
   47    * {@link javax.xml.parsers.SAXParserFactory#newSAXParser()} method.
   48    * Once an instance of this class is obtained, XML can be parsed from
   49    * a variety of input sources. These input sources are InputStreams,
   50    * Files, URLs, and SAX InputSources.<p>
   51    *
   52    * This static method creates a new factory instance based
   53    * on a system property setting or uses the platform default
   54    * if no property has been defined.<p>
   55    *
   56    * The system property that controls which Factory implementation
   57    * to create is named <code>&quot;javax.xml.parsers.SAXParserFactory&quot;</code>.
   58    * This property names a class that is a concrete subclass of this
   59    * abstract class. If no property is defined, a platform default
   60    * will be used.</p>
   61    *
   62    * As the content is parsed by the underlying parser, methods of the
   63    * given {@link org.xml.sax.HandlerBase} or the
   64    * {@link org.xml.sax.helpers.DefaultHandler} are called.<p>
   65    *
   66    * Implementors of this class which wrap an underlaying implementation
   67    * can consider using the {@link org.xml.sax.helpers.ParserAdapter}
   68    * class to initially adapt their SAX1 implementation to work under
   69    * this revised class.
   70    *
   71    * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
   72    * @version $Revision: 584483 $, $Date: 2007-10-13 22:54:48 -0400 (Sat, 13 Oct 2007) $
   73    */
   74   public abstract class SAXParser {
   75       
   76       private static final boolean DEBUG = false;
   77           
   78       /**
   79        * <p>Protected constructor to prevent instantiation.
   80        * Use {@link javax.xml.parsers.SAXParserFactory#newSAXParser()}.</p>
   81        */
   82       protected SAXParser () {
   83       
   84       }
   85   
   86   	/**
   87   	 * <p>Reset this <code>SAXParser</code> to its original configuration.</p>
   88   	 * 
   89   	 * <p><code>SAXParser</code> is reset to the same state as when it was created with
   90   	 * {@link SAXParserFactory#newSAXParser()}.
   91   	 * <code>reset()</code> is designed to allow the reuse of existing <code>SAXParser</code>s
   92   	 * thus saving resources associated with the creation of new <code>SAXParser</code>s.</p>
   93   	 * 
   94   	 * <p>The reset <code>SAXParser</code> is not guaranteed to have the same {@link Schema}
   95   	 * <code>Object</code>, e.g. {@link Object#equals(Object obj)}.  It is guaranteed to have a functionally equal
   96   	 * <code>Schema</code>.</p>
   97   	 * 
   98   	 * @since 1.5
   99   	 */
  100   	public void reset() {
  101   
  102   		// implementors should override this method
  103   		throw new UnsupportedOperationException(
  104   			"This SAXParser, \"" + this.getClass().getName() + "\", does not support the reset functionality."
  105   			+ "  Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""
  106   			+ " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""
  107   			);
  108   	}
  109   
  110       /**
  111        * <p>Parse the content of the given {@link java.io.InputStream}
  112        * instance as XML using the specified {@link org.xml.sax.HandlerBase}.
  113        * <i> Use of the DefaultHandler version of this method is recommended as
  114        * the HandlerBase class has been deprecated in SAX 2.0</i>.</p>
  115        *
  116        * @param is InputStream containing the content to be parsed.
  117        * @param hb The SAX HandlerBase to use.
  118        * 
  119        * @throws IllegalArgumentException If the given InputStream is null.
  120        * @throws SAXException If parse produces a SAX error.
  121        * @throws IOException If an IO error occurs interacting with the
  122        *   <code>InputStream</code>.
  123        * 
  124        * @see org.xml.sax.DocumentHandler
  125        */    
  126       public void parse(InputStream is, HandlerBase hb)
  127           throws SAXException, IOException {
  128           if (is == null) {
  129               throw new IllegalArgumentException("InputStream cannot be null");
  130           }
  131   
  132           InputSource input = new InputSource(is);
  133           this.parse(input, hb);
  134       }
  135   
  136       /**
  137        * <p>Parse the content of the given {@link java.io.InputStream}
  138        * instance as XML using the specified {@link org.xml.sax.HandlerBase}.
  139        * <i> Use of the DefaultHandler version of this method is recommended as
  140        * the HandlerBase class has been deprecated in SAX 2.0</i>.</p>
  141        *
  142        * @param is InputStream containing the content to be parsed.
  143        * @param hb The SAX HandlerBase to use.
  144        * @param systemId The systemId which is needed for resolving relative URIs.
  145        * 
  146        * @throws IllegalArgumentException If the given <code>InputStream</code> is
  147        *   <code>null</code>.
  148        * @throws IOException If any IO error occurs interacting with the
  149        *   <code>InputStream</code>.
  150        * @throws SAXException If any SAX errors occur during processing.
  151        * 
  152        * @see org.xml.sax.DocumentHandler version of this method instead.
  153        */
  154       public void parse(
  155           InputStream is,
  156           HandlerBase hb,
  157           String systemId)
  158           throws SAXException, IOException {
  159           if (is == null) {
  160               throw new IllegalArgumentException("InputStream cannot be null");
  161           }
  162   
  163           InputSource input = new InputSource(is);
  164           input.setSystemId(systemId);
  165           this.parse(input, hb);
  166       }
  167      
  168       /**
  169        * Parse the content of the given {@link java.io.InputStream}
  170        * instance as XML using the specified
  171        * {@link org.xml.sax.helpers.DefaultHandler}.
  172        *
  173        * @param is InputStream containing the content to be parsed.
  174        * @param dh The SAX DefaultHandler to use.
  175        * 
  176        * @throws IllegalArgumentException If the given InputStream is null.
  177        * @throws IOException If any IO errors occur.
  178        * @throws SAXException If any SAX errors occur during processing.
  179        * 
  180        * @see org.xml.sax.DocumentHandler
  181        */
  182       public void parse(InputStream is, DefaultHandler dh)
  183           throws SAXException, IOException {
  184           if (is == null) {
  185               throw new IllegalArgumentException("InputStream cannot be null");
  186           }
  187   
  188           InputSource input = new InputSource(is);
  189           this.parse(input, dh);
  190       }
  191   
  192       /**
  193        * Parse the content of the given {@link java.io.InputStream}
  194        * instance as XML using the specified
  195        * {@link org.xml.sax.helpers.DefaultHandler}.
  196        *
  197        * @param is InputStream containing the content to be parsed.
  198        * @param dh The SAX DefaultHandler to use.
  199        * @param systemId The systemId which is needed for resolving relative URIs.
  200        * 
  201        * @throws IllegalArgumentException If the given InputStream is null.
  202        * @throws IOException If any IO errors occur.
  203        * @throws SAXException If any SAX errors occur during processing.
  204        * 
  205        * @see org.xml.sax.DocumentHandler version of this method instead.
  206        */
  207       public void parse(
  208           InputStream is,
  209           DefaultHandler dh,
  210           String systemId)
  211           throws SAXException, IOException {
  212           if (is == null) {
  213               throw new IllegalArgumentException("InputStream cannot be null");
  214           }
  215   
  216           InputSource input = new InputSource(is);
  217           input.setSystemId(systemId);
  218           this.parse(input, dh);
  219       }
  220   
  221       /**
  222        * Parse the content described by the giving Uniform Resource
  223        * Identifier (URI) as XML using the specified
  224        * {@link org.xml.sax.HandlerBase}.
  225        * <i> Use of the DefaultHandler version of this method is recommended as
  226        * the <code>HandlerBase</code> class has been deprecated in SAX 2.0</i>
  227        *
  228        * @param uri The location of the content to be parsed.
  229        * @param hb The SAX HandlerBase to use.
  230        * 
  231        * @throws IllegalArgumentException If the uri is null.
  232        * @throws IOException If any IO errors occur.
  233        * @throws SAXException If any SAX errors occur during processing.
  234        * 
  235        * @see org.xml.sax.DocumentHandler
  236        */
  237       public void parse(String uri, HandlerBase hb)
  238           throws SAXException, IOException {
  239           if (uri == null) {
  240               throw new IllegalArgumentException("uri cannot be null");
  241           }
  242   
  243           InputSource input = new InputSource(uri);
  244           this.parse(input, hb);
  245       }
  246     
  247       /**
  248        * Parse the content described by the giving Uniform Resource
  249        * Identifier (URI) as XML using the specified
  250        * {@link org.xml.sax.helpers.DefaultHandler}.
  251        *
  252        * @param uri The location of the content to be parsed.
  253        * @param dh The SAX DefaultHandler to use.
  254        * 
  255        * @throws IllegalArgumentException If the uri is null.
  256        * @throws IOException If any IO errors occur.
  257        * @throws SAXException If any SAX errors occur during processing.
  258        * 
  259        * @see org.xml.sax.DocumentHandler
  260        */   
  261       public void parse(String uri, DefaultHandler dh)
  262           throws SAXException, IOException {
  263           if (uri == null) {
  264               throw new IllegalArgumentException("uri cannot be null");
  265           }
  266   
  267           InputSource input = new InputSource(uri);
  268           this.parse(input, dh);
  269       }
  270       
  271       /**
  272        * Parse the content of the file specified as XML using the
  273        * specified {@link org.xml.sax.HandlerBase}.
  274        * <i> Use of the DefaultHandler version of this method is recommended as
  275        * the HandlerBase class has been deprecated in SAX 2.0</i>
  276        *
  277        * @param f The file containing the XML to parse
  278        * @param hb The SAX HandlerBase to use.
  279        * 
  280        * @throws IllegalArgumentException If the File object is null.
  281        * @throws IOException If any IO errors occur.
  282        * @throws SAXException If any SAX errors occur during processing.
  283        * 
  284        * @see org.xml.sax.DocumentHandler
  285        */
  286       public void parse(File f, HandlerBase hb)
  287           throws SAXException, IOException {
  288           if (f == null) {
  289               throw new IllegalArgumentException("File cannot be null");
  290           }
  291           
  292           String escapedURI = FilePathToURI.filepath2URI(f.getAbsolutePath());
  293   
  294           if (DEBUG) {
  295               System.out.println("Escaped URI = " + escapedURI);
  296           }
  297   
  298           InputSource input = new InputSource(escapedURI);
  299           this.parse(input, hb);
  300       }
  301       
  302       /**
  303        * Parse the content of the file specified as XML using the
  304        * specified {@link org.xml.sax.helpers.DefaultHandler}.
  305        *
  306        * @param f The file containing the XML to parse
  307        * @param dh The SAX DefaultHandler to use.
  308        * 
  309        * @throws IllegalArgumentException If the File object is null.
  310        * @throws IOException If any IO errors occur.
  311        * @throws SAXException If any SAX errors occur during processing.
  312        * 
  313        * @see org.xml.sax.DocumentHandler
  314        */
  315       public void parse(File f, DefaultHandler dh)
  316           throws SAXException, IOException {
  317           if (f == null) {
  318               throw new IllegalArgumentException("File cannot be null");
  319           }
  320           
  321           String escapedURI = FilePathToURI.filepath2URI(f.getAbsolutePath());
  322   
  323           if (DEBUG) {
  324               System.out.println("Escaped URI = " + escapedURI);
  325           }
  326   
  327           InputSource input = new InputSource(escapedURI);
  328           this.parse(input, dh);
  329       }
  330       
  331       /**
  332        * Parse the content given {@link org.xml.sax.InputSource}
  333        * as XML using the specified
  334        * {@link org.xml.sax.HandlerBase}.
  335        * <i> Use of the DefaultHandler version of this method is recommended as
  336        * the HandlerBase class has been deprecated in SAX 2.0</i>
  337        *
  338        * @param is The InputSource containing the content to be parsed.
  339        * @param hb The SAX HandlerBase to use.
  340        * 
  341        * @throws IllegalArgumentException If the <code>InputSource</code> object
  342        *   is <code>null</code>.
  343        * @throws IOException If any IO errors occur.
  344        * @throws SAXException If any SAX errors occur during processing.
  345        * 
  346        * @see org.xml.sax.DocumentHandler
  347        */
  348       public void parse(InputSource is, HandlerBase hb)
  349           throws SAXException, IOException {
  350           if (is == null) {
  351               throw new IllegalArgumentException("InputSource cannot be null");
  352           }
  353   
  354           Parser parser = this.getParser();
  355           if (hb != null) {
  356               parser.setDocumentHandler(hb);
  357               parser.setEntityResolver(hb);
  358               parser.setErrorHandler(hb);
  359               parser.setDTDHandler(hb);
  360           }
  361           parser.parse(is);
  362       }
  363       
  364       /**
  365        * Parse the content given {@link org.xml.sax.InputSource}
  366        * as XML using the specified
  367        * {@link org.xml.sax.helpers.DefaultHandler}.
  368        *
  369        * @param is The InputSource containing the content to be parsed.
  370        * @param dh The SAX DefaultHandler to use.
  371        * 
  372        * @throws IllegalArgumentException If the <code>InputSource</code> object
  373        *   is <code>null</code>.
  374        * @throws IOException If any IO errors occur.
  375        * @throws SAXException If any SAX errors occur during processing.
  376        * 
  377        * @see org.xml.sax.DocumentHandler
  378        */
  379       public void parse(InputSource is, DefaultHandler dh)
  380           throws SAXException, IOException {
  381           if (is == null) {
  382               throw new IllegalArgumentException("InputSource cannot be null");
  383           }
  384   
  385           XMLReader reader = this.getXMLReader();
  386           if (dh != null) {
  387               reader.setContentHandler(dh);
  388               reader.setEntityResolver(dh);
  389               reader.setErrorHandler(dh);
  390               reader.setDTDHandler(dh);
  391           }
  392           reader.parse(is);
  393       }
  394       
  395       /**
  396        * Returns the SAX parser that is encapsulated by the
  397        * implementation of this class.
  398        *
  399        * @return The SAX parser that is encapsulated by the
  400        *         implementation of this class.
  401        * 
  402        * @throws SAXException If any SAX errors occur during processing.
  403        */
  404       public abstract org.xml.sax.Parser getParser() throws SAXException;
  405   
  406       /**
  407        * Returns the {@link org.xml.sax.XMLReader} that is encapsulated by the
  408        * implementation of this class.
  409        *
  410        * @return The XMLReader that is encapsulated by the
  411        *         implementation of this class.
  412        * 
  413        * @throws SAXException If any SAX errors occur during processing.
  414        */
  415   
  416       public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;
  417       
  418       /**
  419        * Indicates whether or not this parser is configured to
  420        * understand namespaces.
  421        *
  422        * @return true if this parser is configured to
  423        *         understand namespaces; false otherwise.
  424        */
  425       
  426       public abstract boolean isNamespaceAware();
  427   
  428       /**
  429        * Indicates whether or not this parser is configured to
  430        * validate XML documents.
  431        *
  432        * @return true if this parser is configured to
  433        *         validate XML documents; false otherwise.
  434        */
  435       
  436       public abstract boolean isValidating();
  437   
  438       /**
  439        * <p>Sets the particular property in the underlying implementation of
  440        * {@link org.xml.sax.XMLReader}.
  441        * A list of the core features and properties can be found at
  442        * <a href="http://sax.sourceforge.net/?selected=get-set">
  443        * http://sax.sourceforge.net/?selected=get-set</a>.</p>
  444        *
  445        * @param name The name of the property to be set.
  446        * @param value The value of the property to be set.
  447        * 
  448        * @throws SAXNotRecognizedException When the underlying XMLReader does
  449        *   not recognize the property name.
  450        * @throws SAXNotSupportedException When the underlying XMLReader
  451        *  recognizes the property name but doesn't support the property.
  452        *
  453        * @see org.xml.sax.XMLReader#setProperty
  454        */
  455       public abstract void setProperty(String name, Object value)
  456           throws SAXNotRecognizedException, SAXNotSupportedException;
  457   
  458       /**
  459        * <p>Returns the particular property requested for in the underlying
  460        * implementation of {@link org.xml.sax.XMLReader}.</p>
  461        *
  462        * @param name The name of the property to be retrieved.
  463        * @return Value of the requested property.
  464        *
  465        * @throws SAXNotRecognizedException When the underlying XMLReader does
  466        *    not recognize the property name.
  467        * @throws SAXNotSupportedException When the underlying XMLReader
  468        *  recognizes the property name but doesn't support the property.
  469        *
  470        * @see org.xml.sax.XMLReader#getProperty
  471        */
  472       public abstract Object getProperty(String name)
  473           throws SAXNotRecognizedException, SAXNotSupportedException;
  474   
  475       /** <p>Get a reference to the the {@link Schema} being used by
  476        * the XML processor.</p>
  477        *
  478        * <p>If no schema is being used, <code>null</code> is returned.</p>
  479        *
  480        * @return {@link Schema} being used or <code>null</code>
  481        *  if none in use
  482        * 
  483        * @throws UnsupportedOperationException
  484        *      For backward compatibility, when implementations for
  485        *      earlier versions of JAXP is used, this exception will be
  486        *      thrown.
  487        * 
  488        * @since 1.5
  489        */
  490       public Schema getSchema() {
  491           throw new UnsupportedOperationException(
  492               "This parser does not support specification \""
  493               + this.getClass().getPackage().getSpecificationTitle()
  494               + "\" version \""
  495               + this.getClass().getPackage().getSpecificationVersion()
  496               + "\""
  497               );
  498       }
  499       
  500       /**
  501        * <p>Get the XInclude processing mode for this parser.</p>
  502        * 
  503        * @return
  504        *      the return value of
  505        *      the {@link SAXParserFactory#isXIncludeAware()}
  506        *      when this parser was created from factory.
  507        * 
  508        * @throws UnsupportedOperationException
  509        *      For backward compatibility, when implementations for
  510        *      earlier versions of JAXP is used, this exception will be
  511        *      thrown.
  512        * 
  513        * @since 1.5
  514        * 
  515        * @see SAXParserFactory#setXIncludeAware(boolean)
  516        */
  517       public boolean isXIncludeAware() {
  518           throw new UnsupportedOperationException(
  519               "This parser does not support specification \""
  520               + this.getClass().getPackage().getSpecificationTitle()
  521               + "\" version \""
  522               + this.getClass().getPackage().getSpecificationVersion()
  523               + "\""
  524               );
  525       }
  526   }

Home » xml-commons-external-1.4.01-src » javax » xml » parsers » [javadoc | source]