001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.rio.ntriples;
007:
008: import java.io.IOException;
009: import java.io.OutputStream;
010: import java.io.OutputStreamWriter;
011: import java.io.Writer;
012: import java.nio.charset.Charset;
013:
014: import org.openrdf.model.BNode;
015: import org.openrdf.model.Literal;
016: import org.openrdf.model.Resource;
017: import org.openrdf.model.Statement;
018: import org.openrdf.model.URI;
019: import org.openrdf.model.Value;
020: import org.openrdf.rio.RDFFormat;
021: import org.openrdf.rio.RDFHandlerException;
022: import org.openrdf.rio.RDFWriter;
023:
024: /**
025: * An implementation of the RDFWriter interface that writes RDF documents in
026: * N-Triples format. The N-Triples format is defined in <a
027: * href="http://www.w3.org/TR/rdf-testcases/#ntriples">this section</a> of the
028: * RDF Test Cases document.
029: */
030: public class NTriplesWriter implements RDFWriter {
031:
032: /*-----------*
033: * Variables *
034: *-----------*/
035:
036: private Writer writer;
037:
038: private boolean writingStarted;
039:
040: /*--------------*
041: * Constructors *
042: *--------------*/
043:
044: /**
045: * Creates a new NTriplesWriter that will write to the supplied OutputStream.
046: *
047: * @param out
048: * The OutputStream to write the N-Triples document to.
049: */
050: public NTriplesWriter(OutputStream out) {
051: this (new OutputStreamWriter(out, Charset.forName("US-ASCII")));
052: }
053:
054: /**
055: * Creates a new NTriplesWriter that will write to the supplied Writer.
056: *
057: * @param writer
058: * The Writer to write the N-Triples document to.
059: */
060: public NTriplesWriter(Writer writer) {
061: this .writer = writer;
062: writingStarted = false;
063: }
064:
065: /*---------*
066: * Methods *
067: *---------*/
068:
069: public RDFFormat getRDFFormat() {
070: return RDFFormat.NTRIPLES;
071: }
072:
073: public void startRDF() throws RDFHandlerException {
074: if (writingStarted) {
075: throw new RuntimeException(
076: "Document writing has already started");
077: }
078:
079: writingStarted = true;
080: }
081:
082: public void endRDF() throws RDFHandlerException {
083: if (!writingStarted) {
084: throw new RuntimeException(
085: "Document writing has not yet started");
086: }
087:
088: try {
089: writer.flush();
090: } catch (IOException e) {
091: throw new RDFHandlerException(e);
092: } finally {
093: writingStarted = false;
094: }
095: }
096:
097: public void handleNamespace(String prefix, String name) {
098: // N-Triples does not support namespace prefixes.
099: }
100:
101: public void handleStatement(Statement st)
102: throws RDFHandlerException {
103: if (!writingStarted) {
104: throw new RuntimeException(
105: "Document writing has not yet been started");
106: }
107:
108: Resource subj = st.getSubject();
109: URI pred = st.getPredicate();
110: Value obj = st.getObject();
111:
112: try {
113: // SUBJECT
114: writeResource(subj);
115: writer.write(" ");
116:
117: // PREDICATE
118: writeURI(pred);
119: writer.write(" ");
120:
121: // OBJECT
122: if (obj instanceof Resource) {
123: writeResource((Resource) obj);
124: } else if (obj instanceof Literal) {
125: writeLiteral((Literal) obj);
126: }
127:
128: writer.write(" .");
129: writeNewLine();
130: } catch (IOException e) {
131: throw new RDFHandlerException(e);
132: }
133: }
134:
135: public void handleComment(String comment)
136: throws RDFHandlerException {
137: try {
138: writer.write("# ");
139: writer.write(comment);
140: writeNewLine();
141: } catch (IOException e) {
142: throw new RDFHandlerException(e);
143: }
144: }
145:
146: private void writeResource(Resource res) throws IOException {
147: if (res instanceof BNode) {
148: writeBNode((BNode) res);
149: } else {
150: writeURI((URI) res);
151: }
152: }
153:
154: private void writeURI(URI uri) throws IOException {
155: writer.write(NTriplesUtil.toNTriplesString(uri));
156: }
157:
158: private void writeBNode(BNode bNode) throws IOException {
159: writer.write(NTriplesUtil.toNTriplesString(bNode));
160: }
161:
162: private void writeLiteral(Literal lit) throws IOException {
163: writer.write(NTriplesUtil.toNTriplesString(lit));
164: }
165:
166: private void writeNewLine() throws IOException {
167: writer.write("\n");
168: }
169: }
|