You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/11/12 02:22:45 UTC

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

donaldp     2002/11/11 17:22:45

  Added:       info/src/java/org/apache/avalon/framework/tools/infobuilder
                        XMLInfoWriter.java
  Log:
  Add in the first cut of an xml writer
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/XMLInfoWriter.java
  
  Index: XMLInfoWriter.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.OutputStream;
  import java.io.IOException;
  import java.io.Writer;
  import java.io.OutputStreamWriter;
  import org.apache.avalon.framework.info.ComponentInfo;
  import org.apache.avalon.framework.info.ServiceInfo;
  import org.apache.avalon.framework.info.ComponentDescriptor;
  import org.apache.avalon.framework.info.Attribute;
  import org.apache.avalon.framework.info.LoggerDescriptor;
  import org.apache.avalon.framework.info.ContextDescriptor;
  import org.apache.avalon.framework.info.EntryDescriptor;
  import org.apache.avalon.framework.info.ServiceDescriptor;
  import org.apache.avalon.framework.info.DependencyDescriptor;
  
  /**
   * Write {@link ComponentInfo} and {@link ServiceInfo} objects
   * to a stream as xml documents.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/12 01:22:45 $
   */
  public class XMLInfoWriter
      implements InfoWriter
  {
      private static final String CONTEXT_CLASS = "org.apache.avalon.framework.context.Context";
  
      public void writeServiceInfo( final ServiceInfo info,
                                    final OutputStream outputStream )
          throws Exception
      {
          throw new UnsupportedOperationException();
      }
  
      public void writeComponentInfo( final ComponentInfo info,
                                      final OutputStream outputStream )
          throws Exception
      {
          final Writer writer = new OutputStreamWriter( outputStream );
          writeHeader( writer );
          writeDoctype( writer, "component-info" );
          writer.write( "<component-info>" );
          writeComponent( writer, info.getComponentDescriptor() );
          writeLoggers( writer, info.getLoggers() );
          writeContext( writer, info.getContextDescriptor() );
          writeServices( writer, info.getServices() );
          writeDependencies( writer, info.getDependencies() );
          writer.write( "</component-info>" );
          writer.flush();
      }
  
      private void writeHeader( final Writer writer )
          throws IOException
      {
          writer.write( "<?xml version=\"1.0\" ?>" );
      }
  
      private void writeDoctype( final Writer writer,
                                 final String root )
          throws IOException
      {
          final String doctype =
              "<!DOCTYPE " + root +
              " PUBLIC \"-//AVALON/Component Info DTD Version 1.0//EN\" " +
              "\"http://jakarta.apache.org/avalon/dtds/info/componentinfo_1_0.dtd\" >";
          writer.write( doctype );
      }
  
      private void writeComponent( final Writer writer,
                                   final ComponentDescriptor component )
          throws IOException
      {
          writer.write( "<component type=\"" );
          writer.write( component.getImplementationKey() );
          final Attribute[] attributes = component.getAttributes();
          if( 0 == attributes.length )
          {
              writer.write( "\"/>" );
          }
          else
          {
              writer.write( "\">" );
              writeAttributes( writer, attributes );
              writer.write( "</component>" );
          }
      }
  
      private void writeLoggers( final Writer writer,
                                 final LoggerDescriptor[] loggers )
          throws IOException
      {
          writer.write( "<loggers>" );
          for( int i = 0; i < loggers.length; i++ )
          {
              writeLogger( writer, loggers[ i ] );
          }
  
          writer.write( "</loggers>" );
      }
  
      private void writeLogger( final Writer writer,
                                final LoggerDescriptor logger )
          throws IOException
      {
          writer.write( "<logger name=\"" );
          writer.write( logger.getName() );
          final Attribute[] attributes = logger.getAttributes();
          if( 0 == attributes.length )
          {
              writer.write( "\"/>" );
          }
          else
          {
              writer.write( "\">" );
              writeAttributes( writer, attributes );
              writer.write( "</logger>" );
          }
      }
  
      private void writeContext( final Writer writer,
                                 final ContextDescriptor context )
          throws IOException
      {
          writer.write( "<context" );
  
          final String type = context.getType();
          if( !CONTEXT_CLASS.equals( type ) )
          {
              writer.write( " type=\"" );
              writer.write( type );
              writer.write( "\"" );
          }
  
          final Attribute[] attributes = context.getAttributes();
          final EntryDescriptor[] entrys = context.getEntrys();
  
          if( 0 == attributes.length && 0 == entrys.length )
          {
              writer.write( "/>" );
          }
          else
          {
              writer.write( ">" );
              for( int i = 0; i < entrys.length; i++ )
              {
                  writeEntry( writer, entrys[ i ] );
              }
              writeAttributes( writer, attributes );
          }
  
          writer.write( "</context>" );
      }
  
      private void writeEntry( final Writer writer,
                               final EntryDescriptor entry )
          throws IOException
      {
          writer.write( "<entry key=\"" );
          writer.write( entry.getKey() );
          writer.write( "\" type=\"" );
          writer.write( entry.getType() );
  
          if( entry.isOptional() )
          {
              writer.write( "\" optional=\"true" );
          }
  
          final Attribute[] attributes = entry.getAttributes();
          if( 0 == attributes.length )
          {
              writer.write( "\"/>" );
          }
          else
          {
              writer.write( "\">" );
              writeAttributes( writer, attributes );
              writer.write( "</entry>" );
          }
      }
  
      private void writeServices( final Writer writer,
                                  final ServiceDescriptor[] services )
          throws IOException
      {
          writer.write( "<services>" );
          for( int i = 0; i < services.length; i++ )
          {
              final ServiceDescriptor service = services[ i ];
              writer.write( "<service type=\"" );
              writer.write( service.getImplementationKey() );
              final Attribute[] attributes = service.getAttributes();
              if( 0 == attributes.length )
              {
                  writer.write( "\"/>" );
              }
              else
              {
                  writer.write( "\">" );
                  writeAttributes( writer, attributes );
                  writer.write( "</service>" );
              }
          }
          writer.write( "</services>" );
      }
  
      private void writeDependencies( final Writer writer,
                                      final DependencyDescriptor[] dependencies )
          throws IOException
      {
          writer.write( "<dependencies>" );
          for( int i = 0; i < dependencies.length; i++ )
          {
              final DependencyDescriptor dependency = dependencies[ i ];
              writer.write( "<dependency " );
  
              if( !dependency.getKey().equals( dependency.getType() ) )
              {
                  writer.write( "key=\"" );
                  writer.write( dependency.getKey() );
                  writer.write( "\" " );
              }
  
              writer.write( "type=\"" );
              writer.write( dependency.getType() );
  
              if( dependency.isOptional() )
              {
                  writer.write( "\" optional=\"true" );
              }
  
              final Attribute[] attributes = dependency.getAttributes();
              if( 0 == attributes.length )
              {
                  writer.write( "\"/>" );
              }
              else
              {
                  writer.write( "\">" );
                  writeAttributes( writer, attributes );
                  writer.write( "</dependency>" );
              }
          }
          writer.write( "</dependencies>" );
      }
  
      private void writeAttributes( final Writer writer,
                                    final Attribute[] attributes )
          throws IOException
      {
          for( int i = 0; i < attributes.length; i++ )
          {
              writeAttribute( writer, attributes[ i ] );
          }
      }
  
      private void writeAttribute( final Writer writer,
                                   final Attribute attribute )
          throws IOException
      {
          writer.write( "<attribute name=\"" );
          writer.write( attribute.getName() );
          writer.write( "\">" );
  
          final String[] names = attribute.getParameterNames();
          for( int i = 0; i < names.length; i++ )
          {
              final String name = names[ i ];
              final String value = attribute.getParameter( name );
              writer.write( "<param name=\"" );
              writer.write( name );
              writer.write( "\" value=\"" );
              writer.write( value );
              writer.write( "\"/>" );
          }
  
          writer.write( "</attribute>" );
      }
  }
  
  
  

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