You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ad...@apache.org on 2002/05/17 09:41:28 UTC

cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder ModelElementSaxHandler.java ModelElementUtil.java ATIProjectBuilder.java ConvertingProjectBuilder.java DefaultProjectBuilder.java Resources.properties TransformingProjectBuilder.java

adammurdoch    02/05/17 00:41:28

  Modified:    container/src/java/org/apache/myrmidon/components/builder
                        ATIProjectBuilder.java
                        ConvertingProjectBuilder.java
                        DefaultProjectBuilder.java Resources.properties
                        TransformingProjectBuilder.java
  Added:       container/src/java/org/apache/myrmidon/components/builder
                        ModelElementSaxHandler.java ModelElementUtil.java
  Log:
  Finish moving project builders over from Configuration to ModelElement:
  
  * Changed DefaultProjectBuilder and ConvertingProjectBuilder to work with
    ModelElement.
  
  * Restructured DefaultProjectBuilder, to allow subclasses to separately override
    production of the SAX event stream, and transformation of the model prior to
    assembling the Project.
  
  * Added ModelElementSaxHandler and ModelElementUtil, to build a model from xml.
  
  Revision  Changes    Path
  1.14      +27 -19    jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ATIProjectBuilder.java
  
  Index: ATIProjectBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ATIProjectBuilder.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ATIProjectBuilder.java	29 Mar 2002 12:56:03 -0000	1.13
  +++ ATIProjectBuilder.java	17 May 2002 07:41:27 -0000	1.14
  @@ -18,9 +18,10 @@
   import javax.xml.transform.stream.StreamSource;
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
  -import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
  +import org.apache.avalon.excalibur.io.IOUtil;
   import org.apache.avalon.framework.parameters.Parameterizable;
   import org.apache.avalon.framework.parameters.Parameters;
  +import org.xml.sax.ContentHandler;
   import org.xml.sax.SAXException;
   import org.xml.sax.XMLReader;
   
  @@ -28,7 +29,7 @@
    * Default implementation to construct project from a build file.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.13 $ $Date: 2002/03/29 12:56:03 $
  + * @version $Revision: 1.14 $ $Date: 2002/05/17 07:41:27 $
    * @ant.type type="project-builder" name="ati"
    */
   public class ATIProjectBuilder
  @@ -45,22 +46,24 @@
           m_parameters = parameters;
       }
   
  -    protected void process( final URL sourceID,
  -                            final SAXConfigurationHandler handler )
  +    /**
  +     * Parses the project file using the supplied handler.
  +     */
  +    protected void parseProject( final String systemId,
  +                                 final ContentHandler contentHandler )
           throws Exception
       {
           final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
           final SAXParser saxParser = saxParserFactory.newSAXParser();
           final XMLReader reader = saxParser.getXMLReader();
           reader.setFeature( "http://xml.org/sax/features/validation", false );
  -        reader.setErrorHandler( handler );
   
           final ReactorPIHandler reactorHandler = new ReactorPIHandler();
           reader.setContentHandler( reactorHandler );
   
           try
           {
  -            reader.parse( sourceID.toString() );
  +            reader.parse( systemId );
           }
           catch( final StopParsingException spe )
           {
  @@ -81,7 +84,7 @@
               }
               else if( target.equals( "xsl-params" ) )
               {
  -                handleParameters( data, sourceID );
  +                handleParameters( data, systemId );
               }
               else if( target.equals( "xsl-stylesheet" ) )
               {
  @@ -92,15 +95,15 @@
                   }
   
                   final TransformerFactory factory = TransformerFactory.newInstance();
  -                final String stylesheet = getStylesheet( data, sourceID );
  +                final String stylesheet = getStylesheet( data, systemId );
                   transformer = factory.newTransformer( new StreamSource( stylesheet ) );
               }
           }
   
           if( null == transformer )
           {
  -            reader.setContentHandler( handler );
  -            reader.parse( sourceID.toString() );
  +            reader.setContentHandler( contentHandler );
  +            reader.parse( systemId );
           }
           else
           {
  @@ -112,10 +115,8 @@
                   transformer.setParameter( name, value );
               }
   
  -            final SAXResult result = new SAXResult( handler );
  -            transformer.transform( new StreamSource( sourceID.toString() ), result );
  -            //transformer.transform( new StreamSource( sourceID.toString() ),
  -            //new StreamResult( System.out ) );
  +            final SAXResult result = new SAXResult( contentHandler );
  +            transformer.transform( new StreamSource( systemId ), result );
           }
       }
   
  @@ -149,7 +150,7 @@
           m_parameters.setParameter( name[ 1 ], value[ 1 ] );
       }
   
  -    private void handleParameters( final String data, final URL baseSource )
  +    private void handleParameters( final String data, final String baseSource )
           throws SAXException
       {
           final String[] params = parseAttribute( data );
  @@ -162,9 +163,16 @@
           try
           {
               final Properties properties = new Properties();
  -            final URL url = new URL( baseSource, params[ 1 ] );
  +            final URL url = new URL( new URL ( baseSource ), params[ 1 ] );
               final InputStream input = url.openStream();
  -            properties.load( input );
  +            try
  +            {
  +                properties.load( input );
  +            }
  +            finally
  +            {
  +                IOUtil.shutdownStream( input );
  +            }
               final Parameters parameters = Parameters.fromProperties( properties );
               m_parameters.merge( parameters );
           }
  @@ -175,7 +183,7 @@
           }
       }
   
  -    private String getStylesheet( final String data, final URL baseSource )
  +    private String getStylesheet( final String data, final String baseSource )
           throws SAXException
       {
           final String[] stylesheet = parseAttribute( data );
  @@ -187,7 +195,7 @@
   
           try
           {
  -            return new URL( baseSource, stylesheet[ 1 ] ).toString();
  +            return new URL( new URL( baseSource ), stylesheet[ 1 ] ).toString();
           }
           catch( final Exception e )
           {
  
  
  
  1.8       +46 -169   jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ConvertingProjectBuilder.java
  
  Index: ConvertingProjectBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ConvertingProjectBuilder.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ConvertingProjectBuilder.java	12 May 2002 11:18:39 -0000	1.7
  +++ ConvertingProjectBuilder.java	17 May 2002 07:41:27 -0000	1.8
  @@ -7,18 +7,14 @@
    */
   package org.apache.myrmidon.components.builder;
   
  -import java.util.HashSet;
  -import java.util.Set;
  -import org.apache.avalon.framework.configuration.Configuration;
  -import org.apache.avalon.framework.configuration.DefaultConfiguration;
  -import org.apache.myrmidon.interfaces.builder.ProjectException;
  +import org.apache.myrmidon.api.metadata.ModelElement;
   
   /**
    * A simple ProjectBuilder, which programmatically converts an Ant1 Project
    * configuration into a Myrmidon one.
    *
    * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
  - * @version $Revision: 1.7 $ $Date: 2002/05/12 11:18:39 $
  + * @version $Revision: 1.8 $ $Date: 2002/05/17 07:41:27 $
    *
    * @ant.type type="project-builder" name="xml"
    * @ant.type type="project-builder" name="ant"
  @@ -27,203 +23,84 @@
   public class ConvertingProjectBuilder
       extends DefaultProjectBuilder
   {
  -    private static final String VERSION_ATTRIBUTE = "version";
  -
       /**
  -     * Builds a Configuration from an Ant1 project file, converting it
  -     * into a valid Myrmidon Project.
  -     * @param systemID the xml Systemid of the project file.
  -     * @return the configured project
  -     * @throws ProjectException if an error occurs parsing the project file
  +     * Transforms the project model.  This implementation does nothing.
        */
  -    protected Configuration parseProject( final String systemID )
  -        throws ProjectException
  +    protected void transformProject( final ModelElement model )
  +        throws Exception
       {
  -        final Configuration originalConfig = super.parseProject( systemID );
  -
           // Check the version, if it's present, just use this config.
           // TODO: check for version < 2.0
  -        if( originalConfig.getAttribute( VERSION_ATTRIBUTE, null ) != null )
  +        if( model.getAttribute( VERSION_ATTRIBUTE ) != null )
           {
  -            return originalConfig;
  +            return;
           }
   
  -        // Convert the config by using <if> tasks instead of target 'if=' and 'unless='
  -        final DefaultConfiguration newConfig = copyConfiguration( originalConfig );
  -
           // Put a new version attribute.
  -        newConfig.setAttribute( VERSION_ATTRIBUTE, "2.0" );
  +        model.setAttribute( VERSION_ATTRIBUTE, "2.0" );
   
  -        // Copy the remaining attributes.
  -        final Set omitAttributes = new HashSet();
  -        omitAttributes.add( VERSION_ATTRIBUTE );
  -        copyAttributes( originalConfig, newConfig, omitAttributes );
  -
  -        // Add an "import" task for the ant1compat stuff
  -        DefaultConfiguration typelibDeclaration =
  -            new DefaultConfiguration( "import", originalConfig.getLocation() );
  +        // Add an "import" task for the ant1 tasks
  +        ModelElement typelibDeclaration =
  +            new ModelElement( "import", model.getLocation() );
           typelibDeclaration.setAttribute( "library", "ant1" );
  -        newConfig.addChild( typelibDeclaration );
  +        model.addChild( 0, typelibDeclaration );
   
  -        // Now copy/convert the children
  -        final Configuration[] children = originalConfig.getChildren();
  +        // Convert the child <target> elements
  +        final ModelElement[] children = model.getChildren();
           for( int i = 0; i < children.length; i++ )
           {
  -            final Configuration child = children[ i ];
  +            final ModelElement child = children[ i ];
   
               if( child.getName().equals( "target" ) )
               {
  -                newConfig.addChild( convertTarget( child ) );
  -            }
  -            else
  -            {
  -                newConfig.addChild( child );
  +                convertTarget( child );
               }
           }
  -
  -        return newConfig;
       }
   
       /**
  -     * Converts Configuration for an Ant1 Target into a Myrmidon version.
  -     * @param originalTarget The Ant1 Target
  -     * @return the converted target
  +     * Converts an Ant1 target into a Myrmidon version.
  +     * @param target The target to convert
        */
  -    private Configuration convertTarget( final Configuration originalTarget )
  +    private void convertTarget( final ModelElement target )
       {
  -        final DefaultConfiguration newTarget = copyConfiguration( originalTarget );
  +        final String ifAttrib = target.getAttribute( "if" );
  +        final String unlessAttrib = target.getAttribute( "unless" );
   
  -        // Copy all attributes except 'if' and 'unless'
  -        final Set omitAttributes = new HashSet();
  -        omitAttributes.add( "if" );
  -        omitAttributes.add( "unless" );
  -        copyAttributes( originalTarget, newTarget, omitAttributes );
  -
  -        DefaultConfiguration containerElement = newTarget;
  -
  -        // For 'if="prop-name"', replace with <if> task.
  -        final String ifAttrib = originalTarget.getAttribute( "if", null );
  -        if ( ifAttrib != null )
  +        if( ifAttrib == null && unlessAttrib == null )
           {
  -            final DefaultConfiguration ifElement =
  -                buildIfElement( ifAttrib, false, originalTarget.getLocation() );
  -            containerElement.addChild( ifElement );
  -            // Treat the ifElement as the enclosing target.
  -            containerElement = ifElement;
  +            // Don't need to do anything
  +            return;
           }
   
  -        // For 'unless="prop-name"', replace with <if> task (negated).
  -        final String unlessAttrib = originalTarget.getAttribute( "unless", null );
  -        if ( unlessAttrib != null )
  -        {
  -            final DefaultConfiguration unlessElement =
  -                buildIfElement( unlessAttrib, true, originalTarget.getLocation() );
  -            containerElement.addChild( unlessElement );
  -            // Treat the unlessElement as the enclosing target.
  -            containerElement = unlessElement;
  -        }
  -
  -        // Now copy in all tasks.
  -        copyChildren( originalTarget, containerElement );
  -
  -        return newTarget;
  -    }
  -
  -    /**
  -     * Builds configuration for an <if> task, to replace a "if" or "unless"
  -     * attribute on a Ant1 target.
  -     * @param ifProperty the name of the property from the Ant1 attribute.
  -     * @param unless if the attribute is actually an "unless" attribute.
  -     * @param location the configuration location to use
  -     * @return The configuration for an <if> task
  -     */
  -    private DefaultConfiguration buildIfElement( final String ifProperty,
  -                                                 final boolean unless,
  -                                                 final String location )
  -    {
  -        // <if>
  -        //      <condition>
  -        //          <is-set property="prop-name"/>
  -        //      </condition>
  -        //      .. tasks
  -        // </if>
  -        final DefaultConfiguration isSetElement =
  -            new DefaultConfiguration( "is-set", location );
  -        isSetElement.setAttribute( "property", ifProperty );
  +        target.setAttribute( "if", null );
  +        target.setAttribute( "unless", null );
   
  -        final DefaultConfiguration conditionElement =
  -            new DefaultConfiguration( "condition", location );
  +        // Wrap the children of the target in an <if> task
  +        final ModelElement ifTask = new ModelElement( "if", target.getLocation() );
  +        final ModelElement condition = new ModelElement( "condition", target.getLocation() );
  +        ifTask.addChild( condition );
  +        ifTask.addChildren( target.getChildren() );
  +        ifTask.setContent( target.getContent() );
  +        target.removeChildren();
  +        target.addChild( ifTask );
   
  -        if ( unless )
  +        if( ifAttrib != null )
           {
  -            // Surround <is-set> with <not>
  -            final DefaultConfiguration notElement =
  -                new DefaultConfiguration( "not", location );
  -            notElement.addChild( isSetElement );
  -            conditionElement.addChild( notElement );
  +            // Add an <is-set> test
  +            final ModelElement test = new ModelElement( "is-set", target.getLocation() );
  +            test.setAttribute( "property", ifAttrib );
  +            condition.addChild( test );
           }
  -        else
  +        if( unlessAttrib != null )
           {
  -            conditionElement.addChild( isSetElement );
  +            // Add a ! <is-set> test
  +            final ModelElement test = new ModelElement( "is-set", target.getLocation() );
  +            test.setAttribute( "property", unlessAttrib );
  +            final ModelElement not = new ModelElement( "not", target.getLocation() );
  +            not.addChild( test );
  +            condition.addChild( not );
           }
  -
  -
  -        final DefaultConfiguration ifElement =
  -            new DefaultConfiguration( "if", location );
  -        ifElement.addChild( conditionElement );
  -
  -        return ifElement;
       }
   
  -    /**
  -     * Copies all child elements from one configuration to another
  -     * @param from Configuration to copy from
  -     * @param to Configuration to copy to
  -     */
  -    private void copyChildren( final Configuration from,
  -                               final DefaultConfiguration to )
  -    {
  -        final Configuration[] children = from.getChildren();
  -        for( int i = 0; i < children.length; i++ )
  -        {
  -            to.addChild( children[ i ] );
  -        }
  -    }
  -
  -    /**
  -     * Copies all attributes from one configuration to another, excluding
  -     * specified attribute names.
  -     * @param from Configuration to copy from
  -     * @param to Configuration to copy to
  -     * @param omitAttributes a Set of attribute names to exclude
  -     */
  -    private void copyAttributes( final Configuration from,
  -                                 final DefaultConfiguration to,
  -                                 final Set omitAttributes )
  -    {
  -        // Copy other attributes
  -        final String[] attribs = from.getAttributeNames();
  -        for( int i = 0; i < attribs.length; i++ )
  -        {
  -            final String name = attribs[ i ];
  -            if( omitAttributes.contains( name ) )
  -            {
  -                continue;
  -            }
  -            final String value = from.getAttribute( name, "" );
  -            to.setAttribute( name, value );
  -        }
  -    }
  -
  -    /**
  -     * Creates a DefaultConfiguration with the same name and location as
  -     * the one supplied.
  -     * @param originalConfig the COnfiguration to copy.
  -     * @return the new Configuration
  -     */
  -    private DefaultConfiguration copyConfiguration( final Configuration originalConfig )
  -    {
  -        return new DefaultConfiguration( originalConfig.getName(),
  -                                         originalConfig.getLocation() );
  -    }
   }
  
  
  
  1.50      +64 -106   jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/DefaultProjectBuilder.java
  
  Index: DefaultProjectBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/DefaultProjectBuilder.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- DefaultProjectBuilder.java	12 May 2002 11:18:39 -0000	1.49
  +++ DefaultProjectBuilder.java	17 May 2002 07:41:27 -0000	1.50
  @@ -10,6 +10,7 @@
   import java.io.File;
   import java.util.ArrayList;
   import java.util.HashMap;
  +import java.util.Map;
   import java.util.StringTokenizer;
   import javax.xml.parsers.SAXParser;
   import javax.xml.parsers.SAXParserFactory;
  @@ -17,9 +18,6 @@
   import org.apache.avalon.excalibur.i18n.Resources;
   import org.apache.avalon.excalibur.io.FileUtil;
   import org.apache.avalon.framework.Version;
  -import org.apache.avalon.framework.configuration.Configuration;
  -import org.apache.avalon.framework.configuration.ConfigurationException;
  -import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.myrmidon.api.metadata.ModelElement;
   import org.apache.myrmidon.components.property.DefaultNameValidator;
  @@ -28,13 +26,14 @@
   import org.apache.myrmidon.interfaces.oldmodel.Dependency;
   import org.apache.myrmidon.interfaces.oldmodel.Project;
   import org.apache.myrmidon.interfaces.oldmodel.Target;
  +import org.xml.sax.ContentHandler;
   import org.xml.sax.XMLReader;
   
   /**
    * Default implementation to construct project from a build file.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.49 $ $Date: 2002/05/12 11:18:39 $
  + * @version $Revision: 1.50 $ $Date: 2002/05/17 07:41:27 $
    *
    * @ant.type type="project-builder" name="ant2"
    */
  @@ -45,6 +44,7 @@
       private static final Resources REZ =
           ResourceManager.getPackageResources( DefaultProjectBuilder.class );
   
  +    protected static final String VERSION_ATTRIBUTE = "version";
       private static final Version VERSION = new Version( 2, 0, 0 );
   
       private static final int PROJECT_REFERENCES = 0;
  @@ -68,7 +68,7 @@
           return build( file, new HashMap() );
       }
   
  -    private Project build( final File file, final HashMap projects )
  +    private Project build( final File file, final Map projects )
           throws ProjectException
       {
           try
  @@ -82,14 +82,20 @@
               }
   
               // Parse the project file
  -            final Configuration configuration = parseProject( systemID );
  +            final ModelElementSaxHandler handler = new ModelElementSaxHandler();
  +            parseProject( systemID, handler );
  +
  +            // Tranform the resulting model
  +            final ModelElement model = handler.getModel();
  +            transformProject( model );
  +            model.makeReadOnly();
   
               // Build the project model and add to cache
  -            final DefaultProject project = buildProject( file, configuration );
  +            final DefaultProject project = buildProject( file, model );
               projects.put( systemID, project );
   
               // Build using all top-level attributes
  -            buildTopLevelProject( project, configuration, projects );
  +            buildTopLevelProject( project, model, projects );
   
               return project;
           }
  @@ -102,17 +108,15 @@
       }
   
       /**
  -     * Builds a project configuration from a build file.
  -     * @param systemID the XML system id of the build file
  -     * @return the project configuration
  -     * @throws ProjectException on parse error
  +     * Parses the project file using the supplied content handler.
        */
  -    protected Configuration parseProject( final String systemID )
  -        throws ProjectException
  +    protected void parseProject( final String systemId,
  +                                 final ContentHandler contentHandler )
  +        throws Exception
       {
  +
           try
           {
  -            final SAXConfigurationHandler handler = new SAXConfigurationHandler();
               final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
               final SAXParser saxParser = saxParserFactory.newSAXParser();
               final XMLReader parser = saxParser.getXMLReader();
  @@ -120,11 +124,8 @@
               parser.setFeature( "http://xml.org/sax/features/namespaces", false );
               //parser.setFeature( "http://xml.org/sax/features/validation", false );
   
  -            parser.setContentHandler( handler );
  -            parser.setErrorHandler( handler );
  -            parser.parse( systemID );
  -
  -            return handler.getConfiguration();
  +            parser.setContentHandler( contentHandler );
  +            parser.parse( systemId );
           }
           catch( Exception e )
           {
  @@ -134,28 +135,37 @@
       }
   
       /**
  +     * Transforms the project model.  This implementation does nothing.
  +     */
  +    protected void transformProject( final ModelElement model )
  +        throws Exception
  +    {
  +        // Do nothing
  +    }
  +
  +    /**
        * build project from configuration.
        *
        * @param file the file from which configuration was loaded
  -     * @param configuration the configuration loaded
  +     * @param model the project model
        * @return the created Project
        * @exception ProjectException if an error occurs building the project
        */
       private DefaultProject buildProject( final File file,
  -                                         final Configuration configuration )
  +                                         final ModelElement model )
           throws ProjectException
       {
  -        if( !configuration.getName().equals( "project" ) )
  +        if( !model.getName().equals( "project" ) )
           {
               final String message = REZ.getString( "ant.no-project-element.error" );
               throw new ProjectException( message );
           }
   
           //get project-level attributes
  -        final String projectName = getProjectName( configuration, file );
  -        final String baseDirectoryName = configuration.getAttribute( "basedir", null );
  -        final String defaultTarget = configuration.getAttribute( "default", "main" );
  -        final Version version = getVersion( configuration );
  +        final String projectName = getProjectName( model, file );
  +        final String baseDirectoryName = model.getAttribute( "basedir" );
  +        final String defaultTarget = model.getAttribute( "default", "main" );
  +        final Version version = getVersion( model );
   
           if( !VERSION.complies( version ) )
           {
  @@ -190,14 +200,14 @@
       }
   
       /**
  -     * Get the project name from the configuration, or create a default name if none
  +     * Get the project name from the model, or create a default name if none
        * was supplied.
        */
  -    private String getProjectName( final Configuration configuration,
  +    private String getProjectName( final ModelElement model,
                                      final File file )
           throws ProjectException
       {
  -        String projectName = configuration.getAttribute( "name", null );
  +        String projectName = model.getAttribute( "name" );
   
           if( projectName == null )
           {
  @@ -231,30 +241,18 @@
       }
   
       /**
  -     * Retrieve the version attribute from the specified configuration element.
  +     * Retrieve the version attribute from the specified model.
        * Throw exceptions with meaningful errors if malformed or missing.
        */
  -    private Version getVersion( final Configuration configuration )
  +    private Version getVersion( final ModelElement model)
           throws ProjectException
       {
  -        try
  -        {
  -            final String versionString = configuration.getAttribute( "version" );
  -            return parseVersion( versionString );
  -        }
  -        catch( final ConfigurationException ce )
  +        final String versionString = model.getAttribute( VERSION_ATTRIBUTE );
  +        if( versionString == null )
           {
               final String message = REZ.getString( "ant.version-missing.error" );
  -            throw new ProjectException( message, ce );
  +            throw new ProjectException( message );
           }
  -    }
  -
  -    /**
  -     * Utility function to extract version
  -     */
  -    private Version parseVersion( final String versionString )
  -        throws ProjectException
  -    {
   
           try
           {
  @@ -269,25 +267,25 @@
       }
   
       /**
  -     * Handle all top level elements in configuration.
  +     * Handle all top level elements in a project model.
        *
        * @param project the project
  -     * @param configuration the Configuration
  +     * @param model the model
        * @exception ProjectException if an error occurs
        */
       private void buildTopLevelProject( final DefaultProject project,
  -                                       final Configuration configuration,
  -                                       final HashMap projects )
  +                                       final ModelElement model,
  +                                       final Map projects )
           throws Exception
       {
           final ArrayList implicitTaskList = new ArrayList();
  -        final Configuration[] children = configuration.getChildren();
  +        final ModelElement[] children = model.getChildren();
   
           int state = PROJECT_REFERENCES;
   
           for( int i = 0; i < children.length; i++ )
           {
  -            final Configuration element = children[ i ];
  +            final ModelElement element = children[ i ];
               final String name = element.getName();
   
               if( PROJECT_REFERENCES == state )
  @@ -330,59 +328,20 @@
               }
           }
   
  -        final Configuration[] implicitTasks =
  -            (Configuration[])implicitTaskList.toArray( new Configuration[ 0 ] );
  +        final ModelElement[] implicitTasks =
  +            (ModelElement[])implicitTaskList.toArray( new ModelElement[ implicitTaskList.size() ] );
   
  -        final Target implicitTarget = new Target( toModelElements( implicitTasks ), null );
  +        final Target implicitTarget = new Target( implicitTasks, null );
           project.setImplicitTarget( implicitTarget );
       }
   
  -    private ModelElement[] toModelElements( final Configuration[] implicitTasks )
  -        throws Exception
  -    {
  -        final ModelElement[] elements = new ModelElement[ implicitTasks.length ];
  -        for( int i = 0; i < elements.length; i++ )
  -        {
  -            elements[ i ] = toModelElement( implicitTasks[ i ] );
  -
  -        }
  -        return elements;
  -    }
  -
  -    private ModelElement toModelElement( final Configuration config )
  -        throws Exception
  -    {
  -        final ModelElement model =
  -            new ModelElement( config.getName(),
  -                              config.getLocation() );
  -
  -        final Configuration[] children = config.getChildren();
  -        for( int i = 0; i < children.length; i++ )
  -        {
  -            final ModelElement child = toModelElement( children[ i ] );
  -            model.addChild( child );
  -        }
  -
  -        final String[] names = config.getAttributeNames();
  -        for( int i = 0; i < names.length; i++ )
  -        {
  -            final String name = names[ i ];
  -            final String value = config.getAttribute( name, null );
  -            model.setAttribute( name, value );
  -        }
  -
  -        model.setContent( config.getValue( null ) );
  -
  -        return model;
  -    }
  -
       private void buildProjectRef( final DefaultProject project,
  -                                  final Configuration element,
  -                                  final HashMap projects )
  +                                  final ModelElement element,
  +                                  final Map projects )
           throws ProjectException
       {
  -        final String name = element.getAttribute( "name", null );
  -        final String location = element.getAttribute( "location", null );
  +        final String name = element.getAttribute( "name" );
  +        final String location = element.getAttribute( "location" );
   
           if( null == name )
           {
  @@ -451,11 +410,11 @@
        * @param project the project
        * @param target the Configuration
        */
  -    private void buildTarget( final DefaultProject project, final Configuration target )
  +    private void buildTarget( final DefaultProject project, final ModelElement target )
           throws Exception
       {
  -        final String name = target.getAttribute( "name", null );
  -        final String depends = target.getAttribute( "depends", null );
  +        final String name = target.getAttribute( "name" );
  +        final String depends = target.getAttribute( "depends" );
   
           verifyTargetName( name, target );
   
  @@ -467,14 +426,13 @@
   
           final Dependency[] dependencies = buildDependsList( depends, target );
           final Target targetModel =
  -            new Target( toModelElements( target.getChildren() ),
  -                        dependencies );
  +            new Target( target.getChildren(), dependencies );
   
           //add target to project
           project.addTarget( name, targetModel );
       }
   
  -    private void verifyTargetName( final String name, final Configuration target )
  +    private void verifyTargetName( final String name, final ModelElement target )
           throws ProjectException
       {
           if( null == name )
  @@ -497,7 +455,7 @@
       }
   
       private Dependency[] buildDependsList( final String depends,
  -                                           final Configuration target )
  +                                           final ModelElement target )
           throws ProjectException
       {
           //apply depends attribute
  
  
  
  1.10      +2 -0      jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/Resources.properties,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Resources.properties	12 May 2002 11:18:39 -0000	1.9
  +++ Resources.properties	17 May 2002 07:41:27 -0000	1.10
  @@ -33,3 +33,5 @@
   
   duplicate-project.error=Can not have two projects referenced in a file with the name {0}.
   duplicate-target.error=Can not have two targets in a file with the name {0}.
  +
  +saxhandler.mixedmodel.error={0}: Cannot mix nested elements and text content.
  \ No newline at end of file
  
  
  
  1.6       +13 -28    jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/TransformingProjectBuilder.java
  
  Index: TransformingProjectBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/TransformingProjectBuilder.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TransformingProjectBuilder.java	1 Apr 2002 09:56:25 -0000	1.5
  +++ TransformingProjectBuilder.java	17 May 2002 07:41:27 -0000	1.6
  @@ -17,15 +17,14 @@
   import javax.xml.transform.stream.StreamSource;
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
  -import org.apache.avalon.framework.configuration.Configuration;
  -import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
  -import org.apache.myrmidon.interfaces.builder.ProjectException;
  +import org.xml.sax.ContentHandler;
  +import org.xml.sax.ErrorHandler;
   
   /**
    * A Project Builder which performs an XSL transformation on a project.
    *
    * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
  - * @version $Revision: 1.5 $ $Date: 2002/04/01 09:56:25 $
  + * @version $Revision: 1.6 $ $Date: 2002/05/17 07:41:27 $
    *
    * @ant.type type="project-builder" name="ant-transform"
    */
  @@ -39,14 +38,11 @@
       private Transformer m_transformer;
   
       /**
  -     * Builds a project Configuration from a project file, applying the
  -     * ant1 conversion stylesheet.
  -     * @param systemID the XML system id for the project file
  -     * @return the project configuration
  -     * @throws ProjectException if a parse error occurs
  +     * Parses the project file using the supplied handler.
        */
  -    protected Configuration parseProject( String systemID )
  -        throws ProjectException
  +    protected void parseProject( final String systemId,
  +                                 final ContentHandler contentHandler )
  +        throws Exception
       {
           if( getLogger().isDebugEnabled() )
           {
  @@ -54,25 +50,14 @@
               getLogger().debug( message );
           }
   
  -        try
  -        {
  -            // Create a XSLT source for the build file.
  -            Source source = new StreamSource( systemID );
  -
  -            // Create a configuration handler for the output.
  -            final SAXConfigurationHandler handler = new SAXConfigurationHandler();
  -            Result result = new SAXResult( handler );
  +        // Create a XSLT source for the build file.
  +        final Source source = new StreamSource( systemId );
   
  -            // Perform the transformation.
  -            getTransformer().transform( source, result );
  +        // Create a configuration handler for the output.
  +        final Result result = new SAXResult( contentHandler );
   
  -            return handler.getConfiguration();
  -        }
  -        catch( Exception e )
  -        {
  -            throw new ProjectException( REZ.getString( "ant.project-convert.error" ),
  -                                        e );
  -        }
  +        // Perform the transformation.
  +        getTransformer().transform( source, result );
       }
   
       /**
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ModelElementSaxHandler.java
  
  Index: ModelElementSaxHandler.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.myrmidon.components.builder;
  
  import org.xml.sax.helpers.DefaultHandler;
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.Locator;
  import org.apache.myrmidon.api.metadata.ModelElement;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import java.util.ArrayList;
  
  /**
   * A SAX content handler that assembles a ModelElement.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/17 07:41:27 $
   */
  public class ModelElementSaxHandler
      extends DefaultHandler
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( ModelElementSaxHandler.class );
  
      private ModelElement m_model;
      private ModelElement m_element;
      private Locator m_locator;
      private final ArrayList m_stack = new ArrayList();
      private final StringBuffer m_content = new StringBuffer();
  
      /**
       * Returns the model built by this handler.
       */
      public ModelElement getModel()
      {
          return m_model;
      }
  
      public void setDocumentLocator( final Locator locator )
      {
          m_locator = locator;
      }
  
      public void startElement( final String namespaceUri,
                                final String localName,
                                final String qName,
                                final Attributes attributes )
          throws SAXException
      {
          // Check for mixed model
          if( hasContent() )
          {
              final String message = REZ.getString( "saxhandler.mixedmodel.error",
                                                    getLocation() );
              throw new SAXException( message );
          }
  
          // Create the new element
          final ModelElement element = new ModelElement( qName, getLocation() );
          if( m_element == null )
          {
              m_model = element;
          }
          else
          {
              m_stack.add( 0, m_element );
          }
          m_element = element;
  
          // Set attributes
          final int count = attributes.getLength();
          for( int i = 0; i < count; i++ )
          {
              final String name = attributes.getQName( i );
              final String value = attributes.getValue( i );
              m_element.setAttribute( name, value );
          }
      }
  
      public void endElement( final String namespaceUri,
                              final String localName,
                              final String qName )
          throws SAXException
      {
          // Set text content, if any
          if( hasContent() )
          {
              // TODO: should not trim content here; needs to be moved up
              m_element.setContent( m_content.toString().trim() );
          }
          m_content.setLength( 0 );
  
          // Pop parent element off stack
          if( m_stack.size() > 0 )
          {
              final ModelElement parent = (ModelElement)m_stack.remove( 0 );
              parent.addChild( m_element );
              m_element = parent;
           }
          else
          {
              m_element = null;
          }
      }
  
      public void characters( final char[] chars,
                              final int offset,
                              final int count )
          throws SAXException
      {
          m_content.append( chars, offset, count );
  
          // Check for mixed model.  Should really check before appending, but
          // what can you do ...
          if( m_element.getChildCount() > 0 && hasContent() )
          {
              final String message = REZ.getString( "saxhandler.mixedmodel.error",
                                                    getLocation() );
              throw new SAXException( message );
          }
      }
  
      /**
       * Returns true if the text content bufer contains any non-whitespace chars.
       * @return
       */
      private boolean hasContent()
      {
          final int count = m_content.length();
          for( int i = 0; i < count; i++ )
          {
              if( !Character.isWhitespace( m_content.charAt( i ) ) )
              {
                  return true;
              }
          }
          return false;
      }
  
      /**
       * Builds a descriptive string for the current location.
       */
      private String getLocation()
      {
          return m_locator.getSystemId() + ':' + m_locator.getLineNumber();
      }
  
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ModelElementUtil.java
  
  Index: ModelElementUtil.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.myrmidon.components.builder;
  
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  import org.apache.myrmidon.api.metadata.ModelElement;
  import org.xml.sax.XMLReader;
  
  /**
   * Utility methods for dealing with {@link ModelElement} objects.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/17 07:41:27 $
   */
  public class ModelElementUtil
  {
      /**
       * Loads a model from an XML document.
       *
       * @param systemId The URL to load the model from
       * @return The loaded model.
       */
      public static ModelElement loadModel( final String systemId )
          throws Exception
      {
          final ModelElementSaxHandler contentHandler = new ModelElementSaxHandler();
          final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
          final SAXParser saxParser = saxParserFactory.newSAXParser();
          final XMLReader parser = saxParser.getXMLReader();
          parser.setFeature( "http://xml.org/sax/features/namespace-prefixes", false );
          parser.setFeature( "http://xml.org/sax/features/namespaces", false );
          //parser.setFeature( "http://xml.org/sax/features/validation", false );
  
          parser.setContentHandler( contentHandler );
          parser.parse( systemId );
  
          return contentHandler.getModel();
      }
  }
  
  
  

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


Re: cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder ModelElementSaxHandler.java ModelElementUtil.java ATIProjectBuilder.java ConvertingProjectBuilder.java DefaultProjectBuilder.java Resources.properties TransformingProjectBuilder.java

Posted by Peter Donald <pe...@apache.org>.
On Fri, 17 May 2002 17:41, adammurdoch@apache.org wrote:
> adammurdoch    02/05/17 00:41:28
>
>   Modified:    container/src/java/org/apache/myrmidon/components/builder
>                         ATIProjectBuilder.java
>                         ConvertingProjectBuilder.java
>                         DefaultProjectBuilder.java Resources.properties
>                         TransformingProjectBuilder.java
>   Added:       container/src/java/org/apache/myrmidon/components/builder
>                         ModelElementSaxHandler.java ModelElementUtil.java
>   Log:
>   Finish moving project builders over from Configuration to ModelElement:
>
>   * Changed DefaultProjectBuilder and ConvertingProjectBuilder to work with
>     ModelElement.
>
>   * Restructured DefaultProjectBuilder, to allow subclasses to separately
> override production of the SAX event stream, and transformation of the
> model prior to assembling the Project.
>
>   * Added ModelElementSaxHandler and ModelElementUtil, to build a model
> from xml.

Thanks - I been meaning to do that for ages ;)

-- 
Cheers,

Peter Donald



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