All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ontoware.rdf2go.model.impl.StatementWriter Maven / Gradle / Ivy

Go to download

RDF2go is an implementation-independent Java API with the design goals: portability (hence the name), performance and ease of implementation. This project was started at FZI Forschungszentrum Informatik Karlsruhe, Germany - www.fzi.de

The newest version!
/**
 * LICENSE INFORMATION
 * 
 * Copyright 2005-2008 by FZI (http://www.fzi.de). Licensed under a BSD license
 * (http://www.opensource.org/licenses/bsd-license.php)  = Max Völkel
 *  = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe,
 * Germany  = 2010
 * 
 * Further project information at http://semanticweb.org/wiki/RDF2Go
 */

package org.ontoware.rdf2go.model.impl;

import java.io.IOException;
import java.io.Writer;

import org.ontoware.rdf2go.exception.ModelRuntimeException;
import org.ontoware.rdf2go.model.ModelWriter;
import org.ontoware.rdf2go.model.node.BlankNode;
import org.ontoware.rdf2go.model.node.DatatypeLiteral;
import org.ontoware.rdf2go.model.node.LanguageTagLiteral;
import org.ontoware.rdf2go.model.node.Node;
import org.ontoware.rdf2go.model.node.PlainLiteral;
import org.ontoware.rdf2go.model.node.Resource;
import org.ontoware.rdf2go.model.node.URI;


/**
 * Writes a simple Model in TRiX syntax, using a given context URI.
 * 
 * @author voelkel
 */
public class StatementWriter extends AbstractModelWriter implements ModelWriter {
	
	private Writer writer;
	
	/**
	 * @param w writer
	 * @throws IOException from writer
	 */
	public StatementWriter(Writer w, URI graphName) throws IOException {
		this.writer = w;
		
		w.write("\n" + // .
		        "  \n" + // .
		        "    " + graphName + "\n");
	}
	
	public void close() throws IOException {
		this.writer.write("  \n" + "");
	}
	
	@Override
	public void addStatement(Resource subject, URI predicate, Node object)
	        throws ModelRuntimeException {
		
		try {
			this.writer.write("    \n");
			writeNode(subject);
			writeNode(predicate);
			writeNode(object);
			this.writer.write("    \n");
		} catch(IOException e) {
			throw new ModelRuntimeException(e);
		}
		
	}
	
	private void writeNode(Object node) throws IOException {
		if(node instanceof URI) {
			this.writer.write("      " + ((URI)node).toString() + "\n");
		} else if(node instanceof BlankNode) {
			this.writer.write("      " + ((BlankNode)node).toString() + "\n");
		} else if(node instanceof DatatypeLiteral) {
			this.writer.write("      "
			        + ((DatatypeLiteral)node).getValue() + "\n");
		} else if(node instanceof LanguageTagLiteral) {
			this.writer.write("      "
			        + ((LanguageTagLiteral)node).toString() + "\n");
		} else if(node instanceof PlainLiteral) {
			this.writer.write("      " + ((PlainLiteral)node).getValue()
			        + "\n");
		} else
			throw new RuntimeException("Cannot write to RDF: " + node.getClass());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy