001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: ResourceImpl.java,v 1.47 2008/01/02 12:04:56 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.impl;
008:
009: import com.hp.hpl.jena.rdf.model.*;
010: import com.hp.hpl.jena.datatypes.RDFDatatype;
011: import com.hp.hpl.jena.enhanced.*;
012:
013: import com.hp.hpl.jena.graph.*;
014:
015: /** An implementation of Resource.
016: *
017: * @author bwm
018: * @version Release='$Name: $' Revision='$Revision: 1.47 $' Date='$Date: 2008/01/02 12:04:56 $'
019: */
020:
021: public class ResourceImpl extends EnhNode implements Resource {
022:
023: final static public Implementation factory = new Implementation() {
024: public boolean canWrap(Node n, EnhGraph eg) {
025: return !n.isLiteral();
026: }
027:
028: public EnhNode wrap(Node n, EnhGraph eg) {
029: if (n.isLiteral())
030: throw new ResourceRequiredException(n);
031: return new ResourceImpl(n, eg);
032: }
033: };
034: final static public Implementation rdfNodeFactory = new Implementation() {
035: public boolean canWrap(Node n, EnhGraph eg) {
036: return true;
037: }
038:
039: public EnhNode wrap(Node n, EnhGraph eg) {
040: if (n.isURI() || n.isBlank())
041: return new ResourceImpl(n, eg);
042: if (n.isLiteral())
043: return new LiteralImpl(n, eg);
044: return null;
045: }
046: };
047:
048: /**
049: the master constructor: make a new Resource in the given model,
050: rooted in the given node.
051:
052: NOT FOR PUBLIC USE - used in ModelCom [and ContainerImpl]
053: */
054: public ResourceImpl(Node n, ModelCom m) {
055: super (n, m);
056: }
057:
058: /** Creates new ResourceImpl */
059:
060: public ResourceImpl() {
061: this ((ModelCom) null);
062: }
063:
064: public ResourceImpl(ModelCom m) {
065: this (fresh(null), m);
066: }
067:
068: public ResourceImpl(Node n, EnhGraph m) {
069: super (n, m);
070: }
071:
072: public ResourceImpl(String uri) {
073: super (fresh(uri), null);
074: }
075:
076: public ResourceImpl(String nameSpace, String localName) {
077: super (Node.createURI(nameSpace + localName), null);
078: }
079:
080: public ResourceImpl(AnonId id) {
081: this (id, null);
082: }
083:
084: public ResourceImpl(AnonId id, ModelCom m) {
085: this (Node.createAnon(id), m);
086: }
087:
088: public ResourceImpl(String uri, ModelCom m) {
089: this (fresh(uri), m);
090: }
091:
092: public ResourceImpl(Resource r, ModelCom m) {
093: this (r.asNode(), m);
094: }
095:
096: public ResourceImpl(String nameSpace, String localName, ModelCom m) {
097: this (Node.createURI(nameSpace + localName), m);
098: }
099:
100: public Object visitWith(RDFVisitor rv) {
101: return isAnon() ? rv.visitBlank(this , getId()) : rv.visitURI(
102: this , getURI());
103: }
104:
105: public RDFNode inModel(Model m) {
106: return getModel() == m ? this : isAnon() ? m
107: .createResource(getId())
108: : asNode().isConcrete() == false ? ((ModelCom) m)
109: .getRDFNode(asNode()) : m
110: .createResource(getURI());
111: }
112:
113: private static Node fresh(String uri) {
114: return uri == null ? Node.createAnon() : Node.createURI(uri);
115: }
116:
117: public Node getNode() {
118: return asNode();
119: }
120:
121: public AnonId getId() {
122: return asNode().getBlankNodeId();
123: }
124:
125: public String getURI() {
126: return isAnon() ? null : node.getURI();
127: }
128:
129: public String getNameSpace() {
130: return isAnon() ? null : node.getNameSpace();
131: }
132:
133: public String getLocalName() {
134: return isAnon() ? null : node.getLocalName();
135: }
136:
137: public boolean hasURI(String uri) {
138: return node.hasURI(uri);
139: }
140:
141: public String toString() {
142: return asNode().toString();
143: }
144:
145: protected ModelCom mustHaveModel() {
146: ModelCom model = getModelCom();
147: if (model == null)
148: throw new HasNoModelException(this );
149: return model;
150: }
151:
152: public Statement getRequiredProperty(Property p) {
153: return mustHaveModel().getRequiredProperty(this , p);
154: }
155:
156: public Statement getProperty(Property p) {
157: return mustHaveModel().getProperty(this , p);
158: }
159:
160: public StmtIterator listProperties(Property p) {
161: return mustHaveModel().listStatements(this , p, (RDFNode) null);
162: }
163:
164: public StmtIterator listProperties() {
165: return mustHaveModel().listStatements(this , null,
166: (RDFNode) null);
167: }
168:
169: /**
170: * @deprecated Use {@link #addLiteral(Property,boolean)} instead
171: */
172: public Resource addTypedProperty(Property p, boolean o) {
173: return addLiteral(p, o);
174: }
175:
176: public Resource addLiteral(Property p, boolean o) {
177: ModelCom m = mustHaveModel();
178: m.add(this , p, m.createTypedLiteral(o));
179: return this ;
180: }
181:
182: public Resource addProperty(Property p, long o) {
183: mustHaveModel().addLiteral(this , p, o);
184: return this ;
185: }
186:
187: public Resource addLiteral(Property p, long o) {
188: Model m = mustHaveModel();
189: m.add(this , p, m.createTypedLiteral(o));
190: return this ;
191: }
192:
193: public Resource addLiteral(Property p, char o) {
194: ModelCom m = mustHaveModel();
195: m.add(this , p, m.createTypedLiteral(o));
196: return this ;
197: }
198:
199: public Resource addProperty(Property p, float o) {
200: mustHaveModel().addLiteral(this , p, o);
201: return this ;
202: }
203:
204: public Resource addProperty(Property p, double o) {
205: mustHaveModel().addLiteral(this , p, o);
206: return this ;
207: }
208:
209: public Resource addLiteral(Property p, double o) {
210: Model m = mustHaveModel();
211: m.add(this , p, m.createTypedLiteral(o));
212: return this ;
213: }
214:
215: public Resource addLiteral(Property p, float o) {
216: Model m = mustHaveModel();
217: m.add(this , p, m.createTypedLiteral(o));
218: return this ;
219: }
220:
221: public Resource addProperty(Property p, String o) {
222: mustHaveModel().add(this , p, o);
223: return this ;
224: }
225:
226: public Resource addProperty(Property p, String o, String l) {
227: mustHaveModel().add(this , p, o, l);
228: return this ;
229: }
230:
231: public Resource addProperty(Property p, String lexicalForm,
232: RDFDatatype datatype) {
233: mustHaveModel().add(this , p, lexicalForm, datatype);
234: return this ;
235: }
236:
237: /**
238: * @deprecated Use {@link #addLiteral(Property,Object)} instead
239: */
240: public Resource addTypedProperty(Property p, Object o) {
241: return addLiteral(p, o);
242: }
243:
244: public Resource addLiteral(Property p, Object o) {
245: ModelCom m = mustHaveModel();
246: m.add(this , p, m.createTypedLiteral(o));
247: return this ;
248: }
249:
250: public Resource addProperty(Property p, RDFNode o) {
251: mustHaveModel().add(this , p, o);
252: return this ;
253: }
254:
255: public boolean hasProperty(Property p) {
256: return mustHaveModel().contains(this , p);
257: }
258:
259: /**
260: * @deprecated Use {@link #hasLiteral(Property,boolean)} instead
261: */
262: public boolean hasTypedProperty(Property p, boolean o) {
263: return hasLiteral(p, o);
264: }
265:
266: public boolean hasLiteral(Property p, boolean o) {
267: ModelCom m = mustHaveModel();
268: return m.contains(this , p, m.createTypedLiteral(o));
269: }
270:
271: public boolean hasLiteral(Property p, long o) {
272: ModelCom m = mustHaveModel();
273: return m.contains(this , p, m.createTypedLiteral(o));
274: }
275:
276: public boolean hasLiteral(Property p, char o) {
277: ModelCom m = mustHaveModel();
278: return m.contains(this , p, m.createTypedLiteral(o));
279: }
280:
281: public boolean hasLiteral(Property p, double o) {
282: ModelCom m = mustHaveModel();
283: return m.contains(this , p, m.createTypedLiteral(o));
284: }
285:
286: public boolean hasLiteral(Property p, float o) {
287: ModelCom m = mustHaveModel();
288: return m.contains(this , p, m.createTypedLiteral(o));
289: }
290:
291: public boolean hasProperty(Property p, String o) {
292: return mustHaveModel().contains(this , p, o);
293: }
294:
295: public boolean hasProperty(Property p, String o, String l) {
296: return mustHaveModel().contains(this , p, o, l);
297: }
298:
299: public boolean hasLiteral(Property p, Object o) {
300: ModelCom m = mustHaveModel();
301: return m.contains(this , p, m.createTypedLiteral(o));
302: }
303:
304: public boolean hasProperty(Property p, RDFNode o) {
305: return mustHaveModel().contains(this , p, o);
306: }
307:
308: public Resource removeProperties() {
309: removeAll(null);
310: return this ;
311: }
312:
313: public Resource removeAll(Property p) {
314: mustHaveModel().removeAll(this , p, (RDFNode) null);
315: return this ;
316: }
317:
318: public Resource begin() {
319: mustHaveModel().begin();
320: return this ;
321: }
322:
323: public Resource abort() {
324: mustHaveModel().abort();
325: return this ;
326: }
327:
328: public Resource commit() {
329: mustHaveModel().commit();
330: return this ;
331: }
332:
333: public Model getModel() {
334: return (Model) getGraph();
335: }
336:
337: protected ModelCom getModelCom() {
338: return (ModelCom) getGraph();
339: }
340: }
341: /*
342: * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
343: * All rights reserved.
344: *
345: * Redistribution and use in source and binary forms, with or without
346: * modification, are permitted provided that the following conditions
347: * are met:
348: * 1. Redistributions of source code must retain the above copyright
349: * notice, this list of conditions and the following disclaimer.
350: * 2. Redistributions in binary form must reproduce the above copyright
351: * notice, this list of conditions and the following disclaimer in the
352: * documentation and/or other materials provided with the distribution.
353: * 3. The name of the author may not be used to endorse or promote products
354: * derived from this software without specific prior written permission.
355:
356: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
357: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
358: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
359: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
360: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
361: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
362: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
363: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
364: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
365: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
366: *
367: * ResourceImpl.java
368: *
369: * Created on 03 August 2000, 13:45
370: */
|