KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > Flatten


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.config.serverbeans.validation;
25
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.XMLReader JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import com.sun.org.apache.xml.internal.serializer.ToXMLStream;
30 import java.io.OutputStream JavaDoc;
31 import org.xml.sax.EntityResolver JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38
39
40 public class Flatten
41 {
42     public static void main(String JavaDoc [] args) throws Exception JavaDoc {
43
44         String JavaDoc input = null;
45         OutputStream JavaDoc output = System.out;
46         EntityResolver JavaDoc 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 JavaDoc(new File JavaDoc(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.setParent(getXmlReader());
73
vr.setContentHandler(getContentHandler(output));
74         vr.parse(new InputSource JavaDoc(input));
75     }
76
77     private static XMLReader JavaDoc getXmlReader() throws Exception JavaDoc {
78         return XMLReaderFactory.newInstance(System.err);
79     }
80
81     private static ContentHandler JavaDoc getContentHandler(OutputStream JavaDoc o) throws Exception JavaDoc {
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 JavaDoc
89     {
90         private String JavaDoc myDtd = "";
91
92         MyEntityResolver(){}
93         
94         MyEntityResolver(String JavaDoc dtd){
95             myDtd = dtd;
96         }
97         
98         public InputSource JavaDoc resolveEntity(java.lang.String JavaDoc publicId, java.lang.String JavaDoc systemId)
99             throws IOException JavaDoc, SAXException JavaDoc{
100             if (null != publicId && publicId.equals("-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN")) {
101                 final InputStream JavaDoc is = getClass().getResourceAsStream(myDtd);
102                 return (null != is ) ? new InputSource JavaDoc(is) : null;
103             } else {
104                 return null;
105             }
106         }
107     }
108
109 }
110
Popular Tags