1 31 36 37 package com.hp.hpl.jena.rdf.arp; 38 39 40 41 45 class ARPResource implements AResourceInternal { 46 private final static String RDFNS = 48 "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 49 private final static int RDFNS_LENGTH = RDFNS.length(); 50 54 55 private static AResourceInternal RDF_STATEMENT ; 57 static URIReference RDF_BAG; 58 static URIReference RDF_SEQ ; 59 static URIReference RDF_ALT ; 60 private static AResourceInternal RDF_TYPE ; 62 private static AResourceInternal RDF_SUBJECT ; 63 private static AResourceInternal RDF_PREDICATE ; 64 private static AResourceInternal RDF_OBJECT ; 65 static { 66 try { 67 RDF_STATEMENT = 68 new URIReference((RDFNS + "Statement").intern()); 69 RDF_BAG = 70 new URIReference((RDFNS + "Bag").intern()); 71 RDF_SEQ = 72 new URIReference((RDFNS + "Seq").intern()); 73 RDF_ALT = 74 new URIReference((RDFNS + "Alt").intern()); 75 RDF_TYPE = 78 new URIReference((RDFNS + "type").intern()); 79 RDF_SUBJECT = 80 new URIReference((RDFNS + "subject").intern()); 81 RDF_PREDICATE = 82 new URIReference((RDFNS + "predicate").intern()); 83 RDF_OBJECT = 84 new URIReference((RDFNS + "object").intern()); 85 } 86 catch ( MalformedURIException e ) { 87 System.err.println("Internal error: " + e.toString()); 88 e.printStackTrace(); 89 } 90 } 91 92 private XMLHandler arp; 93 94 String nodeID = null; 95 96 private String uri; static private int genIdCounter = 0; 101 final private int genId = genIdCounter++; 102 private int liCounter = 1; 103 static String dummy = "http://jena.hpl.hp.com/arp/not/a/real/uri/"; 104 static String nullDummy = "nullpointerexception://jena.hpl.hp.com/arp/"; 105 106 ARPResource(XMLHandler parent) { 107 arp = parent; 108 } 109 110 111 void setAbout(URIReference uri){ 112 setAbout(uri.getURI()); 113 } 114 115 void setAbout(String uri){ 116 this.uri = uri.startsWith( dummy ) ? uri.substring( dummy.length() ) : uri; 117 } 118 119 void setLiObject(Object v, String reify) { 120 setPredObject( rdf_n(liCounter++), v, reify(reify) ); 121 } 122 123 void setPredicateObject(AResourceInternal p, ARPString v) { 124 setPredObject(p,v,null); 125 } 126 127 void setPredicateObject(AResourceInternal p, Object v, String reify) { 128 setPredObject(p,v,reify(reify)); 129 } 130 131 AResourceInternal reify(String reify) { 132 ARPResource reification = null; 133 if ( reify != null ) { 134 reification = new ARPResource(arp); 135 reification.setAbout(reify); 136 } 137 return reification; 138 } 139 140 141 void setType(URIReference r) { 142 setPredObject(RDF_TYPE, r, null ); 143 } 144 145 private void setPredObject( AResourceInternal pred, Object obj, AResourceInternal reify ) { 146 triple( this, pred, obj ); 148 149 150 151 if ( reify != null ) { 152 tripleRes( reify, RDF_TYPE, RDF_STATEMENT ); 153 tripleRes( reify, RDF_SUBJECT, this ); 154 tripleRes( reify, RDF_PREDICATE, pred ); 155 triple( reify, RDF_OBJECT, obj ); 156 } 157 158 } 159 160 private void triple( AResourceInternal subj, AResourceInternal pred, Object obj ) { 161 int sw = 0; 162 if ( obj instanceof AResourceInternal ) { 163 sw += 1; 164 } 165 if ( obj instanceof ALiteral ) { 166 sw += 2; 167 } 168 switch (sw) { 169 case 1: 170 tripleRes(subj,pred,(AResourceInternal)obj); 171 break; 172 case 2: 173 tripleLit(subj,pred,(ALiteral)obj); 174 break; 175 case 0: 176 case 3: 177 default: 178 throw new RuntimeException ("Assertion failure: sw = " + sw); 179 } 180 } 181 182 private void tripleRes(final AResourceInternal s,final AResourceInternal p,final AResourceInternal o ) { 183 s.setHasBeenUsed(); 184 o.setHasBeenUsed(); 185 arp.getStatementHandler().statement(s,p,o); 186 187 } 188 189 private void tripleLit( AResourceInternal s, final AResourceInternal p, final ALiteral o ) { 190 s.setHasBeenUsed(); 191 arp.getStatementHandler().statement(s,p,o); 192 } 193 194 static private URIReference _rdf_n[] = new URIReference[0]; 195 196 static private URIReference rdf_n(int i) { 197 if (i>=_rdf_n.length) { 198 int newLength = (i+10)*3/2; 199 URIReference new_rdf_n[] = new URIReference[newLength]; 200 System.arraycopy(_rdf_n,0,new_rdf_n,0,_rdf_n.length); 201 for (int j=_rdf_n.length;j<newLength;j++) { 202 try { 203 new_rdf_n[j] = new URIReference(RDFNS+"_"+j); 204 } 205 catch (MalformedURIException disaster) { 206 throw new RuntimeException ("Impossible: RDF_nnn handling logic error."); 207 } 208 } 209 _rdf_n = new_rdf_n; 210 } 211 return _rdf_n[i]; 212 } 213 214 static int isRDF_N(AResourceInternal r) { 215 if ( !r.isAnonymous() ) { 216 String uri = r.getURI(); 217 if ( uri.length() > RDFNS_LENGTH && uri.charAt(RDFNS_LENGTH) == '_' ) { 218 try { 219 String sub = uri.substring(RDFNS_LENGTH+1); 220 int rslt = Integer.parseInt(sub); 221 if ( rslt>0 && uri.startsWith(RDFNS) ) { 222 return rslt; 223 } 224 } 225 catch (NumberFormatException e) { 226 } 227 } 228 } 229 return -1; 230 } 231 232 public boolean isAnonymous() { 234 return uri == null; 235 } 236 237 public String getAnonymousID() { 238 return nodeID==null 239 ? ( "A" + Integer.toString(genId) ) 240 : "U" + nodeID; 241 } 242 243 void setNodeId(String s) { 244 nodeID = s; 245 } 246 247 public String getURI() { 248 return this.uri; 249 } 250 251 public String toString() { 252 return uri==null?("_:"+getAnonymousID()):("<"+uri+">"); 253 } 254 255 public int hashCode() { 256 if ( uri==null && nodeID==null) 257 return genId; 258 else if ( uri != null ) 259 return uri.hashCode(); 260 else 261 return nodeID.hashCode(); 262 } 263 264 public boolean equals(Object o) { 265 if ( o == null || !(o instanceof AResourceInternal)) 266 return false; 267 if ( this == o) 268 return true; 269 AResourceInternal a=(AResourceInternal)o; 270 if ( uri != null ) 271 return (!a.isAnonymous()) && uri.equals(a.getURI()); 272 ARPResource aa = (ARPResource)a; 273 return nodeID != null && nodeID.equals(aa.nodeID); 274 } 275 276 private Object userData; 277 278 public Object getUserData() { 279 if ( uri != null ) 280 throw new IllegalStateException ("User data only supported on blank nodes"); 281 if ( nodeID != null ) 282 return arp.getUserData(nodeID); 283 else 284 return userData; 285 } 286 287 public void setUserData(Object d) { 288 if ( uri != null ) 289 throw new IllegalStateException ("User data only supported on blank nodes"); 290 if ( nodeID == null ) 291 userData = d; 292 else 293 arp.setUserData(nodeID,d); 294 } 295 296 299 public boolean hasNodeID() { 300 return nodeID!=null; 301 } 302 private boolean used = false; 303 304 307 public void setHasBeenUsed() { 308 used = true; 309 } 310 311 312 315 public boolean getHasBeenUsed() { 316 return used; 317 } 318 319 } 320 | Popular Tags |