1 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 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 48 public ResourceImpl( Node n, ModelCom m ) { 49 super( n, m ); 50 } 51 52 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 uri ) { 68 super( fresh( uri ), null ); 69 } 70 71 public ResourceImpl(String nameSpace, String 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 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 nameSpace, String localName, ModelCom m) { 92 this( Node.createURI( nameSpace + localName ), m ); 93 } 94 95 public Object 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 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 getURI() { 117 return isAnon() ? null : node.getURI(); 118 } 119 120 public String getNameSpace() { 121 return isAnon() ? null : node.getNameSpace(); 122 } 123 124 public String getLocalName() { 125 return isAnon() ? null : node.getLocalName(); 126 } 127 128 public boolean hasURI( String uri ) { 129 return node.hasURI( uri ); 130 } 131 132 public String 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 o) { 186 mustHaveModel().add( this, p, o ); 187 return this; 188 } 189 190 public Resource addProperty(Property p, String o, String l) 191 { 192 mustHaveModel().add( this, p, o, l ); 193 return this; 194 } 195 196 public Resource addProperty(Property p, Object 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 o) { 231 return mustHaveModel().contains( this, p, o ); 232 } 233 234 public boolean hasProperty(Property p, String o, String l) { 235 return mustHaveModel().contains( this, p, o, l ); 236 } 237 238 public boolean hasProperty(Property p, Object 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 308 | Popular Tags |