bloritsch    2002/09/30 08:59:25

  Modified:    infomover build.xml
               infomover/src/java/org/apache/infomover/jobmanager/impl
                        JobImpl.java
               infomover/src/java/org/apache/infomover/notifier
                        Notifier.java
               infomover/src/java/org/apache/infomover/output Output.java
  Added:       infomover/src/java/org/apache/infomover/input TestInput.java
               infomover/src/java/org/apache/infomover/notifier
                        LogNotifier.java
               infomover/src/java/org/apache/infomover/output
                        XMLOutput.java
  Log:
  update InfoMover with the work I have been doing
  
  Revision  Changes    Path
  1.4       +1 -1      jakarta-avalon-apps/infomover/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-apps/infomover/build.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- build.xml 3 Sep 2002 07:37:25 -0000       1.3
  +++ build.xml 30 Sep 2002 15:59:25 -0000      1.4
  @@ -1,6 +1,6 @@
   <?xml version="1.0"?>
   
  -<project name="Overlord" default="main" basedir=".">
  +<project name="InfoMover" default="main" basedir=".">
   
   <!-- 
==========================================================================================
       PROPERTY SETUP
  
  
  
  1.1                  
jakarta-avalon-apps/infomover/src/java/org/apache/infomover/input/TestInput.java
  
  Index: TestInput.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
      must not be used to endorse or promote products derived from this  software
      without  prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.infomover.input;
  
  import org.apache.infomover.transaction.*;
  
  
  /**
   * @author <a href="${[EMAIL PROTECTED]}">Berin Loritsch</a>
   */
  public class TestInput implements Input
  {
      /**
       * Get the next transaction to process.  This method will return
       * <code>null</code> when there is no more transcations to process.
       */
      public Transaction getNextTransaction()
      {
          Transaction trans = new Transaction();
          Record rec = new Record("test", Record.REFERENCE);
          rec.addField( new Field("message", "Test Message", Field.STRING));
          trans.addRecord( rec );
  
          return trans;
      }
  }
  
  
  
  1.4       +99 -10    
jakarta-avalon-apps/infomover/src/java/org/apache/infomover/jobmanager/impl/JobImpl.java
  
  Index: JobImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-apps/infomover/src/java/org/apache/infomover/jobmanager/impl/JobImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JobImpl.java      9 Sep 2002 21:11:01 -0000       1.3
  +++ JobImpl.java      30 Sep 2002 15:59:25 -0000      1.4
  @@ -50,11 +50,17 @@
   package org.apache.infomover.jobmanager.impl;
   
   import org.apache.infomover.jobmanager.Job;
  -import org.apache.infomover.jobmanager.JobException;
  +import org.apache.infomover.notifier.Notifier;
  +import org.apache.infomover.input.Input;
  +import org.apache.infomover.manipulator.Manipulator;
  +import org.apache.infomover.output.Output;
   import org.apache.avalon.framework.activity.*;
   import org.apache.avalon.framework.configuration.*;
   import org.apache.avalon.framework.logger.*;
   import org.apache.avalon.framework.service.*;
  +import org.apache.avalon.framework.context.*;
  +import org.apache.avalon.framework.container.ContainerUtil;
  +import org.apache.avalon.framework.parameters.Parameters;
   
   /**
    * The <code>Job</code> interface represents a job that can be
  @@ -64,17 +70,23 @@
    * @version 1.0
    */
   public class JobImpl extends AbstractLogEnabled
  -    implements Job, Configurable, Suspendable,
  -               Serviceable, Startable, Initializable
  +    implements Job, Configurable, Suspendable, Contextualizable,
  +               Serviceable, Startable, Initializable, Disposable
   {
       private Configuration m_config;
       private ServiceManager m_manager;
  +    private Context m_context;
  +    private ClassLoader m_loader;
   
       private boolean m_isScheduled;
       private int m_transactionCount;
       private int m_successfulTransactionCount;
       private long m_lastRun;
   
  +    private Input m_input;
  +    private Manipulator[] m_manipulators;
  +    private Output m_output;
  +
       private JobRunner m_runner;
   
       /**
  @@ -85,6 +97,7 @@
           m_transactionCount = 0;
           m_successfulTransactionCount = 0;
           m_lastRun = -1;
  +        m_isScheduled = false;
       }
   
       /**
  @@ -159,11 +172,57 @@
           m_manager = manager;
       }
   
  +    public void contextualize( Context context )
  +    {
  +        m_context = context;
  +
  +        try
  +        {
  +            m_loader = (ClassLoader) context.get( "avalon.classloader" );
  +        }
  +        catch ( Exception e )
  +        {
  +            m_loader = Thread.currentThread().getContextClassLoader();
  +
  +            if ( null == m_loader )
  +            {
  +                m_loader = getClass().getClassLoader();
  +            }
  +        }
  +    }
  +
       public void initialize()
           throws Exception
       {
  -        // Set up the resources.
  -        throw new UnsupportedOperationException();
  +        m_isScheduled = m_config.getChild( "schedule" ) != null;
  +        Configuration inputConfig = m_config.getChild( "input" );
  +        Configuration[] manipulatorConfigs = m_config.getChildren( "manipulator" );
  +        Configuration outputConfig = m_config.getChild( "output" );
  +
  +        m_input = (Input) createComponent( inputConfig );
  +        m_output = (Output) createComponent( outputConfig );
  +        m_manipulators = new Manipulator[ manipulatorConfigs.length ];
  +
  +        for ( int i = 0; i < manipulatorConfigs.length; i++ )
  +        {
  +            m_manipulators[i] = (Manipulator) createComponent( 
manipulatorConfigs[i] );
  +        }
  +    }
  +
  +    private Object createComponent(Configuration config)
  +        throws Exception
  +    {
  +        String className = config.getAttribute( "type" );
  +        Object component = m_loader.loadClass( className ).newInstance();
  +
  +        ContainerUtil.enableLogging( component, getLogger() );
  +        ContainerUtil.contextualize( component, m_context );
  +        ContainerUtil.service( component, m_manager );
  +        ContainerUtil.configure( component, config );
  +        ContainerUtil.parameterize( component, Parameters.fromConfiguration( config 
) );
  +        ContainerUtil.initialize( component );
  +
  +        return component;
       }
   
       public void suspend()
  @@ -177,27 +236,57 @@
       }
   
       public void start()
  +        throws Exception
       {
           m_lastRun = System.currentTimeMillis();
   
  -        // Get the resources needed to process
  -        m_runner = new JobRunner( null, null, null, null );
  +        ContainerUtil.start( m_input );
  +        ContainerUtil.start( m_output );
  +
  +        for ( int i = 0; i < m_manipulators.length; i++ )
  +        {
  +            ContainerUtil.start( m_manipulators[i] );
  +        }
   
  -        throw new UnsupportedOperationException();
  +        Notifier notifier = (Notifier) m_manager.lookup( Notifier.ROLE );
  +
  +        m_runner = new JobRunner( m_input, m_manipulators, m_output, notifier );
       }
   
       public void stop()
  +        throws Exception
       {
           m_transactionCount = m_runner.transactionCount();
           m_successfulTransactionCount = m_runner.successfulTransactionCount();
   
           if ( m_runner.isRunning() ) m_runner.cancel();
   
  -        // Release the resources needed to process
  +        ContainerUtil.stop( m_input );
  +        ContainerUtil.stop( m_output );
  +
  +        for ( int i = 0; i < m_manipulators.length; i++ )
  +        {
  +            ContainerUtil.stop( m_manipulators[i] );
  +        }
  +
  +        m_manager.release( m_runner.getNotifier() );
   
           m_runner = null;
  +    }
  +
  +    public void dispose()
  +    {
  +        ContainerUtil.dispose( m_input );
  +        ContainerUtil.dispose( m_output );
  +
  +        for ( int i = 0; i < m_manipulators.length; i++ )
  +        {
  +            ContainerUtil.dispose( m_manipulators[i] );
  +        }
   
  -        throw new UnsupportedOperationException();
  +        m_input = null;
  +        m_manipulators = null;
  +        m_output = null;
       }
   }
   
  
  
  
  1.2       +4 -1      
jakarta-avalon-apps/infomover/src/java/org/apache/infomover/notifier/Notifier.java
  
  Index: Notifier.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-apps/infomover/src/java/org/apache/infomover/notifier/Notifier.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Notifier.java     9 Sep 2002 20:06:46 -0000       1.1
  +++ Notifier.java     30 Sep 2002 15:59:25 -0000      1.2
  @@ -49,7 +49,7 @@
   */
   package org.apache.infomover.notifier;
   
  -import org.apache.infomover.transaction.*;
  +import org.apache.infomover.transaction.Response;
   
   /**
    * The Notifier handles all the result notification policies.
  @@ -59,6 +59,9 @@
    */
   public interface Notifier
   {
  +    /** Role name */
  +    String ROLE = Notifier.class.getName();
  +
       /**
        * Perform the actual notification.
        */
  
  
  
  1.1                  
jakarta-avalon-apps/infomover/src/java/org/apache/infomover/notifier/LogNotifier.java
  
  Index: LogNotifier.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
      must not be used to endorse or promote products derived from this  software
      without  prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.infomover.notifier;
  
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.framework.parameters.Parameterizable;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.infomover.transaction.Response;
  
  
  /**
   * The <code>LogNotifier</code> will log all the results to a subcategory of this
   * component.
   */
  public class LogNotifier extends AbstractLogEnabled implements Notifier, 
Parameterizable
  {
      private String m_category;
  
      /**
       * Send the notification messages to the logger.  Success messages will be 
logged
       * with the "info" level, and unseccessful messages will be logged with the 
"error"
       * level.
       */
      public void notify(Response result)
      {
          if ( result.isSuccessful() )
          {
              getNotifyLogger().info( result.getMessage() );
          }
          else
          {
              getNotifyLogger().error( result.getMessage(), result.getCauseOfFailure() 
);
          }
      }
  
      private final Logger getNotifyLogger()
      {
          return getLogger().getChildLogger( m_category );
      }
  
      /**
       * Set the parameters that control this component.  At this current time, the 
LogNotifier
       * only has one parameter that it understands: "category".  If "category" is 
set, we will
       * use the specified subcategory.  Otherwise, it will default to the "notifier" 
subcategory.
       *
       * @param parameters  The parameters for the LogNotifier
       */
      public void parameterize(Parameters parameters)
      {
          m_category = parameters.getParameter( "category", "notifier" );
      }
  }
  
  
  
  1.3       +2 -1      
jakarta-avalon-apps/infomover/src/java/org/apache/infomover/output/Output.java
  
  Index: Output.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-apps/infomover/src/java/org/apache/infomover/output/Output.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Output.java       19 Aug 2002 16:49:48 -0000      1.2
  +++ Output.java       30 Sep 2002 15:59:25 -0000      1.3
  @@ -49,7 +49,8 @@
   */
   package org.apache.infomover.output;
   
  -import org.apache.infomover.transaction.*;
  +import org.apache.infomover.transaction.Response;
  +import org.apache.infomover.transaction.Transaction;
   
   /**
    * An Output component puts information from a transaction into a
  
  
  
  1.1                  
jakarta-avalon-apps/infomover/src/java/org/apache/infomover/output/XMLOutput.java
  
  Index: XMLOutput.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
      must not be used to endorse or promote products derived from this  software
      without  prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.infomover.output;
  
  import org.apache.avalon.framework.activity.*;
  import org.apache.avalon.framework.logger.*;
  import org.apache.avalon.framework.parameters.*;
  
  import org.apache.infomover.transaction.Field;
  import org.apache.infomover.transaction.Record;
  import org.apache.infomover.transaction.Response;
  import org.apache.infomover.transaction.Transaction;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.AttributesImpl;
  
  import javax.xml.transform.Templates;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerException;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.sax.SAXSource;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.transform.stream.StreamSource;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.Iterator;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  
  /**
   * The XMLOutput allows you to output your transactions as a set of XML files.
   * There will be one file per Transaction object processed.
   *
   * @author <a href="[EMAIL PROTECTED]">Berin Loritsch</a>
   */
  public class XMLOutput extends AbstractLogEnabled
          implements Output, Parameterizable, Initializable
  {
      private Templates m_template;
      private URL m_templateSource;
      private String m_outputURI;
      private final static Pattern m_pattern = Pattern.compile( "\\$\\{(.*)\\}" );
      private int m_transactionCount = 0;
  
      /**
       * Processes a transaction, placing the information in a resource.
       */
      public Response process( Transaction trans )
      {
          OutputStream outputStream = null;
          try
          {
              outputStream = resolve( m_outputURI 
).openConnection().getOutputStream();
          }
          catch( Exception e )
          {
              trans.setResponse( e.getMessage(), e );
          }
  
          if( trans.isSuccessful() )
          {
              SAXSource source = new SAXSource();
              StreamResult result = new StreamResult( outputStream );
  
              ContentHandler handler = source.getXMLReader().getContentHandler();
  
              try
              {
                  handler.startDocument();
  
                  processTransaction( trans, handler );
  
                  handler.endDocument();
              }
              catch( SAXException se )
              {
                  trans.setResponse( se.getMessage(), se );
              }
  
              if( m_template != null )
              {
                  try
                  {
                      Transformer transformer = m_template.newTransformer();
                      transformer.transform( source, result );
                  }
                  catch( TransformerException te )
                  {
                      trans.setResponse( te.getMessage(), te );
                  }
              }
  
              try
              {
                  outputStream.flush();
                  outputStream.close();
              }
              catch( IOException ioe )
              {
                  trans.setResponse( ioe.getMessage(), ioe );
              }
          }
          return trans.getResponse();
      }
  
      /** TODO: Move resolve to a static method */
      private URL resolve( String outputURI ) throws MalformedURLException
      {
          if( null == outputURI || "".equals( outputURI ) )
          {
              throw new MalformedURLException( "Destination URI is not set." );
          }
  
          Matcher matcher = m_pattern.matcher( outputURI );
          StringBuffer sb = new StringBuffer();
  
          while( matcher.find() )
          {
              matcher.appendReplacement( sb, resolveItem( matcher.group() ) );
          }
  
          return new URL( outputURI );
      }
  
      /** TODO: add real implementation for resolveItem */
      private String resolveItem( String str )
      {
          if( null == str ) throw new IllegalArgumentException( "Cannot resolve a null 
String" );
  
          String value = str;
  
          if( str.startsWith( "date" ) )
          {
              // resolve date
              value = new java.util.Date().toString();
          }
          else if( str.equals( "name" ) )
          {
              value = "job";
          }
          else if( str.equals( "transaction" ) )
          {
              value = Integer.toString( m_transactionCount );
          }
  
          return value;
      }
  
      private void processTransaction( Transaction trans, ContentHandler handler ) 
throws SAXException
      {
          AttributesImpl attrs = new AttributesImpl();
          handler.startElement( "", "transaction", "transaction", attrs );
  
          Iterator it = trans.iterator();
          while( it.hasNext() )
          {
              processRecord( (Record) it.next(), handler );
          }
  
          handler.endElement( "", "transaction", "transaction" );
      }
  
      private void processRecord( Record record, ContentHandler handler ) throws 
SAXException
      {
          AttributesImpl attrs = new AttributesImpl();
          attrs.addAttribute( "", "name", "name", "CDATA", record.getName() );
          attrs.addAttribute( "", "action", "action", "CDATA", getRecordType( 
record.getType() ) );
  
          handler.startElement( "", "record", "record", attrs );
  
          Iterator it = record.iterator();
          while( it.hasNext() )
          {
              processField( (Field) it.next(), handler );
          }
  
          handler.endElement( "", "record", "record" );
      }
  
      private String getRecordType( int type )
      {
          String answer = "";
  
          switch( type )
          {
              case Record.ADD:
                  answer = "add";
                  break;
  
              case Record.DELETE:
                  answer = "delete";
                  break;
  
              case Record.REFERENCE:
                  answer = "ref";
                  break;
  
              case Record.UPDATE:
                  answer = "update";
                  break;
  
              default:
                  // should never reach here
                  answer = "UNKNOWN";
                  break;
          }
  
          return answer;
      }
  
      private void processField( Field field, ContentHandler handler ) throws 
SAXException
      {
          AttributesImpl attrs = new AttributesImpl();
          attrs.addAttribute( "", "name", "name", "CDATA", field.getName() );
          attrs.addAttribute( "", "type", "type", "CDATA", getFieldType( 
field.getType() ) );
  
          String value = field.getValue().toString();
  
          handler.startElement( "", "field", "field", attrs );
          handler.characters( value.toCharArray(), 0, value.length() );
          handler.endElement( "", "field", "field" );
      }
  
      private String getFieldType( int type )
      {
          String answer = "";
  
          switch( type )
          {
              case Field.BOOLEAN:
                  answer = "boolean";
                  break;
  
              case Field.DATE:
                  answer = "date";
                  break;
  
              case Field.FLOAT:
                  answer = "float";
                  break;
  
              case Field.INTEGER:
                  answer = "integer";
                  break;
  
              case Field.STRING:
                  answer = "string";
                  break;
  
              case Field.TIME:
                  answer = "time";
                  break;
  
              case Field.TIMESTAMP
                  ;
                  answer = "timestamp";
                  break;
  
              default:
                  // should never reach here
                  answer = "UNKNOWN";
                  break;
          }
  
          return answer;
      }
  
      public void parameterize( Parameters params ) throws ParameterException
      {
          m_outputURI = params.getParameter( "source" );
          String templateURI = params.getParameter( "transformer", "" );
  
          if( !"".equals( templateURI ) )
          {
              try
              {
                  m_templateSource = new URL( templateURI );
              }
              catch( MalformedURLException mue )
              {
                  throw new ParameterException( "Invalid template URI: " + 
templateURI, mue );
              }
          }
      }
  
      public void initialize() throws Exception
      {
          if( m_templateSource != null )
          {
              try
              {
                  TransformerFactory factory = TransformerFactory.newInstance();
                  m_template = factory.newTemplates( new StreamSource( 
m_templateSource.openStream() ) );
              }
              catch( Exception e )
              {
                  getLogger().error( "Could not set up the Transformer", e );
                  throw e;
              }
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to