KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jena > schemagen_orig


1 /*
2  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $Id: schemagen_orig.java,v 1.6 2005/02/21 11:49:15 andy_seaborne Exp $
28  */

29
30 package jena;
31
32 import com.hp.hpl.jena.rdf.model.impl.Util;
33 import com.hp.hpl.jena.rdf.model.*;
34 import com.hp.hpl.jena.vocabulary.RDF;
35 import com.hp.hpl.jena.vocabulary.RDFS;
36
37 import java.net.URL JavaDoc;
38 import java.io.FileOutputStream JavaDoc;
39 import java.io.FileReader JavaDoc;
40 import java.io.PrintStream JavaDoc;
41 import java.util.Date JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Set JavaDoc;
44 import java.util.HashSet JavaDoc;
45
46 /** <p>
47  * The original version of a program to read in an RDF schema and generate a corresponding Jena
48  * constant schema class. Jena now provides a new version of schemagen, with extended functionality
49  * and a different set of command line options. The previous version has been renamed schemagen_orig,
50  * so that users with scripts that depend on the behaviour of the original schemagen can still have
51  * access to it.
52  * </p>
53  *
54  * <p>This program will read an RDF schema and generate the source for
55  * a Jena Vocabulary class for that schema.</p>
56  *
57  * <pre>java jena.schemagen_orig name schemaURIRef input output [lang]
58  *
59  * name is the vocabulary name e.g. RDF or RDFS
60  * schemaURIRef is the URI ref for the schema being processed
61  * input can be a file name or a URI
62  * output must be a file name or '-' for standard out
63  * lang is the language of the input and defaults to RDF/XML.
64  * </pre>
65  *
66  * <p>This program will make feeble attempt to convert names to legal java
67  * names, i.e. convert '-' and '.' characters to '_' characters. The
68  * user may have to correct the output if more exotic character sequences
69  * are used, or this fixup leads to name clashes.</p>
70  *
71  * @author bwm
72  * @version $Name: $ $Revision: 1.6 $ $Date: 2005/02/21 11:49:15 $
73  */

74 public class schemagen_orig extends java.lang.Object JavaDoc {
75
76     /**
77     * @param args the command line arguments
78     */

79     public static void main(String JavaDoc args[]) {
80
81         if (args.length < 4 || args.length > 5) {
82             usage();
83             System.exit(-1);
84         }
85
86         String JavaDoc name = args[0];
87         String JavaDoc schemaURIRef = args[1];
88         String JavaDoc input = args[2];
89         String JavaDoc output = args[3];
90         String JavaDoc lang = "RDF/XML";
91         if (args.length > 4) {
92             lang = args[4];
93         }
94
95         try {
96             Model schema = ModelFactory.createDefaultModel();
97
98             read(schema, input, lang);
99
100             PrintStream JavaDoc out = null;
101             if (output.equals("-")) {
102                 out = System.out;
103             } else {
104                 out = new PrintStream JavaDoc(new FileOutputStream JavaDoc(output));
105             }
106
107             renderVocabularyClass(name, schemaURIRef, schema, out);
108
109         } catch (Exception JavaDoc e) {
110             System.err.println("Unhandled exception:");
111             System.err.println(" " + e.toString());
112             System.exit(-1);
113         }
114     }
115
116     protected static void usage() {
117         System.err.println("usage:");
118         System.err.println(
119             " java jena.schemagen_orig name schemaURIRef input output [lang]");
120         System.err.println();
121         System.err.println(" name is the name of the vocabulary");
122         System.err.println(" It may be simple, e.g. RDF, or it may" +
123                                     " be fully qualified");
124         System.err.println(" input can be URL's or filenames");
125         System.err.println(" lang can take values");
126         System.err.println(" RDF/XML");
127         System.err.println(" N-TRIPLE");
128         System.err.println(" lang defaults to RDF/XML");
129         System.err.println();
130     }
131
132     protected static void read(Model model, String JavaDoc in, String JavaDoc lang)
133         throws java.io.FileNotFoundException JavaDoc {
134         try {
135             new URL JavaDoc(in);
136             model.read(in, lang);
137         } catch (java.net.MalformedURLException JavaDoc e) {
138             model.read(new FileReader JavaDoc(in), "", lang);
139         }
140     }
141
142     protected static void renderVocabularyClass(
143         String JavaDoc name,
144         String JavaDoc uriRef,
145         Model schema,
146         PrintStream JavaDoc out)
147         {
148         Set JavaDoc classNames = listNames(uriRef, schema, RDFS.Class);
149         Set JavaDoc propertyNames = listNames(uriRef, schema, RDF.Property);
150         renderPreamble(name, uriRef, out);
151         renderDeclarations(classNames, "Resource", out);
152         renderDeclarations(propertyNames, "Property", out);
153         renderInitializer(classNames, propertyNames, out);
154         renderPostamble(out);
155     }
156
157     protected static Set JavaDoc listNames(String JavaDoc uriRef, Model schema, Resource type)
158          {
159
160         Set JavaDoc result = new HashSet JavaDoc();
161
162         // extract all the resources of the given type in the schema
163
StmtIterator iter =
164             schema.listStatements( null, RDF.type, type );
165         // for each one
166
while (iter.hasNext()) {
167             Resource r = iter.nextStatement().getSubject();
168             // ignore if bNode
169
if (!r.isAnon()) {
170                 // get the URI and check if it matches the vocabulary
171
String JavaDoc s = r.getURI();
172                 if (s.startsWith(uriRef)) {
173                     // add the name to the set
174
result.add(s.substring(uriRef.length()));
175                 }
176             }
177         }
178
179         return result;
180     }
181
182     protected static void renderDeclarations(
183         Set JavaDoc names,
184         String JavaDoc type,
185         PrintStream JavaDoc out)
186          {
187         Iterator JavaDoc iter = names.iterator();
188         while (iter.hasNext()) {
189             String JavaDoc name = (String JavaDoc) iter.next();
190             String JavaDoc jname = makeJavaLegalId(name);
191             out.println(
192                 " static String n" + jname + " = \"" + name + "\";");
193             out.println(" public static " + type + " " + jname + ";");
194         }
195     }
196
197     protected static void renderInitializer(
198         Set JavaDoc classNames,
199         Set JavaDoc propertyNames,
200         PrintStream JavaDoc out)
201          {
202         out.println();
203         out.println(" static {");
204         out.println(" try {");
205         renderTypedInitializer(classNames, "Resource", out);
206         renderTypedInitializer(propertyNames, "Property", out);
207         out.println(" } catch (Exception e) {");
208         out.println(" ErrorHelper.logInternalError(\"RDF\", 1, e);");
209         out.println(" }");
210         out.println(" }");
211     }
212
213     protected static void renderTypedInitializer(
214         Set JavaDoc names,
215         String JavaDoc type,
216         PrintStream JavaDoc out) {
217         Iterator JavaDoc iter = names.iterator();
218         while (iter.hasNext()) {
219             String JavaDoc jname = makeJavaLegalId((String JavaDoc) iter.next());
220             out.println(
221                 " "
222                     + jname
223                     + " = ResourceFactory.create"
224                     + type
225                     + "(uri + n"
226                     + jname
227                     + ");");
228         }
229     }
230
231     protected static void renderPreamble(
232         String JavaDoc name,
233         String JavaDoc uriRef,
234         PrintStream JavaDoc out) {
235         
236         // compute the package name
237
String JavaDoc packageName;
238         if (name.indexOf('.') == -1) {
239             packageName = "com.hp.hpl.jena.vocabulary";
240         } else {
241             packageName = name.substring(0, name.lastIndexOf('.'));
242             name = name.substring(name.lastIndexOf('.') + 1);
243         }
244         
245         out.println(
246             "/* Vocabulary Class generated by Jena vocabulary generator");
247         out.println(" *");
248         out.println(" * On: " + (new Date JavaDoc()).toString());
249         out.println(
250             " * Version $" + "Id" + "$"); // the line split up deliberately
251
out.println(" */");
252         out.println("package " + packageName + ";");
253         out.println();
254
255         out.println("import com.hp.hpl.jena.rdf.model.impl.ErrorHelper;");
256         out.println("import com.hp.hpl.jena.rdf.model.Model;");
257         out.println("import com.hp.hpl.jena.rdf.model.Resource;");
258         out.println("import com.hp.hpl.jena.rdf.model.ResourceFactory;");
259         out.println("import com.hp.hpl.jena.rdf.model.Property;");
260         out.println("import com.hp.hpl.jena.rdf.model.RDFException;");
261         out.println();
262
263         out.println(
264             "/** " + name + " vocabulary class for namespace " + uriRef);
265         out.println(" */");
266         out.println("public class " + name + " {");
267         out.println();
268
269         out.println(
270             " protected static final String uri =\"" + uriRef + "\";");
271         out.println();
272
273         out.println(" /** returns the URI for this schema");
274         out.println(" * @return the URI for this schema");
275         out.println(" */");
276         out.println(" public static String getURI() {");
277         out.println(" return uri;");
278         out.println(" }");
279     }
280
281     protected static void renderPostamble(PrintStream JavaDoc out) {
282         out.println("}");
283     }
284
285     protected static String JavaDoc makeJavaLegalId(String JavaDoc name) {
286         // this code is imperfect. It switch '-' and '.'
287
// chars to "_" which are legal in java.
288
// the user may have to fix up the output themselves
289
// if this doesn't work.
290

291         name = Util.replace(name, "-", "_");
292         return Util.replace(name, ".", "_");
293     }
294
295 }
296
Popular Tags