001 /** 002 * 003 * Copyright 2006 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 */ 017 package org.apache.geronimo.kernel.util; 018 019 import javax.xml.parsers.SAXParserFactory; 020 import javax.xml.parsers.DocumentBuilderFactory; 021 import javax.xml.parsers.FactoryConfigurationError; 022 import javax.xml.transform.TransformerFactory; 023 import javax.xml.transform.TransformerFactoryConfigurationError; 024 025 import org.apache.geronimo.kernel.ClassLoading; 026 027 /** 028 * @version $Rev: 437254 $ $Date: 2006-08-26 17:07:55 -0700 (Sat, 26 Aug 2006) $ 029 */ 030 public final class XmlUtil { 031 public static final String DOCUMENT_BUILDER_FACTORY = "geronimo.xml.parsers.DocumentBuilderFactory"; 032 public static final String SAX_PARSER_FACTORY = "geronimo.xml.parsers.SAXParserFactory"; 033 public static final String TRANSFORMER_FACTORY = "geronimo.xml.transform.TransformerFactory"; 034 035 private XmlUtil() { 036 } 037 038 public static DocumentBuilderFactory newDocumentBuilderFactory() { 039 return newDocumentBuilderFactory(getClassLoader()); 040 } 041 042 public static DocumentBuilderFactory newDocumentBuilderFactory(ClassLoader classLoader) { 043 String documentBuilderName = getSystemProperty(DOCUMENT_BUILDER_FACTORY); 044 if (documentBuilderName != null && documentBuilderName.length() != 0) { 045 try { 046 Class documentBuilderClass = ClassLoading.loadClass(documentBuilderName, classLoader); 047 DocumentBuilderFactory documentBuilderFactory = (DocumentBuilderFactory) documentBuilderClass.newInstance(); 048 return documentBuilderFactory; 049 } catch (Exception e) { 050 throw new FactoryConfigurationError(e, "Unable to create DocumentBuilderFactory " + 051 documentBuilderName + ", which was specified in the " + DOCUMENT_BUILDER_FACTORY + " system property"); 052 } 053 } 054 055 return DocumentBuilderFactory.newInstance(); 056 } 057 058 public static SAXParserFactory newSAXParserFactory() { 059 return newSAXParserFactory(getClassLoader()); 060 } 061 062 public static SAXParserFactory newSAXParserFactory(ClassLoader classLoader) { 063 String saxParserName = getSystemProperty(SAX_PARSER_FACTORY); 064 if (saxParserName != null && saxParserName.length() != 0) { 065 try { 066 Class saxParserClass = ClassLoading.loadClass(saxParserName, classLoader); 067 SAXParserFactory saxParserFactory = (SAXParserFactory) saxParserClass.newInstance(); 068 return saxParserFactory; 069 } catch (Exception e) { 070 throw new FactoryConfigurationError(e, "Unable to create SAXParserFactory " + 071 saxParserName + ", which was specified in the " + SAX_PARSER_FACTORY + " system property"); 072 } 073 } 074 075 return SAXParserFactory.newInstance(); 076 } 077 078 public static TransformerFactory newTransformerFactory() { 079 return newTransformerFactory(getClassLoader()); 080 } 081 082 public static TransformerFactory newTransformerFactory(ClassLoader classLoader) { 083 String transformerName = getSystemProperty(TRANSFORMER_FACTORY); 084 if (transformerName != null && transformerName.length() != 0) { 085 try { 086 Class transformerClass = ClassLoading.loadClass(transformerName, classLoader); 087 TransformerFactory transformerFactory = (TransformerFactory) transformerClass.newInstance(); 088 return transformerFactory; 089 } catch (Exception e) { 090 throw new TransformerFactoryConfigurationError(e, "Unable to create TransformerFactory " + 091 transformerName + ", which was specified in the " + TRANSFORMER_FACTORY + " system property"); 092 } 093 } 094 095 return TransformerFactory.newInstance(); 096 } 097 098 private static ClassLoader getClassLoader() { 099 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 }