comparison NGSrich_0.5.5/src/org/jdom/input/JAXPParserFactory.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:89ad0a9cca52
1 /*--
2
3 $Id: JAXPParserFactory.java,v 1.6 2007/11/10 05:29:00 jhunter Exp $
4
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11
12 1. Redistributions of source code must retain the above copyright
13 notice, this list of conditions, and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions, and the disclaimer that follows
17 these conditions in the documentation and/or other materials
18 provided with the distribution.
19
20 3. The name "JDOM" must not be used to endorse or promote products
21 derived from this software without prior written permission. For
22 written permission, please contact <request_AT_jdom_DOT_org>.
23
24 4. Products derived from this software may not be called "JDOM", nor
25 may "JDOM" appear in their name, without prior written permission
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
27
28 In addition, we request (but do not require) that you include in the
29 end-user documentation provided with the redistribution and/or in the
30 software itself an acknowledgement equivalent to the following:
31 "This product includes software developed by the
32 JDOM Project (http://www.jdom.org/)."
33 Alternatively, the acknowledgment may be graphical using the logos
34 available at http://www.jdom.org/images/logos.
35
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 SUCH DAMAGE.
48
49 This software consists of voluntary contributions made by many
50 individuals on behalf of the JDOM Project and was originally
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53 on the JDOM Project, please see <http://www.jdom.org/>.
54
55 */
56
57 package org.jdom.input;
58
59 import java.util.*;
60
61 import javax.xml.parsers.*;
62
63 import org.jdom.*;
64 import org.xml.sax.*;
65
66 /**
67 * A non-public utility class to allocate JAXP SAX parsers.
68 *
69 * @version $Revision: 1.6 $, $Date: 2007/11/10 05:29:00 $
70 * @author Laurent Bihanic
71 */
72 class JAXPParserFactory { // package protected
73
74 private static final String CVS_ID =
75 "@(#) $RCSfile: JAXPParserFactory.java,v $ $Revision: 1.6 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1_1 $";
76
77 /** JAXP 1.2 schema language property id. */
78 private static final String JAXP_SCHEMA_LANGUAGE_PROPERTY =
79 "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
80
81 /** JAXP 1.2 schema location property id. */
82 private static final String JAXP_SCHEMA_LOCATION_PROPERTY =
83 "http://java.sun.com/xml/jaxp/properties/schemaSource";
84
85 /**
86 * Private constructor to forbid allocating instances of this utility
87 * class.
88 */
89 private JAXPParserFactory() {
90 // Never called.
91 }
92
93 /* Implementor's note regarding createParser() design: The features and
94 properties are normally set in SAXBuilder, but we take them in
95 createParser() as well because some features or properties may need to be
96 applied during the JAXP parser construction. Today, for example, properties
97 is used as it's the only way to configure schema validation: JAXP defines
98 schema validation properties but SAX does not. This reflects in the Apache
99 Xerces implementation where the SAXParser implementation supports the JAXP
100 properties but the XMLReader does not. Hence, configuring schema validation
101 must be done on the SAXParser object which is only visible in
102 JAXParserFactory. Features is also passed in case some future JAXP release
103 defines JAXP-specific features.
104 */
105
106 /**
107 * Creates a SAX parser allocated through the configured JAXP SAX
108 * parser factory.
109 *
110 * @param validating whether a validating parser is requested.
111 * @param features the user-defined SAX features.
112 * @param properties the user-defined SAX properties.
113 *
114 * @return a configured XMLReader.
115 *
116 * @throws JDOMException if any error occurred when allocating or
117 * configuring the JAXP SAX parser.
118 */
119 public static XMLReader createParser(boolean validating,
120 Map features, Map properties) throws JDOMException {
121 try {
122 SAXParser parser = null;
123
124 // Allocate and configure JAXP SAX parser factory.
125 SAXParserFactory factory = SAXParserFactory.newInstance();
126 factory.setValidating(validating);
127 factory.setNamespaceAware(true);
128
129 try {
130 // Allocate parser.
131 parser = factory.newSAXParser();
132 }
133 catch (ParserConfigurationException e) {
134 throw new JDOMException("Could not allocate JAXP SAX Parser", e);
135 }
136
137 // Set user-defined JAXP properties (if any)
138 setProperty(parser, properties, JAXP_SCHEMA_LANGUAGE_PROPERTY);
139 setProperty(parser, properties, JAXP_SCHEMA_LOCATION_PROPERTY);
140
141 // Return configured SAX XMLReader.
142 return parser.getXMLReader();
143 }
144 catch (SAXException e) {
145 throw new JDOMException("Could not allocate JAXP SAX Parser", e);
146 }
147 }
148
149 /**
150 * Sets a property on a JAXP SAX parser object if and only if it
151 * is declared in the user-defined properties.
152 *
153 * @param parser the JAXP SAX parser to configure.
154 * @param properties the user-defined SAX properties.
155 * @param name the name of the property to set.
156 *
157 * @throws JDOMException if any error occurred while configuring
158 * the property.
159 */
160 private static void setProperty(SAXParser parser,
161 Map properties, String name) throws JDOMException {
162 try {
163 if (properties.containsKey(name)) {
164 parser.setProperty(name, properties.get(name));
165 }
166 }
167 catch (SAXNotSupportedException e) {
168 throw new JDOMException(
169 name + " property not supported for JAXP parser " +
170 parser.getClass().getName());
171 }
172 catch (SAXNotRecognizedException e) {
173 throw new JDOMException(
174 name + " property not recognized for JAXP parser " +
175 parser.getClass().getName());
176 }
177 }
178 }
179