1
2
3
4
5
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
39 } catch (SAXNotRecognizedException e) {
40
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
54 } catch (SAXNotRecognizedException e) {
55
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
87
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
124 try {
125 return JAXPHelper.createXMLReader(validating, namespaceAware);
126 } catch (Throwable e) {
127 if (!loggedWarning) {
128 loggedWarning = true;
129
130 if (isVerboseErrorReporting()) {
131
132
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
153
154 }
155
156 return true;
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
185
186
187
188
189
190
191
192
193
194
195