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