KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > XMLFileTransformer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.util;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28  
29 import org.w3c.dom.Document JavaDoc;
30
31 // For write operation
32
import javax.xml.transform.Transformer JavaDoc;
33 import javax.xml.transform.TransformerFactory JavaDoc;
34 import javax.xml.transform.dom.DOMSource JavaDoc;
35 import javax.xml.transform.stream.StreamSource JavaDoc;
36 import javax.xml.transform.stream.StreamResult JavaDoc;
37
38 import java.io.File JavaDoc;
39 import java.io.FileInputStream JavaDoc;
40
41 import org.xml.sax.helpers.DefaultHandler JavaDoc;
42 import org.xml.sax.InputSource JavaDoc;
43 import org.xml.sax.SAXException JavaDoc;
44
45 import javax.xml.parsers.ParserConfigurationException JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47 import javax.xml.transform.TransformerConfigurationException JavaDoc;
48 import javax.xml.transform.TransformerException JavaDoc;
49 import java.io.IOException JavaDoc;
50 import org.xml.sax.SAXParseException JavaDoc;
51
52 /**
53  * The internal NOOPHandler is needed to act as an entity resolver when
54  * parsing the domain.xml.template file. The problem is that the
55  * DOCTYPE in this template has a token of the form ${com.sun.aas.installRoot}
56  * which cannot be resolved using the standard entity resolver. Instead
57  * we explicitly pass the DTD file name to parse against in the
58  * constructor of this class.
59  **/

60 class NOOPHandler extends DefaultHandler JavaDoc {
61    
62     static String JavaDoc _dtdFileName;
63     
64     NOOPHandler(String JavaDoc dtdFileName) {
65         super();
66         _dtdFileName = dtdFileName;
67     }
68
69     public InputSource JavaDoc resolveEntity(String JavaDoc publicId,
70          String JavaDoc systemId) throws SAXException JavaDoc
71     {
72         InputSource JavaDoc is = null;
73         try {
74             is = new InputSource JavaDoc(new FileInputStream JavaDoc(_dtdFileName));
75         } catch(Exception JavaDoc e) {
76             throw new SAXException JavaDoc("cannot resolve dtd", e);
77         }
78         return is;
79     }
80
81 }
82
83 /**
84  * The DomainXMLTransformer takes as input the DTD file name,
85  * XML input file name to transform confroming to this DTD, the
86  * name of the XSL transform file to apply, and the output file
87  * name.
88  *
89  * This class is used to transform the default-domain.xml.template
90  * file into an domain xml template that can be used in the RI
91  * or SE versions of the product.
92  **/

93 public class XMLFileTransformer
94 {
95     private static void doTransform(String JavaDoc domainXMLdtd,
96         String JavaDoc domainXMLinput,
97         String JavaDoc domainXMLtransform, String JavaDoc domainXMLoutput)
98         throws ParserConfigurationException JavaDoc, SAXException JavaDoc,
99             TransformerConfigurationException JavaDoc, TransformerException JavaDoc,
100             IOException JavaDoc
101     {
102         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
103         //factory.setNamespaceAware(false);
104
//factory.setValidating(false);
105
//factory.setExpandEntityReferences(false);
106

107         File JavaDoc stylesheet = new File JavaDoc(domainXMLtransform);
108         File JavaDoc inputfile = new File JavaDoc(domainXMLinput);
109         File JavaDoc outputfile = new File JavaDoc(domainXMLoutput);
110  
111         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
112         builder.setEntityResolver(new NOOPHandler(domainXMLdtd));
113         Document JavaDoc document = builder.parse(inputfile);
114  
115         // Use a Transformer for output
116
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
117         StreamSource JavaDoc stylesource = new StreamSource JavaDoc(stylesheet);
118         Transformer JavaDoc transformer = tFactory.newTransformer(stylesource);
119  
120         DOMSource JavaDoc source = new DOMSource JavaDoc(document);
121         StreamResult JavaDoc result = new StreamResult JavaDoc(outputfile);
122         transformer.transform(source, result);
123     }
124     
125     public static void main (String JavaDoc argv [])
126     {
127         if (argv.length != 4) {
128             System.err.println ("Usage: java XMLFileTransformer dtd inputfile stylesheet outputfile");
129         } else {
130             try {
131                 doTransform(argv[0], argv[1], argv[2], argv[3]);
132                 System.exit(0);
133             } catch (SAXParseException JavaDoc spe){
134                 System.err.println(spe.getMessage());
135             } catch (Exception JavaDoc ex) {
136                 ex.printStackTrace();
137             }
138         }
139         System.exit (1);
140     }
141 }
142
143
Popular Tags