1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.pipeline.config;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import org.apache.commons.digester.Digester;
28 import org.apache.commons.digester.RuleSet;
29 import org.apache.commons.pipeline.PipelineCreationException;
30 import org.apache.commons.pipeline.Pipeline;
31 import org.xml.sax.SAXException;
32
33
34
35
36
37
38
39 public class DigesterPipelineFactory implements org.apache.commons.pipeline.PipelineFactory {
40
41
42 private List<RuleSet> ruleSets = new ArrayList<RuleSet>();
43
44
45
46
47
48 private URL confURL;
49
50
51
52
53
54
55 public DigesterPipelineFactory(URL confURL) {
56 if (confURL == null) throw new IllegalArgumentException("Configuration file URL may not be null.");
57 this.confURL = confURL;
58
59
60
61
62 ruleSets.add(new PipelineRuleSet(ruleSets));
63 }
64
65
66
67
68
69
70 public Pipeline createPipeline() throws PipelineCreationException {
71 try {
72 Digester digester = new Digester();
73 this.initDigester(digester);
74
75 InputStream in = confURL.openStream();
76 try {
77 return (Pipeline) digester.parse(in);
78 } finally {
79 in.close();
80 }
81 } catch (IOException e) {
82 throw new PipelineCreationException("An IOException occurred reading the configuration file: " + e.getMessage(), e);
83 } catch (SAXException e) {
84 throw new PipelineCreationException("A formatting error exists in the configuration file: " + e.getMessage(), e);
85 }
86 }
87
88
89
90
91
92 public void initDigester(Digester digester) {
93 for (Iterator iter = ruleSets.iterator(); iter.hasNext();) {
94 digester.addRuleSet((RuleSet) iter.next());
95 }
96 }
97
98
99
100
101
102
103 public void addRuleSet(RuleSet ruleSet) {
104 this.ruleSets.add(ruleSet);
105 }
106
107
108
109
110
111
112
113
114
115
116 public static void main(String[] argv) {
117 try {
118 File configFile = new File(argv[0]);
119
120 DigesterPipelineFactory factory = new DigesterPipelineFactory(configFile.toURL());
121 Pipeline pipeline = factory.createPipeline();
122 for (int i = 1; i < argv.length; i++) {
123 pipeline.getSourceFeeder().feed(argv[i]);
124 }
125
126 System.out.println("Pipeline created, about to begin processing...");
127
128 pipeline.start();
129 pipeline.finish();
130
131 System.out.println("Pipeline successfully finished processing. See logs for details.");
132 } catch (Exception e) {
133 e.printStackTrace(System.err);
134 }
135 }
136 }