1 10 package com.hp.hpl.jena.util; 11 12 import java.util.*; 13 import java.io.*; 14 import com.hp.hpl.jena.vocabulary.*; 15 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; 16 import com.hp.hpl.jena.graph.*; 17 import com.hp.hpl.jena.graph.impl.*; 18 import com.hp.hpl.jena.rdf.model.*; 19 import com.hp.hpl.jena.reasoner.TriplePattern; 20 import com.hp.hpl.jena.shared.PrefixMapping; 21 22 33 public class PrintUtil { 34 35 protected static PrefixMapping prefixMapping = PrefixMapping.Factory.create(); 36 37 38 public static final String egNS = "urn:x-hp:eg/"; 39 40 static { 41 init(); 42 } 43 44 47 public static void init() { 48 registerPrefix("rdf", RDF.getURI()); 49 registerPrefix("rdfs", RDFS.getURI()); 50 registerPrefix("drdfs", "urn:x-hp-direct-predicate:http_//www.w3.org/2000/01/rdf-schema#"); 51 registerPrefix("owl", OWL.getURI()); 52 registerPrefix("daml", DAML_OIL.NAMESPACE_DAML.getURI()); 53 registerPrefix("jr", ReasonerVocabulary.getJenaReasonerNS()); 54 registerPrefix("rb", ReasonerVocabulary.getRBNamespace()); 55 registerPrefix("eg", egNS); 56 registerPrefix("xsd", XSDDatatype.XSD + "#"); 57 } 58 59 63 public static void registerPrefix(String prefix, String namespace) { 64 prefixMapping.setNsPrefix( prefix, namespace ); 65 } 66 67 70 public static String print(Node node) { 71 if (node instanceof Node_URI) { 72 return node.toString( prefixMapping ); 73 } else if (node instanceof Node_Literal) { 74 LiteralLabel ll = node.getLiteral(); 75 String lf = ll.getLexicalForm(); 76 return ll.getDatatype() == null ? "'" + lf + "'" : lf + "^^" + ll.getDatatypeURI(); 77 } else if (node instanceof Node_ANY) { 78 return "*"; 79 } 80 if (node == null) { 81 return "null"; 82 } 83 return node.toString(); 84 } 85 86 89 public static String print(RDFNode node) { 90 if (node == null) return "null"; 91 return print(node.asNode()); 92 } 93 94 97 public static String print(Triple triple) { 98 if (triple == null) return "(null)"; 99 return "(" + print(triple.getSubject()) + " " + 100 print(triple.getPredicate()) + " " + 101 print(triple.getObject()) + ")"; 102 } 103 104 107 public static String print(TriplePattern triple) { 108 if (triple == null) return "(null)"; 109 return "(" + print(triple.getSubject()) + " " + 110 print(triple.getPredicate()) + " " + 111 print(triple.getObject()) + ")"; 112 } 113 114 117 public static String print(Statement stmt) { 118 if (stmt == null) return "(null)"; 119 return print(stmt.asTriple()); 120 } 121 122 125 public static String print(Object obj) { 126 if (obj == null) return "null"; 127 if (obj instanceof Triple) { 128 return print((Triple)obj); 129 } else if (obj instanceof TriplePattern) { 130 return print((TriplePattern)obj); 131 } else if (obj instanceof Node) { 132 return print((Node)obj); 133 } else if (obj instanceof RDFNode) { 134 return print((RDFNode)obj); 135 } else if (obj instanceof Statement) { 136 return print((Statement)obj); 137 } else { 138 return obj.toString(); 139 } 140 } 141 142 147 public static String expandQname(String uri) { 148 return prefixMapping.expandPrefix( uri ); 149 } 150 151 154 public static void printIndent(PrintWriter out, int indent) { 155 StringBuffer spaces = new StringBuffer (); 156 for (int i = 0; i < indent; i++) spaces.append(" "); 157 out.print(spaces.toString()); 158 } 159 160 163 public static void printOut(Iterator it) { 164 while (it.hasNext()) { 165 System.out.println(" " + print(it.next())); 166 } 167 } 168 } 169 170 | Popular Tags |