1 23 24 package com.sun.enterprise.util; 25 26 import javax.xml.parsers.DocumentBuilder ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 29 import org.w3c.dom.Document ; 30 31 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerFactory ; 34 import javax.xml.transform.dom.DOMSource ; 35 import javax.xml.transform.stream.StreamSource ; 36 import javax.xml.transform.stream.StreamResult ; 37 38 import java.io.File ; 39 import java.io.FileInputStream ; 40 41 import org.xml.sax.helpers.DefaultHandler ; 42 import org.xml.sax.InputSource ; 43 import org.xml.sax.SAXException ; 44 45 import javax.xml.parsers.ParserConfigurationException ; 46 import org.xml.sax.SAXException ; 47 import javax.xml.transform.TransformerConfigurationException ; 48 import javax.xml.transform.TransformerException ; 49 import java.io.IOException ; 50 import org.xml.sax.SAXParseException ; 51 52 60 class NOOPHandler extends DefaultHandler { 61 62 static String _dtdFileName; 63 64 NOOPHandler(String dtdFileName) { 65 super(); 66 _dtdFileName = dtdFileName; 67 } 68 69 public InputSource resolveEntity(String publicId, 70 String systemId) throws SAXException 71 { 72 InputSource is = null; 73 try { 74 is = new InputSource (new FileInputStream (_dtdFileName)); 75 } catch(Exception e) { 76 throw new SAXException ("cannot resolve dtd", e); 77 } 78 return is; 79 } 80 81 } 82 83 93 public class XMLFileTransformer 94 { 95 private static void doTransform(String domainXMLdtd, 96 String domainXMLinput, 97 String domainXMLtransform, String domainXMLoutput) 98 throws ParserConfigurationException , SAXException , 99 TransformerConfigurationException , TransformerException , 100 IOException 101 { 102 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 103 107 File stylesheet = new File (domainXMLtransform); 108 File inputfile = new File (domainXMLinput); 109 File outputfile = new File (domainXMLoutput); 110 111 DocumentBuilder builder = factory.newDocumentBuilder(); 112 builder.setEntityResolver(new NOOPHandler(domainXMLdtd)); 113 Document document = builder.parse(inputfile); 114 115 TransformerFactory tFactory = TransformerFactory.newInstance(); 117 StreamSource stylesource = new StreamSource (stylesheet); 118 Transformer transformer = tFactory.newTransformer(stylesource); 119 120 DOMSource source = new DOMSource (document); 121 StreamResult result = new StreamResult (outputfile); 122 transformer.transform(source, result); 123 } 124 125 public static void main (String 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 spe){ 134 System.err.println(spe.getMessage()); 135 } catch (Exception ex) { 136 ex.printStackTrace(); 137 } 138 } 139 System.exit (1); 140 } 141 } 142 143 | Popular Tags |