KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > parser > ParsedLiteral


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.rdql.parser;
7
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.rdql.*;
11 import com.hp.hpl.jena.graph.query.IndexValues ;
12 import com.hp.hpl.jena.graph.impl.LiteralLabel;
13
14 import java.io.PrintWriter JavaDoc;
15
16 // An implementation of value that is created from the parsing process.
17
// Explict declaration of the Expr interface causes Eclipse to put it
18
// in the type hierarchy directly which helps development.
19

20 public class ParsedLiteral extends ExprNode implements Expr, NodeValue
21 {
22     // Used to create resources and literals
23
static Model model = ModelFactory.createDefaultModel() ;
24
25     protected boolean isSet = false ;
26
27     private boolean isInt = false ;
28     private boolean isBoolean = false ;
29     private boolean isDouble = false ;
30     private boolean isURI = false ;
31     private boolean isString = false ;
32     private boolean isGraphNode = false ;
33     
34     //private boolean isRDFResource = false ;
35
//private boolean isRDFLiteral = false ;
36

37     private long valInt ;
38     private boolean valBoolean ;
39     private double valDouble ;
40     private String JavaDoc valString ;
41     private String JavaDoc valURI ;
42     private com.hp.hpl.jena.graph.Node valGraphNode ;
43     
44     //private Literal valRDFLiteral ;
45
//private Resource valRDFResource ;
46

47     // Constructors used by the parser
48
ParsedLiteral(int id) { super(id); }
49
50     ParsedLiteral(RDQLParser p, int id) { super(p, id); }
51
52     public ParsedLiteral() { super(-1) ; unset() ; }
53     
54     // Used by working var to clone values.
55
protected ParsedLiteral(NodeValue v)
56     {
57         super(-1) ;
58         if ( v.isBoolean() )
59         {
60             _setBoolean(v.getBoolean()) ;
61             return ;
62         }
63             
64         if ( v.isInt() )
65         {
66             _setInt(v.getInt()) ;
67             return ;
68         }
69
70         if ( v.isDouble() )
71         {
72             _setDouble(v.getDouble()) ;
73             return ;
74         }
75
76         if ( v.isURI() )
77         {
78             _setURI(v.getURI()) ;
79             return ;
80         }
81
82         if ( v.isNode() )
83         {
84             _setNode(v.getNode()) ;
85             return ;
86         }
87         
88         if ( v.isString() )
89         {
90             _setString(v.getString()) ;
91             return ;
92         }
93
94     }
95         
96
97
98     protected void unset()
99     {
100         isSet = false ;
101         valString = null ;
102         valGraphNode = null ;
103         valInt = 0 ;
104         valBoolean = false ;
105         valDouble = 0 ;
106         valURI = null ;
107         valGraphNode = null ;
108
109         isInt = false ;
110         isBoolean = false ;
111         isDouble = false ;
112         isURI = false ;
113         isString = false ;
114     }
115
116     public NodeValue eval(Query q, IndexValues env)
117     {
118         if ( ! isSet )
119             throw new EvalFailureException("Literal value not set") ;
120
121          return this ;
122     }
123
124     public boolean isSet() { return isSet ; }
125
126     public boolean isNumber() { forceNumber() ; return isSet && (isInt || isDouble) ; }
127     public boolean isInt() { forceInt() ; return isSet && isInt ; }
128     public boolean isDouble() { forceDouble() ; return isSet && isDouble ; }
129     public boolean isBoolean() { return isSet && isBoolean ; }
130     public boolean isString() { return isSet && isString ; }
131     public boolean isURI() { return isSet && isURI ; }
132     public boolean isNode() { return isSet && isGraphNode ; }
133     
134
135     protected void _setInt(long i) { unset() ; isSet = true ; isInt = true ; valInt = i ; }
136     protected void _setDouble(double d) { unset() ; isSet = true ; isDouble = true ; valDouble = d ; }
137     protected void _setBoolean(boolean b) { unset() ; isSet = true ; isBoolean = true ; valBoolean = b ; }
138     protected void _setString(String JavaDoc s) { unset() ; isSet = true ; isString = true ; valString = s ; }
139     protected void _setURI(String JavaDoc uri) { unset() ; isSet = true ; isURI = true ; isString = true ; valURI = uri ; valString = uri ; }
140     
141     protected void _setNode(com.hp.hpl.jena.graph.Node n)
142     {
143         unset();
144         isSet = true;
145         isGraphNode = true;
146         valGraphNode = n ;
147         isString = false ;
148         valString = null ;
149         
150         if ( n.isLiteral() )
151             valString = n.getLiteral().getLexicalForm() ;
152         if ( n.isURI() )
153         {
154             valString = n.getURI() ;
155             valURI = n.getURI() ;
156             isURI = true ;
157         }
158         if ( n.isBlank() )
159             valString = n.getBlankNodeId().toString() ;
160         
161         if ( valString != null )
162             isString = true ;
163     }
164     
165     private void forceInt()
166     {
167         if ( ! isSet || isInt || ! isString ) return ;
168         try {
169             valInt = Long.parseLong(valString) ;
170             isInt = true ;
171             isDouble = true ;
172             valDouble = valInt ;
173         } catch (NumberFormatException JavaDoc e) { return ; }
174     }
175
176     private void forceDouble()
177     {
178         if ( ! isSet || isDouble || ! isString ) return ;
179         try {
180             valDouble = Double.parseDouble(valString) ;
181             isDouble = true ;
182         } catch (NumberFormatException JavaDoc e) { return ; }
183     }
184
185     private void forceNumber()
186     {
187         if ( ! isSet || isInt || isDouble || ! isString )
188                 return ;
189         
190         forceInt() ;
191         if ( ! isInt )
192             forceDouble() ;
193     }
194
195
196     public long getInt()
197     {
198         if ( ! isSet || ! isInt ) throw new ValueException("Not an int: "+this) ;
199         return valInt ;
200     }
201
202     public double getDouble()
203     {
204         if ( ! isSet || ! ( isDouble || isInt ) ) throw new ValueException("Not a double: "+this) ;
205         if ( isInt )
206             return valInt ;
207         return valDouble ;
208     }
209
210     public boolean getBoolean()
211     {
212         if ( ! isSet || ! isBoolean ) throw new ValueException("Not a boolean: "+this) ;
213         return valBoolean ;
214     }
215
216     public String JavaDoc getString()
217     {
218         if ( ! isSet || ! isString ) throw new ValueException("Not a string: "+this) ;
219         return valString ;
220     }
221
222     public String JavaDoc getURI()
223     {
224         if ( ! isSet || ! isURI ) throw new ValueException("Not a URI: "+this) ;
225         return valURI ;
226     }
227
228     public com.hp.hpl.jena.graph.Node getNode()
229     {
230         if ( ! isSet ) throw new ValueException("Not a graph node: "+this) ;
231         return valGraphNode ;
232     }
233     
234     // Expressions
235

236     // -- Constants (literals - but that name is confusing with RDF literals).
237
public boolean isConstant() { return true; }
238     // This may be null (as it is not a jena.graph value).
239
public Object JavaDoc getValue() { return getNode() ; }
240         
241     // In all these stringification operations, order matters e.g. URI before string
242
public String JavaDoc asQuotedString()
243     {
244         if ( ! isSet ) return "literal:unset" ;
245         if ( isInt ) return Long.toString(valInt) ;
246         if ( isDouble ) return Double.toString(valDouble) ;
247         if ( isBoolean ) return (valBoolean?"true":"false") ;
248         
249         if ( isGraphNode )
250         {
251             if ( valGraphNode.isLiteral() )
252             {
253                 StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc() ;
254                 
255                 LiteralLabel l = valGraphNode.getLiteral() ;
256                 sBuff.append('"') ;
257                 sBuff.append(l.getLexicalForm()) ;
258                 sBuff.append('"') ;
259
260                 String JavaDoc dt = l.getDatatypeURI() ;
261                 if ( dt != null ) { sBuff.append("^^") ; sBuff.append(dt) ; }
262                 
263                 String JavaDoc lang = l.language() ;
264                 if ( lang != null ) { sBuff.append("@") ; sBuff.append(lang) ; }
265             }
266
267             if ( valGraphNode.isURI() )
268                 valString = "<"+valGraphNode.getURI()+">" ;
269             if ( valGraphNode.isBlank() )
270                 valString = valGraphNode.getBlankNodeId().toString() ;
271         }
272             
273         // Escaping needed
274
if ( isURI ) return "<"+valURI+">" ;
275         // Escaping needed
276
if ( isString ) return "\""+valString+"\"" ;
277
278         return "literal:unknown" ;
279     }
280
281     // Does not quote strings or URIs
282
public String JavaDoc asUnquotedString()
283     {
284         if ( ! isSet ) return "literal:unset" ;
285         if ( isInt ) return Long.toString(valInt) ;
286         if ( isDouble ) return Double.toString(valDouble) ;
287         if ( isBoolean ) return (valBoolean?"true":"false") ;
288         if ( isURI ) return valURI ;
289         if ( isString ) return valString ;
290         if ( isGraphNode ) return valGraphNode.toString() ;
291
292         return "literal:unknown" ;
293     }
294
295     public String JavaDoc asInfixString() { return asQuotedString() ; }
296
297     public String JavaDoc asPrefixString()
298     {
299         if ( ! isSet ) return "literal:unset" ;
300         if ( isInt ) return "int:"+Long.toString(valInt) ;
301         if ( isDouble ) return "double:"+Double.toString(valDouble) ;
302         if ( isBoolean ) return "boolean:"+(valBoolean?"true":"false") ;
303         if ( isURI ) return "URI:"+valURI ;
304         if ( isString ) return "string:"+valString ;
305         if ( isGraphNode) return "node:"+valGraphNode ;
306         
307         return "literal:unknown" ;
308     }
309
310     // Print prefix notation (multiline) for debugging
311
public void print(PrintWriter JavaDoc pw, int level)
312     {
313         QueryPrintUtils.indent(pw, level) ;
314         pw.println(this.asPrefixString()) ;
315     }
316
317     // Subclasses may override this.
318
public String JavaDoc valueString() { return asUnquotedString() ; }
319
320     // This is used in the filtering stage to get values for testing - must be unquoted.
321
public String JavaDoc toString()
322     {
323         return asUnquotedString() ;
324     }
325     
326     // Used by QueryResultsMem to build values
327

328     public static ParsedLiteral makeString(String JavaDoc s)
329     {
330         ParsedLiteral l = new ParsedLiteral(0) ;
331         l._setString(s) ;
332         return l ;
333     }
334     
335     // Q_URI is not public.
336
public static Q_URI makeURI(String JavaDoc s) { return Q_URI.makeURI(s) ; }
337 }
338
339 /*
340  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
341  * All rights reserved.
342  *
343  * Redistribution and use in source and binary forms, with or without
344  * modification, are permitted provided that the following conditions
345  * are met:
346  * 1. Redistributions of source code must retain the above copyright
347  * notice, this list of conditions and the following disclaimer.
348  * 2. Redistributions in binary form must reproduce the above copyright
349  * notice, this list of conditions and the following disclaimer in the
350  * documentation and/or other materials provided with the distribution.
351  * 3. The name of the author may not be used to endorse or promote products
352  * derived from this software without specific prior written permission.
353  *
354  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
355  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
356  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
357  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
358  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
359  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
360  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
361  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
362  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
363  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
364  */

365
Popular Tags