1 6 7 package com.hp.hpl.jena.rdf.model.impl; 8 9 import com.hp.hpl.jena.rdf.model.*; 10 import com.hp.hpl.jena.shared.*; 11 import com.hp.hpl.jena.vocabulary.*; 12 13 26 public class ModelSpecFactory 27 { 28 31 protected static ModelSpecCreatorRegistry defaultRegistry = ModelSpecCreatorRegistry.instance; 32 33 38 protected static ModelSpec create( ModelSpecCreatorRegistry registry, Model m, Resource root ) 39 { Resource type = findSpecificType( root, JenaModelSpec.ModelSpec ); 40 ModelSpecCreator sc = registry.getCreator( type ); 41 if (sc == null) throw new BadDescriptionException( "no model-spec creator found for " + type, m ); 42 return sc.create( root, m ); } 43 44 48 public static ModelSpec createSpec( ModelSpecCreatorRegistry registry, Model m ) 49 { Model full = withSchema( m ); 50 return create( registry, full, findRootByType( full, JenaModelSpec.ModelSpec ) ); } 51 52 56 public static ModelSpec createSpec( Model m, Resource root ) 57 { Model full = withSchema( m ); 58 return create( defaultRegistry, full, (Resource) root.inModel( full ) ); } 59 60 65 public static ModelSpec createSpec( Model m ) 66 { Model full = withSchema( m ); 67 return create( defaultRegistry, full, findRootByType( full, JenaModelSpec.ModelSpec ) ); } 68 69 72 public static Model withSchema( Model m ) 73 { return withSpecSchema( m ); } 74 75 79 public static Resource findRootByType( Model m, Resource type ) 80 { StmtIterator it = m.listStatements( null, RDF.type, type ); 81 if (!it.hasNext()) throw new BadDescriptionNoRootException( m, type ); 82 Resource root = it.nextStatement().getSubject(); 83 if (it.hasNext()) throw new BadDescriptionMultipleRootsException( m, type ); 84 return root; } 85 86 96 public static Resource findSpecificType( Resource root, Resource type ) 97 { StmtIterator it = root.listProperties( RDF.type ); 98 Model desc = root.getModel(); 99 while (it.hasNext()) 100 { Resource candidate = it.nextStatement().getResource(); 101 if (desc.contains( candidate, RDFS.subClassOf, type )) type = candidate; } 102 return type; } 103 104 public static Model withSpecSchema( Model m ) 105 { 106 Model result = ModelFactory.createDefaultModel(); 107 Model schema = JenaModelSpec.getSchema(); 108 result.add( m ); 109 addJMSSubclassesFrom( result, schema ); 110 addDomainTypes( result, m, schema ); 111 addSupertypesFrom( result, schema ); 112 addSupertypesFrom( result, m ); 113 return result; 114 } 115 116 protected static final RDFNode nullObject = (RDFNode) null; 117 118 protected static void addDomainTypes( Model result, Model m, Model schema ) 119 { 120 for (StmtIterator it = schema.listStatements( null, RDFS.domain, nullObject ); it.hasNext();) 121 { 122 Statement s = it.nextStatement(); 123 Property property = (Property) s.getSubject().as( Property.class ); 124 for (StmtIterator x = m.listStatements( null, property, nullObject ); x.hasNext();) 125 { 126 Statement t = x.nextStatement(); 127 result.add( t.getSubject(), RDF.type, s.getObject() ); 128 } 129 } 130 } 131 132 protected static void addJMSSubclassesFrom( Model result, Model schema ) 133 { 134 for (StmtIterator it = schema.listStatements( null, RDFS.subClassOf, nullObject ); it.hasNext();) 135 { 136 Statement s = it.nextStatement(); 137 if (s.getSubject().getNameSpace().equals( JenaModelSpec.baseURI ) && s.getResource().getNameSpace().equals( JenaModelSpec.baseURI )) 138 result.add( s ); 139 } 140 } 141 142 protected static void addSupertypesFrom( Model result, Model source ) 143 { 144 Model temp = ModelFactory.createDefaultModel(); 145 for (StmtIterator it = result.listStatements( null, RDF.type, nullObject ); it.hasNext();) 146 { 147 Statement s = it.nextStatement(); 148 for (StmtIterator subclasses = source.listStatements( s.getResource(), RDFS.subClassOf, nullObject ); subclasses.hasNext();) 149 temp.add( s.getSubject(), RDF.type, subclasses.nextStatement().getObject() ); 150 } 151 result.add( temp ); 152 } 153 154 } 155 156 157 | Popular Tags |