1 6 7 package com.hp.hpl.jena.rdf.model; 8 9 import com.hp.hpl.jena.rdf.model.impl.*; 10 import com.hp.hpl.jena.shared.NotFoundException; 11 import com.hp.hpl.jena.ontology.*; 12 import com.hp.hpl.jena.util.FileManager; 13 import com.hp.hpl.jena.vocabulary.*; 14 15 import java.util.*; 16 17 24 public class ModelSpecCreatorRegistry 25 { 26 protected Map creators = new HashMap(); 27 28 public static final ModelSpecCreatorRegistry instance = 29 new ModelSpecCreatorRegistry( "etc/modelspec-config.n3", true ); 30 31 public ModelSpecCreatorRegistry() 32 {} 33 34 public ModelSpecCreatorRegistry( String name ) 35 { this( name, false ); } 36 37 public ModelSpecCreatorRegistry( String name, boolean ignoreMissingModel ) 38 { this(); 39 addFromModelNamed( name, ignoreMissingModel ); } 40 41 45 public static ModelSpecCreatorRegistry registryWith( Resource type, ModelSpecCreator c ) 46 { 47 ModelSpecCreatorRegistry result = new ModelSpecCreatorRegistry(); 48 result.registerCreator( type, c ); 49 return result; 50 } 51 52 public ModelSpecCreator getCreator( Resource type ) 53 { return (ModelSpecCreator) creators.get( type ); } 54 55 public static ModelSpecCreator findCreator( Resource type ) 56 { return instance.getCreator( type ); } 57 58 public void registerCreator( Resource type, ModelSpecCreator c ) 59 { creators.put( type, c ); } 60 61 public static void register( Resource type, ModelSpecCreator c ) 62 { instance.registerCreator( type, c ); } 63 64 71 protected void addFromModelNamed( String name, boolean ignoreMissingModel ) 72 { 73 try 74 { 75 Model m = FileManager.get().loadModel( name ); 76 StmtIterator it = m.listStatements( null, JenaModelSpec.typeCreatedBy, (RDFNode) null ); 77 while (it.hasNext()) addFromStatement( it.nextStatement() ); 78 } 79 catch (NotFoundException e) 80 { if (!ignoreMissingModel) throw e; } 81 } 82 83 87 protected void addFromStatement( Statement s ) 88 { 89 registerCreator( s.getSubject(), new ModelSpecCreatorByClassname( s.getString() ) ); 90 } 91 92 static class InfSpecCreator implements ModelSpecCreator 93 { 94 public ModelSpec create( Resource root, Model desc ) 95 { return new InfModelSpec( root, desc ); } 96 } 97 98 static class PlainSpecCreator implements ModelSpecCreator 99 { 100 public ModelSpec create( Resource root, Model desc ) 101 { return new PlainModelSpec( root, desc ); } 102 } 103 104 static class OntSpecCreator implements ModelSpecCreator 105 { 106 public ModelSpec create( Resource root, Model desc ) 107 { return new OntModelSpec( root, desc ); } 108 } 109 110 static 111 { 112 register( JenaModelSpec.InfModelSpec, new InfSpecCreator() ); 113 register( JenaModelSpec.OntModelSpec, new OntSpecCreator() ); 114 register( JenaModelSpec.PlainModelSpec, new PlainSpecCreator() ); 115 116 register( JenaModelSpec.ModelSpec, new PlainSpecCreator() ); 117 register( JenaModelSpec.DefaultModelSpec, new PlainSpecCreator() ); 118 } 119 } 120 121 | Popular Tags |