Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/io/SAXHelper.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   * 
   * This software is open source. 
   * See the bottom of this file for the licence.
   * 
   * $Id: SAXHelper.java,v 1.16 2004/11/12 21:31:08 maartenc Exp $
   */
 
  package org.dom4j.io;
 
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXNotRecognizedException;
  import org.xml.sax.SAXNotSupportedException;
  import org.xml.sax.XMLReader;
  import org.xml.sax.helpers.XMLReaderFactory;
 
  /** <p><code>SAXHelper</code> contains some helper methods for working with 
    * SAX and XMLReader objects.
    *
    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
    * @version $Revision: 1.16 $
    */
0x   class SAXHelper {
 
      private static boolean loggedWarning = true;
      
      public static boolean setParserProperty(XMLReader reader, String propertyName, Object value) {    
          try {
22370x               reader.setProperty(propertyName, value);
11186x               return true;
          } 
          catch (SAXNotSupportedException e) {
              // ignore
          } 
          catch (SAXNotRecognizedException e) {
              // ignore
0x           }
11184x           return false;
      }
 
      public static boolean setParserFeature(XMLReader reader, String featureName, boolean value) {    
          try {
44736x               reader.setFeature(featureName, value);
33552x               return true;
          } 
          catch (SAXNotSupportedException e) {
              // ignore
          } 
          catch (SAXNotRecognizedException e) {
              // ignore
0x           }
11184x           return false;
      }
 
      /** Creats a default XMLReader via the org.xml.sax.driver system property 
        * or JAXP if the system property is not set.
        */
      public static XMLReader createXMLReader(boolean validating) throws SAXException {
11170x           XMLReader reader = null;
1/2 11170x           if ( reader == null ) {
11170x               reader = createXMLReaderViaJAXP( validating, true );
          }
1/2 11170x           if ( reader == null ) {
              try {
0x                   reader = XMLReaderFactory.createXMLReader();
              }
              catch (Exception e) {
0/2 0x                   if ( isVerboseErrorReporting() ) {
                      // log all exceptions as warnings and carry
                      // on as we have a default SAX parser we can use
0x                       System.out.println( 
                          "Warning: Caught exception attempting to use SAX to "
                           + "load a SAX XMLReader " 
                      );
0x                       System.out.println( "Warning: Exception was: " + e );
0x                       System.out.println( 
                          "Warning: I will print the stack trace then carry on "
                           + "using the default SAX parser" 
                       );
0x                       e.printStackTrace();
                  }
0x                   throw new SAXException(e);
0x               }
          }
1/2 11170x           if ( reader == null ) {
0x               throw new SAXException("Couldn't create SAX reader");
          }
11170x           return reader;
      }
 
      /** This method attempts to use JAXP to locate the  
        * SAX2 XMLReader implementation.  
        * This method uses reflection to avoid being dependent directly
        * on the JAXP classes.
        */
      protected static XMLReader createXMLReaderViaJAXP(boolean validating, boolean namespaceAware) {
          // try use JAXP to load the XMLReader...
          try {
11170x               return JAXPHelper.createXMLReader( validating, namespaceAware );
          }
          catch (Throwable e) {
0/2 0x               if ( ! loggedWarning ) {                    
0x                   loggedWarning = true;
0/2 0x                   if ( isVerboseErrorReporting() ) {
                      // log all exceptions as warnings and carry
                      // on as we have a default SAX parser we can use
0x                       System.out.println( 
                          "Warning: Caught exception attempting to use JAXP to "
                           + "load a SAX XMLReader " 
                      );
0x                       System.out.println( "Warning: Exception was: " + e );
0x                       e.printStackTrace();
                  }
              }
          }
0x           return null;
      }
 
      protected static boolean isVerboseErrorReporting() {
          try {
0x               String flag = System.getProperty( "org.dom4j.verbose" );
0/4 0x               if ( flag != null && flag.equalsIgnoreCase( "true" ) ) {
0x                   return true;
              }
          }
          catch (Exception e) {
              // in case a security exception
              // happens in an applet or similar JVM
0x           }
0x           return true;
      }
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * $Id: SAXHelper.java,v 1.16 2004/11/12 21:31:08 maartenc Exp $
   */