001 /*
002 * Literal.java
003 *
004 * Niraj Aswani, 09/March/07
005 *
006 * $Id: Literal.html,v 1.0 2007/03/09 16:13:01 niraj Exp $
007 */
008 package gate.creole.ontology;
009
010 import java.util.Locale;
011
012 /**
013 * Literal represents a single value or a value with language used for
014 * annotation properties, or a value with datatype used for the datatype
015 * properties.
016 *
017 * @author Niraj Aswani
018 */
019 public class Literal {
020 /**
021 * The actual value of the literal
022 */
023 private String value;
024
025 /**
026 * Specified language for the literal.
027 */
028 private Locale language;
029
030 /**
031 * Assigned Datatype to this instance of literal
032 */
033 private DataType dataType;
034
035 /**
036 * Constructor
037 *
038 * @param value
039 */
040 public Literal(String value) {
041 this.value = value;
042 this.language = OConstants.ENGLISH;
043 this.dataType = DataType.getStringDataType();
044 }
045
046 /**
047 * Constructor
048 *
049 * @param value
050 * @param language
051 */
052 public Literal(String value, Locale language) {
053 this.value = value;
054 this.language = language;
055 this.dataType = DataType.getStringDataType();
056 }
057
058 /**
059 * Constructor
060 *
061 * @param value
062 * @param dataType
063 * @throws InvalidValueException
064 */
065 public Literal(String value, DataType dataType) throws InvalidValueException {
066 this.value = value;
067 this.language = OConstants.ENGLISH;
068 this.dataType = dataType;
069 // lets check if the provided value is valid for the supplied
070 // dataType
071 if(!dataType.isValidValue(this.value)) {
072 throw new InvalidValueException("The value :\"" + this.value
073 + "\" is not compatible with the dataType \""
074 + dataType.getXmlSchemaURIString() + "\"");
075 }
076 }
077
078 /**
079 * Gets the assigned datatype. This may return null if user did not
080 * use the Literal(String, Datatype) constructor to instantiate the
081 * instance.
082 *
083 * @return
084 */
085 public DataType getDataType() {
086 return dataType;
087 }
088
089 /**
090 * Returns the value associated with this instance of literal.
091 *
092 * @return
093 */
094 public String getValue() {
095 return value;
096 }
097
098 /**
099 * Returns the language associated with this literal. This may return
100 * null if use did not use the Literal(String, String) constructor to
101 * instantiate the instance.
102 *
103 * @return
104 */
105 public Locale getLanguage() {
106 return language;
107 }
108
109 public String toString() {
110 return value;
111 }
112
113
114 public String toTurtle() {
115 // TODO: do the escaping correctly!
116 String value = this.value;
117 value = value.replace("\"", "\\\"");
118 value = "\""+value+"\"";
119 if(dataType.isStringDataType()) {
120 if(language != null) {
121 value = value+"@"+language;
122 } else {
123 value = value+"^^<" + dataType.getXmlSchemaURIString() + ">";
124 }
125 } else {
126 value = value+"^^<" + dataType.getXmlSchemaURIString() + ">";
127 }
128 return value;
129 }
130
131 @Override
132 public int hashCode() {
133 return value.hashCode();
134 }
135
136 @Override
137 public boolean equals(Object obj) {
138 if (obj == null) {
139 return false;
140 }
141 if (getClass() != obj.getClass()) {
142 return false;
143 }
144 final Literal other = (Literal) obj;
145 // if both are Literals, they are only equal if the dataTypes are the same
146 // and if the languages are the same and if the values are the same
147 if ((this.value == null) && (other.value == null)) {
148 return true;
149 }
150 if(!this.dataType.equals(other.dataType) ||
151 !this.language.equals(other.language) ||
152 !this.value.equals(other.value)) {
153 return false;
154 }
155 return true;
156 }
157
158 }
|