KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > util > XmlUtil


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel.util;
18
19 import javax.xml.parsers.SAXParserFactory JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
22 import javax.xml.transform.TransformerFactory JavaDoc;
23 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
24
25 import org.apache.geronimo.kernel.ClassLoading;
26
27 /**
28  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
29  */

30 public final class XmlUtil {
31     public static final String JavaDoc DOCUMENT_BUILDER_FACTORY = "geronimo.xml.parsers.DocumentBuilderFactory";
32     public static final String JavaDoc SAX_PARSER_FACTORY = "geronimo.xml.parsers.SAXParserFactory";
33     public static final String JavaDoc TRANSFORMER_FACTORY = "geronimo.xml.transform.TransformerFactory";
34
35     private XmlUtil() {
36     }
37
38     public static DocumentBuilderFactory JavaDoc newDocumentBuilderFactory() {
39         return newDocumentBuilderFactory(getClassLoader());
40     }
41
42     public static DocumentBuilderFactory JavaDoc newDocumentBuilderFactory(ClassLoader JavaDoc classLoader) {
43         String JavaDoc documentBuilderName = getSystemProperty(DOCUMENT_BUILDER_FACTORY);
44         if (documentBuilderName != null && documentBuilderName.length() != 0) {
45             try {
46                 Class JavaDoc documentBuilderClass = ClassLoading.loadClass(documentBuilderName, classLoader);
47                 DocumentBuilderFactory JavaDoc documentBuilderFactory = (DocumentBuilderFactory JavaDoc) documentBuilderClass.newInstance();
48                 return documentBuilderFactory;
49             } catch (Exception JavaDoc e) {
50                 throw new FactoryConfigurationError JavaDoc(e, "Unable to create DocumentBuilderFactory " +
51                         documentBuilderName + ", which was specified in the " + DOCUMENT_BUILDER_FACTORY + " system property");
52             }
53         }
54
55         return DocumentBuilderFactory.newInstance();
56     }
57
58     public static SAXParserFactory JavaDoc newSAXParserFactory() {
59         return newSAXParserFactory(getClassLoader());
60     }
61
62     public static SAXParserFactory JavaDoc newSAXParserFactory(ClassLoader JavaDoc classLoader) {
63         String JavaDoc saxParserName = getSystemProperty(SAX_PARSER_FACTORY);
64         if (saxParserName != null && saxParserName.length() != 0) {
65             try {
66                 Class JavaDoc saxParserClass = ClassLoading.loadClass(saxParserName, classLoader);
67                 SAXParserFactory JavaDoc saxParserFactory = (SAXParserFactory JavaDoc) saxParserClass.newInstance();
68                 return saxParserFactory;
69             } catch (Exception JavaDoc e) {
70                 throw new FactoryConfigurationError JavaDoc(e, "Unable to create SAXParserFactory " +
71                         saxParserName + ", which was specified in the " + SAX_PARSER_FACTORY + " system property");
72             }
73         }
74
75         return SAXParserFactory.newInstance();
76     }
77
78     public static TransformerFactory JavaDoc newTransformerFactory() {
79         return newTransformerFactory(getClassLoader());
80     }
81
82     public static TransformerFactory JavaDoc newTransformerFactory(ClassLoader JavaDoc classLoader) {
83         String JavaDoc transformerName = getSystemProperty(TRANSFORMER_FACTORY);
84         if (transformerName != null && transformerName.length() != 0) {
85             try {
86                 Class JavaDoc transformerClass = ClassLoading.loadClass(transformerName, classLoader);
87                 TransformerFactory JavaDoc transformerFactory = (TransformerFactory JavaDoc) transformerClass.newInstance();
88                 return transformerFactory;
89             } catch (Exception JavaDoc e) {
90                 throw new TransformerFactoryConfigurationError JavaDoc(e, "Unable to create TransformerFactory " +
91                         transformerName + ", which was specified in the " + TRANSFORMER_FACTORY + " system property");
92             }
93         }
94
95         return TransformerFactory.newInstance();
96     }
97
98     private static ClassLoader JavaDoc getClassLoader() {
99         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
100         if (classLoader != null) {
101             return classLoader;
102         } else {
103             return XmlUtil.class.getClassLoader();
104         }
105     }
106
107     private static String JavaDoc getSystemProperty(String JavaDoc key) {
108         String JavaDoc value = System.getProperty(key);
109         if (value != null) value = value.trim();
110         return value;
111     }
112 }
113
Popular Tags