donaldp     01/12/14 20:55:59

  Added:       src/java/org/apache/avalon/phoenix/tools/configuration
                        ConfigurationBuilder.java DTDInfo.java
                        DTDResolver.java
  Log:
  Add in some basic utilities to resolve DTDs to resources inside the jar.
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/tools/configuration/ConfigurationBuilder.java
  
  Index: ConfigurationBuilder.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.phoenix.tools.configuration;
  
  import java.io.IOException;
  import java.io.InputStream;
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.XMLReader;
  
  /**
   * Utility class used to load Configuration trees from XML files.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2001/12/15 04:55:59 $
   */
  public class ConfigurationBuilder
  {
      private static final DTDInfo[] c_dtdInfo = new DTDInfo[]
      {
          new DTDInfo( "-//PHOENIX/Block Info DTD Version 1.0//EN",
                       "http://jakarta.apache.org/phoenix/blockinfo_1_0.dtd";,
                       "org/apache/avalon/phoenix/tools/blockinfo.dtd" ),
          new DTDInfo( "-//PHOENIX/Assembly DTD Version 1.0//EN",
                       "http://jakarta.apache.org/phoenix/assembly_1_0.dtd";,
                       "/org/apache/avalon/phoenix/tools/assembly.dtd" )
      };
  
      private static final DTDResolver c_resolver =
          new DTDResolver( c_dtdInfo, 
ConfigurationBuilder.class.getClassLoader() );
  
      /**
       * Private constructor to block instantiation.
       */
      private ConfigurationBuilder()
      {
      }
  
      /**
       * Utility method to create a new XML reader.
       */
      private static XMLReader createXMLReader()
          throws SAXException, ParserConfigurationException
      {
          final SAXParserFactory saxParserFactory = 
SAXParserFactory.newInstance();
          saxParserFactory.setNamespaceAware( false );
          final SAXParser saxParser = saxParserFactory.newSAXParser();
          return saxParser.getXMLReader();
      }
  
      /**
       * Internally sets up the XMLReader
       */
      private static void setupXMLReader( final XMLReader reader,
                                          final SAXConfigurationHandler handler 
)
      {
          reader.setEntityResolver( c_resolver );
          reader.setContentHandler( handler );
          reader.setErrorHandler( handler );
      }
  
      /**
       * Build a configuration object using an InputStream.
       */
      public static Configuration build( final InputStream inputStream )
          throws SAXException, ParserConfigurationException, IOException, 
ConfigurationException
      {
          return build( new InputSource( inputStream ) );
      }
  
      /**
       * Build a configuration object using an URI
       */
      public static Configuration build( final String uri )
          throws SAXException, ParserConfigurationException, IOException, 
ConfigurationException
      {
          return build( new InputSource( uri ) );
      }
  
      /**
       * Build a configuration object using an XML InputSource object
       */
      public static Configuration build( final InputSource input )
          throws SAXException, ParserConfigurationException, IOException, 
ConfigurationException
      {
          final XMLReader reader = createXMLReader();
          final SAXConfigurationHandler handler = new SAXConfigurationHandler();
          setupXMLReader( reader, handler );
          reader.parse( input );
          return handler.getConfiguration();
      }
  }
  
  
  
  1.1                  
jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/tools/configuration/DTDInfo.java
  
  Index: DTDInfo.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.phoenix.tools.configuration;
  
  /**
   * Holds information about a given DTD.
   */
  public class DTDInfo
  {
     /**
      * The public identifier. Null if unknown.
      */
     private final String m_publicId;
  
     /**
      * The system identifier.  Null if unknown.
      */
     private final String m_systemId;
  
     /**
      * The resource name, if a copy of the document is available.
      */
     private final String m_resource;
  
     public DTDInfo( final String publicId,
                     final String systemId,
                     final String resource )
     {
        m_publicId = publicId;
        m_systemId = systemId;
        m_resource = resource;
     }
  
     public String getPublicId()
     {
        return m_publicId;
     }
  
     public String getSystemId()
     {
        return m_systemId;
     }
  
     public String getResource()
     {
        return m_resource;
     }
  }
  
  
  
  1.1                  
jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/tools/configuration/DTDResolver.java
  
  Index: DTDResolver.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.phoenix.tools.configuration;
  
  import java.io.IOException;
  import java.io.InputStream;
  import org.xml.sax.EntityResolver;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  /**
   * A Class to help to resolve Entitys for items such as DTDs or
   * Schemas.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2001/12/15 04:55:59 $
   */
  public class DTDResolver
      implements EntityResolver
  {
      /**
       * The list of DTDs that can be resolved by this class.
       */
      private final DTDInfo[] m_dtdInfos;
  
      /**
       * The ClassLoader to use when loading resources for DTDs.
       */
      private final ClassLoader m_classLoader;
  
      /**
       * Construct a resolver using specified DTDInfos.
       */
      public DTDResolver( final DTDInfo[] dtdInfos )
      {
          this( dtdInfos, Thread.currentThread().getContextClassLoader() );
      }
  
      /**
       * Construct a resolver using specified DTDInfos where resources are 
loaded
       * from specified ClassLoader.
       */
      public DTDResolver( final DTDInfo[] dtdInfos, final ClassLoader 
classLoader )
      {
          m_dtdInfos = dtdInfos;
          m_classLoader = classLoader;
      }
  
      /**
       * Resolve an entity in the XML file.
       * Called by parser to resolve DTDs.
       */
      public InputSource resolveEntity( final String publicId, final String 
systemId )
          throws IOException, SAXException
      {
          for( int i = 0; i < m_dtdInfos.length; i++ )
          {
              final DTDInfo info = m_dtdInfos[ i ];
  
              if( ( publicId != null && publicId.equals( info.getPublicId() ) ) 
||
                  ( systemId != null && systemId.equals( info.getSystemId() ) ) 
)
              {
                  final ClassLoader classLoader = getClassLoader();
                  final InputStream inputStream = 
classLoader.getResourceAsStream( info.getResource() );
                  return new InputSource( inputStream );
              }
          }
  
          return null;
      }
  
      /**
       * Return CLassLoader to load resource from.
       * If a ClassLoader is specified in the constructor use that,
       * else use ContextClassLoader unless that is null in which case
       * use the current classes ClassLoader.
       */
      private ClassLoader getClassLoader()
      {
          ClassLoader classLoader = m_classLoader;
          if( null == classLoader )
          {
              classLoader = Thread.currentThread().getContextClassLoader();
          }
          if( null == classLoader )
          {
              classLoader = getClass().getClassLoader();
          }
          return classLoader;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to