KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > ModelSpecCreatorRegistry


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: ModelSpecCreatorRegistry.java,v 1.12 2005/04/10 12:45:49 chris-dollin Exp $
5 */

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 /**
18     The registry of creators appropriate for different ModelSpec types; it maps RDF types
19     (subtypes of jms:ModelSpec) to ModelSpecCreators which, when requested, will
20     deliver ModelSpecs.
21     
22     @author hedgehog
23 */

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 JavaDoc name )
35         { this( name, false ); }
36     
37     public ModelSpecCreatorRegistry( String JavaDoc name, boolean ignoreMissingModel )
38         { this();
39         addFromModelNamed( name, ignoreMissingModel ); }
40
41     /**
42         Answer a registry with a single entry, mapping <code>type</code>
43         to <code>c</code>.
44     */

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     /**
65         Add registry entries (R, L) from those statements of the model
66         located by <code>name</code> which are (R, jms:typeCreatedBy, L).
67         
68         <p>if ignoreMissingModel is true, a missing model is ignored,
69         rather than raising an exception.
70     */

71     protected void addFromModelNamed( String JavaDoc 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     /**
84         Add a registery entry whose key is the resource R and whose creator
85         is the class named by L from the statement s=(R, jms.typecreatedBy, L).
86     */

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 /*
122     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
123     All rights reserved.
124
125     Redistribution and use in source and binary forms, with or without
126     modification, are permitted provided that the following conditions
127     are met:
128
129     1. Redistributions of source code must retain the above copyright
130        notice, this list of conditions and the following disclaimer.
131
132     2. Redistributions in binary form must reproduce the above copyright
133        notice, this list of conditions and the following disclaimer in the
134        documentation and/or other materials provided with the distribution.
135
136     3. The name of the author may not be used to endorse or promote products
137        derived from this software without specific prior written permission.
138
139     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
141     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
142     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
143     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
144     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
145     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
146     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
147     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
148     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
149 */
Popular Tags