001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: // To do:
007: // Better detection of illegal characters in qnames (? and = for example)
008: package com.hp.hpl.jena.n3;
009:
010: //import org.apache.commons.logging.*;
011: import java.io.OutputStream;
012: import java.io.Writer;
013:
014: import com.hp.hpl.jena.JenaRuntime;
015: import com.hp.hpl.jena.rdf.model.*;
016:
017: /** Entry point for N3 writers. This writer will choose the actual writer
018: * to use by looking at the system property
019: * <code>com.hp.hpl.jena.n3.N3JenaWriter.writer</code> to get the
020: * writer name.
021: * <p>
022: * The following N3 writers are provided:
023: * <ul>
024: * <li>N3-PP: Pretty Printer (the default)</li>
025: * <li>N3-PLAIN: Plain, record/frame-oriented format</li>
026: * <li>N3-TRIPLES: Triples, with prefixes.</li>
027: * </ul>
028: * </p>
029: *
030: * @author Andy Seaborne
031: * @version $Id: N3JenaWriter.java,v 1.32 2008/01/02 12:04:49 andy_seaborne Exp $
032: */
033:
034: public class N3JenaWriter implements RDFWriter {
035: //static Log logger = LogFactory.getLog(N3JenaWriter.class) ;
036: static public boolean DEBUG = false;
037:
038: // Note: properties are URIs, not java convention package/class names.
039: static protected final String propBase = "http://jena.hpl.hp.com/n3/properties/";
040:
041: /** Compatibility.
042: * @deprecated Set <code>com.hp.hpl.jena.n3.N3JenaWriter.writer</code> to the name of the writer instead.
043: */
044:
045: static public final String propWriteSimple = "com.hp.hpl.jena.n3.N3JenaWriter.writeSimple";
046:
047: /** System property name that sets the default N3 writer name */
048: static public final String propWriterName = propBase + "writer";
049:
050: /**
051: * General name for the N3 writer. Will make a decision on exactly which
052: * writer to use (pretty writer, plain writer or simple writer) when created.
053: * Default is the pretty writer but can be overridden with system property
054: * <code>com.hp.hpl.jena.n3.N3JenaWriter.writer</code>.
055: */
056:
057: static public final String n3Writer = "N3";
058:
059: /**
060: * Name of the N3 pretty writer. The pretty writer
061: * uses a frame-like layout, with prefixing, clustering like properties
062: * and embedding one-referenced bNodes.
063: */
064: static public final String n3WriterPrettyPrinter = "N3-PP";
065:
066: /**
067: * Name of the N3 plain writer. The plain writer writes records
068: * by subject.
069: */
070: static public final String n3WriterPlain = "N3-PLAIN";
071:
072: /**
073: * Name of the N3 triples writer. This writer writes one line per statement,
074: * like N-Triples, but does N3-style prefixing.
075: */
076: static public final String n3WriterTriples = "N3-TRIPLES";
077:
078: /**
079: * Alternative name for the N3 triples writer.
080: */
081: static public final String n3WriterTriplesAlt = "N3-TRIPLE";
082:
083: /**
084: * Turtle writer.
085: * http://www.dajobe.org/2004/01/turtle/
086: */
087: static public final String turtleWriter = "TURTLE";
088: static public final String turtleWriterAlt1 = "Turtle";
089: static public final String turtleWriterAlt2 = "TTL";
090:
091: protected N3JenaWriterCommon writer = null;
092:
093: public N3JenaWriter() {
094: writer = chooseWriter();
095: }
096:
097: public N3JenaWriter(N3JenaWriterCommon w) {
098: writer = w;
099: }
100:
101: N3JenaWriterCommon chooseWriter() {
102: // Compatibility with Jena1
103: if (JenaRuntime.getSystemProperty(propWriteSimple, "false")
104: .equals("true"))
105: return new N3JenaWriterCommon();
106:
107: // Choose the writer
108: String writerName = JenaRuntime
109: .getSystemProperty(propWriterName);
110: if (writerName == null || writerName.equals("N3")
111: || writerName.equals(n3WriterPrettyPrinter))
112: return new N3JenaWriterPP();
113:
114: if (writerName.equalsIgnoreCase(n3WriterPlain))
115: return new N3JenaWriterCommon();
116:
117: if (writerName.equalsIgnoreCase(n3WriterTriples)
118: || writerName.equalsIgnoreCase(n3WriterTriplesAlt))
119: return new N3JenaWriterTriples();
120:
121: if (writerName.equalsIgnoreCase(turtleWriter)) {
122: N3JenaWriterPP w = new N3JenaWriterPP();
123: w.useWellKnownPropertySymbols = false;
124: return w;
125: }
126:
127: // Don't know or default.
128: return new N3JenaWriterPP();
129: }
130:
131: /** Write the model out in N3, encoded in in UTF-8
132: * @see #write(Model,Writer,String)
133: */
134:
135: public void write(Model model, Writer out, String base) {
136: writer.write(model, out, base);
137: }
138:
139: /** Write the model out in N3. The writer should be one suitable for UTF-8 which
140: * excludes a PrintWriter or a FileWriter which use default character set.
141: *
142: * Examples:
143: * <pre>
144: * try {
145: * Writer w = new BufferedWriter(new OutputStreamWriter(output, "UTF-8")) ;
146: * model.write(w, base) ;
147: * try { w.flush() ; } catch (IOException ioEx) {}
148: * } catch (java.io.UnsupportedEncodingException ex) {} //UTF-8 is required so can't happen
149: * </pre>
150: * or
151: * <pre>
152: * try {
153: * OutputStream out = new FileOutputStream(file) ;
154: * Writer w = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")) ;
155: * model.write(w, base) ;
156: * }
157: * catch (java.io.UnsupportedEncodingException ex) {}
158: * catch (java.io.FileNotFoundException noFileEx) { ... }
159: * </pre>
160: * @see #write(Model,Writer,String)
161: */
162:
163: public void write(Model model, OutputStream out, String base) {
164: writer.write(model, out, base);
165: }
166:
167: /**
168: * @see com.hp.hpl.jena.rdf.model.RDFWriter#setProperty(java.lang.String, java.lang.Object)
169: */
170: public Object setProperty(String propName, Object propValue) {
171: return writer.setProperty(propName, propValue);
172: }
173:
174: /**
175: * @see com.hp.hpl.jena.rdf.model.RDFWriter#setErrorHandler(com.hp.hpl.jena.rdf.model.RDFErrorHandler)
176: */
177: public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler) {
178: return writer.setErrorHandler(errHandler);
179: }
180:
181: }
182:
183: /*
184: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
185: * All rights reserved.
186: *
187: * Redistribution and use in source and binary forms, with or without
188: * modification, are permitted provided that the following conditions
189: * are met:
190: * 1. Redistributions of source code must retain the above copyright
191: * notice, this list of conditions and the following disclaimer.
192: * 2. Redistributions in binary form must reproduce the above copyright
193: * notice, this list of conditions and the following disclaimer in the
194: * documentation and/or other materials provided with the distribution.
195: * 3. The name of the author may not be used to endorse or promote products
196: * derived from this software without specific prior written permission.
197: *
198: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
200: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
201: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
203: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
204: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
205: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
206: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
207: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
208: */
|