You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by le...@apache.org on 2002/11/30 04:16:11 UTC

cvs commit: jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools LegacyBlockInfoWriter.java LegacyUtil.java

leif        2002/11/29 19:16:11

  Added:       info/src/java/org/apache/avalon/framework/tools
                        LegacyBlockInfoWriter.java LegacyUtil.java
  Log:
  Commit for PeterD
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/LegacyBlockInfoWriter.java
  
  Index: LegacyBlockInfoWriter.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.infobuilder;
  
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  import org.apache.avalon.framework.info.ComponentInfo;
  import org.apache.avalon.framework.info.DependencyDescriptor;
  import org.apache.avalon.framework.info.FeatureDescriptor;
  import org.apache.avalon.framework.info.SchemaDescriptor;
  import org.apache.avalon.framework.info.ServiceDescriptor;
  
  /**
   * Write {@link ComponentInfo} objects to a stream as xml
   * documents in legacy BlockInfo format.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/30 03:16:11 $
   */
  public class LegacyBlockInfoWriter
      implements InfoWriter
  {
      /**
       * Write out info representation to xml.
       *
       * @param info the info object
       * @param outputStream the stream to write to
       * @throws IOException if unable to write xml
       */
      public void writeComponentInfo( final ComponentInfo info,
                                      final OutputStream outputStream )
          throws Exception
      {
          final Writer writer = new OutputStreamWriter( outputStream );
          writeHeader( writer );
          writeDoctype( writer );
          writer.write( "<blockinfo>" );
          writeBlock( writer, info );
          writeServices( writer, info.getServices() );
          writeMxServices( writer, info.getServices() );
          writeDependencies( writer, info.getDependencies() );
          writer.write( "</blockinfo>" );
          writer.flush();
      }
  
      private void writeHeader( final Writer writer )
          throws IOException
      {
          writer.write( "<?xml version=\"1.0\" ?>" );
      }
  
      /**
       * Write out DOCType delcaration.
       *
       * @param writer the writer
       * @throws IOException if unable to write xml
       */
      private void writeDoctype( final Writer writer )
          throws IOException
      {
          final String doctype =
              "<!DOCTYPE blockinfo " +
              "PUBLIC \"-//PHOENIX/Block Info DTD Version 1.0//EN\" " +
              "\"http://jakarta.apache.org/avalon/dtds/phoenix/blockinfo_1_0.dtd\">";
          writer.write( doctype );
      }
  
      /**
       * Write out xml representation of a component.
       *
       * @param writer the writer
       * @param info the component info
       * @throws IOException if unable to write xml
       */
      private void writeBlock( final Writer writer,
                               final ComponentInfo info )
          throws IOException
      {
          writer.write( "<block>\n" );
          writer.write( "  <version>1.0</version>" );
  
          final SchemaDescriptor schema = info.getConfigurationSchema();
          if( null != schema )
          {
              final String output =
                  "  <schema-type>" + schema.getType() + "</schema-type>";
              writer.write( output );
          }
  
          writer.write( "</block>" );
      }
  
      /**
       * Write out xml representation of a set of services.
       *
       * @param writer the writer
       * @param services the services
       * @throws IOException if unable to write xml
       */
      private void writeServices( final Writer writer,
                                  final ServiceDescriptor[] services )
          throws IOException
      {
          if( 0 == services.length )
          {
              return;
          }
  
          writer.write( "<services>" );
          for( int i = 0; i < services.length; i++ )
          {
              final ServiceDescriptor service = services[ i ];
              if( !LegacyUtil.isMxService( service ) )
              {
                  writeService( writer, service.getType(), service );
              }
          }
          writer.write( "</services>" );
      }
  
  
      /**
       * Write out xml representation of a set of services.
       *
       * @param writer the writer
       * @param services the services
       * @throws IOException if unable to write xml
       */
      private void writeMxServices( final Writer writer,
                                  final ServiceDescriptor[] services )
          throws IOException
      {
          if( 0 == services.length )
          {
              return;
          }
  
          writer.write( "<management-access-points>" );
          for( int i = 0; i < services.length; i++ )
          {
              final ServiceDescriptor service = services[ i ];
              if( LegacyUtil.isMxService( service ) )
              {
                  writeService( writer, service.getType(), service );
              }
          }
          writer.write( "</management-access-points>" );
      }
  
      /**
       * Write out xml representation of a set of dependencies.
       *
       * @param writer the writer
       * @param dependencies the dependencies
       * @throws IOException if unable to write xml
       */
      private void writeDependencies( final Writer writer,
                                      final DependencyDescriptor[] dependencies )
          throws IOException
      {
          if( 0 == dependencies.length )
          {
              return;
          }
  
          writer.write( "<dependencies>" );
          for( int i = 0; i < dependencies.length; i++ )
          {
              final DependencyDescriptor dependency = dependencies[ i ];
              if( dependency.isOptional() )
              {
                  continue;
              }
  
              writer.write( "<dependency>" );
              final String key = dependency.getKey();
              final String type = dependency.getType();
              if( !key.equals( type ) )
              {
                  writer.write( "<role>" );
                  writer.write( key );
                  writer.write( "</role>" );
              }
              writeService( writer, type, dependency );
              writer.write( "</dependency>" );
          }
          writer.write( "</dependencies>" );
      }
  
      /**
       * Write out xml representation of a service.
       *
       * @param writer the writer
       * @param type the type of the service
       * @param feature the feature describing service
       * @throws IOException if unable to write xml
       */
      private void writeService( final Writer writer,
                                 final String type,
                                 final FeatureDescriptor feature )
          throws IOException
      {
          writer.write( "<service name=\"" );
          writer.write( type );
  
          final String version = LegacyUtil.getVersionString( feature );
          if( null != version )
          {
              writer.write( "\" version=\"" );
              writer.write( version );
          }
  
          writer.write( "\"/>" );
      }
  
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/LegacyUtil.java
  
  Index: LegacyUtil.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.infobuilder;
  
  import org.apache.avalon.framework.info.Attribute;
  import org.apache.avalon.framework.info.ContextDescriptor;
  import org.apache.avalon.framework.info.EntryDescriptor;
  import org.apache.avalon.framework.info.FeatureDescriptor;
  import org.apache.avalon.framework.info.ServiceDescriptor;
  import org.apache.avalon.framework.info.ComponentInfo;
  import org.apache.avalon.framework.info.LoggerDescriptor;
  import org.apache.avalon.framework.info.DependencyDescriptor;
  import org.apache.avalon.framework.info.ComponentDescriptor;
  import org.apache.avalon.framework.Version;
  import java.util.Properties;
  
  /**
   * This is a set of constants and utility methods
   * to enablesupport of Legacy BlockInfo files.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/30 03:16:11 $
   */
  public class LegacyUtil
  {
      public static final String MX_ATTRIBUTE_NAME = "phoenix:mx";
      public static final Attribute MX_ATTRIBUTE = new Attribute( MX_ATTRIBUTE_NAME, null );
      public static final String VERSION_ATTRIBUTE_NAME = "phoenix:version";
      public static final String VERSION_ATTRIBUTE_PARAMETER = "version";
      public static final ContextDescriptor CONTEXT_DESCRIPTOR =
          new ContextDescriptor( "org.apache.avalon.phoenix.BlockContext",
                                 EntryDescriptor.EMPTY_SET,
                                 Attribute.EMPTY_SET );
  
      private LegacyUtil()
      {
      }
  
      /**
       * Return the version specified (if any) for feature.
       *
       * @param type the type
       * @return the translated schema type
       */
      public static String translateToSchemaUri( final String type )
      {
          if( type.equals( "relax-ng" ) )
          {
              return "http://relaxng.org/ns/structure/1.0";
          }
          else
          {
              return type;
          }
      }
  
      /**
       * Return the version specified (if any) for feature.
       *
       * @param feature the feature
       * @return the version string
       */
      public static String getVersionString( final FeatureDescriptor feature )
      {
          final Attribute tag = feature.getAttribute( "avalon" );
          if( null != tag )
          {
              return tag.getParameter( "version" );
          }
          return null;
      }
  
      public static Attribute createVersionAttribute( final String version )
      {
          final Properties parameters = new Properties();
          parameters.setProperty( VERSION_ATTRIBUTE_PARAMETER, version );
          return new Attribute( VERSION_ATTRIBUTE_NAME, parameters );
      }
  
      /**
       * Return true if specified service is a management service.
       *
       * @param service the service
       * @return true if specified service is a management service, false otherwise.
       */
      public static boolean isMxService( final ServiceDescriptor service )
      {
          final Attribute tag = service.getAttribute( MX_ATTRIBUTE_NAME );
          return null != tag;
      }
  
      /**
       * Create a version for a feature. Defaults to 1.0 if not specified.
       *
       * @param feature the feature
       * @return the Version object
       */
      public static Version toVersion( final FeatureDescriptor feature )
      {
          final String version = getVersionString( feature );
          if( null == version )
          {
              return new Version( 1, 0, 0 );
          }
          else
          {
              return Version.getVersion( version );
          }
      }
  
      /**
       * Create a {@link ComponentInfo} for a Listener with specified classname.
       *
       * @param implementationKey the classname of listener
       * @return the ComponentInfo for listener
       */
      public static ComponentInfo createListenerInfo( final String implementationKey )
      {
          final ComponentDescriptor descriptor =
              new ComponentDescriptor( implementationKey, Attribute.EMPTY_SET );
          return new ComponentInfo( descriptor,
                                    ServiceDescriptor.EMPTY_SET,
                                    LoggerDescriptor.EMPTY_SET,
                                    ContextDescriptor.EMPTY_CONTEXT,
                                    DependencyDescriptor.EMPTY_SET,
                                    null,
                                    null );
      }
  }
  
  
  

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