1
2
3
4
5
6
7
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 public static boolean setParserProperty(XMLReader reader, String propertyName, Object value) {
30 try {
31 reader.setProperty(propertyName, value);
32 return true;
33 }
34 catch (SAXNotSupportedException e) {
35
36 }
37 catch (SAXNotRecognizedException e) {
38
39 }
40 return false;
41 }
42
43 public static boolean setParserFeature(XMLReader reader, String featureName, boolean value) {
44 try {
45 reader.setFeature(featureName, value);
46 return true;
47 }
48 catch (SAXNotSupportedException e) {
49
50 }
51 catch (SAXNotRecognizedException e) {
52
53 }
54 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 public static XMLReader createXMLReader(boolean validating) throws SAXException {
61 XMLReader reader = null;
62 if ( reader == null ) {
63 reader = createXMLReaderViaJAXP( validating, true );
64 }
65 if ( reader == null ) {
66 try {
67 reader = XMLReaderFactory.createXMLReader();
68 }
69 catch (Exception e) {
70 if ( isVerboseErrorReporting() ) {
71
72
73 System.out.println(
74 "Warning: Caught exception attempting to use SAX to "
75 + "load a SAX XMLReader "
76 );
77 System.out.println( "Warning: Exception was: " + e );
78 System.out.println(
79 "Warning: I will print the stack trace then carry on "
80 + "using the default SAX parser"
81 );
82 e.printStackTrace();
83 }
84 }
85 }
86 if ( reader == null ) {
87 System.out.println(
88 "Warning: Error occurred using SAX to load a SAXParser. " +
89 "Will use Aelfred instead");
90 reader = new SAXDriver();
91 }
92 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 protected static XMLReader createXMLReaderViaJAXP(boolean validating, boolean namespaceAware) {
101
102 try {
103 return JAXPHelper.createXMLReader( validating, namespaceAware );
104 }
105 catch (Throwable e) {
106 if ( ! loggedWarning ) {
107 loggedWarning = true;
108 if ( isVerboseErrorReporting() ) {
109
110
111 System.out.println(
112 "Warning: Caught exception attempting to use JAXP to "
113 + "load a SAX XMLReader "
114 );
115 System.out.println( "Warning: Exception was: " + e );
116 e.printStackTrace();
117 }
118 }
119 }
120 return null;
121 }
122
123 protected static boolean isVerboseErrorReporting() {
124 try {
125 String flag = System.getProperty( "org.dom4j.verbose" );
126 if ( flag != null && flag.equalsIgnoreCase( "true" ) ) {
127 return true;
128 }
129 }
130 catch (Exception e) {
131
132
133 }
134 return true;
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184