KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > LiteralImpl


1 /*
2  * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * LiteralImpl.java
28  *
29  * Created on 03 August 2000, 14:42
30  */

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 /** An implementation of Literal.
43  *
44  * @author bwm and der
45  * @version Release='$Name: $' Revision='$Revision: 1.18 $' Date='$Date: 2005/02/21 12:14:32 $'
46  */

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 JavaDoc visitWith( RDFVisitor rv )
68         { return rv.visitLiteral( this ); }
69         
70     /**
71         Literals are not in any particular model, and so inModel can return this.
72         @param m a model to move the literal into
73         @return this
74     */

75     public RDFNode inModel( Model m )
76         { return this; }
77
78     /**
79      *@deprecated Please use the createLiteral methods on Model.
80      *Model implementors should use Literal instructors which include the Model.
81      */

82     public LiteralImpl(boolean b) {this(String.valueOf(b));}
83     
84     /**
85      *@deprecated Please use the createLiteral methods on Model.
86      *Model implementors should use Literal instructors which include the Model.
87      */

88     public LiteralImpl(long l) {this(String.valueOf(l));}
89     
90     /**
91      *@deprecated Please use the createLiteral methods on Model.
92      *Model implementors should use Literal instructors which include the Model.
93      */

94     public LiteralImpl(char c) {this(String.valueOf(c));}
95     
96     /**
97      *@deprecated Please use the createLiteral methods on Model.
98      *Model implementors should use Literal instructors which include the Model.
99      */

100     public LiteralImpl(float f) {this(String.valueOf(f));}
101     
102     /**
103      *@deprecated Please use the createLiteral methods on Model.
104      *Model implementors should use Literal instructors which include the Model.
105      */

106     public LiteralImpl(double d) {this(String.valueOf(d));}
107     
108     /**
109      *@deprecated Please use the createLiteral methods on Model.
110      *Model implementors should use Literal instructors which include the Model.
111      */

112     public LiteralImpl(String JavaDoc s) {this(s,"");}
113     
114     /**
115      *@deprecated Please use the createLiteral methods on Model.
116      *Model implementors should use Literal instructors which include the Model.
117      */

118     public LiteralImpl(String JavaDoc s, String JavaDoc l) {this(s,l,false);}
119     
120     /**
121      *@deprecated Please use the createLiteral methods on Model.
122      *Model implementors should use Literal instructors which include the Model.
123      */

124     public LiteralImpl(String JavaDoc s, boolean wellFormed) {
125         this(s,"",wellFormed);
126     }
127     
128     /**
129      *@deprecated Please use the createLiteral methods on Model.
130      *Model implementors should use Literal instructors which include the Model.
131      */

132     public LiteralImpl(String JavaDoc s, String JavaDoc l, boolean wellFormed) {
133         this(s,l,wellFormed,null);
134     }
135     
136     
137     /**
138      *@deprecated Please use the createLiteral methods on Model.
139      *Model implementors should use Literal instructors which include the Model.
140      */

141     public LiteralImpl( String JavaDoc s, String JavaDoc l, boolean wellFormed, ModelCom m ) {
142         this(Node.createLiteral(s,l,wellFormed),m);
143     }
144     
145     /**
146      *@deprecated Please use the createLiteral methods on Model.
147      *Model implementors should use Literal instructors which include the Model.
148      */

149     public LiteralImpl(Object JavaDoc o) {this( o.toString());}
150     
151     
152     public boolean isLiteral() {
153         return true;
154     }
155     
156     public String JavaDoc toString() {
157         return asNode().toString( PrefixMapping.Standard, false );
158     }
159     
160     /**
161      * Return the value of the literal. In the case of plain literals
162      * this will return the literal string. In the case of typed literals
163      * it will return a java object representing the value. In the case
164      * of typed literals representing a java primitive then the appropriate
165      * java wrapper class (Integer etc) will be returned.
166      */

167     public Object JavaDoc getValue() {
168         return asNode().getLiteral().getValue();
169     }
170     
171     /**
172      * Return the datatype of the literal. This will be null in the
173      * case of plain literals.
174      */

175     public RDFDatatype getDatatype() {
176         return asNode().getLiteral().getDatatype();
177     }
178      
179     /**
180      * Return the uri of the datatype of the literal. This will be null in the
181      * case of plain literals.
182      */

183     public String JavaDoc getDatatypeURI() {
184         return asNode().getLiteral().getDatatypeURI();
185     }
186     
187     /**
188      * Return true if this is a "plain" (i.e. old style, not typed) literal.
189      */

190     public boolean isPlainLiteral() {
191         return asNode().getLiteral().getDatatype() == null;
192     }
193     
194     /**
195      * Return the lexical form of the literal.
196      */

197     public String JavaDoc getLexicalForm() {
198         return asNode().getLiteral().getLexicalForm();
199     }
200
201     public boolean getBoolean() {
202         Object JavaDoc value = asNode().getLiteral().getValue();
203         if (isPlainLiteral()) {
204             // old style plain literal - try parsing the string
205
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             // typed literal
214
if (value instanceof Boolean JavaDoc) {
215                 return ((Boolean JavaDoc)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 JavaDoc value = getValue();
263             if (value instanceof Character JavaDoc) {
264                 return ((Character JavaDoc) 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 JavaDoc getString() {
288         return asNode().getLiteral().getLexicalForm();
289     }
290     
291     public Object JavaDoc getObject(ObjectF f) {
292         if (isPlainLiteral()) {
293             try {
294                 return f.createObject(getString());
295             } catch (Exception JavaDoc e) {
296                 throw new JenaException(e);
297             }
298         } else {
299             return getValue();
300         }
301     }
302     
303     public String JavaDoc getLanguage() {
304         return asNode().getLiteral().language();
305     }
306     
307     public boolean getWellFormed() {
308         return asNode().getLiteral().isXML();
309     }
310    
311     /**
312      * Test that two literals are semantically equivalent.
313      * In some cases this may be the sames as equals, in others
314      * equals is stricter. For example, two xsd:int literals with
315      * the same value but different language tag are semantically
316      * equivalent but distinguished by the java equality function
317      * in order to support round tripping.
318      */

319     public boolean sameValueAs(Literal other) {
320         return asNode().sameValueAs(other.asNode());
321     }
322         
323      // Internal helper method to convert a value to number
324
private Number JavaDoc asNumber(Object JavaDoc value) {
325         if (value instanceof Number JavaDoc) {
326             return ((Number JavaDoc)value);
327         } else {
328             throw new DatatypeFormatException(value.toString() + " is not a Number");
329         }
330     }
331         
332 }
333
Popular Tags