KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MakeCatalog


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 import org.xml.sax.InputSource JavaDoc;
25 import java.util.Properties JavaDoc;
26 import org.xml.sax.helpers.DefaultHandler JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import java.io.IOException JavaDoc;
30 import javax.xml.parsers.SAXParserFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import org.xml.sax.SAXNotRecognizedException JavaDoc;
33 import javax.xml.parsers.SAXParser JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.FileReader JavaDoc;
36 import java.io.FileOutputStream JavaDoc;
37
38 class MakeCatalog extends DefaultHandler JavaDoc
39 {
40     final String JavaDoc prefix;
41
42     MakeCatalog(){
43         prefix = "";
44     }
45     
46     MakeCatalog(final String JavaDoc prefix){
47         this.prefix = (prefix != null ? prefix +"." : "");
48     }
49     
50
51     public static void main(String JavaDoc [] args) throws Exception JavaDoc {
52         if (args.length < 2 || 3 < args.length ){
53             useage();
54             System.exit(1);
55         }
56
57         final String JavaDoc prefix = (args.length == 2 ? null : args[0]);
58         final String JavaDoc in = (args.length == 2 ? args[0] : args[1]);
59         final String JavaDoc out = (args.length == 2 ? args[1] : args[2]);
60         
61         final MakeCatalog mc = new MakeCatalog(prefix);
62         final InputSource JavaDoc is = new InputSource JavaDoc(new FileReader JavaDoc(new File JavaDoc(in)));
63         is.setSystemId(in);
64         mc.makeCatalog(is);
65         mc.getProperties().save(new FileOutputStream JavaDoc(new File JavaDoc(out)), null);
66         System.exit(0);
67     }
68
69     private static void useage(){
70         System.err.println("Useage: MakeCatalog [prefix] in out");
71     }
72     
73         
74     private static final String JavaDoc NAMESPACES="http://xml.org/sax/features/namespaces";
75     private static final String JavaDoc PREFIXES="http://xml.org/sax/features/namespace-prefixes";
76     private static final File JavaDoc SCHEMA=new File JavaDoc("./message-catalog.rng");
77         
78     void makeCatalog(InputSource JavaDoc is) throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
79 // final SAXParserFactory spf = SAXParserFactory.newInstance();
80
final SAXParserFactory JavaDoc spf = new com.sun.msv.verifier.jaxp.SAXParserFactoryImpl();
81
82         spf.setNamespaceAware(true);
83         spf.setValidating(false);
84         spf.setFeature(NAMESPACES, true);
85         spf.setFeature(PREFIXES, false);
86
87         final SAXParser JavaDoc p = spf.newSAXParser();
88         p.setProperty("http://www.sun.com/xml/msv/schema", SCHEMA);
89 // spf.newSAXParser().parse(is, this);
90
p.parse(is, this);
91     }
92     
93     Properties JavaDoc getProperties(){
94         return properties;
95     }
96
97     Properties JavaDoc properties;
98
99     public void startDocument() throws SAXException JavaDoc{
100         properties = new Properties JavaDoc();
101     }
102
103     public void startElement(final String JavaDoc uri,
104                          final String JavaDoc localName,
105                          final String JavaDoc qName,
106                          final Attributes JavaDoc attributes)
107         throws SAXException JavaDoc{
108         if (null == localName){
109             throw new SAXException JavaDoc("localName is null");
110         }
111         
112         if (localName.equals("messages")){ // do nothing
113
} else if (localName.equals("message")) {
114             startMessage(attributes);
115         } else {
116             throw new SAXException JavaDoc("Unknown element: "+uri+":"+localName);
117         }
118     }
119
120     public void characters(char[] ch,
121                        int start,
122                        int length)
123         throws SAXException JavaDoc{
124         if (null != sb) {
125             sb.append(ch, start, length);
126         }
127     }
128     
129     public void endElement(final String JavaDoc uri,
130                            final String JavaDoc localName,
131                            final String JavaDoc qName) throws SAXException JavaDoc {
132         if (localName.equals("messages")){ // do nothing
133
} else if (localName.equals("message")) {
134             endMessage();
135         } else {
136             throw new SAXException JavaDoc("Unknown element: "+uri+":"+localName);
137         }
138     }
139
140
141     private String JavaDoc key;
142     private StringBuffer JavaDoc sb;
143     
144     private void startMessage(final Attributes JavaDoc attributes) throws SAXException JavaDoc {
145         key = (prefix != null ? prefix : "") + attributes.getValue("", "id");
146         if (null == key){
147             throw new SAXException JavaDoc("message element had no id attribute");
148         }
149         sb = new StringBuffer JavaDoc();
150     }
151
152     private void endMessage() throws SAXException JavaDoc {
153         if (null != sb){
154             properties.setProperty(key, sb.toString().trim());
155         }
156         sb = null;
157     }
158 }
159
Popular Tags