001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.engine.util;
046:
047: import org.obe.OBERuntimeException;
048: import org.xml.sax.SAXException;
049: import org.xml.sax.SAXNotRecognizedException;
050: import org.xml.sax.SAXNotSupportedException;
051:
052: import javax.xml.parsers.*;
053: import javax.xml.transform.*;
054:
055: public class XMLHelper {
056: // private static final Log _logger = LogFactory.getLog(XMLHelper.class);
057: private static final String[][] JAXP_CONFIGURATION = {
058: { "javax.xml.parsers.SAXParserFactory",
059: "apache.xerces.jaxp.SAXParserFactoryImpl" },
060: { "javax.xml.parsers.DocumentBuilderFactory",
061: "apache.xerces.jaxp.DocumentBuilderFactoryImpl" },
062: { "javax.xml.transform.TransformerFactory",
063: "apache.xalan.processor.TransformerFactoryImpl" } };
064: private static final SAXParserFactory saxFactory;
065: private static final DocumentBuilderFactory domFactory;
066: private static final DocumentBuilderFactory vDomFactory;
067: private static final TransformerFactory tFactory;
068:
069: static {
070: try {
071: for (int i = 0, n = JAXP_CONFIGURATION.length; i < n; i++) {
072: if (System.getProperty(JAXP_CONFIGURATION[i][0]) == null) {
073: System.setProperty(JAXP_CONFIGURATION[i][0],
074: JAXP_CONFIGURATION[i][1]);
075: }
076: }
077:
078: saxFactory = SAXParserFactory.newInstance();
079: saxFactory.setFeature(
080: "http://xml.org/sax/features/namespaces", true);
081: saxFactory.setFeature(
082: "http://xml.org/sax/features/namespace-prefixes",
083: false);
084: domFactory = DocumentBuilderFactory.newInstance();
085: domFactory.setNamespaceAware(true);
086: vDomFactory = DocumentBuilderFactory.newInstance();
087: vDomFactory.setNamespaceAware(true);
088: vDomFactory.setValidating(true);
089: tFactory = TransformerFactory.newInstance();
090: } catch (SAXNotSupportedException e) {
091: throw new OBERuntimeException(e);
092: } catch (ParserConfigurationException e) {
093: throw new OBERuntimeException(e);
094: } catch (SAXNotRecognizedException e) {
095: throw new OBERuntimeException(e);
096: }
097: }
098:
099: public static synchronized SAXParser getSAXParser()
100: throws ParserConfigurationException, SAXException {
101:
102: return saxFactory.newSAXParser();
103: }
104:
105: public static synchronized DocumentBuilder getDocumentBuilder()
106: throws ParserConfigurationException {
107:
108: return getDocumentBuilder(false);
109: }
110:
111: public static synchronized DocumentBuilder getDocumentBuilder(
112: boolean validating) throws ParserConfigurationException {
113:
114: return (validating ? vDomFactory : domFactory)
115: .newDocumentBuilder();
116: }
117:
118: public static synchronized Transformer getTransformer(
119: URIResolver uriResolver)
120: throws TransformerConfigurationException {
121:
122: tFactory.setURIResolver(uriResolver);
123: return tFactory.newTransformer();
124: }
125:
126: public static synchronized Transformer getTransformer(Source src)
127: throws TransformerConfigurationException {
128:
129: tFactory.setURIResolver(null);
130: return tFactory.newTransformer(src);
131: }
132:
133: public static synchronized Transformer getTransformer(
134: URIResolver uriResolver, Source src)
135: throws TransformerConfigurationException {
136:
137: tFactory.setURIResolver(uriResolver);
138: return tFactory.newTransformer(src);
139: }
140:
141: public static synchronized Templates getTemplates(
142: URIResolver uriResolver, Source src)
143: throws TransformerConfigurationException {
144:
145: tFactory.setURIResolver(uriResolver);
146: return tFactory.newTemplates(src);
147: }
148:
149: private XMLHelper() {
150: }
151: }
|