1 6 7 package com.hp.hpl.jena.graph.impl; 8 9 import com.hp.hpl.jena.datatypes.*; 10 import com.hp.hpl.jena.datatypes.xsd.*; 11 import com.hp.hpl.jena.datatypes.xsd.impl.*; 12 import com.hp.hpl.jena.shared.impl.JenaParameters; 13 14 21 final public class LiteralLabel { 22 23 26 30 private String lexicalForm; 31 32 38 private Object value; 39 40 44 final private RDFDatatype dtype; 45 46 51 final private String lang; 52 53 59 private boolean wellformed = true; 60 61 64 74 public LiteralLabel(String lex, String lang, RDFDatatype dtype) 75 throws DatatypeFormatException { 76 lexicalForm = lex; 77 this.dtype = dtype; 78 this.lang = (lang == null ? "" : lang); 79 if (dtype == null) { 80 value = lex; 81 } else { 82 setValue(lex); 83 } 84 } 85 86 91 public LiteralLabel(String lex, String lang) { 92 this(lex, lang, null); 93 } 94 95 103 public LiteralLabel(Object value, String lang, RDFDatatype dtype) throws DatatypeFormatException { 104 this.dtype = dtype; 105 this.lang = (lang == null ? "" : lang); 106 if (value instanceof String ) { 107 String lex = (String )value; 108 lexicalForm = lex; 109 if (dtype == null) { 110 value = lex; 111 } else { 112 setValue(lex); 113 } 114 } else { 115 this.value = value; 116 } 117 } 118 119 129 public LiteralLabel( 130 String lex, 131 Object value, 132 String lang, 133 RDFDatatype dtype) { 134 this(value, lang, dtype); 135 this.lexicalForm = lex; 136 } 137 138 144 public LiteralLabel(Object value) { 145 this(value, "", TypeMapper.getInstance().getTypeByValue(value)); 146 } 147 148 154 public LiteralLabel(String s, String lg, boolean xml) { 155 this.lexicalForm = s; 156 this.lang = (lg == null ? "" : lg); 157 if (xml) { 158 this.dtype = XMLLiteralType.theXMLLiteralType; 160 value = s; 161 wellformed = true; 162 } else { 163 this.value = s; 165 this.dtype = null; 166 } 167 } 168 169 175 private void setValue(String lex) throws DatatypeFormatException { 176 try { 177 value = dtype.parse(lex); 178 wellformed = true; 179 } catch (DatatypeFormatException e) { 180 if (JenaParameters.enableEagerLiteralValidation) { 181 e.fillInStackTrace(); 182 throw e; 183 } else { 184 wellformed = false; 185 } 186 } 187 } 188 189 192 196 public boolean isXML() { 197 return dtype == XMLLiteralType.theXMLLiteralType && this.wellformed; 198 } 199 200 204 public boolean isWellFormed() { 205 return dtype != null && this.wellformed; 206 } 207 208 212 public String toString(boolean quoting) { 213 StringBuffer b = new StringBuffer (); 214 if (quoting) b.append('"'); 215 b.append(getLexicalForm()); 216 if (quoting) b.append('"'); 217 if (lang != null && !lang.equals( "" )) b.append( "@" ).append(lang); 218 if (dtype != null) b.append( "^^" ).append(dtype.getURI()); 219 return b.toString(); 220 } 221 222 public String toString() { 223 return toString(false); 224 } 225 226 233 public String getLexicalForm() { 234 if (lexicalForm == null) 235 lexicalForm = 236 (dtype == null ? value.toString() : dtype.unparse(value)); 237 return lexicalForm; 238 } 239 240 247 public String language() { 248 return lang; 249 } 250 251 256 public Object getValue() throws DatatypeFormatException { 257 if (wellformed) { 258 return value; 259 } else { 260 throw new DatatypeFormatException( 261 lexicalForm, 262 dtype, 263 " in getValue()"); 264 } 265 } 266 267 272 public RDFDatatype getDatatype() { 273 return dtype; 274 } 275 276 280 public String getDatatypeURI() { 281 if (dtype == null) 282 return null; 283 return dtype.getURI(); 284 } 285 286 291 public boolean equals(Object other) { 292 if (other == null || !(other instanceof LiteralLabel)) { 293 return false; 294 } 295 LiteralLabel otherLiteral = (LiteralLabel) other; 296 boolean typeEqual = 297 (dtype == null 298 ? otherLiteral.dtype == null 299 : dtype.equals(otherLiteral.dtype)); 300 boolean langEqual = 301 (dtype == null ? lang.equalsIgnoreCase(otherLiteral.lang) : true); 302 return typeEqual 303 && langEqual 304 && getLexicalForm().equals(otherLiteral.getLexicalForm()); 305 } 313 314 322 public boolean sameValueAs(LiteralLabel other) { 323 if (other == null) 324 return false; 325 if (!wellformed || !other.wellformed) { 326 if (!other.wellformed) { 327 return lexicalForm.equals(other.lexicalForm) 330 && lang.equalsIgnoreCase(other.lang); 331 } else { 332 return false; 333 } 334 } 335 if (dtype == null) { 336 if (other.dtype == null 338 || (JenaParameters.enablePlainLiteralSameAsString 339 && other.dtype.equals(XSDDatatype.XSDstring))) { 340 return lexicalForm.equals(other.lexicalForm) 341 && lang.equalsIgnoreCase(other.lang); 342 } else { 343 return false; 344 } 345 } else { 346 return dtype.isEqual(this, other); 348 } 349 } 350 351 356 public int hashCode() { 357 return (wellformed ? value : getLexicalForm()).hashCode(); 358 } 359 360 } 361 362 391 | Popular Tags |