You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2002/07/12 09:58:14 UTC

cvs commit: jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/meta package.html DTDResolver.java DTDInfo.java ConfigurationBuilder.java componentinfo.dtd

mcconnell    2002/07/12 00:58:14

  Added:       assembly/src/java/org/apache/excalibur/meta package.html
                        DTDResolver.java DTDInfo.java
                        ConfigurationBuilder.java componentinfo.dtd
  Log:
  general utilities that are reusable beyond the meta info model
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/meta/package.html
  
  Index: package.html
  ===================================================================
  
  <body>
  <p>
  Generic build resources related to meta content generation.
  </p>
  </body>
  
  
  
  1.1                  jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/meta/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.excalibur.meta;
  
  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:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/07/12 07:58:14 $
   */
  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 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;
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/meta/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.excalibur.meta;
  
  /**
   * Holds information about a given DTD.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/07/12 07:58:14 $
   */
  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-excalibur/assembly/src/java/org/apache/excalibur/meta/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.excalibur.meta;
  
  import java.io.IOException;
  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.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:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/07/12 07:58:14 $
   */
  public class ConfigurationBuilder
  {
      private static final DTDInfo[] c_dtdInfo = new DTDInfo[]
      {
          new DTDInfo( "-//AVALON/Component Info DTD Version 1.0//EN",
                       "http://jakarta.apache.org/avalon/componentinfo_1_0.dtd",
                       "org/apache/excalibur/meta/componentinfo.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 URI
       * @param uri an input source system identifier
       * @exception SAXException is a parser exception is encountered
       * @exception ParserConfigurationException if a parser configuration failure occurs
       * @exception IOException if an IO exception occurs while attempting to read the
       *    resource identified by the system identifier
       */
      public static Configuration build( final String uri )
          throws SAXException, ParserConfigurationException, IOException
      {
          return build( new InputSource( uri ) );
      }
  
      /**
       * Build a configuration object using an XML InputSource object
       * @param input an input source
       * @exception SAXException is a parser exception is encountered
       * @exception ParserConfigurationException if a parser configuration failure occurs
       * @exception IOException if an IO exception occurs while attempting to read the
       *    resource associated with the input source
       */
      public static Configuration build( final InputSource input )
          throws SAXException, ParserConfigurationException, IOException
      {
          if( input == null ) 
            throw new NullPointerException("null input source");
  
          final XMLReader reader = createXMLReader();
          final SAXConfigurationHandler handler = new SAXConfigurationHandler();
          setupXMLReader( reader, handler );
          reader.parse( input );
          return handler.getConfiguration();
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/meta/componentinfo.dtd
  
  Index: componentinfo.dtd
  ===================================================================
  <!--
  
     This is the DTD defining the Avalon Meta Model Type 1.0
     descriptor (XML) file format/syntax.
  
     Author: Stephen McConnell <mc...@apache.org>
     Author: Peter Donald <pe...@apache.org>
  
     An xinfo file is an XML file used to describe Component types and is located side-by-side with
     the .class file of the component. It describes the services the component requires to operate
     (its dependecies), the services the component is capable of offerring other component, the 
     context entrys that component requires, logging catagories that the compoent may use, and other 
     support meta data in the form of attributes.
  
     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.
  
    -->
  
  <!--
  The component-info is the document root, it defines:
  
  component    the specifc details about this component
  loggers      the loggers used by this component
  context      the context required by this component
  services     the services offered by this component
  dependencies the services that this component require to operate
  -->
  <!ELEMENT component-info (component, loggers?, context?, services?, dependencies?)>
  <!--
    !ATTLIST component-info id ID #IMPLIED
            xmlns CDATA #FIXED "http://jakarta.apache.org/avalon/componentinfo_1_0.dtd"
   -->
  
  <!--
  The component element describes the component, it defines:
  
  name	        the human readable name of component type. Must be a string
               containing alphanumeric characters, '.', '_' and starting
               with a letter.
  version	     the version of the component in (in the format #.#.#, #.# or # where
               # is a integer
  -->
  <!ELEMENT component      (name?,version,attributes?)>
    <!ELEMENT name         (#PCDATA) >
    <!ELEMENT version      (#PCDATA) >
  
  <!--
  The logger element defines the loggers that are available to component.
  The element has one attribute specifying name of Logger. It contains:
  
  attributes	  Optional attributes about logger
  -->
  <!ELEMENT logger   (attributes?) >
    <!ATTLIST logger name CDATA #IMPLIED >
  
  <!--
  The context element defines what values and type of context
  is available to component.
  It contains:
  
  entrys    	  Key value pairs that component uses
  attributes	  Optional attributes about service
  -->
  <!ELEMENT context   (entry*,attributes?) >
    <!ATTLIST context type CDATA #IMPLIED >
  
  <!--
  The service element defines a service that the component
  can provide to other component.
  It contains:
  
  service-ref  the reference to service.
  attributes	  Optional attributes about service
  -->
  <!ELEMENT service   (service-ref,attributes?) >
  
  <!--
  The service element defines a reference to a service that the component
  can provide to other component, or this component depends upon.
  It defines:
  
  type         the name of the service. This must be equal to the class name of the
               interface that defines the service.
  version	     the version of the block in (in the format #.#.#, #.# or # where
               # is a integer
  -->
  <!ELEMENT service-ref   EMPTY >
    <!ATTLIST service-ref
         type CDATA #REQUIRED
         version CDATA #IMPLIED >
  
  <!--
  The service dependency describes a service that the component
  requires. It defines:
  
  role         the role of the service. This is the value that is used to lookup the
               service in the ComponentManager. If not provided it defaults to the
               value specified in the name attribute of service element
  service-ref  the service that is required
  -->
  <!ELEMENT dependency  (role?,service-ref,attributes?) >
    <!ATTLIST dependency optional CDATA #IMPLIED >
    <!ELEMENT role        (#PCDATA) >
  
  <!--
  The loggers element contains a list of loggers that component uses.
  -->
  <!ELEMENT loggers    (logger*)>
  
  <!--
  The services element contains a list of services that this component supports.
  It contains service elements.
  -->
  <!ELEMENT services    (service*)>
  
  <!--
  The dependencies element contains a list of services that this component requires.
  It contains dependency elements.
  -->
  <!ELEMENT dependencies    (dependency*)>
  
  <!--
  The attributes element contains a list of attributes for feature.
  -->
  <!ELEMENT attributes    (attribute*)>
  
  <!--
  The attribute element defines an attribute (an opaque key-value pair for a feature).
  It defines:
  
  key          the key for attribute.
  value  	     the value of attribute.
  -->
  <!ELEMENT attribute   EMPTY >
    <!ATTLIST attribute
         key CDATA #REQUIRED
         value CDATA #REQUIRED
    >
  
  <!--
  The entry element defines entry in context.
  It defines:
  
  key          the key for entry.
  value  	     the value of entry.
  optional     is entry optional
  -->
  <!ELEMENT entry   EMPTY >
    <!ATTLIST entry
         key CDATA #REQUIRED
         type CDATA #REQUIRED
         optional CDATA #IMPLIED
    >
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>