1 31 32 package com.hp.hpl.jena.rdf.model.impl; 33 34 import com.hp.hpl.jena.rdf.model.*; 35 import com.hp.hpl.jena.graph.*; 36 import com.hp.hpl.jena.shared.*; 37 38 import com.hp.hpl.jena.datatypes.DatatypeFormatException; 39 import com.hp.hpl.jena.datatypes.RDFDatatype; 40 import com.hp.hpl.jena.enhanced.*; 41 42 47 public class LiteralImpl extends EnhNode implements Literal { 48 49 final static public Implementation factory = new Implementation() { 50 public boolean canWrap( Node n, EnhGraph eg ) 51 { return n.isLiteral(); } 52 53 public EnhNode wrap(Node n, EnhGraph eg) { 54 if (!n.isLiteral()) throw new LiteralRequiredException( n ); 55 return new LiteralImpl(n,eg); 56 } 57 }; 58 59 public LiteralImpl( Node n, ModelCom m) { 60 super( n, m ); 61 } 62 63 public LiteralImpl( Node n, EnhGraph m ) { 64 super( n, m ); 65 } 66 67 public Object visitWith( RDFVisitor rv ) 68 { return rv.visitLiteral( this ); } 69 70 75 public RDFNode inModel( Model m ) 76 { return this; } 77 78 82 public LiteralImpl(boolean b) {this(String.valueOf(b));} 83 84 88 public LiteralImpl(long l) {this(String.valueOf(l));} 89 90 94 public LiteralImpl(char c) {this(String.valueOf(c));} 95 96 100 public LiteralImpl(float f) {this(String.valueOf(f));} 101 102 106 public LiteralImpl(double d) {this(String.valueOf(d));} 107 108 112 public LiteralImpl(String s) {this(s,"");} 113 114 118 public LiteralImpl(String s, String l) {this(s,l,false);} 119 120 124 public LiteralImpl(String s, boolean wellFormed) { 125 this(s,"",wellFormed); 126 } 127 128 132 public LiteralImpl(String s, String l, boolean wellFormed) { 133 this(s,l,wellFormed,null); 134 } 135 136 137 141 public LiteralImpl( String s, String l, boolean wellFormed, ModelCom m ) { 142 this(Node.createLiteral(s,l,wellFormed),m); 143 } 144 145 149 public LiteralImpl(Object o) {this( o.toString());} 150 151 152 public boolean isLiteral() { 153 return true; 154 } 155 156 public String toString() { 157 return asNode().toString( PrefixMapping.Standard, false ); 158 } 159 160 167 public Object getValue() { 168 return asNode().getLiteral().getValue(); 169 } 170 171 175 public RDFDatatype getDatatype() { 176 return asNode().getLiteral().getDatatype(); 177 } 178 179 183 public String getDatatypeURI() { 184 return asNode().getLiteral().getDatatypeURI(); 185 } 186 187 190 public boolean isPlainLiteral() { 191 return asNode().getLiteral().getDatatype() == null; 192 } 193 194 197 public String getLexicalForm() { 198 return asNode().getLiteral().getLexicalForm(); 199 } 200 201 public boolean getBoolean() { 202 Object value = asNode().getLiteral().getValue(); 203 if (isPlainLiteral()) { 204 if (value.equals("true")) { 206 return true; 207 } else if (value.equals("false")) { 208 return false; 209 } else { 210 throw new BadBooleanException( value.toString() ); 211 } 212 } else { 213 if (value instanceof Boolean ) { 215 return ((Boolean )value).booleanValue(); 216 } else { 217 throw new DatatypeFormatException(this.toString() + " is not a Boolean"); 218 } 219 } 220 } 221 222 public byte getByte() { 223 if (isPlainLiteral()) { 224 return Byte.parseByte(getLexicalForm()); 225 } else { 226 return asNumber(getValue()).byteValue(); 227 } 228 } 229 230 public short getShort() { 231 if (isPlainLiteral()) { 232 return Short.parseShort(getLexicalForm()); 233 } else { 234 return asNumber(getValue()).shortValue(); 235 } 236 } 237 238 public int getInt() { 239 if (isPlainLiteral()) { 240 return Integer.parseInt(getLexicalForm()); 241 } else { 242 return asNumber(getValue()).intValue(); 243 } 244 } 245 246 public long getLong() { 247 if (isPlainLiteral()) { 248 return Long.parseLong(getLexicalForm()); 249 } else { 250 return asNumber(getValue()).longValue(); 251 } 252 } 253 254 public char getChar() { 255 if (isPlainLiteral()) { 256 if (getString().length()==1) { 257 return (getString().charAt(0)); 258 } else { 259 throw new BadCharLiteralException( getString() ); 260 } 261 } else { 262 Object value = getValue(); 263 if (value instanceof Character ) { 264 return ((Character ) value).charValue(); 265 } else { 266 throw new DatatypeFormatException(value.toString() + " is not a Character"); 267 } 268 } 269 } 270 271 public float getFloat() { 272 if (isPlainLiteral()) { 273 return Float.parseFloat(getLexicalForm()); 274 } else { 275 return asNumber(getValue()).floatValue(); 276 } 277 } 278 279 public double getDouble() { 280 if (isPlainLiteral()) { 281 return Double.parseDouble(getLexicalForm()); 282 } else { 283 return asNumber(getValue()).doubleValue(); 284 } 285 } 286 287 public String getString() { 288 return asNode().getLiteral().getLexicalForm(); 289 } 290 291 public Object getObject(ObjectF f) { 292 if (isPlainLiteral()) { 293 try { 294 return f.createObject(getString()); 295 } catch (Exception e) { 296 throw new JenaException(e); 297 } 298 } else { 299 return getValue(); 300 } 301 } 302 303 public String getLanguage() { 304 return asNode().getLiteral().language(); 305 } 306 307 public boolean getWellFormed() { 308 return asNode().getLiteral().isXML(); 309 } 310 311 319 public boolean sameValueAs(Literal other) { 320 return asNode().sameValueAs(other.asNode()); 321 } 322 323 private Number asNumber(Object value) { 325 if (value instanceof Number ) { 326 return ((Number )value); 327 } else { 328 throw new DatatypeFormatException(value.toString() + " is not a Number"); 329 } 330 } 331 332 } 333 | Popular Tags |