View Javadoc

    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j.io;
    9
    10  import org.xml.sax.SAXException;
    11  import org.xml.sax.SAXNotRecognizedException;
    12  import org.xml.sax.SAXNotSupportedException;
    13  import org.xml.sax.XMLReader;
    14  import org.xml.sax.helpers.XMLReaderFactory;
    15
    16  /***
    17   * <p>
    18   * <code>SAXHelper</code> contains some helper methods for working with SAX
    19   * and XMLReader objects.
    20   * </p>
    21   *
    22   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    23   * @version $Revision: 1.18 $
    24   */
    25  class SAXHelper {
    26      private static boolean loggedWarning = true;
    27
    28      protected SAXHelper() {
    29      }
    30
    31      public static boolean setParserProperty(XMLReader reader,
    32              String propertyName, Object value) {
    33          try {
    34              reader.setProperty(propertyName, value);
    35
    36              return true;
    37          } catch (SAXNotSupportedException e) {
    38              // ignore
    39          } catch (SAXNotRecognizedException e) {
    40              // ignore
    41          }
    42
    43          return false;
    44      }
    45
    46      public static boolean setParserFeature(XMLReader reader,
    47              String featureName, boolean value) {
    48          try {
    49              reader.setFeature(featureName, value);
    50
    51              return true;
    52          } catch (SAXNotSupportedException e) {
    53              // ignore
    54          } catch (SAXNotRecognizedException e) {
    55              // ignore
    56          }
    57
    58          return false;
    59      }
    60
    61      /***
    62       * Creats a default XMLReader via the org.xml.sax.driver system property or
    63       * JAXP if the system property is not set.
    64       *
    65       * @param validating
    66       *            DOCUMENT ME!
    67       *
    68       * @return DOCUMENT ME!
    69       *
    70       * @throws SAXException
    71       *             DOCUMENT ME!
    72       */
    73      public static XMLReader createXMLReader(boolean validating)
    74              throws SAXException {
    75          XMLReader reader = null;
    76
    77          if (reader == null) {
    78              reader = createXMLReaderViaJAXP(validating, true);
    79          }
    80
    81          if (reader == null) {
    82              try {
    83                  reader = XMLReaderFactory.createXMLReader();
    84              } catch (Exception e) {
    85                  if (isVerboseErrorReporting()) {
    86                      // log all exceptions as warnings and carry
    87                      // on as we have a default SAX parser we can use
    88                      System.out.println("Warning: Caught exception attempting "
    89                              + "to use SAX to load a SAX XMLReader ");
    90                      System.out.println("Warning: Exception was: " + e);
    91                      System.out
    92                              .println("Warning: I will print the stack trace "
    93                                      + "then carry on using the default "
    94                                      + "SAX parser");
    95                      e.printStackTrace();
    96                  }
    97
    98                  throw new SAXException(e);
    99              }
    100         }
    101
    102         if (reader == null) {
    103             throw new SAXException("Couldn't create SAX reader");
    104         }
    105
    106         return reader;
    107     }
    108
    109     /***
    110      * This method attempts to use JAXP to locate the SAX2 XMLReader
    111      * implementation. This method uses reflection to avoid being dependent
    112      * directly on the JAXP classes.
    113      *
    114      * @param validating
    115      *            DOCUMENT ME!
    116      * @param namespaceAware
    117      *            DOCUMENT ME!
    118      *
    119      * @return DOCUMENT ME!
    120      */
    121     protected static XMLReader createXMLReaderViaJAXP(boolean validating,
    122             boolean namespaceAware) {
    123         // try use JAXP to load the XMLReader...
    124         try {
    125             return JAXPHelper.createXMLReader(validating, namespaceAware);
    126         } catch (Throwable e) {
    127             if (!loggedWarning) {
    128                 loggedWarning = true;
    129
    130                 if (isVerboseErrorReporting()) {
    131                     // log all exceptions as warnings and carry
    132                     // on as we have a default SAX parser we can use
    133                     System.out.println("Warning: Caught exception attempting "
    134                             + "to use JAXP to load a SAX XMLReader");
    135                     System.out.println("Warning: Exception was: " + e);
    136                     e.printStackTrace();
    137                 }
    138             }
    139         }
    140
    141         return null;
    142     }
    143
    144     protected static boolean isVerboseErrorReporting() {
    145         try {
    146             String flag = System.getProperty("org.dom4j.verbose");
    147
    148             if ((flag != null) && flag.equalsIgnoreCase("true")) {
    149                 return true;
    150             }
    151         } catch (Exception e) {
    152             // in case a security exception
    153             // happens in an applet or similar JVM
    154         }
    155
    156         return true;
    157     }
    158 }
    159
    160 /*
    161  * Redistribution and use of this software and associated documentation
    162  * ("Software"), with or without modification, are permitted provided that the
    163  * following conditions are met:
    164  *
    165  * 1. Redistributions of source code must retain copyright statements and
    166  * notices. Redistributions must also contain a copy of this document.
    167  *
    168  * 2. Redistributions in binary form must reproduce the above copyright notice,
    169  * this list of conditions and the following disclaimer in the documentation
    170  * and/or other materials provided with the distribution.
    171  *
    172  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    173  * from this Software without prior written permission of MetaStuff, Ltd. For
    174  * written permission, please contact dom4j-info@metastuff.com.
    175  *
    176  * 4. Products derived from this Software may not be called "DOM4J" nor may
    177  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    178  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    179  *
    180  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    181  *
    182  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    183  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    184  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    185  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    186  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    187  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    188  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    189  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    190  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    191  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    192  * POSSIBILITY OF SUCH DAMAGE.
    193  *
    194  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    195  */