donaldp 2002/06/24 20:39:16
Modified:
containerkit/src/java/org/apache/excalibur/containerkit/infobuilder
ComponentInfoBuilder.java
Added:
containerkit/src/java/org/apache/excalibur/containerkit/infobuilder
componentinfo.dtd ConfigurationBuilder.java
DTDInfo.java DTDResolver.java
Removed:
containerkit/src/java/org/apache/excalibur/containerkit/configuration
componentinfo.dtd ConfigurationBuilder.java
DTDInfo.java DTDResolver.java package.html
Log:
Move configuration loading/dtd validating stuff into infobuilder package.
No real reason to keep them separate.
Revision Changes Path
1.6 +57 -4
jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/ComponentInfoBuilder.java
Index: ComponentInfoBuilder.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/ComponentInfoBuilder.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ComponentInfoBuilder.java 23 Jun 2002 22:50:05 -0000 1.5
+++ ComponentInfoBuilder.java 25 Jun 2002 03:39:16 -0000 1.6
@@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Properties;
+import java.io.InputStream;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.avalon.framework.Version;
@@ -23,6 +24,7 @@
import org.apache.excalibur.containerkit.metainfo.EntryDescriptor;
import org.apache.excalibur.containerkit.metainfo.ServiceDescriptor;
import org.apache.excalibur.containerkit.metainfo.ServiceDesignator;
+import org.xml.sax.InputSource;
/**
* A ComponentInfoBuilder is responsible for building [EMAIL PROTECTED]
ComponentInfo}
@@ -40,6 +42,57 @@
ResourceManager.getPackageResources( ComponentInfoBuilder.class );
/**
+ * Create a [EMAIL PROTECTED] ComponentInfo} object for specified
+ * classname, loaded from specified URI.
+ *
+ * @param classname The classname of Component
+ * @param uri the URI to load ComponentInfo from
+ * @return the created ComponentInfo
+ * @throws ConfigurationException if an error occurs
+ */
+ public ComponentInfo build( final String classname,
+ final String uri )
+ throws Exception
+ {
+ final InputSource input = new InputSource( uri );
+ return build( classname, input );
+ }
+
+ /**
+ * Create a [EMAIL PROTECTED] ComponentInfo} object for specified
+ * classname, loaded from specified [EMAIL PROTECTED] InputStream}.
+ *
+ * @param classname The classname of Component
+ * @param inputSource the InputStream to load ComponentInfo from
+ * @return the created ComponentInfo
+ * @throws ConfigurationException if an error occurs
+ */
+ public ComponentInfo build( final String classname,
+ final InputStream inputSource )
+ throws Exception
+ {
+ final InputSource input = new InputSource( inputSource );
+ return build( classname, input );
+ }
+
+ /**
+ * Create a [EMAIL PROTECTED] ComponentInfo} object for specified
+ * classname, loaded from specified [EMAIL PROTECTED] InputSource}.
+ *
+ * @param classname The classname of Component
+ * @param inputSource the InputSource to load ComponentInfo from
+ * @return the created ComponentInfo
+ * @throws ConfigurationException if an error occurs
+ */
+ public ComponentInfo build( final String classname,
+ final InputSource inputSource )
+ throws Exception
+ {
+ final Configuration configuration = ConfigurationBuilder.build(
inputSource );
+ return build( classname, configuration );
+ }
+
+ /**
* Create a <code>ComponentInfo</code> object for specified classname
from
* specified configuration data.
*
@@ -48,8 +101,8 @@
* @return the created ComponentInfo
* @throws ConfigurationException if an error occurs
*/
- public ComponentInfo build( final String classname,
- final Configuration info )
+ private ComponentInfo build( final String classname,
+ final Configuration info )
throws Exception
{
if( getLogger().isDebugEnabled() )
@@ -326,7 +379,7 @@
throws ConfigurationException
{
final String name = component.getChild( "name" ).getValue( null );
- final Version version = buildVersion( component.getChild( "version"
).getValue("1.0") );
+ final Version version = buildVersion( component.getChild( "version"
).getValue( "1.0" ) );
final Properties attributes =
buildAttributes( component.getChild( "attributes" ) );
1.1
jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/componentinfo.dtd
Index: componentinfo.dtd
===================================================================
<!--
This is the DTD defining the Avalon ComponentInfo 1.0
descriptor (XML) file format/syntax.
Author: Peter Donald <[EMAIL PROTECTED]>
A BlockInfo is an XML file used to describe Components and located
side-by-side with
the Component .class file. It describes the services the Component
requires to operate,
the services the Component is capable of offerring other Component, the
context entrys
that Component requires and other support meta data.
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
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, 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 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 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
>
1.1
jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/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.containerkit.infobuilder;
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:[EMAIL PROTECTED]">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/06/25 03:39:16 $
*/
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/containerkit/infobuilder/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
{
final XMLReader reader = createXMLReader();
final SAXConfigurationHandler handler = new SAXConfigurationHandler();
setupXMLReader( reader, handler );
reader.parse( input );
return handler.getConfiguration();
}
}
1.1
jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/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.containerkit.infobuilder;
/**
* Holds information about a given DTD.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/06/25 03:39:16 $
*/
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/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/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.containerkit.infobuilder;
import java.io.IOException;
import java.io.InputStream;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.excalibur.containerkit.infobuilder.DTDInfo;
/**
* 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: 2002/06/25 03:39:16 $
*/
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;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>