KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Pipeline


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import org.apache.commons.digester.Digester;
18 import org.apache.commons.digester.plugins.PluginRules;
19 import org.apache.commons.digester.plugins.PluginCreateRule;
20 import java.io.*;
21
22 /**
23  * This is the "main" class for this example.
24  * <p>
25  * It can be run via <code>java Pipeline config-file-name</code>.
26  * <p>
27  * The specified config file is parsed using the Apache Commons Digester.
28  * This config file specifies an input file to be read, a number of
29  * user-defined transform classes to be instantiated and configured from
30  * the config file, and an output file.
31  * <p>
32  * The contents of the input file is then passed to the transform objects,
33  * and the output written to the output file.
34  * <p>
35  * Why not try writing your own transform classes, and plugging them in.
36  * Note that they can configure themselves from the main config file in
37  * any manner the Digester supports, without changing a line of this
38  * application.
39  */

40  
41 public class Pipeline {
42     
43     private String JavaDoc source;
44     private String JavaDoc dest;
45     private Transform transformer;
46
47     public static void main(String JavaDoc[] args) {
48         if (args.length != 1) {
49             System.err.println("usage: pipeline config-file");
50             System.exit(-1);
51         }
52         String JavaDoc configFile = args[0];
53         
54         Digester digester = new Digester();
55         PluginRules rc = new PluginRules();
56         digester.setRules(rc);
57         
58         digester.addObjectCreate("pipeline", Pipeline.class);
59         
60         digester.addCallMethod("pipeline/source", "setSource", 1);
61         digester.addCallParam("pipeline/source", 0, "file");
62         
63         PluginCreateRule pcr = new PluginCreateRule(Transform.class);
64         digester.addRule("pipeline/transform", pcr);
65         digester.addSetNext("pipeline/transform", "setTransform");
66         
67         digester.addCallMethod("pipeline/destination", "setDest", 1);
68         digester.addCallParam("pipeline/destination", 0, "file");
69         
70         Pipeline pipeline = null;
71         try {
72             pipeline = (Pipeline) digester.parse(configFile);
73         }
74         catch(Exception JavaDoc e) {
75             System.err.println("oops exception occurred during parse.");
76             e.printStackTrace();
77             System.exit(-1);
78         }
79         
80         try {
81             pipeline.execute();
82         }
83         catch (Exception JavaDoc e) {
84             System.err.println("oops exception occurred during pipeline execution.");
85             e.printStackTrace();
86             System.exit(-1);
87         }
88     }
89     
90     public void setSource(String JavaDoc source) {
91         this.source = source;
92     }
93     
94     public void setDest(String JavaDoc dest) {
95         this.dest = dest;
96     }
97     
98     public void setTransform(Transform transformer) {
99         this.transformer = transformer;
100     }
101     
102     private void execute() throws IOException {
103         FileReader inRaw = new FileReader(source);
104         FileWriter out = new FileWriter(dest);
105
106         BufferedReader in = new BufferedReader(inRaw);
107         
108         while(true) {
109             String JavaDoc inStr = in.readLine();
110             if (inStr==null)
111                 break;
112             
113             String JavaDoc outStr = transformer.transform(inStr);
114             out.write(outStr);
115             out.write('\n');
116         }
117         
118         
119         inRaw.close();
120         out.close();
121         
122         System.out.println(
123             "Contents of file " + source + " have been transformed, and"
124             + " written to file " + dest + ".");
125     }
126 }
127
Popular Tags