KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > QueryPrintUtils


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;
7
8 import java.io.* ;
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import com.hp.hpl.jena.graph.Node;
13 import com.hp.hpl.jena.graph.impl.LiteralLabel;
14 import com.hp.hpl.jena.rdf.model.*;
15 import com.hp.hpl.jena.rdql.parser.*;
16 import com.hp.hpl.jena.shared.PrefixMapping;
17 import com.hp.hpl.jena.vocabulary.XSD;
18
19 /**
20  * @author Andy Seaborne
21  * @version $Id: QueryPrintUtils.java,v 1.7 2005/02/21 12:15:24 andy_seaborne Exp $
22  */

23
24 public class QueryPrintUtils
25 {
26     // Helper operations
27
static final String JavaDoc indentPrefix = " " ;
28     public static boolean multiLineExpr = false ;
29     public static boolean printName = true ;
30     static int bNodeCounter = 0 ;
31     static Map JavaDoc bNodeStrings = new HashMap JavaDoc() ;
32     
33
34
35     public static void print(PrintWriter pw, Expr expr)
36     {
37         //expr.print(pw, 0) ;
38
pw.println(expr.asPrefixString());
39     }
40
41     public static String JavaDoc asInfixString1(Expr expr, String JavaDoc opName, String JavaDoc symbol)
42     {
43         StringBuffer JavaDoc sb = new StringBuffer JavaDoc() ;
44         sb.append("(") ;
45         sb.append(symbol) ;
46         sb.append(" ") ;
47         sb.append(expr.asInfixString()) ;
48         sb.append(")");
49         return sb.toString() ;
50     }
51
52     public static String JavaDoc asInfixString2(Expr left, Expr right, String JavaDoc opName, String JavaDoc symbol)
53     {
54         StringBuffer JavaDoc sb = new StringBuffer JavaDoc() ;
55         sb.append("( ") ;
56         sb.append(left.asInfixString()) ;
57         sb.append(" ") ;
58         sb.append( symbol ) ;
59         sb.append(" ") ;
60         sb.append(right.asInfixString()) ;
61         sb.append(" )");
62         return sb.toString() ;
63     }
64
65     public static String JavaDoc asPrefixString(Expr left, Expr right, String JavaDoc opName, String JavaDoc symbol)
66     {
67         StringBuffer JavaDoc sb = new StringBuffer JavaDoc() ;
68
69         sb.append("(") ;
70         sb.append( printName?opName:symbol ) ;
71         sb.append(" ") ;
72         sb.append(left.asPrefixString()) ;
73         if ( right != null )
74         {
75             sb.append(" ") ;
76             sb.append(right.asPrefixString()) ;
77         }
78         sb.append(")");
79         return sb.toString() ;
80     }
81
82     // Prints prefix notation, multiline.
83
public static void print(PrintWriter pw, Expr left, Expr right, String JavaDoc opName, String JavaDoc symbol, int level)
84     {
85         indent(pw, level) ;
86         pw.println("("+(printName?opName:symbol)) ;
87         left.print(pw, level+1);
88         if ( right != null )
89             right.print(pw, level+1);
90         indent(pw, level) ;
91         pw.println(")") ;
92     }
93
94     public static void indent(PrintWriter pw, int level)
95     {
96         for ( int i = 0 ; i < level ; i++ )
97           pw.print(indentPrefix);
98     }
99     // Formatting various items
100

101     public static String JavaDoc stringForObject(Object JavaDoc obj)
102     {
103         if ( obj == null )
104             return "<<null>>" ;
105
106         if ( obj instanceof RDFNode )
107             return stringForRDFNode((RDFNode)obj) ;
108         if ( obj instanceof Node )
109             return stringForNode((Node)obj) ;
110         return obj.toString() ;
111     }
112     
113     
114     public static String JavaDoc stringForRDFNode(RDFNode obj)
115     {
116         if ( obj == null )
117             return "<<null>>" ;
118         
119         if ( obj instanceof Literal )
120             return stringForLiteral((Literal)obj) ;
121         
122         if ( obj instanceof Resource )
123             return stringForResource((Resource)obj) ;
124         
125         return obj.toString() ;
126     }
127     
128     public static String JavaDoc stringForLiteral(Literal literal)
129     {
130         return stringForLiteralLabel(literal.asNode().getLiteral()) ;
131     }
132         
133     public static String JavaDoc stringForLiteralLabel(LiteralLabel literal)
134     {
135         String JavaDoc datatype = literal.getDatatypeURI() ;
136         String JavaDoc lang = literal.language() ;
137         String JavaDoc s = literal.getLexicalForm() ;
138         
139         if ( datatype != null )
140         {
141             // Special form we know how to handle?
142
// Assume valid text
143
if ( datatype.equals(XSD.integer.getURI()) )
144             {
145                 try {
146                     new java.math.BigInteger JavaDoc(s) ;
147                     return s ;
148                 } catch (NumberFormatException JavaDoc nfe) {}
149                 // No luck. Continue.
150
// Continuing is always safe.
151
}
152             
153             if ( datatype.equals(XSD.xdouble.getURI()) )
154             {
155                 // Must have an '.' or 'e' or 'E'
156
if ( s.indexOf('.') >= 0 ||
157                 s.indexOf('e') >= 0 ||
158                 s.indexOf('E') >= 0 )
159                 {
160                     try {
161                         Double.parseDouble(s) ;
162                         return s ;
163                     } catch (NumberFormatException JavaDoc nfe) {}
164                     // No luck. Continue.
165
}
166             }
167         }
168         
169         // Format the text - no escaping.
170
StringBuffer JavaDoc sbuff = new StringBuffer JavaDoc() ;
171         sbuff.append("\"") ;
172         sbuff.append(s) ;
173         sbuff.append("\"") ;
174         
175         // Format the language tag
176
if ( lang != null && lang.length()>0)
177         {
178             sbuff.append("@") ;
179             sbuff.append(lang) ;
180         }
181         
182         return sbuff.toString() ;
183     }
184     
185     
186     public static String JavaDoc stringForResource(Resource r)
187     {
188         return stringForNode(r.getNode(), r.getModel()) ;
189 // if ( r.isAnon() )
190
// {
191
// AnonId a = r.getId() ;
192
// if ( ! bNodeStrings.containsKey(a) )
193
// bNodeStrings.put(a, "_:b"+(bNodeCounter++)) ;
194
// //return "anon:"+r.getId() ;
195
// return (String)bNodeStrings.get(a) ;
196
// }
197
// else
198
// {
199
// String u = r.getURI() ;
200
// String tmp = r.getModel().shortForm(u) ;
201
// if ( u.equals(tmp) )
202
// return "<"+u+">" ;
203
// return tmp ;
204
// }
205
//
206
}
207     
208     public static String JavaDoc stringForNode(Node n) { return stringForNode(n, null) ; }
209     
210     public static String JavaDoc stringForNode(Node n, PrefixMapping mapping)
211     {
212         if ( n == null )
213             return "<<null>>" ;
214         
215         if ( n.isBlank() )
216         {
217             AnonId a = n.getBlankNodeId() ;
218             if ( ! bNodeStrings.containsKey(a) )
219                 bNodeStrings.put(a, "_:b"+(bNodeCounter++)) ;
220             //return "anon:"+r.getId() ;
221
return (String JavaDoc)bNodeStrings.get(a) ;
222         }
223         if ( n.isLiteral() )
224         {
225             LiteralLabel ll = n.getLiteral() ;
226             return stringForLiteralLabel(ll) ;
227         }
228         if ( n.isURI() )
229         {
230             String JavaDoc uri = n.getURI() ;
231             if ( mapping != null )
232             {
233                 String JavaDoc tmp = mapping.shortForm(n.getURI()) ;
234                 if ( tmp != null && !tmp.equals(uri) )
235                     return tmp ;
236             }
237             
238             return "<"+uri+">" ;
239         }
240         if ( n.isVariable() )
241             return "?"+n.getName() ;
242
243         return n.toString() ;
244     }
245
246 }
247
248 /*
249  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
250  * All rights reserved.
251  *
252  * Redistribution and use in source and binary forms, with or without
253  * modification, are permitted provided that the following conditions
254  * are met:
255  * 1. Redistributions of source code must retain the above copyright
256  * notice, this list of conditions and the following disclaimer.
257  * 2. Redistributions in binary form must reproduce the above copyright
258  * notice, this list of conditions and the following disclaimer in the
259  * documentation and/or other materials provided with the distribution.
260  * 3. The name of the author may not be used to endorse or promote products
261  * derived from this software without specific prior written permission.
262  *
263  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
264  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
265  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
266  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
267  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
268  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
269  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
270  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
272  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273  */

274
Popular Tags