KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > UseStylesheetParam


1 /*
2  * Copyright 1999-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  * $Id: UseStylesheetParam.java,v 1.9 2004/02/17 19:11:45 minchau Exp $
18  */

19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22
23 import javax.xml.transform.Transformer JavaDoc;
24 import javax.xml.transform.TransformerConfigurationException JavaDoc;
25 import javax.xml.transform.TransformerException JavaDoc;
26 import javax.xml.transform.TransformerFactory JavaDoc;
27 import javax.xml.transform.stream.StreamResult JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
29
30 import org.xml.sax.SAXException JavaDoc;
31
32   /**
33    * Use command-line input as a stylesheet parameter.
34    */

35
36 public class UseStylesheetParam
37 {
38   public static void main(String JavaDoc[] args)
39     throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc,
40          SAXException JavaDoc, IOException JavaDoc
41     {
42     if(args.length != 1)
43     {
44       System.err.println("Please pass one string to this program");
45       return;
46     }
47     // Get the parameter value from the command line.
48
String JavaDoc paramValue = args[0];
49     
50     TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
51     Transformer JavaDoc transformer = tFactory.newTransformer(new StreamSource JavaDoc("foo.xsl"));
52
53     // Set the parameter. I can't get non-null namespaces to work!!
54
transformer.setParameter("param1", /* parameter name */
55                                          paramValue /* parameter value */ );
56     
57     transformer.transform(new StreamSource JavaDoc("foo.xml"), new StreamResult JavaDoc(new OutputStreamWriter JavaDoc(System.out)));
58   }
59 }
60
Popular Tags