View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.util;
21  
22  import java.io.File;
23  
24  import javax.xml.transform.Transformer;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.stream.StreamResult;
27  import javax.xml.transform.stream.StreamSource;
28  
29  
30  /**
31   * A simple runnable class that transforms a stylesheet and an XML document
32   * into a HTML document. It works by using default Java 1.4 XML transformer.
33   */
34  public class XMLTransformer
35  {
36  	/**
37  	 * Starts the transformer and transforms XML document. As an input, the
38  	 * main method takes two string arguments:
39  	 * 
40  	 * <ul>
41  	 * <li>
42  	 * <b>The first argument </b> specifies the full name (filename and path)
43  	 * of the XSL transformation stylesheet.
44  	 * </li>
45  	 * <li>
46  	 * <b>The second argument </b> specifies the full name (filename and path)
47  	 * of the XML file that is to be transformed into HTML.
48  	 * </li>
49  	 * </ul>
50  	 * 
51  	 * As a result, the program places the HTML file into the directory, where
52  	 * the source XML was found. The resulting filename is the same as the
53  	 * filename of the input XML file, except that the resulting file has HTML
54  	 * extension instead  of XML.
55  	 *
56  	 * @param args DOCUMENT ME!
57  	 */
58  	public static void main(String[] args)
59  	{
60  		try {
61  			TransformerFactory tf = TransformerFactory.newInstance();
62  
63  			// Transformer t = tf.newTransformer(new StreamSource(new File(args[0])));
64  			// String result = args[1].substring(0, args[1].lastIndexOf('.')) + ".html";
65  			// t.transform(new StreamSource(new File(args[1])), new StreamResult(new File(result)));
66  			Transformer t = tf.newTransformer();
67  			String result = args[0].substring(0, args[0].lastIndexOf('.'))
68  				+ ".html";
69  			t.transform(new StreamSource(new File(args[0])),
70  			    new StreamResult(new File(result)));
71  		} catch (Exception e) {
72  			e.printStackTrace();
73  		}
74  	}
75  }
76  
77  /* __oOo__ */