/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jena3.graph.impl; import jena3.graph.NodeVisitor ; import org.apache.jena.atlas.lib.NotImplemented ; import com.hp.hpl.jena.datatypes.RDFDatatype ; import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ; public class NodeLiteral extends NodeImpl { private static final Object badLexicalForm = new Object() ; private static final Object unsetValue = new Object() ; /** * The lexical form of the literal, may be null if the literal was * created programatically and has not yet been serialized */ final private String lexicalForm; /** * The value form of the literal. It will be null only if the value * has not been parsed or if it is an illegal value. * For plain literals and xsd:string literals * the value is the same as the lexicalForm. */ private Object value = unsetValue ; // XXX Delay evaluation. /** * The type of the literal. A null type indicates a classic "plain" literal. * The type of a literal is fixed when it is created. */ // XXX Needs to define hashCode and equals. final private RDFDatatype dtype; /** * The xml:lang tag. For xsd literals this is ignored and not part of * equality. For plain literals it is not ignored. The lang of a * literal is fixed when it is created. */ final private String lang; // /** // * Indicates whether this is a legal literal. The working groups requires // * ill-formed literals to be treated as syntactically correct so instead // * of only storing well-formed literals we hack around it this way. // * N.B. This applies to any literal, not just XML well-formed literals. // */ // private boolean wellformed = true; // // /** // * keeps the message provided by the DatatypeFormatException // * if parsing failed for delayed exception thrown in getValue() // */ // private String exceptionMsg = null; // Suggested by Andreas Langegger public NodeLiteral(String lexicalForm, RDFDatatype datatype, String lang) { this.lexicalForm = lexicalForm ; if ( datatype == null ) datatype = XSDDatatype.XSDstring ; if ( lang != null ) // XXX Check datatype = null ; //XSDDatatype.XSDlangString ; this.dtype = datatype ; if ( lang == null ) lang = "" ; this.lang = lang ; } /** * Answer the literal value of a literal node, or throw an * UnsupportedOperationException if it's not a literal node */ @Override public NodeLiteral getLiteral() { throw new UnsupportedOperationException(this + " is not a literal node") ; } /** * Answer the value of this node's literal value, if it is a literal; * otherwise die horribly. */ @Override public Object getLiteralValue() { throw new NotImplemented() ; //return value ; } /** * Answer the lexical form of this node's literal value, if it is a literal; * otherwise die horribly. */ @Override public String getLiteralLexicalForm() { return lexicalForm ; } /** * Answer the language of this node's literal value, if it is a literal; * otherwise die horribly. */ @Override public String getLiteralLanguage() { return lang ; } /** * Answer the data-type URI of this node's literal value. */ @Override public String getLiteralDatatypeURI() { return dtype.getURI() ; } /** * Answer the RDF datatype object of this node's literal value. */ @Override public RDFDatatype getLiteralDatatype() { return dtype ; } @Override public Object visit(NodeVisitor v) { throw new NotImplemented() ; } @Override public int hashCode() { final int prime = 97 ; int result = 1 ; result = prime * result + ((dtype == null) ? 0 : dtype.hashCode()) ; result = prime * result + ((lang == null) ? 0 : lang.hashCode()) ; result = prime * result + ((lexicalForm == null) ? 0 : lexicalForm.hashCode()) ; return result ; } @Override public boolean equals(Object obj) { if ( this == obj ) return true ; if ( obj == null ) return false ; if ( !(obj instanceof NodeLiteral) ) return false ; NodeLiteral other = (NodeLiteral)obj ; if ( dtype == null ) { if ( other.dtype != null ) return false ; } else if ( !dtype.equals(other.dtype) ) return false ; if ( lang == null ) { if ( other.lang != null ) return false ; } else if ( !lang.equals(other.lang) ) return false ; if ( lexicalForm == null ) { if ( other.lexicalForm != null ) return false ; } else if ( !lexicalForm.equals(other.lexicalForm) ) return false ; return true ; } @Override public String toString() { // XXX String s = "\""+lexicalForm+"\"" ; if ( lang == null ) { if ( ! XSDDatatype.XSDstring.equals(dtype) ) s = s+"^^<"+dtype.getURI()+">" ; } else s = s+"@"+lang ; return s ; } }