1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.XMLReader ; 28 import org.xml.sax.InputSource ; 29 import com.sun.org.apache.xml.internal.serializer.ToXMLStream; 30 import java.io.OutputStream ; 31 import org.xml.sax.EntityResolver ; 32 import java.io.FileOutputStream ; 33 import java.io.File ; 34 import java.io.InputStream ; 35 import java.io.IOException ; 36 import org.xml.sax.SAXException ; 37 38 39 40 public class Flatten 41 { 42 public static void main(String [] args) throws Exception { 43 44 String input = null; 45 OutputStream output = System.out; 46 EntityResolver entityResolver = null; 47 48 if ( args.length < 1 || 5 < args.length){ 49 System.err.println("Flatten [-out output-file] [-dtd dtd] input "); 50 } 51 52 for (int i = 0; i < args.length; i++){ 53 if (args[i].equals("-out")){ 54 output = new FileOutputStream (new File (args[++i])); 55 break; 56 } 57 if (args[i].equals("-dtd")){ 58 entityResolver = new MyEntityResolver(args[++i]); 59 break; 60 } 61 input = args[i]; 62 } 63 64 65 66 67 VariableResolver vr = new VariableResolver(); 68 if (null != entityResolver){ 69 vr.setEntityResolver(entityResolver); 70 } 71 72 vr.setContentHandler(getContentHandler(output)); 74 vr.parse(new InputSource (input)); 75 } 76 77 private static XMLReader getXmlReader() throws Exception { 78 return XMLReaderFactory.newInstance(System.err); 79 } 80 81 private static ContentHandler getContentHandler(OutputStream o) throws Exception { 82 ToXMLStream xml = new ToXMLStream(); 83 xml.setOutputStream(o); 84 xml.setOmitXMLDeclaration(true); 85 return xml.asContentHandler(); 86 } 87 88 private static class MyEntityResolver implements EntityResolver 89 { 90 private String myDtd = ""; 91 92 MyEntityResolver(){} 93 94 MyEntityResolver(String dtd){ 95 myDtd = dtd; 96 } 97 98 public InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId) 99 throws IOException , SAXException { 100 if (null != publicId && publicId.equals("-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN")) { 101 final InputStream is = getClass().getResourceAsStream(myDtd); 102 return (null != is ) ? new InputSource (is) : null; 103 } else { 104 return null; 105 } 106 } 107 } 108 109 } 110 | Popular Tags |