1 23 24 package com.sun.enterprise.deployment.node; 25 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.OutputStream ; 29 import java.io.IOException ; 30 31 import java.util.logging.Level ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.DOMImplementation ; 37 import org.w3c.dom.Node ; 38 39 import javax.xml.transform.OutputKeys ; 40 import javax.xml.transform.Result ; 41 import javax.xml.transform.Source ; 42 import javax.xml.transform.Transformer ; 43 import javax.xml.transform.TransformerFactory ; 44 import javax.xml.transform.dom.DOMSource ; 45 import javax.xml.transform.stream.StreamResult ; 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 60 public class J2EEDocumentBuilder { 61 62 63 public J2EEDocumentBuilder() { 64 } 65 66 72 public static Document newDocument() { 73 try { 74 DocumentBuilderFactory factory = 75 DocumentBuilderFactory.newInstance(); 76 77 DocumentBuilder builder = factory.newDocumentBuilder(); 78 79 DOMImplementation domImplementation = 80 builder.getDOMImplementation(); 81 82 Document document = builder.newDocument(); 83 return document; 84 } catch (Exception e) { 85 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError", 86 new Object [] {"JAXP configuration error"}); 87 e.printStackTrace(); 88 } 89 return null; 90 } 91 92 96 public static Document getDocument(Descriptor descriptor, XMLNode node) { 97 try { 98 Node domNode = node.writeDescriptor(newDocument(), descriptor); 99 if (domNode instanceof Document ) 100 return (Document ) domNode; 101 else 102 return domNode.getOwnerDocument(); 103 } catch (Exception e) { 104 e.printStackTrace(); 105 } 106 return null; 107 } 108 109 public static void write (Descriptor descriptor, final RootXMLNode node, final File resultFile) throws Exception { 110 if (node==null) { 111 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 112 new Object [] {descriptor, null}); 113 return; 114 } 115 if (resultFile.getParent() != null) 116 (new File (resultFile.getParent())).mkdirs(); 117 FileOutputStream out = new FileOutputStream (resultFile); 118 write(descriptor, node, out); 119 out.close(); 120 } 121 122 public static void write (Descriptor descriptor, final RootXMLNode node, final OutputStream os) throws Exception { 123 Result output = new StreamResult (os); 124 write(descriptor, node, output); 125 } 126 127 public static void write (Descriptor descriptor, final RootXMLNode node, final Result output) 128 throws Exception { 129 if (node==null) { 130 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 131 new Object [] {descriptor, null}); 132 return; 133 } 134 try { 135 Document document = getDocument(descriptor, node); 136 Source source = new DOMSource (document); 137 TransformerFactory factory = TransformerFactory.newInstance(); 138 Transformer transformer = factory.newTransformer(); 139 setTransformerProperties(node, transformer); 140 transformer.transform(source, output); 141 } catch(Exception e) { 142 e.printStackTrace(); 143 throw e; 144 } 145 } 146 147 148 private static void setTransformerProperties (RootXMLNode node, Transformer 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 descriptorToString(Descriptor descriptor, final DeploymentDescriptorFile ddFile) 164 throws Exception { 165 java.io.StringWriter sw = new java.io.StringWriter (); 166 StreamResult sr = new StreamResult (sw); 167 if (descriptor != null) { 168 write(descriptor, ddFile.getRootXMLNode(descriptor), sr); 169 } 170 return sw.toString(); 171 } 172 } 173 | Popular Tags |