001: /*
002: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: * 1. Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * 3. The name of the author may not be used to endorse or promote products
014: * derived from this software without specific prior written permission.
015:
016: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: *
027: * $Id: schemagen_orig.java,v 1.9 2008/01/02 12:08:16 andy_seaborne Exp $
028: */
029:
030: package jena;
031:
032: import com.hp.hpl.jena.rdf.model.impl.Util;
033: import com.hp.hpl.jena.rdf.model.*;
034: import com.hp.hpl.jena.vocabulary.RDF;
035: import com.hp.hpl.jena.vocabulary.RDFS;
036:
037: import java.net.URL;
038: import java.io.FileOutputStream;
039: import java.io.FileReader;
040: import java.io.PrintStream;
041: import java.util.Date;
042: import java.util.Iterator;
043: import java.util.Set;
044: import java.util.HashSet;
045:
046: /** <p>
047: * The original version of a program to read in an RDF schema and generate a corresponding Jena
048: * constant schema class. Jena now provides a new version of schemagen, with extended functionality
049: * and a different set of command line options. The previous version has been renamed schemagen_orig,
050: * so that users with scripts that depend on the behaviour of the original schemagen can still have
051: * access to it.
052: * </p>
053: *
054: * <p>This program will read an RDF schema and generate the source for
055: * a Jena Vocabulary class for that schema.</p>
056: *
057: * <pre>java jena.schemagen_orig name schemaURIRef input output [lang]
058: *
059: * name is the vocabulary name e.g. RDF or RDFS
060: * schemaURIRef is the URI ref for the schema being processed
061: * input can be a file name or a URI
062: * output must be a file name or '-' for standard out
063: * lang is the language of the input and defaults to RDF/XML.
064: * </pre>
065: *
066: * <p>This program will make feeble attempt to convert names to legal java
067: * names, i.e. convert '-' and '.' characters to '_' characters. The
068: * user may have to correct the output if more exotic character sequences
069: * are used, or this fixup leads to name clashes.</p>
070: *
071: * @author bwm
072: * @version $Name: $ $Revision: 1.9 $ $Date: 2008/01/02 12:08:16 $
073: */
074: public class schemagen_orig extends java.lang.Object {
075:
076: /**
077: * @param args the command line arguments
078: */
079: public static void main(String args[]) {
080:
081: if (args.length < 4 || args.length > 5) {
082: usage();
083: System.exit(-1);
084: }
085:
086: String name = args[0];
087: String schemaURIRef = args[1];
088: String input = args[2];
089: String output = args[3];
090: String lang = "RDF/XML";
091: if (args.length > 4) {
092: lang = args[4];
093: }
094:
095: try {
096: Model schema = ModelFactory.createDefaultModel();
097:
098: read(schema, input, lang);
099:
100: PrintStream out = null;
101: if (output.equals("-")) {
102: out = System.out;
103: } else {
104: out = new PrintStream(new FileOutputStream(output));
105: }
106:
107: renderVocabularyClass(name, schemaURIRef, schema, out);
108:
109: } catch (Exception 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
119: .println(" 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
123: .println(" It may be simple, e.g. RDF, or it may"
124: + " be fully qualified");
125: System.err.println(" input can be URL's or filenames");
126: System.err.println(" lang can take values");
127: System.err.println(" RDF/XML");
128: System.err.println(" N-TRIPLE");
129: System.err.println(" lang defaults to RDF/XML");
130: System.err.println();
131: }
132:
133: protected static void read(Model model, String in, String lang)
134: throws java.io.FileNotFoundException {
135: try {
136: new URL(in);
137: model.read(in, lang);
138: } catch (java.net.MalformedURLException e) {
139: model.read(new FileReader(in), "", lang);
140: }
141: }
142:
143: protected static void renderVocabularyClass(String name,
144: String uriRef, Model schema, PrintStream out) {
145: Set classNames = listNames(uriRef, schema, RDFS.Class);
146: Set propertyNames = listNames(uriRef, schema, RDF.Property);
147: renderPreamble(name, uriRef, out);
148: renderDeclarations(classNames, "Resource", out);
149: renderDeclarations(propertyNames, "Property", out);
150: renderInitializer(classNames, propertyNames, out);
151: renderPostamble(out);
152: }
153:
154: protected static Set listNames(String uriRef, Model schema,
155: Resource type) {
156:
157: Set result = new HashSet();
158:
159: // extract all the resources of the given type in the schema
160: StmtIterator iter = schema.listStatements(null, RDF.type, type);
161: // for each one
162: while (iter.hasNext()) {
163: Resource r = iter.nextStatement().getSubject();
164: // ignore if bNode
165: if (!r.isAnon()) {
166: // get the URI and check if it matches the vocabulary
167: String s = r.getURI();
168: if (s.startsWith(uriRef)) {
169: // add the name to the set
170: result.add(s.substring(uriRef.length()));
171: }
172: }
173: }
174:
175: return result;
176: }
177:
178: protected static void renderDeclarations(Set names, String type,
179: PrintStream out) {
180: Iterator iter = names.iterator();
181: while (iter.hasNext()) {
182: String name = (String) iter.next();
183: String jname = makeJavaLegalId(name);
184: out.println(" static String n" + jname + " = \""
185: + name + "\";");
186: out
187: .println(" public static " + type + " " + jname
188: + ";");
189: }
190: }
191:
192: protected static void renderInitializer(Set classNames,
193: Set propertyNames, PrintStream out) {
194: out.println();
195: out.println(" static {");
196: out.println(" try {");
197: renderTypedInitializer(classNames, "Resource", out);
198: renderTypedInitializer(propertyNames, "Property", out);
199: out.println(" } catch (Exception e) {");
200: out
201: .println(" ErrorHelper.logInternalError(\"RDF\", 1, e);");
202: out.println(" }");
203: out.println(" }");
204: }
205:
206: protected static void renderTypedInitializer(Set names,
207: String type, PrintStream out) {
208: Iterator iter = names.iterator();
209: while (iter.hasNext()) {
210: String jname = makeJavaLegalId((String) iter.next());
211: out.println(" " + jname
212: + " = ResourceFactory.create" + type + "(uri + n"
213: + jname + ");");
214: }
215: }
216:
217: protected static void renderPreamble(String name, String uriRef,
218: PrintStream out) {
219:
220: // compute the package name
221: String packageName;
222: if (name.indexOf('.') == -1) {
223: packageName = "com.hp.hpl.jena.vocabulary";
224: } else {
225: packageName = name.substring(0, name.lastIndexOf('.'));
226: name = name.substring(name.lastIndexOf('.') + 1);
227: }
228:
229: out
230: .println("/* Vocabulary Class generated by Jena vocabulary generator");
231: out.println(" *");
232: out.println(" * On: " + (new Date()).toString());
233: out.println(" * Version $" + "Id" + "$"); // the line split up deliberately
234: out.println(" */");
235: out.println("package " + packageName + ";");
236: out.println();
237:
238: out
239: .println("import com.hp.hpl.jena.rdf.model.impl.ErrorHelper;");
240: out.println("import com.hp.hpl.jena.rdf.model.Model;");
241: out.println("import com.hp.hpl.jena.rdf.model.Resource;");
242: out
243: .println("import com.hp.hpl.jena.rdf.model.ResourceFactory;");
244: out.println("import com.hp.hpl.jena.rdf.model.Property;");
245: out.println("import com.hp.hpl.jena.rdf.model.RDFException;");
246: out.println();
247:
248: out.println("/** " + name + " vocabulary class for namespace "
249: + uriRef);
250: out.println(" */");
251: out.println("public class " + name + " {");
252: out.println();
253:
254: out.println(" protected static final String uri =\""
255: + uriRef + "\";");
256: out.println();
257:
258: out.println(" /** returns the URI for this schema");
259: out.println(" * @return the URI for this schema");
260: out.println(" */");
261: out.println(" public static String getURI() {");
262: out.println(" return uri;");
263: out.println(" }");
264: }
265:
266: protected static void renderPostamble(PrintStream out) {
267: out.println("}");
268: }
269:
270: protected static String makeJavaLegalId(String name) {
271: // this code is imperfect. It switch '-' and '.'
272: // chars to "_" which are legal in java.
273: // the user may have to fix up the output themselves
274: // if this doesn't work.
275:
276: name = Util.replace(name, "-", "_");
277: return Util.replace(name, ".", "_");
278: }
279:
280: }
|