KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > J2EEDocumentBuilder


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.deployment.node;
25
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import java.util.logging.Level JavaDoc;
32
33 import javax.xml.parsers.DocumentBuilder JavaDoc;
34 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.DOMImplementation JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38
39 import javax.xml.transform.OutputKeys JavaDoc;
40 import javax.xml.transform.Result JavaDoc;
41 import javax.xml.transform.Source JavaDoc;
42 import javax.xml.transform.Transformer JavaDoc;
43 import javax.xml.transform.TransformerFactory JavaDoc;
44 import javax.xml.transform.dom.DOMSource JavaDoc;
45 import javax.xml.transform.stream.StreamResult JavaDoc;
46
47 import com.sun.enterprise.deployment.Descriptor;
48 import com.sun.enterprise.deployment.Application;
49 import com.sun.enterprise.deployment.io.DeploymentDescriptorFile;
50
51 import com.sun.enterprise.deployment.util.DOLUtils;
52
53 /**
54  * This class is responsible for producing DOM document instances from
55  * the descriptor classes
56  *
57  * @author Jerome Dochez
58  * @version
59  */

60 public class J2EEDocumentBuilder {
61
62     /** Creates new J2EEDocumentBuilder */
63     public J2EEDocumentBuilder() {
64     }
65
66     /**
67      * Creates and Return a new DOM document based on the current
68      * configuration
69      *
70      * @return the new DOM Document object
71      */

72     public static Document JavaDoc newDocument() {
73         try {
74             DocumentBuilderFactory JavaDoc factory =
75                 DocumentBuilderFactory.newInstance();
76             
77             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
78             
79             DOMImplementation JavaDoc domImplementation =
80                 builder.getDOMImplementation();
81             
82             Document JavaDoc document = builder.newDocument();
83             return document;
84         } catch (Exception JavaDoc e) {
85             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError",
86                     new Object JavaDoc[] {"JAXP configuration error"});
87             e.printStackTrace();
88         }
89         return null;
90     }
91     
92     /**
93      * Return a document containing a result node based
94      * on the given result descriptor.
95      */

96     public static Document JavaDoc getDocument(Descriptor descriptor, XMLNode node) {
97         try {
98             Node JavaDoc domNode = node.writeDescriptor(newDocument(), descriptor);
99             if (domNode instanceof Document JavaDoc)
100                 return (Document JavaDoc) domNode;
101             else
102                 return domNode.getOwnerDocument();
103         } catch (Exception JavaDoc e) {
104             e.printStackTrace();
105         }
106         return null;
107     }
108         
109     public static void write (Descriptor descriptor, final RootXMLNode node, final File JavaDoc resultFile) throws Exception JavaDoc {
110         if (node==null) {
111             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
112                 new Object JavaDoc[] {descriptor, null});
113             return;
114         }
115         if (resultFile.getParent() != null)
116             (new File JavaDoc(resultFile.getParent())).mkdirs();
117         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(resultFile);
118         write(descriptor, node, out);
119         out.close();
120     }
121     
122     public static void write (Descriptor descriptor, final RootXMLNode node, final OutputStream JavaDoc os) throws Exception JavaDoc {
123         Result JavaDoc output = new StreamResult JavaDoc(os);
124         write(descriptor, node, output);
125     }
126
127     public static void write (Descriptor descriptor, final RootXMLNode node, final Result JavaDoc output)
128                                 throws Exception JavaDoc {
129         if (node==null) {
130             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
131                 new Object JavaDoc[] {descriptor, null});
132             return;
133         }
134         try {
135             Document JavaDoc document = getDocument(descriptor, node);
136             Source JavaDoc source = new DOMSource JavaDoc(document);
137             TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
138             Transformer JavaDoc transformer = factory.newTransformer();
139             setTransformerProperties(node, transformer);
140             transformer.transform(source, output);
141         } catch(Exception JavaDoc e) {
142             e.printStackTrace();
143             throw e;
144         }
145     }
146     
147
148     private static void setTransformerProperties (RootXMLNode node, Transformer JavaDoc transformer) {
149         if (node.getDocType()!=null) {
150             transformer.setOutputProperty(
151                 OutputKeys.DOCTYPE_PUBLIC, node.getDocType());
152             if (node.getSystemID()!=null) {
153                 transformer.setOutputProperty(
154                     OutputKeys.DOCTYPE_SYSTEM, node.getSystemID());
155             }
156         }
157         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
158         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
159         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
160         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
161     }
162     
163     public static String JavaDoc descriptorToString(Descriptor descriptor, final DeploymentDescriptorFile ddFile)
164                                 throws Exception JavaDoc {
165         java.io.StringWriter JavaDoc sw = new java.io.StringWriter JavaDoc();
166         StreamResult JavaDoc sr = new StreamResult JavaDoc(sw);
167     if (descriptor != null) {
168         write(descriptor, ddFile.getRootXMLNode(descriptor), sr);
169     }
170         return sw.toString();
171     }
172 }
173
Popular Tags