1 6 7 package com.hp.hpl.jena.graph; 8 9 import com.hp.hpl.jena.rdf.model.AnonId; 10 11 import com.hp.hpl.jena.datatypes.*; 12 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; 13 import com.hp.hpl.jena.graph.impl.*; 14 import com.hp.hpl.jena.shared.*; 15 16 import org.apache.commons.logging.*; 17 18 26 27 public abstract class Node { 28 29 final protected Object label; 30 static final int THRESHOLD = 10000; 31 32 static final NodeCache present = new NodeCache(); 33 35 static final Log log = LogFactory.getLog( Node.class ); 36 37 41 public static final Node ANY = new Node_ANY(); 42 43 static final String RDFprefix = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 44 45 62 public static Node create( String x ) 63 { return create( PrefixMapping.Extended, x ); } 64 65 72 public static Node create( PrefixMapping pm, String x ) 73 { 74 if (x.equals( "" )) 75 throw new JenaException( "Node.create does not accept an empty string as argument" ); 76 char first = x.charAt( 0 ); 77 if (first == '\'' || first == '\"') 78 return Node.createLiteral( newString( pm, first, x ) ); 79 if (Character.isDigit( first )) 80 return Node.createLiteral( new LiteralLabel( x, "", XSDDatatype.XSDinteger ) ); 81 if (first == '_') 82 return Node.createAnon( new AnonId( x ) ); 83 if (x.equals( "??" )) 84 return Node.ANY; 85 if (first == '?') 86 return Node.createVariable( x.substring( 1 ) ); 87 if (first == '&') 88 return Node.createURI( "q:" + x.substring( 1 ) ); 89 int colon = x.indexOf( ':' ); 90 String d = pm.getNsPrefixURI( "" ); 91 return colon < 0 92 ? Node.createURI( (d == null ? "eh:/" : d) + x ) 93 : Node.createURI( pm.expandPrefix( x ) ) 94 ; 95 } 96 97 private static RDFDatatype getType( String s ) 98 { return TypeMapper.getInstance().getSafeTypeByName( s ); } 99 100 private static LiteralLabel literal( PrefixMapping pm, String spelling, String langOrType ) 101 { 102 String content = unEscape( spelling ); 103 int colon = langOrType.indexOf( ':' ); 104 return colon < 0 105 ? new LiteralLabel( content, langOrType, false ) 106 : new LiteralLabel( content, "", getType( pm.expandPrefix( langOrType ) ) ) 107 ; 108 } 109 110 private static String unEscape( String spelling ) 111 { 112 if (spelling.indexOf( '\\' ) < 0) return spelling; 113 StringBuffer result = new StringBuffer ( spelling.length() ); 114 int start = 0; 115 while (true) 116 { 117 int b = spelling.indexOf( '\\', start ); 118 if (b < 0) break; 119 result.append( spelling.substring( start, b ) ); 120 result.append( unEscape( spelling.charAt( b + 1 ) ) ); 121 start = b + 2; 122 } 123 result.append( spelling.substring( start ) ); 124 return result.toString(); 125 } 126 127 private static char unEscape( char ch ) 128 { 129 switch (ch) 130 { 131 case '\\': 132 case '\"': 133 case '\'': return ch; 134 case 'n': return '\n'; 135 case 's': return ' '; 136 case 't': return '\t'; 137 default: return 'Z'; 138 } 139 } 140 141 private static LiteralLabel newString( PrefixMapping pm, char quote, String nodeString ) 142 { 143 int close = nodeString.lastIndexOf( quote ); 144 return literal( pm, nodeString.substring( 1, close ), nodeString.substring( close + 1 ) ); 145 } 146 147 148 public static Node createAnon( AnonId id ) 149 { return create( makeAnon, id ); } 150 151 152 public static Node createLiteral( LiteralLabel lit ) 153 { return create( makeLiteral, lit ); } 154 155 156 public static Node createURI( String uri ) 157 { return create( makeURI, uri ); } 158 159 160 public static Node createAnon() 161 { return createAnon( new AnonId() ); } 162 163 164 public static Node createVariable( String name ) 165 { return create( makeVariable, Node_Variable.variable( name ) ); } 166 167 public static Node createLiteral( String value ) 168 { return createLiteral( value, "", false ); } 169 170 178 public static Node createLiteral( String lit, String lang, boolean isXml ) 179 { 180 if (lit == null) throw new NullPointerException ( "null for literals has been illegal since Jena 2.0" ); 181 return createLiteral( new LiteralLabel( lit, lang, isXml ) ); 187 } 188 189 199 public static Node createLiteral( String lex, String lang, RDFDatatype dtype ) 200 throws DatatypeFormatException 201 { return createLiteral( new LiteralLabel( lex, lang, dtype ) ); } 202 203 public static Node createUncachedLiteral( Object value, String lang, RDFDatatype dtype ) 204 throws DatatypeFormatException 205 { return new Node_Literal( new LiteralLabel( value, lang, dtype ) ); } 206 207 214 public abstract Object visitWith( NodeVisitor v ); 215 216 219 public abstract boolean isConcrete(); 220 221 222 public boolean isLiteral() 223 { return false; } 224 225 226 public boolean isBlank() 227 { return false; } 228 229 230 public boolean isURI() 231 { return false; } 232 233 234 public boolean isVariable() 235 { return false; } 236 237 238 public AnonId getBlankNodeId() 239 { throw new UnsupportedOperationException ( this + " is not a blank node" ); } 240 241 242 public LiteralLabel getLiteral() 243 { throw new UnsupportedOperationException ( this + " is not a literal node" ); } 244 245 246 public String getURI() 247 { throw new UnsupportedOperationException ( this + " is not a URI node" ); } 248 249 250 public String getNameSpace() 251 { throw new UnsupportedOperationException ( this + " is not a URI node" ); } 252 253 254 public String getLocalName() 255 { throw new UnsupportedOperationException ( this + " is not a URI node" ); } 256 257 258 public String getName() 259 { throw new UnsupportedOperationException ( "this (" + this.getClass() + ") is not a variable node" ); } 260 261 262 public boolean hasURI( String uri ) 263 { return false; } 264 265 266 static abstract class NodeMaker { abstract Node construct( Object x ); } 267 268 static final NodeMaker makeAnon = new NodeMaker() 269 { Node construct( Object x ) { return new Node_Blank( x ); } }; 270 271 static final NodeMaker makeLiteral = new NodeMaker() 272 { Node construct( Object x ) { return new Node_Literal( x ); } }; 273 274 static final NodeMaker makeURI = new NodeMaker() 275 { Node construct( Object x ) { return new Node_URI( x ); } }; 276 277 static final NodeMaker makeVariable = new NodeMaker() 278 { Node construct( Object x ) { return new Node_Variable( x ); } }; 279 280 285 public static final Node NULL = new Node_NULL(); 286 287 290 291 Node( Object label ) 292 { this.label = label; } 293 294 static private boolean caching = true; 295 296 301 public static void cache( boolean wantCache ) 302 { 303 if (wantCache == false) present.clear(); 304 caching = wantCache; 305 } 306 307 314 public static synchronized Node create( NodeMaker maker, Object label ) 315 { 316 if (label == null) throw new JenaException( "Node.make: null label" ); 317 Node node = (Node) present.get( label ); 318 return node == null ? cacheNewNode( label, maker.construct( label ) ) : node; 319 } 320 321 325 private static Node cacheNewNode( Object label, Node n ) 326 { 327 if (present.size() > THRESHOLD) { present.clear(); } 328 if (caching) present.put( label, n ); 329 return n; 330 } 331 332 335 public abstract boolean equals(Object o); 336 337 347 public boolean sameValueAs(Object o) { 348 return equals(o); 349 } 350 351 public int hashCode() { 352 return label.hashCode(); 353 } 354 355 363 public boolean matches( Node other ) 364 { return equals( other ); } 365 366 371 public String toString() 372 { return toString( null ); } 373 374 378 public String toString( boolean quoting ) 379 { return toString( null, quoting ); } 380 381 385 public String toString( PrefixMapping pm ) 386 { return toString( pm, true ); } 387 388 392 public String toString( PrefixMapping pm, boolean quoting ) 393 { return label.toString(); } 394 395 } 396 397 426 | Popular Tags |