1 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 ; 38 import java.io.FileOutputStream ; 39 import java.io.FileReader ; 40 import java.io.PrintStream ; 41 import java.util.Date ; 42 import java.util.Iterator ; 43 import java.util.Set ; 44 import java.util.HashSet ; 45 46 74 public class schemagen_orig extends java.lang.Object { 75 76 79 public static void main(String args[]) { 80 81 if (args.length < 4 || args.length > 5) { 82 usage(); 83 System.exit(-1); 84 } 85 86 String name = args[0]; 87 String schemaURIRef = args[1]; 88 String input = args[2]; 89 String output = args[3]; 90 String 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 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.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 in, String lang) 133 throws java.io.FileNotFoundException { 134 try { 135 new URL (in); 136 model.read(in, lang); 137 } catch (java.net.MalformedURLException e) { 138 model.read(new FileReader (in), "", lang); 139 } 140 } 141 142 protected static void renderVocabularyClass( 143 String name, 144 String uriRef, 145 Model schema, 146 PrintStream out) 147 { 148 Set classNames = listNames(uriRef, schema, RDFS.Class); 149 Set 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 listNames(String uriRef, Model schema, Resource type) 158 { 159 160 Set result = new HashSet (); 161 162 StmtIterator iter = 164 schema.listStatements( null, RDF.type, type ); 165 while (iter.hasNext()) { 167 Resource r = iter.nextStatement().getSubject(); 168 if (!r.isAnon()) { 170 String s = r.getURI(); 172 if (s.startsWith(uriRef)) { 173 result.add(s.substring(uriRef.length())); 175 } 176 } 177 } 178 179 return result; 180 } 181 182 protected static void renderDeclarations( 183 Set names, 184 String type, 185 PrintStream out) 186 { 187 Iterator iter = names.iterator(); 188 while (iter.hasNext()) { 189 String name = (String ) iter.next(); 190 String 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 classNames, 199 Set propertyNames, 200 PrintStream 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 names, 215 String type, 216 PrintStream out) { 217 Iterator iter = names.iterator(); 218 while (iter.hasNext()) { 219 String jname = makeJavaLegalId((String ) 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 name, 233 String uriRef, 234 PrintStream out) { 235 236 String 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 ()).toString()); 249 out.println( 250 " * Version $" + "Id" + "$"); 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 out) { 282 out.println("}"); 283 } 284 285 protected static String makeJavaLegalId(String name) { 286 291 name = Util.replace(name, "-", "_"); 292 return Util.replace(name, ".", "_"); 293 } 294 295 } 296 | Popular Tags |