1 5 6 package com.hp.hpl.jena.rdql.parser; 7 8 9 import com.hp.hpl.jena.rdf.model.*; 10 import com.hp.hpl.jena.rdql.*; 11 import com.hp.hpl.jena.graph.query.IndexValues ; 12 import com.hp.hpl.jena.graph.impl.LiteralLabel; 13 14 import java.io.PrintWriter ; 15 16 20 public class ParsedLiteral extends ExprNode implements Expr, NodeValue 21 { 22 static Model model = ModelFactory.createDefaultModel() ; 24 25 protected boolean isSet = false ; 26 27 private boolean isInt = false ; 28 private boolean isBoolean = false ; 29 private boolean isDouble = false ; 30 private boolean isURI = false ; 31 private boolean isString = false ; 32 private boolean isGraphNode = false ; 33 34 37 private long valInt ; 38 private boolean valBoolean ; 39 private double valDouble ; 40 private String valString ; 41 private String valURI ; 42 private com.hp.hpl.jena.graph.Node valGraphNode ; 43 44 47 ParsedLiteral(int id) { super(id); } 49 50 ParsedLiteral(RDQLParser p, int id) { super(p, id); } 51 52 public ParsedLiteral() { super(-1) ; unset() ; } 53 54 protected ParsedLiteral(NodeValue v) 56 { 57 super(-1) ; 58 if ( v.isBoolean() ) 59 { 60 _setBoolean(v.getBoolean()) ; 61 return ; 62 } 63 64 if ( v.isInt() ) 65 { 66 _setInt(v.getInt()) ; 67 return ; 68 } 69 70 if ( v.isDouble() ) 71 { 72 _setDouble(v.getDouble()) ; 73 return ; 74 } 75 76 if ( v.isURI() ) 77 { 78 _setURI(v.getURI()) ; 79 return ; 80 } 81 82 if ( v.isNode() ) 83 { 84 _setNode(v.getNode()) ; 85 return ; 86 } 87 88 if ( v.isString() ) 89 { 90 _setString(v.getString()) ; 91 return ; 92 } 93 94 } 95 96 97 98 protected void unset() 99 { 100 isSet = false ; 101 valString = null ; 102 valGraphNode = null ; 103 valInt = 0 ; 104 valBoolean = false ; 105 valDouble = 0 ; 106 valURI = null ; 107 valGraphNode = null ; 108 109 isInt = false ; 110 isBoolean = false ; 111 isDouble = false ; 112 isURI = false ; 113 isString = false ; 114 } 115 116 public NodeValue eval(Query q, IndexValues env) 117 { 118 if ( ! isSet ) 119 throw new EvalFailureException("Literal value not set") ; 120 121 return this ; 122 } 123 124 public boolean isSet() { return isSet ; } 125 126 public boolean isNumber() { forceNumber() ; return isSet && (isInt || isDouble) ; } 127 public boolean isInt() { forceInt() ; return isSet && isInt ; } 128 public boolean isDouble() { forceDouble() ; return isSet && isDouble ; } 129 public boolean isBoolean() { return isSet && isBoolean ; } 130 public boolean isString() { return isSet && isString ; } 131 public boolean isURI() { return isSet && isURI ; } 132 public boolean isNode() { return isSet && isGraphNode ; } 133 134 135 protected void _setInt(long i) { unset() ; isSet = true ; isInt = true ; valInt = i ; } 136 protected void _setDouble(double d) { unset() ; isSet = true ; isDouble = true ; valDouble = d ; } 137 protected void _setBoolean(boolean b) { unset() ; isSet = true ; isBoolean = true ; valBoolean = b ; } 138 protected void _setString(String s) { unset() ; isSet = true ; isString = true ; valString = s ; } 139 protected void _setURI(String uri) { unset() ; isSet = true ; isURI = true ; isString = true ; valURI = uri ; valString = uri ; } 140 141 protected void _setNode(com.hp.hpl.jena.graph.Node n) 142 { 143 unset(); 144 isSet = true; 145 isGraphNode = true; 146 valGraphNode = n ; 147 isString = false ; 148 valString = null ; 149 150 if ( n.isLiteral() ) 151 valString = n.getLiteral().getLexicalForm() ; 152 if ( n.isURI() ) 153 { 154 valString = n.getURI() ; 155 valURI = n.getURI() ; 156 isURI = true ; 157 } 158 if ( n.isBlank() ) 159 valString = n.getBlankNodeId().toString() ; 160 161 if ( valString != null ) 162 isString = true ; 163 } 164 165 private void forceInt() 166 { 167 if ( ! isSet || isInt || ! isString ) return ; 168 try { 169 valInt = Long.parseLong(valString) ; 170 isInt = true ; 171 isDouble = true ; 172 valDouble = valInt ; 173 } catch (NumberFormatException e) { return ; } 174 } 175 176 private void forceDouble() 177 { 178 if ( ! isSet || isDouble || ! isString ) return ; 179 try { 180 valDouble = Double.parseDouble(valString) ; 181 isDouble = true ; 182 } catch (NumberFormatException e) { return ; } 183 } 184 185 private void forceNumber() 186 { 187 if ( ! isSet || isInt || isDouble || ! isString ) 188 return ; 189 190 forceInt() ; 191 if ( ! isInt ) 192 forceDouble() ; 193 } 194 195 196 public long getInt() 197 { 198 if ( ! isSet || ! isInt ) throw new ValueException("Not an int: "+this) ; 199 return valInt ; 200 } 201 202 public double getDouble() 203 { 204 if ( ! isSet || ! ( isDouble || isInt ) ) throw new ValueException("Not a double: "+this) ; 205 if ( isInt ) 206 return valInt ; 207 return valDouble ; 208 } 209 210 public boolean getBoolean() 211 { 212 if ( ! isSet || ! isBoolean ) throw new ValueException("Not a boolean: "+this) ; 213 return valBoolean ; 214 } 215 216 public String getString() 217 { 218 if ( ! isSet || ! isString ) throw new ValueException("Not a string: "+this) ; 219 return valString ; 220 } 221 222 public String getURI() 223 { 224 if ( ! isSet || ! isURI ) throw new ValueException("Not a URI: "+this) ; 225 return valURI ; 226 } 227 228 public com.hp.hpl.jena.graph.Node getNode() 229 { 230 if ( ! isSet ) throw new ValueException("Not a graph node: "+this) ; 231 return valGraphNode ; 232 } 233 234 236 public boolean isConstant() { return true; } 238 public Object getValue() { return getNode() ; } 240 241 public String asQuotedString() 243 { 244 if ( ! isSet ) return "literal:unset" ; 245 if ( isInt ) return Long.toString(valInt) ; 246 if ( isDouble ) return Double.toString(valDouble) ; 247 if ( isBoolean ) return (valBoolean?"true":"false") ; 248 249 if ( isGraphNode ) 250 { 251 if ( valGraphNode.isLiteral() ) 252 { 253 StringBuffer sBuff = new StringBuffer () ; 254 255 LiteralLabel l = valGraphNode.getLiteral() ; 256 sBuff.append('"') ; 257 sBuff.append(l.getLexicalForm()) ; 258 sBuff.append('"') ; 259 260 String dt = l.getDatatypeURI() ; 261 if ( dt != null ) { sBuff.append("^^") ; sBuff.append(dt) ; } 262 263 String lang = l.language() ; 264 if ( lang != null ) { sBuff.append("@") ; sBuff.append(lang) ; } 265 } 266 267 if ( valGraphNode.isURI() ) 268 valString = "<"+valGraphNode.getURI()+">" ; 269 if ( valGraphNode.isBlank() ) 270 valString = valGraphNode.getBlankNodeId().toString() ; 271 } 272 273 if ( isURI ) return "<"+valURI+">" ; 275 if ( isString ) return "\""+valString+"\"" ; 277 278 return "literal:unknown" ; 279 } 280 281 public String asUnquotedString() 283 { 284 if ( ! isSet ) return "literal:unset" ; 285 if ( isInt ) return Long.toString(valInt) ; 286 if ( isDouble ) return Double.toString(valDouble) ; 287 if ( isBoolean ) return (valBoolean?"true":"false") ; 288 if ( isURI ) return valURI ; 289 if ( isString ) return valString ; 290 if ( isGraphNode ) return valGraphNode.toString() ; 291 292 return "literal:unknown" ; 293 } 294 295 public String asInfixString() { return asQuotedString() ; } 296 297 public String asPrefixString() 298 { 299 if ( ! isSet ) return "literal:unset" ; 300 if ( isInt ) return "int:"+Long.toString(valInt) ; 301 if ( isDouble ) return "double:"+Double.toString(valDouble) ; 302 if ( isBoolean ) return "boolean:"+(valBoolean?"true":"false") ; 303 if ( isURI ) return "URI:"+valURI ; 304 if ( isString ) return "string:"+valString ; 305 if ( isGraphNode) return "node:"+valGraphNode ; 306 307 return "literal:unknown" ; 308 } 309 310 public void print(PrintWriter pw, int level) 312 { 313 QueryPrintUtils.indent(pw, level) ; 314 pw.println(this.asPrefixString()) ; 315 } 316 317 public String valueString() { return asUnquotedString() ; } 319 320 public String toString() 322 { 323 return asUnquotedString() ; 324 } 325 326 328 public static ParsedLiteral makeString(String s) 329 { 330 ParsedLiteral l = new ParsedLiteral(0) ; 331 l._setString(s) ; 332 return l ; 333 } 334 335 public static Q_URI makeURI(String s) { return Q_URI.makeURI(s) ; } 337 } 338 339 365 | Popular Tags |