View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  //Checkstyle: UncommentedMain off
19  //Checkstyle: MagicNumber off
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   * XmlMerge as a Command-line tool.
36   *
37   * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/xml_merge/console/src/main/java/ch/elca/el4j/services/xmlmerge/tool/XmlMergeTool.java $
38   *
39   * @author Laurent Bovet (LBO)
40   * @author Alex Mathey (AMA)
41   */
42  public class XmlMergeTool {
43  	
44  	/**
45  	 * Hide default constructor.
46  	 */
47  	protected XmlMergeTool() { }
48  	
49  	/**
50  	 * Starts the XmlMerge Command-line tool.
51  	 * @param args The names of the files to merge
52  	 * @throws Exception If an error occurs during the execution of the tool
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 			// Should not happen...
122 			ioex.printStackTrace(System.err);
123 			System.exit(1);
124 		}
125 	}
126 
127 	/**
128 	 * Desribes the usage of this tool.
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 //Checkstyle: UncommentedMain on
138 //Checkstyle: MagicNumber on