KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > n3 > N3JenaWriter


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

5
6 // To do:
7
// Better detection of illegal characters in qnames (? and = for example)
8

9 package com.hp.hpl.jena.n3;
10
11 //import org.apache.commons.logging.*;
12
import java.io.OutputStream JavaDoc;
13 import java.io.Writer JavaDoc;
14
15 import com.hp.hpl.jena.rdf.model.*;
16
17 /** Entry point for N3 writers. This writer will choose the actual writer
18  * to use by looking at the system property
19  * <code>com.hp.hpl.jena.n3.N3JenaWriter.writer</code> to get the
20  * writer name.
21  * <p>
22  * The following N3 writers are provided:
23  * <ul>
24  * <li>N3-PP: Pretty Printer (the default)</li>
25  * <li>N3-PLAIN: Plain, record/frame-oriented format</li>
26  * <li>N3-TRIPLES: Triples, with prefixes.</li>
27  * </ul>
28  * </p>
29  *
30  * @author Andy Seaborne
31  * @version $Id: N3JenaWriter.java,v 1.27 2005/02/21 12:04:04 andy_seaborne Exp $
32  */

33
34
35
36 public class N3JenaWriter implements RDFWriter
37 {
38     //static Log logger = LogFactory.getLog(N3JenaWriter.class) ;
39
static public boolean DEBUG = false ;
40     
41     // Note: properties are URIs, not java convention package/class names.
42
static protected final String JavaDoc propBase = "http://jena.hpl.hp.com/n3/properties/" ;
43     
44     /** Compatibility.
45      * @deprecated Set <code>com.hp.hpl.jena.n3.N3JenaWriter.writer</code> to the name of the writer instead.
46      */

47     
48     static public final String JavaDoc propWriteSimple = "com.hp.hpl.jena.n3.N3JenaWriter.writeSimple" ;
49     
50     /** System property name that sets the default N3 writer name */
51     static public final String JavaDoc propWriterName = propBase+"writer" ;
52
53     /**
54      * General name for the N3 writer. Will make a decision on exactly which
55      * writer to use (pretty writer, plain writer or simple writer) when created.
56      * Default is the pretty writer but can be overridden with system property
57      * <code>com.hp.hpl.jena.n3.N3JenaWriter.writer</code>.
58      */

59      
60     static public final String JavaDoc n3Writer = "N3" ;
61     
62     /**
63      * Name of the N3 pretty writer. The pretty writer
64      * uses a frame-like layout, with prefixing, clustering like properties
65      * and embedding one-referenced bNodes.
66      */

67     static public final String JavaDoc n3WriterPrettyPrinter = "N3-PP" ;
68     
69     /**
70      * Name of the N3 plain writer. The plain writer writes records
71      * by subject.
72      */

73     static public final String JavaDoc n3WriterPlain = "N3-PLAIN" ;
74     
75     /**
76      * Name of the N3 triples writer. This writer writes one line per statement,
77      * like N-Triples, but does N3-style prefixing.
78      */

79     static public final String JavaDoc n3WriterTriples = "N3-TRIPLES" ;
80     
81     /**
82      * Alternative name for the N3 triples writer.
83      */

84     static public final String JavaDoc n3WriterTriplesAlt = "N3-TRIPLE" ;
85
86     /**
87      * Turtle writer.
88      * http://www.ilrt.bris.ac.uk/discovery/2004/01/turtle/
89      */

90     static public final String JavaDoc turtleWriter = "TURTLE" ;
91
92     
93     protected N3JenaWriterCommon writer = null ;
94     
95     public N3JenaWriter() { writer = chooseWriter() ; }
96     public N3JenaWriter(N3JenaWriterCommon w) { writer = w ;}
97     
98     N3JenaWriterCommon chooseWriter()
99     {
100         // Compatibility with Jena1
101
if ( System.getProperty(propWriteSimple, "false").equals("true"))
102             return new N3JenaWriterCommon() ;
103         
104         // Choose the writer
105
String JavaDoc writerName = System.getProperty(propWriterName) ;
106         if ( writerName == null ||
107              writerName.equals("N3") || writerName.equals(n3WriterPrettyPrinter) )
108             return new N3JenaWriterPP() ;
109         
110         if ( writerName.equalsIgnoreCase(n3WriterPlain) )
111             return new N3JenaWriterCommon() ;
112         
113         if ( writerName.equalsIgnoreCase(n3WriterTriples) ||
114              writerName.equalsIgnoreCase(n3WriterTriplesAlt) )
115             return new N3JenaWriterTriples() ;
116             
117         if ( writerName.equalsIgnoreCase(turtleWriter) )
118         {
119             N3JenaWriterPP w = new N3JenaWriterPP() ;
120             w.useWellKnownPropertySymbols = false ;
121             return w ;
122         }
123         
124         // Don't know or default.
125
return new N3JenaWriterPP() ;
126     }
127     
128     
129     /** Write the model out in N3, encoded in in UTF-8
130      * @see #write(Model,Writer,String)
131      */

132
133     public void write(Model model, Writer JavaDoc out, String JavaDoc base)
134     {
135         writer.write(model, out, base) ;
136     }
137
138     /** Write the model out in N3. The writer should be one suitable for UTF-8 which
139     * excludes a PrintWriter or a FileWriter which use default character set.
140     *
141     * Examples:
142     * <pre>
143     * try {
144     * Writer w = new BufferedWriter(new OutputStreamWriter(output, "UTF-8")) ;
145     * model.write(w, base) ;
146     * try { w.flush() ; } catch (IOException ioEx) {}
147     * } catch (java.io.UnsupportedEncodingException ex) {} //UTF-8 is required so can't happen
148     * </pre>
149     * or
150     * <pre>
151     * try {
152     * OutputStream out = new FileOutputStream(file) ;
153     * Writer w = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")) ;
154     * model.write(w, base) ;
155     * }
156     * catch (java.io.UnsupportedEncodingException ex) {}
157     * catch (java.io.FileNotFoundException noFileEx) { ... }
158     * </pre>
159     * @see #write(Model,Writer,String)
160     */

161
162     public void write(Model model, OutputStream JavaDoc out, String JavaDoc base)
163     {
164         writer.write(model, out, base) ;
165    }
166
167
168     /**
169      * @see com.hp.hpl.jena.rdf.model.RDFWriter#setProperty(java.lang.String, java.lang.Object)
170      */

171     public Object JavaDoc setProperty(String JavaDoc propName, Object JavaDoc propValue)
172     {
173         return writer.setProperty(propName, propValue) ;
174     }
175
176     /**
177      * @see com.hp.hpl.jena.rdf.model.RDFWriter#setErrorHandler(com.hp.hpl.jena.rdf.model.RDFErrorHandler)
178      */

179     public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler)
180     {
181         return writer.setErrorHandler(errHandler) ;
182     }
183    
184 }
185
186 /*
187  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
188  * All rights reserved.
189  *
190  * Redistribution and use in source and binary forms, with or without
191  * modification, are permitted provided that the following conditions
192  * are met:
193  * 1. Redistributions of source code must retain the above copyright
194  * notice, this list of conditions and the following disclaimer.
195  * 2. Redistributions in binary form must reproduce the above copyright
196  * notice, this list of conditions and the following disclaimer in the
197  * documentation and/or other materials provided with the distribution.
198  * 3. The name of the author may not be used to endorse or promote products
199  * derived from this software without specific prior written permission.
200  *
201  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
202  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
203  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
204  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
205  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
206  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
207  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
208  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
209  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
210  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
211  */

212
Popular Tags