avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/generator FormatEnum.java MetaGenerateTask.java
Date Tue, 12 Nov 2002 01:23:34 GMT
donaldp     2002/11/11 17:23:34

  Added:       info/src/java/org/apache/avalon/framework/tools/generator
                        FormatEnum.java MetaGenerateTask.java
  Log:
  Add in a simple task to generate metadata from source files
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/generator/FormatEnum.java
  
  Index: FormatEnum.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.framework.tools.generator;
  
  import org.apache.tools.ant.types.EnumeratedAttribute;
  
  /**
   * This is an enumeration that gives the option of either
   * outputting as xml or as a serialized format.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/12 01:23:34 $
   */
  public class FormatEnum
      extends EnumeratedAttribute
  {
      public String[] getValues()
      {
          return new String[]{"xml", "serialized"};
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/generator/MetaGenerateTask.java
  
  Index: MetaGenerateTask.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.framework.tools.generator;
  
  import com.thoughtworks.qdox.ant.AbstractQdoxTask;
  import com.thoughtworks.qdox.model.DocletTag;
  import com.thoughtworks.qdox.model.JavaClass;
  import java.io.File;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.FileOutputStream;
  import org.apache.avalon.framework.info.ComponentInfo;
  import org.apache.avalon.framework.tools.infobuilder.SerializedInfoWriter;
  import org.apache.avalon.framework.tools.infobuilder.InfoWriter;
  import org.apache.avalon.framework.tools.infobuilder.XMLInfoWriter;
  import org.apache.tools.ant.BuildException;
  
  /**
   * Generate MetaData for info package from the source files.
   * See XXXXXXX for a description of the format in which the
   *
   * @author Paul Hammant
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/12 01:23:34 $
   */
  public final class MetaGenerateTask
      extends AbstractQdoxTask
  {
      /**
       * A utility object that writes out info as xml files.
       */
      private static final InfoWriter c_xmlWriter = new XMLInfoWriter();
  
      /**
       * A utility object that writes out info as serialized object files.
       */
      private static final InfoWriter c_serWriter = new SerializedInfoWriter();
  
      /**
       * The destination directory for metadata files.
       */
      private File m_destDir;
  
      /**
       * Variable that indicates whether the output
       * should be done as xml (if true) or as serialized
       * objests (if false).
       */
      private boolean m_xmlOutput = true;
  
      /**
       * Set the desitation directory to generate output files to.
       *
       * @param destDir The destination directory
       */
      public void setDestDir( final File destDir )
      {
          m_destDir = destDir;
      }
  
      /**
       * Specify the output format. Must be one of xml or serialized.
       *
       * @param format the output format
       */
      public void setFormat( final FormatEnum format )
      {
          m_xmlOutput = "xml".equals( format.getValue() );
      }
  
      /**
       * Execute generator task.
       */
      public void execute()
          throws BuildException
      {
          if( null == m_destDir )
          {
              final String message = "DestDir not specified";
              throw new BuildException( message );
          }
          if( !m_destDir.isDirectory() )
          {
              final String message = "DestDir is not a directory.";
              throw new BuildException( message );
          }
  
          if( !m_destDir.mkdirs() )
          {
              final String message = "DestDir could not be created.";
              throw new BuildException( message );
          }
  
          String message = "Writing Info descriptors as ";
          if( m_xmlOutput )
          {
              message += "xml.";
          }
          else
          {
              message += "serialized objects.";
          }
          log( message );
  
          super.execute();
  
          try
          {
              writeInfoMetaData();
          }
          catch( final Exception e )
          {
              throw new BuildException( e.toString(), e );
          }
      }
  
      /**
       * Output the metadata files.
       *
       * @throws IOException If a problem writing output
       */
      private void writeInfoMetaData() throws IOException
      {
          final int size = allClasses.size();
          for( int i = 0; i < size; i++ )
          {
              final JavaClass javaClass = (JavaClass)allClasses.get( i );
              final DocletTag tag = javaClass.getTagByName( "avalon.component" );
              if( null != tag )
              {
                  final ComponentInfo info = InfoBuilder.buildComponentInfo( javaClass );
                  final String fqn = javaClass.getFullyQualifiedName();
                  final String filename =
                      fqn.replace( '.', File.separatorChar ) + "-info";
                  final String baseFile =
                      new File( m_destDir, filename ).getCanonicalPath();
  
                  if( m_xmlOutput )
                  {
                      final OutputStream outputStream = new FileOutputStream( baseFile + ".xml"
);
                      try
                      {
                          c_xmlWriter.writeComponentInfo( info, outputStream );
                      }
                      catch( final Exception e )
                      {
                          log( "Error writing " + baseFile + ". Cause: " + e );
                      }
                      finally
                      {
                          shutdownStream( outputStream );
                      }
  
                  }
                  else
                  {
                      final OutputStream outputStream = new FileOutputStream( baseFile + ".ser"
);
                      try
                      {
                          c_serWriter.writeComponentInfo( info, outputStream );
                      }
                      catch( final Exception e )
                      {
                          log( "Error writing " + baseFile + ". Cause: " + e );
                      }
                      finally
                      {
                          shutdownStream( outputStream );
                      }
                  }
              }
          }
      }
  
      /**
       * Close the specified output stream and swallow any exceptions.
       *
       * @param outputStream the output stream
       */
      private void shutdownStream( final OutputStream outputStream )
      {
          if( null != outputStream )
          {
              try
              {
                  outputStream.close();
              }
              catch( IOException e )
              {
              }
          }
      }
  
  }
  
  
  

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


Mime
View raw message