1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package ch.elca.el4j.services.xmlmerge.tool;
22
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Properties;
28
29 import ch.elca.el4j.services.xmlmerge.XmlMerge;
30 import ch.elca.el4j.services.xmlmerge.config.ConfigurableXmlMerge;
31 import ch.elca.el4j.services.xmlmerge.config.PropertyXPathConfigurer;
32 import ch.elca.el4j.services.xmlmerge.merge.DefaultXmlMerge;
33
34
35
36
37
38
39
40
41
42 public class XmlMergeTool {
43
44
45
46
47 protected XmlMergeTool() { }
48
49
50
51
52
53
54 public static void main(String[] args) throws Exception {
55
56 if (args.length < 2) {
57 usage();
58 }
59
60 int argCursor = 0;
61
62 String configFilename = null;
63
64 if ("-config".equals(args[0])) {
65 if (args[1] != null) {
66 configFilename = args[1];
67 }
68
69 argCursor = 2;
70
71 if (args.length < 4) {
72 usage();
73 }
74 }
75
76 XmlMerge xmlMerge;
77
78 if (configFilename != null) {
79 Properties props = new Properties();
80
81 try {
82 props.load(new FileInputStream(configFilename));
83 } catch (FileNotFoundException ex) {
84 System.err.println("File not found: " + configFilename);
85 System.exit(1);
86 } catch (IOException ioe) {
87 System.err.println("Cannot read file: " + configFilename);
88 System.exit(1);
89 }
90
91 xmlMerge = new ConfigurableXmlMerge(new DefaultXmlMerge(),
92 new PropertyXPathConfigurer(props));
93 } else {
94 xmlMerge = new DefaultXmlMerge();
95 }
96
97 InputStream[] sources = new InputStream[args.length - argCursor];
98
99 for (int i = argCursor; i < args.length; i++) {
100
101 String filename = args[i];
102
103 try {
104 sources[i - argCursor] = new FileInputStream(filename);
105 } catch (FileNotFoundException ex) {
106 System.err.println("File not found: " + filename);
107 System.exit(1);
108 }
109 }
110
111 InputStream stream = xmlMerge.merge(sources);
112
113 byte[] buffer = new byte[2048];
114 int len;
115
116 try {
117 while ((len = stream.read(buffer)) != -1) {
118 System.out.write(buffer, 0, len);
119 }
120 } catch (IOException ioex) {
121
122 ioex.printStackTrace(System.err);
123 System.exit(1);
124 }
125 }
126
127
128
129
130 public static void usage() {
131 System.err
132 .println("xmlmerge [-config <config-file>] file1 file2 [file3 ...]");
133 System.exit(1);
134 }
135 }
136
137
138