KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > ResourceImpl


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

6
7 package com.hp.hpl.jena.rdf.model.impl;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.enhanced.*;
11
12 import com.hp.hpl.jena.graph.*;
13
14 /** An implementation of Resource.
15  *
16  * @author bwm
17  * @version Release='$Name: $' Revision='$Revision: 1.31 $' Date='$Date: 2005/02/21 12:14:52 $'
18  */

19
20 public class ResourceImpl extends EnhNode implements Resource {
21     
22     final static public Implementation factory = new Implementation() {
23         public boolean canWrap( Node n, EnhGraph eg )
24             { return !n.isLiteral(); }
25         public EnhNode wrap(Node n,EnhGraph eg) {
26             if (n.isLiteral()) throw new ResourceRequiredException( n );
27             return new ResourceImpl(n,eg);
28         }
29     };
30     final static public Implementation rdfNodeFactory = new Implementation() {
31         public boolean canWrap( Node n, EnhGraph eg )
32             { return true; }
33         public EnhNode wrap(Node n,EnhGraph eg) {
34         if ( n.isURI() || n.isBlank() )
35           return new ResourceImpl(n,eg);
36         if ( n.isLiteral() )
37           return new LiteralImpl(n,eg);
38         return null;
39     }
40 };
41         
42     /**
43         the master constructor: make a new Resource in the given model,
44         rooted in the given node.
45     
46         NOT FOR PUBLIC USE - used in ModelCom [and ContainerImpl]
47     */

48      public ResourceImpl( Node n, ModelCom m ) {
49         super( n, m );
50     }
51
52     /** Creates new ResourceImpl */
53
54     public ResourceImpl() {
55         this( (ModelCom) null );
56     }
57
58     public ResourceImpl( ModelCom m ) {
59         this( fresh( null ), m );
60     }
61
62      
63     public ResourceImpl( Node n, EnhGraph m ) {
64         super( n, m );
65     }
66
67     public ResourceImpl( String JavaDoc uri ) {
68         super( fresh( uri ), null );
69     }
70
71     public ResourceImpl(String JavaDoc nameSpace, String JavaDoc localName) {
72         super( Node.createURI( nameSpace + localName ), null );
73     }
74
75     public ResourceImpl(AnonId id) {
76         this( id, null );
77     }
78
79     public ResourceImpl(AnonId id, ModelCom m) {
80         this( Node.createAnon( id ), m );
81     }
82
83     public ResourceImpl(String JavaDoc uri, ModelCom m) {
84         this( fresh( uri ), m );
85     }
86     
87     public ResourceImpl( Resource r, ModelCom m ) {
88         this( r.getNode(), m );
89     }
90     
91     public ResourceImpl(String JavaDoc nameSpace, String JavaDoc localName, ModelCom m) {
92         this( Node.createURI( nameSpace + localName ), m );
93     }
94
95     public Object JavaDoc visitWith( RDFVisitor rv )
96         { return isAnon() ? rv.visitBlank( this, getId() ) : rv.visitURI( this, getURI() ); }
97         
98     public RDFNode inModel( Model m )
99         {
100         return
101             getModel() == m ? this
102             : isAnon() ? m.createResource( getId() )
103             : asNode().isConcrete() == false ? ((ModelCom) m).getRDFNode( asNode() )
104             : m.createResource( getURI() );
105         }
106     
107     private static Node fresh( String JavaDoc uri )
108         { return uri == null ? Node.createAnon( new AnonId() ) : Node.createURI( uri ); }
109
110     public Node getNode()
111         { return asNode(); }
112
113     public AnonId getId()
114         { return asNode().getBlankNodeId(); }
115
116     public String JavaDoc getURI() {
117         return isAnon() ? null : node.getURI();
118     }
119
120     public String JavaDoc getNameSpace() {
121         return isAnon() ? null : node.getNameSpace();
122     }
123     
124     public String JavaDoc getLocalName() {
125         return isAnon() ? null : node.getLocalName();
126     }
127
128     public boolean hasURI( String JavaDoc uri ) {
129         return node.hasURI( uri );
130     }
131     
132     public String JavaDoc toString() {
133         return asNode().toString();
134     }
135
136     public boolean isAnon() {
137         return asNode().isBlank();
138     }
139
140     protected ModelCom mustHaveModel()
141         {
142         ModelCom model = getModelCom();
143         if (model == null) throw new HasNoModelException( this );
144         return model;
145         }
146         
147     public Statement getRequiredProperty(Property p)
148         { return mustHaveModel().getRequiredProperty( this, p ); }
149         
150     public Statement getProperty( Property p )
151         { return mustHaveModel().getProperty( this, p ); }
152
153     public StmtIterator listProperties(Property p)
154         { return mustHaveModel().listStatements( this, p, (RDFNode) null ); }
155
156     public StmtIterator listProperties()
157         { return mustHaveModel().listStatements( this, null, (RDFNode) null ); }
158
159     public Resource addProperty(Property p, boolean o)
160         {
161         mustHaveModel().add( this, p, o );
162         return this;
163         }
164
165     public Resource addProperty(Property p, long o) {
166         mustHaveModel().add( this, p, o );
167         return this;
168     }
169     
170     public Resource addProperty(Property p, char o) {
171         mustHaveModel().add( this, p, o );
172         return this;
173     }
174
175     public Resource addProperty(Property p, float o) {
176         mustHaveModel().add( this, p, o );
177         return this;
178     }
179
180     public Resource addProperty(Property p, double o) {
181         mustHaveModel().add( this, p, o );
182         return this;
183     }
184
185     public Resource addProperty(Property p, String JavaDoc o) {
186         mustHaveModel().add( this, p, o );
187         return this;
188     }
189
190     public Resource addProperty(Property p, String JavaDoc o, String JavaDoc l)
191     {
192         mustHaveModel().add( this, p, o, l );
193         return this;
194     }
195
196     public Resource addProperty(Property p, Object JavaDoc o) {
197         mustHaveModel().add( this, p, o );
198         return this;
199     }
200
201     public Resource addProperty(Property p, RDFNode o) {
202         mustHaveModel().add( this, p, o );
203         return this;
204     }
205
206     public boolean hasProperty(Property p) {
207         return mustHaveModel().contains( this, p );
208     }
209     
210     public boolean hasProperty(Property p, boolean o) {
211         return mustHaveModel().contains( this, p, o );
212     }
213
214     public boolean hasProperty(Property p, long o) {
215         return mustHaveModel().contains( this, p, o );
216     }
217
218     public boolean hasProperty(Property p, char o) {
219         return mustHaveModel().contains( this, p, o );
220     }
221
222     public boolean hasProperty(Property p, float o) {
223         return mustHaveModel().contains( this, p, o );
224     }
225
226     public boolean hasProperty(Property p, double o) {
227         return mustHaveModel().contains( this, p, o );
228     }
229
230     public boolean hasProperty(Property p, String JavaDoc o) {
231         return mustHaveModel().contains( this, p, o );
232     }
233
234     public boolean hasProperty(Property p, String JavaDoc o, String JavaDoc l) {
235         return mustHaveModel().contains( this, p, o, l );
236     }
237
238     public boolean hasProperty(Property p, Object JavaDoc o) {
239         return mustHaveModel().contains( this, p, o );
240     }
241
242     public boolean hasProperty(Property p, RDFNode o) {
243         return mustHaveModel().contains( this, p, o );
244     }
245
246     public Resource removeProperties() {
247         mustHaveModel().remove( listProperties() );
248         return this;
249     }
250     
251     public Resource removeAll( Property p ) {
252         mustHaveModel().remove( listProperties( p ) );
253         return this;
254     }
255     
256     public Resource begin() {
257         mustHaveModel().begin();
258         return this;
259     }
260
261     public Resource abort() {
262         mustHaveModel().abort();
263         return this;
264     }
265
266     public Resource commit() {
267         mustHaveModel().commit();
268         return this;
269     }
270
271     public Model getModel() {
272         return (Model) getGraph();
273     }
274     
275     protected ModelCom getModelCom()
276         { return (ModelCom) getGraph(); }
277 }
278 /*
279  * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
280  * All rights reserved.
281  *
282  * Redistribution and use in source and binary forms, with or without
283  * modification, are permitted provided that the following conditions
284  * are met:
285  * 1. Redistributions of source code must retain the above copyright
286  * notice, this list of conditions and the following disclaimer.
287  * 2. Redistributions in binary form must reproduce the above copyright
288  * notice, this list of conditions and the following disclaimer in the
289  * documentation and/or other materials provided with the distribution.
290  * 3. The name of the author may not be used to endorse or promote products
291  * derived from this software without specific prior written permission.
292
293  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
294  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
295  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
296  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
297  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
298  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
299  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
300  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
301  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
302  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
303  *
304  * ResourceImpl.java
305  *
306  * Created on 03 August 2000, 13:45
307  */

308
Popular Tags