KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > PrintUtil


1 /******************************************************************
2  * File: PrintUtil.java
3  * Created by: Dave Reynolds
4  * Created on: 29-Mar-03
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: PrintUtil.java,v 1.15 2005/02/21 12:18:57 andy_seaborne Exp $
9  *****************************************************************/

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 /**
23  * A collection of small utilites for pretty printing nodes, triples
24  * and associated things. The core functionality here is a static
25  * prefix map which is preloaded with known prefixes.
26  *
27  * <p>updated by Chris March 2004 to use a PrefixMapping rather than the
28  * specialised tables.
29  *
30  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
31  * @version $Revision: 1.15 $ on $Date: 2005/02/21 12:18:57 $
32  */

33 public class PrintUtil {
34     
35     protected static PrefixMapping prefixMapping = PrefixMapping.Factory.create();
36     
37     /** Default built in eg namespace used in testing */
38     public static final String JavaDoc egNS = "urn:x-hp:eg/";
39         
40     static {
41         init();
42     }
43     
44     /**
45      * Load built in prefixes.
46      */

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     /**
60      * Register a new prefix/namespace mapping which will be used to shorten
61      * the print strings for resources in known namespaces.
62      */

63     public static void registerPrefix(String JavaDoc prefix, String JavaDoc namespace) {
64         prefixMapping.setNsPrefix( prefix, namespace );
65     }
66     
67     /**
68      * Return a simplified print string for a Node.
69      */

70     public static String JavaDoc 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 JavaDoc 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     /**
87      * Return a simplified print string for an RDFNode.
88      */

89     public static String JavaDoc print(RDFNode node) {
90         if (node == null) return "null";
91         return print(node.asNode());
92     }
93     
94     /**
95      * Return a simplified print string for a Triple
96      */

97     public static String JavaDoc 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     /**
105      * Return a simplified print string for a TriplePattern
106      */

107     public static String JavaDoc 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     /**
115      * Return a simplified print string for a statment
116      */

117     public static String JavaDoc print(Statement stmt) {
118         if (stmt == null) return "(null)";
119         return print(stmt.asTriple());
120     }
121     
122     /**
123      * Default print which just uses tostring
124      */

125     public static String JavaDoc print(Object JavaDoc 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     /**
143      * Expand qnames to URIs. If the given URI appears
144      * to start with one of the registered prefixes then
145      * expand the prefix, otherwise return the original URI
146      */

147     public static String JavaDoc expandQname(String JavaDoc uri) {
148         return prefixMapping.expandPrefix( uri );
149     }
150     
151     /**
152      * Print an n-space indent to the given output stream
153      */

154     public static void printIndent(PrintWriter out, int indent) {
155         StringBuffer JavaDoc spaces = new StringBuffer JavaDoc();
156         for (int i = 0; i < indent; i++) spaces.append(" ");
157         out.print(spaces.toString());
158     }
159     
160     /**
161      * Print all the Triple values from a find iterator.
162      */

163     public static void printOut(Iterator it) {
164         while (it.hasNext()) {
165             System.out.println(" " + print(it.next()));
166         }
167     }
168 }
169
170 /*
171     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
172     All rights reserved.
173
174     Redistribution and use in source and binary forms, with or without
175     modification, are permitted provided that the following conditions
176     are met:
177
178     1. Redistributions of source code must retain the above copyright
179        notice, this list of conditions and the following disclaimer.
180
181     2. Redistributions in binary form must reproduce the above copyright
182        notice, this list of conditions and the following disclaimer in the
183        documentation and/or other materials provided with the distribution.
184
185     3. The name of the author may not be used to endorse or promote products
186        derived from this software without specific prior written permission.
187
188     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
190     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
193     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
194     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
195     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
196     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
197     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
198 */
Popular Tags