KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: ResourceFactory.java,v 1.8 2005/02/21 12:14:25 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model;
8
9 import com.hp.hpl.jena.rdf.model.impl.*;
10
11 /** A Factory class for creating resources.
12  *
13  * <p> This class creates resources and properties and things of that ilk.</p>
14  *
15  * <p> It is designed as a singleton. There are static convenience
16  * methods on this class itself, so the easy way to create resource
17  * is for example to do something like:</p>
18  * <pre>
19  * <code>Resource r = ResourceFactory.createResource();</code>
20  * </pre>
21  *
22  * <p> If a factory object is needed, then this can be obtained using
23  * the <code>getInstance</code> method on the class. The factory
24  * object used may be changed using the <code>setInstance</code>
25  * method.</p>
26 */

27 public class ResourceFactory {
28     protected static Interface instance = new Impl();
29
30     private ResourceFactory() {
31     }
32
33     /** get the current factory object.
34      *
35      * @return the current factory object
36      */

37     public static Interface getInstance() {
38         return instance;
39     }
40     /** set the current factory object.
41      *
42      * @param newInstance the new factory object
43      * @return the previous factory object
44      */

45     public static Interface setInstance(Interface newInstance) {
46         Interface previousInstance = instance;
47         instance = newInstance;
48         return previousInstance;
49     }
50
51     /** create a new anonymous resource.
52      *
53      * <p>Uses the current factory object to create a new anonymous resource.</p>
54      *
55      * @return a new anonymous resource
56      */

57     public static Resource createResource() {
58         return instance.createResource();
59     }
60
61     /** create a new resource.
62      *
63      * <p>Uses the current factory object to create a new resource.</p>
64      *
65      * @param uriref URIREF of the resource
66      * @return a new resource
67      */

68     public static Resource createResource(String JavaDoc uriref) {
69         return instance.createResource(uriref);
70     }
71     
72     public static Literal createPlainLiteral( String JavaDoc string ) {
73         return instance.createPlainLiteral( string );
74     }
75
76     /** create a new property.
77      *
78      * <p>Uses the current factory object to create a new resource.</p>
79      *
80      * @param uriref URIREF of the property
81      * @return a new property
82      */

83     public static Property createProperty(String JavaDoc uriref) {
84         return instance.createProperty(uriref);
85     }
86
87     /** create a new property.
88      *
89      * <p>Uses the current factory object to create a new property.</p>
90      *
91      * @param namespace URIREF of the namespace of the property
92      * @param localName localname of the property
93      * @return a new property
94      */

95     public static Property createProperty(String JavaDoc namespace, String JavaDoc localName) {
96         return instance.createProperty(namespace, localName);
97     }
98
99     /** create a new statement.
100      * Uses the current factory object to create a new statement.</p>
101      *
102      * @param subject the subject of the new statement
103      * @param predicate the predicate of the new statement
104      * @param object the objectof the new statement
105      * @return a new resource
106      */

107     public static Statement createStatement(
108         Resource subject,
109         Property predicate,
110         RDFNode object) {
111         return instance.createStatement(subject, predicate, object);
112     }
113
114     /** the interface to resource factory objects.
115      */

116     public interface Interface {
117
118         /** create a new anonymous resource.
119          *
120          * @return a new anonymous resource
121          */

122         public Resource createResource();
123
124         /** create a new resource.
125          *
126          * @param uriref URIREF of the resource
127          * @return a new resource
128          */

129         public Resource createResource(String JavaDoc uriref);
130         
131         /**
132             Answer a plain (untyped) literal with no language and the given content.
133             @param string the string which forms the value of the literal
134             @return a Literal node with that string as value
135         */

136         public Literal createPlainLiteral( String JavaDoc string );
137
138         /** create a new property.
139          *
140          * @param uriref URIREF of the property
141          * @return a new property
142          */

143         public Property createProperty(String JavaDoc uriref);
144
145         /** create a new property.
146          *
147          * @param namespace uriref of the namespace
148          * @param localName localname of the property
149          * @return a new property
150          */

151         public Property createProperty(String JavaDoc namespace, String JavaDoc localName);
152
153         /** create a new statement.
154          *
155          * @param subject subject of the new statement
156          * @param predicate predicate of the new statement
157          * @param object object of the new statement
158          * @return a new statement
159          */

160         public Statement createStatement(
161             Resource subject,
162             Property predicate,
163             RDFNode object);
164     }
165
166     static class Impl implements Interface {
167
168         Impl() {
169         }
170
171         public Resource createResource() {
172             return new ResourceImpl();
173         }
174
175         public Resource createResource(String JavaDoc uriref) {
176             return new ResourceImpl(uriref);
177         }
178         
179         public Literal createPlainLiteral( String JavaDoc string ) {
180             return new LiteralImpl( string );
181         }
182
183         public Property createProperty(String JavaDoc uriref) {
184             return new PropertyImpl(uriref);
185         }
186
187         public Property createProperty(String JavaDoc namespace, String JavaDoc localName) {
188             return new PropertyImpl(namespace, localName);
189         }
190
191         public Statement createStatement(
192             Resource subject,
193             Property predicate,
194             RDFNode object) {
195             return new StatementImpl(subject, predicate, object);
196         }
197
198     }
199 }
200
201 /*
202     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
203     All rights reserved.
204
205     Redistribution and use in source and binary forms, with or without
206     modification, are permitted provided that the following conditions
207     are met:
208
209     1. Redistributions of source code must retain the above copyright
210        notice, this list of conditions and the following disclaimer.
211
212     2. Redistributions in binary form must reproduce the above copyright
213        notice, this list of conditions and the following disclaimer in the
214        documentation and/or other materials provided with the distribution.
215
216     3. The name of the author may not be used to endorse or promote products
217        derived from this software without specific prior written permission.
218
219     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
220     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
221     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
222     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
224     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
225     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
226     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
227     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
228     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
229 */
Popular Tags