01: /*
02: * LICENSE INFORMATION
03: * Copyright 2005-2007 by FZI (http://www.fzi.de).
04: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
05: * <OWNER> = Max Völkel
06: * <ORGANIZATION> = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe, Germany
07: * <YEAR> = 2007
08: *
09: * Project information at http://semweb4j.org/rdf2go
10: */
11: package org.ontoware.rdf2go.model.impl;
12:
13: import java.io.IOException;
14: import java.io.Writer;
15:
16: import org.ontoware.rdf2go.exception.ModelRuntimeException;
17: import org.ontoware.rdf2go.model.ModelWriter;
18: import org.ontoware.rdf2go.model.node.BlankNode;
19: import org.ontoware.rdf2go.model.node.DatatypeLiteral;
20: import org.ontoware.rdf2go.model.node.LanguageTagLiteral;
21: import org.ontoware.rdf2go.model.node.Node;
22: import org.ontoware.rdf2go.model.node.PlainLiteral;
23: import org.ontoware.rdf2go.model.node.Resource;
24: import org.ontoware.rdf2go.model.node.URI;
25:
26: /**
27: * Writes a simple Model in TRiX syntax, using a given context URI.
28: * @author voelkel
29: */
30: public class StatementWriter extends AbstractModelWriter implements
31: ModelWriter {
32:
33: private Writer writer;
34:
35: /**
36: * @param w
37: * @throws IOException
38: */
39: public StatementWriter(Writer w, URI graphName) throws IOException {
40: this .writer = w;
41:
42: w
43: .write("<TriX xmlns=\"http://www.w3.org/2004/03/trix/trix-1/\">\n"
44: + // .
45: " <graph>\n" + // .
46: " <uri>" + graphName + "</uri>\n");
47: }
48:
49: public void close() throws IOException {
50: this .writer.write(" </graph>\n" + "</TriX>");
51: }
52:
53: @Override
54: public void addStatement(Resource subject, URI predicate,
55: Node object) throws ModelRuntimeException {
56:
57: try {
58: this .writer.write(" <triple>\n");
59: writeNode(subject);
60: writeNode(predicate);
61: writeNode(object);
62: this .writer.write(" </triple>\n");
63: } catch (IOException e) {
64: throw new ModelRuntimeException(e);
65: }
66:
67: }
68:
69: private void writeNode(Object node) throws IOException {
70: if (node instanceof URI) {
71: this .writer.write(" <uri>" + ((URI) node).toString()
72: + "</uri>\n");
73: } else if (node instanceof BlankNode) {
74: this .writer.write(" <id>"
75: + ((BlankNode) node).toString() + "</id>\n");
76: } else if (node instanceof DatatypeLiteral) {
77: this .writer.write(" <typedLiteral datatype=\""
78: + ((DatatypeLiteral) node).getDatatype() + "\">"
79: + ((DatatypeLiteral) node).getValue()
80: + "</typedLiteral>\n");
81: } else if (node instanceof LanguageTagLiteral) {
82: this .writer.write(" <plainLiteral xml:lang=\""
83: + ((LanguageTagLiteral) node).getLanguageTag()
84: + "\">" + ((LanguageTagLiteral) node).toString()
85: + "</plainLiteral>\n");
86: } else if (node instanceof PlainLiteral) {
87: this .writer.write(" <plainLiteral>"
88: + ((PlainLiteral) node).getValue()
89: + "</plainLiteral>\n");
90: } else
91: throw new RuntimeException("Cannot write to RDF: "
92: + node.getClass());
93: }
94: }
|