001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.kernel.util;
017:
018: import javax.xml.parsers.SAXParserFactory;
019: import javax.xml.parsers.DocumentBuilderFactory;
020: import javax.xml.parsers.FactoryConfigurationError;
021: import javax.xml.transform.TransformerFactory;
022: import javax.xml.transform.TransformerFactoryConfigurationError;
023:
024: import org.apache.geronimo.kernel.ClassLoading;
025:
026: /**
027: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
028: */
029: public final class XmlUtil {
030: public static final String DOCUMENT_BUILDER_FACTORY = "geronimo.xml.parsers.DocumentBuilderFactory";
031: public static final String SAX_PARSER_FACTORY = "geronimo.xml.parsers.SAXParserFactory";
032: public static final String TRANSFORMER_FACTORY = "geronimo.xml.transform.TransformerFactory";
033:
034: private XmlUtil() {
035: }
036:
037: public static DocumentBuilderFactory newDocumentBuilderFactory() {
038: return newDocumentBuilderFactory(getClassLoader());
039: }
040:
041: public static DocumentBuilderFactory newDocumentBuilderFactory(
042: ClassLoader classLoader) {
043: String documentBuilderName = getSystemProperty(DOCUMENT_BUILDER_FACTORY);
044: if (documentBuilderName != null
045: && documentBuilderName.length() != 0) {
046: try {
047: Class documentBuilderClass = ClassLoading.loadClass(
048: documentBuilderName, classLoader);
049: DocumentBuilderFactory documentBuilderFactory = (DocumentBuilderFactory) documentBuilderClass
050: .newInstance();
051: return documentBuilderFactory;
052: } catch (Exception e) {
053: throw new FactoryConfigurationError(e,
054: "Unable to create DocumentBuilderFactory "
055: + documentBuilderName
056: + ", which was specified in the "
057: + DOCUMENT_BUILDER_FACTORY
058: + " system property");
059: }
060: }
061:
062: return DocumentBuilderFactory.newInstance();
063: }
064:
065: public static SAXParserFactory newSAXParserFactory() {
066: return newSAXParserFactory(getClassLoader());
067: }
068:
069: public static SAXParserFactory newSAXParserFactory(
070: ClassLoader classLoader) {
071: String saxParserName = getSystemProperty(SAX_PARSER_FACTORY);
072: if (saxParserName != null && saxParserName.length() != 0) {
073: try {
074: Class saxParserClass = ClassLoading.loadClass(
075: saxParserName, classLoader);
076: SAXParserFactory saxParserFactory = (SAXParserFactory) saxParserClass
077: .newInstance();
078: return saxParserFactory;
079: } catch (Exception e) {
080: throw new FactoryConfigurationError(e,
081: "Unable to create SAXParserFactory "
082: + saxParserName
083: + ", which was specified in the "
084: + SAX_PARSER_FACTORY
085: + " system property");
086: }
087: }
088:
089: return SAXParserFactory.newInstance();
090: }
091:
092: public static TransformerFactory newTransformerFactory() {
093: return newTransformerFactory(getClassLoader());
094: }
095:
096: public static TransformerFactory newTransformerFactory(
097: ClassLoader classLoader) {
098: String transformerName = getSystemProperty(TRANSFORMER_FACTORY);
099: if (transformerName != null && transformerName.length() != 0) {
100: try {
101: Class transformerClass = ClassLoading.loadClass(
102: transformerName, classLoader);
103: TransformerFactory transformerFactory = (TransformerFactory) transformerClass
104: .newInstance();
105: return transformerFactory;
106: } catch (Exception e) {
107: throw new TransformerFactoryConfigurationError(e,
108: "Unable to create TransformerFactory "
109: + transformerName
110: + ", which was specified in the "
111: + TRANSFORMER_FACTORY
112: + " system property");
113: }
114: }
115:
116: return TransformerFactory.newInstance();
117: }
118:
119: private static ClassLoader getClassLoader() {
120: ClassLoader classLoader = Thread.currentThread()
121: .getContextClassLoader();
122: if (classLoader != null) {
123: return classLoader;
124: } else {
125: return XmlUtil.class.getClassLoader();
126: }
127: }
128:
129: private static String getSystemProperty(String key) {
130: String value = System.getProperty(key);
131: if (value != null)
132: value = value.trim();
133: return value;
134: }
135: }
|