001package org.apache.commons.digester3.examples.plugins.pipeline;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */ 
019
020import org.apache.commons.digester3.Digester;
021import org.apache.commons.digester3.plugins.PluginRules;
022import org.apache.commons.digester3.plugins.PluginCreateRule;
023import java.io.*;
024
025/**
026 * This is the "main" class for this example.
027 * <p>
028 * It can be run via <code>java Pipeline config-file-name</code>.
029 * <p>
030 * The specified config file is parsed using the Apache Commons Digester.
031 * This config file specifies an input file to be read, a number of
032 * user-defined transform classes to be instantiated and configured from
033 * the config file, and an output file.
034 * <p>
035 * The contents of the input file is then passed to the transform objects,
036 * and the output written to the output file. 
037 * <p>
038 * Why not try writing your own transform classes, and plugging them in.
039 * Note that they can configure themselves from the main config file in
040 * any manner the Digester supports, without changing a line of this
041 * application.
042 */
043public class Pipeline
044{
045
046    private String source;
047
048    private String dest;
049
050    private Transform transformer;
051
052    public static void main( String[] args )
053    {
054        if ( args.length != 1 )
055        {
056            System.err.println( "usage: pipeline config-file" );
057            System.exit( -1 );
058        }
059        String configFile = args[0];
060
061        Digester digester = new Digester();
062        PluginRules rc = new PluginRules();
063        digester.setRules( rc );
064
065        digester.addObjectCreate( "pipeline", Pipeline.class );
066
067        digester.addCallMethod( "pipeline/source", "setSource", 1 );
068        digester.addCallParam( "pipeline/source", 0, "file" );
069
070        PluginCreateRule pcr = new PluginCreateRule( Transform.class );
071        digester.addRule( "pipeline/transform", pcr );
072        digester.addSetNext( "pipeline/transform", "setTransform" );
073
074        digester.addCallMethod( "pipeline/destination", "setDest", 1 );
075        digester.addCallParam( "pipeline/destination", 0, "file" );
076
077        Pipeline pipeline = null;
078        try
079        {
080            pipeline = digester.parse( configFile );
081        }
082        catch ( Exception e )
083        {
084            System.err.println( "oops exception occurred during parse." );
085            e.printStackTrace();
086            System.exit( -1 );
087        }
088
089        try
090        {
091            pipeline.execute();
092        }
093        catch ( Exception e )
094        {
095            System.err.println( "oops exception occurred during pipeline execution." );
096            e.printStackTrace();
097            System.exit( -1 );
098        }
099    }
100
101    public void setSource( String source )
102    {
103        this.source = source;
104    }
105
106    public void setDest( String dest )
107    {
108        this.dest = dest;
109    }
110
111    public void setTransform( Transform transformer )
112    {
113        this.transformer = transformer;
114    }
115
116    private void execute()
117        throws IOException
118    {
119        FileReader inRaw = new FileReader( source );
120        FileWriter out = new FileWriter( dest );
121
122        BufferedReader in = new BufferedReader( inRaw );
123
124        while ( true )
125        {
126            String inStr = in.readLine();
127            if ( inStr == null )
128                break;
129
130            String outStr = transformer.transform( inStr );
131            out.write( outStr );
132            out.write( '\n' );
133        }
134
135        inRaw.close();
136        out.close();
137
138        System.out.println( "Contents of file " + source + " have been transformed, and" + " written to file " + dest
139            + "." );
140    }
141
142}