ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder TransformingModelBuilder.java
Date Sun, 19 May 2002 09:15:31 GMT
donaldp     02/05/19 02:15:31

  Added:       container/src/java/org/apache/myrmidon/components/builder
                        TransformingModelBuilder.java
  Log:
  Copy across the transformngProjectBuilder
  into a TransformingModelBuilder
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/TransformingModelBuilder.java
  
  Index: TransformingModelBuilder.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 java.io.InputStream;
  import javax.xml.transform.Result;
  import javax.xml.transform.Source;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerConfigurationException;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.sax.SAXResult;
  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.logger.AbstractLogEnabled;
  import org.apache.myrmidon.api.metadata.ModelElement;
  import org.apache.myrmidon.api.metadata.ModelException;
  import org.apache.myrmidon.interfaces.builder.ModelBuilder;
  import org.apache.myrmidon.interfaces.model.ModelElementSaxHandler;
  
  /**
   * A simple ModelBuilder that will build a {@link ModelElement}
   * tree after processing XML through an XSL transformation.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/19 09:15:31 $
   */
  public class TransformingModelBuilder
      extends AbstractLogEnabled
      implements ModelBuilder
  {
      private final static Resources REZ =
          ResourceManager.getPackageResources( TransformingModelBuilder.class );
  
      /**
       * The stylesheet to transform an ant1 project into ant2.
       */
      private static final String STYLESHEET = "ant1convert.xsl";
  
      /**
       * The transformer to use when building model.
       */
      private Transformer m_transformer;
  
      public ModelElement build( final String systemID )
          throws ModelException
      {
          try
          {
              if( getLogger().isDebugEnabled() )
              {
                  final String message = REZ.getString( "ant.project-convert.notice" );
                  getLogger().debug( message );
              }
  
              // Create a XSLT source for the build file.
              final Source source = new StreamSource( systemID );
  
  
              // Create a configuration handler for the output.
              final ModelElementSaxHandler contentHandler = new ModelElementSaxHandler();
              final Result result = new SAXResult( contentHandler );
  
              // Perform the transformation.
              getTransformer().transform( source, result );
  
              return contentHandler.getModel();
          }
          catch( final Exception e )
          {
              final String message = e.getMessage();
              throw new ModelException( message, e );
          }
      }
  
      /**
       * Lazy load a Transformer with the conversion stylesheet.
       *
       * @return the initialised Transformer
       * @throws TransformerConfigurationException on error
       */
      private Transformer getTransformer()
          throws TransformerConfigurationException
      {
          // Lazy loading of stylesheet source.
          if( m_transformer == null )
          {
              m_transformer = createTransformer();
          }
          return m_transformer;
      }
  
      /**
       * Actually create the transformer. Subclasses that wish to
       * use a different stylesheet should overide this method.
       *
       * @return the newly created transformer
       * @throws TransformerConfigurationException on error
       */
      protected Transformer createTransformer()
          throws TransformerConfigurationException
      {
          final InputStream stylesheet = getClass().getResourceAsStream( STYLESHEET );
          final StreamSource source = new StreamSource( stylesheet );
          final TransformerFactory factory = TransformerFactory.newInstance();
          return factory.newTransformer( source );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message