001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: ResourceFactory.java,v 1.15 2008/01/02 12:05:48 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model;
008:
009: import java.util.Calendar;
010:
011: import com.hp.hpl.jena.datatypes.RDFDatatype;
012: import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
013: import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
014: import com.hp.hpl.jena.graph.Node;
015: import com.hp.hpl.jena.graph.impl.LiteralLabel;
016: import com.hp.hpl.jena.rdf.model.impl.*;
017:
018: /** A Factory class for creating resources.
019: *
020: * <p> This class creates resources and properties and things of that ilk.
021: * These resources are <i>not</i> associated with a user-modifiable
022: * model: doing getModel() on them will return null.
023: * </p>
024: *
025: * <p> It is designed as a singleton. There are static convenience
026: * methods on this class itself, so the easy way to create resource
027: * is for example to do something like:</p>
028: * <pre>
029: * <code>Resource r = ResourceFactory.createResource();</code>
030: * </pre>
031: *
032: * <p> If a factory object is needed, then this can be obtained using
033: * the <code>getInstance</code> method on the class. The factory
034: * object used may be changed using the <code>setInstance</code>
035: * method.</p>
036: */
037: public class ResourceFactory {
038: protected static Interface instance = new Impl();
039:
040: private ResourceFactory() {
041: }
042:
043: /** get the current factory object.
044: *
045: * @return the current factory object
046: */
047: public static Interface getInstance() {
048: return instance;
049: }
050:
051: /** set the current factory object.
052: *
053: * @param newInstance the new factory object
054: * @return the previous factory object
055: */
056: public static Interface setInstance(Interface newInstance) {
057: Interface previousInstance = instance;
058: instance = newInstance;
059: return previousInstance;
060: }
061:
062: /** create a new anonymous resource.
063: *
064: * <p>Uses the current factory object to create a new anonymous resource.</p>
065: *
066: * @return a new anonymous resource
067: */
068: public static Resource createResource() {
069: return instance.createResource();
070: }
071:
072: /** create a new resource.
073: *
074: * <p>Uses the current factory object to create a new resource.</p>
075: *
076: * @param uriref URIREF of the resource
077: * @return a new resource
078: */
079: public static Resource createResource(String uriref) {
080: return instance.createResource(uriref);
081: }
082:
083: public static Literal createPlainLiteral(String string) {
084: return instance.createPlainLiteral(string);
085: }
086:
087: public static Literal createTypedLiteral(String string,
088: RDFDatatype dType) {
089: return instance.createTypedLiteral(string, dType);
090: }
091:
092: /**
093: Answer a typed literal.
094: @param value a java Object, the default RDFDatatype for that object will be used
095: @return a Literal node with that value
096: */
097: public static Literal createTypedLiteral(Object value) {
098: return instance.createTypedLiteral(value);
099: }
100:
101: /** create a new property.
102: *
103: * <p>Uses the current factory object to create a new resource.</p>
104: *
105: * @param uriref URIREF of the property
106: * @return a new property
107: */
108: public static Property createProperty(String uriref) {
109: return instance.createProperty(uriref);
110: }
111:
112: /** create a new property.
113: *
114: * <p>Uses the current factory object to create a new property.</p>
115: *
116: * @param namespace URIREF of the namespace of the property
117: * @param localName localname of the property
118: * @return a new property
119: */
120: public static Property createProperty(String namespace,
121: String localName) {
122: return instance.createProperty(namespace, localName);
123: }
124:
125: /** create a new statement.
126: * Uses the current factory object to create a new statement.</p>
127: *
128: * @param subject the subject of the new statement
129: * @param predicate the predicate of the new statement
130: * @param object the objectof the new statement
131: * @return a new resource
132: */
133: public static Statement createStatement(Resource subject,
134: Property predicate, RDFNode object) {
135: return instance.createStatement(subject, predicate, object);
136: }
137:
138: /** the interface to resource factory objects.
139: */
140: public interface Interface {
141:
142: /** create a new anonymous resource.
143: *
144: * @return a new anonymous resource
145: */
146: public Resource createResource();
147:
148: /** create a new resource.
149: *
150: * @param uriref URIREF of the resource
151: * @return a new resource
152: */
153: public Resource createResource(String uriref);
154:
155: /**
156: Answer a plain (untyped) literal with no language and the given content.
157: @param string the string which forms the value of the literal
158: @return a Literal node with that string as value
159: */
160: public Literal createPlainLiteral(String string);
161:
162: /**
163: Answer a typed literal.
164: @param string the string which forms the value of the literal
165: @param datatype RDFDatatype of the type literal
166: @return a Literal node with that string as value
167: */
168: public Literal createTypedLiteral(String string,
169: RDFDatatype datatype);
170:
171: /**
172: Answer a typed literal.
173: @param value a java Object, the default RDFDatatype for that object will be used
174: @return a Literal node with that value
175: */
176: public Literal createTypedLiteral(Object value);
177:
178: /** create a new property.
179: *
180: * @param uriref URIREF of the property
181: * @return a new property
182: */
183: public Property createProperty(String uriref);
184:
185: /** create a new property.
186: *
187: * @param namespace uriref of the namespace
188: * @param localName localname of the property
189: * @return a new property
190: */
191: public Property createProperty(String namespace,
192: String localName);
193:
194: /** create a new statement.
195: *
196: * @param subject subject of the new statement
197: * @param predicate predicate of the new statement
198: * @param object object of the new statement
199: * @return a new statement
200: */
201: public Statement createStatement(Resource subject,
202: Property predicate, RDFNode object);
203: }
204:
205: static class Impl implements Interface {
206:
207: Impl() {
208: }
209:
210: public Resource createResource() {
211: return new ResourceImpl();
212: }
213:
214: public Resource createResource(String uriref) {
215: return new ResourceImpl(uriref);
216: }
217:
218: public Literal createPlainLiteral(String string) {
219: return new LiteralImpl(string);
220: }
221:
222: public Literal createTypedLiteral(String string,
223: RDFDatatype dType) {
224: return new LiteralImpl(Node
225: .createLiteral(string, "", dType), null);
226: }
227:
228: public Literal createTypedLiteral(Object value) {
229: LiteralLabel ll = null;
230: if (value instanceof Calendar) {
231: Object valuec = new XSDDateTime((Calendar) value);
232: ll = new LiteralLabel(valuec, "",
233: XSDDatatype.XSDdateTime);
234: } else {
235: ll = new LiteralLabel(value);
236: }
237: return new LiteralImpl(Node.createLiteral(ll), null);
238: }
239:
240: public Property createProperty(String uriref) {
241: return new PropertyImpl(uriref);
242: }
243:
244: public Property createProperty(String namespace,
245: String localName) {
246: return new PropertyImpl(namespace, localName);
247: }
248:
249: public Statement createStatement(Resource subject,
250: Property predicate, RDFNode object) {
251: return new StatementImpl(subject, predicate, object);
252: }
253:
254: }
255: }
256:
257: /*
258: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
259: All rights reserved.
260:
261: Redistribution and use in source and binary forms, with or without
262: modification, are permitted provided that the following conditions
263: are met:
264:
265: 1. Redistributions of source code must retain the above copyright
266: notice, this list of conditions and the following disclaimer.
267:
268: 2. Redistributions in binary form must reproduce the above copyright
269: notice, this list of conditions and the following disclaimer in the
270: documentation and/or other materials provided with the distribution.
271:
272: 3. The name of the author may not be used to endorse or promote products
273: derived from this software without specific prior written permission.
274:
275: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
276: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
277: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
278: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
279: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
280: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
281: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
282: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
283: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
284: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
285: */
|