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.util.FileManager; 11 import com.hp.hpl.jena.vocabulary.*; 12 import com.hp.hpl.jena.shared.*; 13 14 import java.util.*; 15 16 26 public abstract class ModelSpecImpl implements ModelSpec 27 { 28 31 protected ModelMaker maker; 32 33 36 private static Map values = new HashMap(); 37 38 43 public ModelSpecImpl( ModelMaker maker ) 44 { 45 if (maker == null) throw new RuntimeException ( "null maker not allowed" ); 46 this.maker = maker; 47 } 48 49 public ModelSpecImpl( Resource root, Model description ) 50 { 51 this( createMaker( getMaker( root, description ), description ) ); 52 this.root = root; 53 this.description = description; 54 } 55 56 public static final Model emptyModel = ModelFactory.createDefaultModel(); 57 58 public static final Model defaultModel = ModelFactory.createDefaultModel(); 59 60 public static final Resource emptyResource = emptyModel.createResource(); 61 62 protected Model description = emptyModel; 63 64 protected Resource root = ResourceFactory.createResource( "" ); 65 66 70 public abstract Model createModel(); 71 72 public Model getModel() 73 { return defaultModel; } 74 75 81 public abstract Model createModelOver( String name ); 82 83 89 public abstract Property getMakerProperty(); 90 91 96 public Model openModel( String URI ) 97 { return ModelFactory.createDefaultModel(); } 98 99 103 public Model openModelIfPresent( String URI ) 104 { return null; } 105 106 public static Resource getMaker( Resource root, Model desc ) 107 { 108 StmtIterator it = desc.listStatements( root, JenaModelSpec.maker, (RDFNode) null ); 109 if (it.hasNext()) 110 return it.nextStatement().getResource(); 111 else 112 { 113 Resource r = desc.createResource(); 114 desc.add( root, JenaModelSpec.maker, r ); 115 return r; 116 } 118 } 119 120 124 public ModelMaker getModelMaker() 125 { return maker; } 126 127 public Model getDescription() 128 { return getDescription( ResourceFactory.createResource() ); } 129 130 public Model getDescription( Resource root ) 131 { return addDescription( ModelFactory.createDefaultModel(), root ); } 132 133 public Model addDescription( Model desc, Resource root ) 134 { 135 Resource makerRoot = desc.createResource(); 136 desc.add( root, JenaModelSpec.maker, makerRoot ); 137 maker.addDescription( desc, makerRoot ); 138 return desc; 139 } 140 141 150 public static Resource createValue( Object value ) 151 { 152 Resource it = ResourceFactory.createResource(); 153 values.put( it, value ); 154 return it; 155 } 156 157 164 public static Object getValue( RDFNode it ) 165 { return values.get( it ); } 166 167 175 public static Resource findRootByType( Model description, Resource type ) 176 { return ModelSpecFactory.findRootByType( ModelSpecFactory.withSchema( description ), type ); } 177 178 186 public static ModelMaker createMaker( Model description ) 187 { Model d = ModelSpecFactory.withSchema( description ); 188 return createMakerByRoot( ModelSpecFactory.findRootByType( d, JenaModelSpec.MakerSpec ), d ); } 189 190 public static ModelMaker createMaker( Resource root, Model d ) 191 { return createMakerByRoot( root, ModelSpecFactory.withSchema( d ) ); } 192 193 public static ModelMaker createMakerByRoot( Resource root, Model fullDesc ) 194 { Resource type = ModelSpecFactory.findSpecificType( (Resource) root.inModel( fullDesc ), JenaModelSpec.MakerSpec ); 195 ModelMakerCreator mmc = ModelMakerCreatorRegistry.findCreator( type ); 196 if (mmc == null) throw new RuntimeException ( "no maker type" ); 197 return mmc.create( fullDesc, root ); } 198 199 204 public static Model readModel( Resource source ) 205 { 206 String uri = source.getURI(); 207 return FileManager.get().loadModel( uri ); 208 } 209 210 protected Model loadFiles(Model m) 211 { 212 StmtIterator it = description.listStatements( root, JenaModelSpec.loadWith, (RDFNode) null ); 213 while (it.hasNext()) FileManager.get().readModel( m, it.nextStatement().getResource().getURI() ); 214 return m; 215 } 216 217 } 218 219 | Popular Tags |