1 5 6 package com.hp.hpl.jena.rdql; 7 8 import java.io.* ; 9 import java.util.HashMap ; 10 import java.util.Map ; 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 23 24 public class QueryPrintUtils 25 { 26 static final String indentPrefix = " " ; 28 public static boolean multiLineExpr = false ; 29 public static boolean printName = true ; 30 static int bNodeCounter = 0 ; 31 static Map bNodeStrings = new HashMap () ; 32 33 34 35 public static void print(PrintWriter pw, Expr expr) 36 { 37 pw.println(expr.asPrefixString()); 39 } 40 41 public static String asInfixString1(Expr expr, String opName, String symbol) 42 { 43 StringBuffer sb = new StringBuffer () ; 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 asInfixString2(Expr left, Expr right, String opName, String symbol) 53 { 54 StringBuffer sb = new StringBuffer () ; 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 asPrefixString(Expr left, Expr right, String opName, String symbol) 66 { 67 StringBuffer sb = new StringBuffer () ; 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 public static void print(PrintWriter pw, Expr left, Expr right, String opName, String 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 101 public static String stringForObject(Object 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 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 stringForLiteral(Literal literal) 129 { 130 return stringForLiteralLabel(literal.asNode().getLiteral()) ; 131 } 132 133 public static String stringForLiteralLabel(LiteralLabel literal) 134 { 135 String datatype = literal.getDatatypeURI() ; 136 String lang = literal.language() ; 137 String s = literal.getLexicalForm() ; 138 139 if ( datatype != null ) 140 { 141 if ( datatype.equals(XSD.integer.getURI()) ) 144 { 145 try { 146 new java.math.BigInteger (s) ; 147 return s ; 148 } catch (NumberFormatException nfe) {} 149 } 152 153 if ( datatype.equals(XSD.xdouble.getURI()) ) 154 { 155 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 nfe) {} 164 } 166 } 167 } 168 169 StringBuffer sbuff = new StringBuffer () ; 171 sbuff.append("\"") ; 172 sbuff.append(s) ; 173 sbuff.append("\"") ; 174 175 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 stringForResource(Resource r) 187 { 188 return stringForNode(r.getNode(), r.getModel()) ; 189 } 207 208 public static String stringForNode(Node n) { return stringForNode(n, null) ; } 209 210 public static String 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 (String )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 uri = n.getURI() ; 231 if ( mapping != null ) 232 { 233 String 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 274 | Popular Tags |