1 /** 2 * 3 * Copyright 2006 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * 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; 20 import javax.xml.parsers.DocumentBuilderFactory; 21 import javax.xml.parsers.FactoryConfigurationError; 22 import javax.xml.transform.TransformerFactory; 23 import javax.xml.transform.TransformerFactoryConfigurationError; 24 25 import org.apache.geronimo.kernel.ClassLoading; 26 27 /** 28 * @version $Rev: 437254 $ $Date: 2006-08-26 17:07:55 -0700 (Sat, 26 Aug 2006) $ 29 */ 30 public final class XmlUtil { 31 public static final String DOCUMENT_BUILDER_FACTORY = "geronimo.xml.parsers.DocumentBuilderFactory"; 32 public static final String SAX_PARSER_FACTORY = "geronimo.xml.parsers.SAXParserFactory"; 33 public static final String TRANSFORMER_FACTORY = "geronimo.xml.transform.TransformerFactory"; 34 35 private XmlUtil() { 36 } 37 38 public static DocumentBuilderFactory newDocumentBuilderFactory() { 39 return newDocumentBuilderFactory(getClassLoader()); 40 } 41 42 public static DocumentBuilderFactory newDocumentBuilderFactory(ClassLoader classLoader) { 43 String documentBuilderName = getSystemProperty(DOCUMENT_BUILDER_FACTORY); 44 if (documentBuilderName != null && documentBuilderName.length() != 0) { 45 try { 46 Class documentBuilderClass = ClassLoading.loadClass(documentBuilderName, classLoader); 47 DocumentBuilderFactory documentBuilderFactory = (DocumentBuilderFactory) documentBuilderClass.newInstance(); 48 return documentBuilderFactory; 49 } catch (Exception e) { 50 throw new FactoryConfigurationError(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 newSAXParserFactory() { 59 return newSAXParserFactory(getClassLoader()); 60 } 61 62 public static SAXParserFactory newSAXParserFactory(ClassLoader classLoader) { 63 String saxParserName = getSystemProperty(SAX_PARSER_FACTORY); 64 if (saxParserName != null && saxParserName.length() != 0) { 65 try { 66 Class saxParserClass = ClassLoading.loadClass(saxParserName, classLoader); 67 SAXParserFactory saxParserFactory = (SAXParserFactory) saxParserClass.newInstance(); 68 return saxParserFactory; 69 } catch (Exception e) { 70 throw new FactoryConfigurationError(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 newTransformerFactory() { 79 return newTransformerFactory(getClassLoader()); 80 } 81 82 public static TransformerFactory newTransformerFactory(ClassLoader classLoader) { 83 String transformerName = getSystemProperty(TRANSFORMER_FACTORY); 84 if (transformerName != null && transformerName.length() != 0) { 85 try { 86 Class transformerClass = ClassLoading.loadClass(transformerName, classLoader); 87 TransformerFactory transformerFactory = (TransformerFactory) transformerClass.newInstance(); 88 return transformerFactory; 89 } catch (Exception e) { 90 throw new TransformerFactoryConfigurationError(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 getClassLoader() { 99 ClassLoader 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 getSystemProperty(String key) { 108 String value = System.getProperty(key); 109 if (value != null) value = value.trim(); 110 return value; 111 } 112 }