View Javadoc

1   package org.apache.commons.digester3.examples.plugins.pipeline;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */ 
19  
20  import org.apache.commons.digester3.Digester;
21  import org.apache.commons.digester3.plugins.PluginRules;
22  import org.apache.commons.digester3.plugins.PluginCreateRule;
23  import java.io.*;
24  
25  /**
26   * This is the "main" class for this example.
27   * <p>
28   * It can be run via <code>java Pipeline config-file-name</code>.
29   * <p>
30   * The specified config file is parsed using the Apache Commons Digester.
31   * This config file specifies an input file to be read, a number of
32   * user-defined transform classes to be instantiated and configured from
33   * the config file, and an output file.
34   * <p>
35   * The contents of the input file is then passed to the transform objects,
36   * and the output written to the output file. 
37   * <p>
38   * Why not try writing your own transform classes, and plugging them in.
39   * Note that they can configure themselves from the main config file in
40   * any manner the Digester supports, without changing a line of this
41   * application.
42   */
43  public class Pipeline
44  {
45  
46      private String source;
47  
48      private String dest;
49  
50      private Transform transformer;
51  
52      public static void main( String[] args )
53      {
54          if ( args.length != 1 )
55          {
56              System.err.println( "usage: pipeline config-file" );
57              System.exit( -1 );
58          }
59          String configFile = args[0];
60  
61          Digester digester = new Digester();
62          PluginRules rc = new PluginRules();
63          digester.setRules( rc );
64  
65          digester.addObjectCreate( "pipeline", Pipeline.class );
66  
67          digester.addCallMethod( "pipeline/source", "setSource", 1 );
68          digester.addCallParam( "pipeline/source", 0, "file" );
69  
70          PluginCreateRule pcr = new PluginCreateRule( Transform.class );
71          digester.addRule( "pipeline/transform", pcr );
72          digester.addSetNext( "pipeline/transform", "setTransform" );
73  
74          digester.addCallMethod( "pipeline/destination", "setDest", 1 );
75          digester.addCallParam( "pipeline/destination", 0, "file" );
76  
77          Pipeline pipeline = null;
78          try
79          {
80              pipeline = digester.parse( configFile );
81          }
82          catch ( Exception e )
83          {
84              System.err.println( "oops exception occurred during parse." );
85              e.printStackTrace();
86              System.exit( -1 );
87          }
88  
89          try
90          {
91              pipeline.execute();
92          }
93          catch ( Exception e )
94          {
95              System.err.println( "oops exception occurred during pipeline execution." );
96              e.printStackTrace();
97              System.exit( -1 );
98          }
99      }
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 }