Home » geronimo-2.2-source-release » org.apache.geronimo.kernel.util » [javadoc | source]

    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;
   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: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 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   }

Home » geronimo-2.2-source-release » org.apache.geronimo.kernel.util » [javadoc | source]