⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 n3writer.java

📁 这是外国一个开源推理机
💻 JAVA
字号:
/*  Sesame - Storage and Querying architecture for RDF and RDF Schema *  Copyright (C) 2001-2005 Aduna * *  Contact:  *  	Aduna *  	Prinses Julianaplein 14 b *  	3817 CS Amersfoort *  	The Netherlands *  	tel. +33 (0)33 465 99 87 *  	fax. +33 (0)33 465 99 87 * *  	http://aduna.biz/ *  	http://www.openrdf.org/ *   *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2.1 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  You should have received a copy of the GNU Lesser General Public *  License along with this library; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */package org.openrdf.rio.n3;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.openrdf.util.StringUtil;import org.openrdf.model.BNode;import org.openrdf.model.Literal;import org.openrdf.model.Resource;import org.openrdf.model.URI;import org.openrdf.model.Value;import org.openrdf.rio.RdfDocumentWriter;import org.openrdf.rio.ntriples.NTriplesUtil;/** * An implementation of the RdfDocumentWriter interface that writes RDF * documents in N3 format. * <p> * <em>Note: due to lack of any clear documentation on N3, it is not known * whether the output adheres for 100% to the N3 format.</em> **/public class N3Writer implements RdfDocumentWriter {/*---------------------------------+| Variables                        |+---------------------------------*/	private Writer _out;	private Map _namespaceTable;	private boolean _writingStarted;	private Resource _lastWrittenSubject;/*---------------------------------+| Constructors                     |+---------------------------------*/	/**	 * Creates a new N3Writer that will write to the supplied OutputStream.	 * 	 * @param out The OutputStream to write the N3 document to.	 **/	public N3Writer(OutputStream out) {		this( new OutputStreamWriter(out) );	}	/**	 * Creates a new N3Writer that will write to the supplied Writer.	 * 	 * @param out The Writer to write the N3 document to.	 **/	public N3Writer(Writer out) {		_out = out;		_namespaceTable = new HashMap();		_writingStarted = false;		_lastWrittenSubject = null;	}/*---------------------------------+| Methods from interface RdfWriter |+---------------------------------*/	public void setNamespace(String prefix, String name)		throws IOException	{		// setNamespace should not overwrite existing namespace mappings		if (!_namespaceTable.containsKey(name)) {			_namespaceTable.put(name, prefix);						if (_writingStarted) {				if (_lastWrittenSubject != null) {					// The last statement still has to be closed:					_out.write(".");					_writeNewLine();										_lastWrittenSubject = null;				}								_writeNamespace(prefix, name);				_writeNewLine();			}		}	}	public void startDocument()		throws IOException	{		_writingStarted = true;		// Write namespace declarations		Iterator nameIterator = _namespaceTable.keySet().iterator();		while (nameIterator.hasNext()) {			String name = (String)nameIterator.next();			String prefix = (String)_namespaceTable.get(name);			_writeNamespace(prefix, name);			_writeNewLine();		}		_writeNewLine();	}	public void endDocument()		throws IOException	{		if (_lastWrittenSubject != null) {			// The last statement still has to be closed:			_out.write(".");			_lastWrittenSubject = null;		}		_out.flush();		_writingStarted = false;	}	public void writeStatement(Resource subj, URI pred, Value obj)		throws IOException	{		if (!_writingStarted) {			throw new RuntimeException("Writing has not yet started");		}		// SUBJECT		if (subj.equals(_lastWrittenSubject)) {			_out.write(";");			_writeNewLine();			_writeIndent();		}		else {			if (_lastWrittenSubject != null) {				// The previous statement still has to be closed:				_out.write(".");				_writeNewLine();			}			// Write new subject:			_writeNewLine();			_writeResource(subj);			_lastWrittenSubject = subj;		}		// PREDICATE		_writeURI(pred);		// OBJECT		if (obj instanceof Resource) {			_writeResource( (Resource)obj );		}		else if (obj instanceof Literal) {			_writeLiteral( (Literal)obj );		}		// Don't close the line just yet. Maybe the next statement		// has the same subject.	}	public void writeComment(String comment)		throws IOException	{		if (_lastWrittenSubject != null) {			// The last statement still has to be closed:			_out.write(".");			_writeNewLine();			_lastWrittenSubject = null;		}		_out.write("# ");		_out.write(comment);		_writeNewLine();	}/*---------------------------------+| Other methods                    |+---------------------------------*/	private void _writeNamespace(String prefix, String name)		throws IOException	{		_out.write("@prefix ");		_out.write(prefix);		_out.write(": <");		_out.write(name);		_out.write("> .");	}	private void _writeResource(Resource res)		throws IOException	{		if (res instanceof BNode) {			_writeBNode((BNode)res);		}		else {			_writeURI((URI)res);		}	}	private void _writeURI(URI uri)		throws IOException	{		String namespace = uri.getNamespace();		String prefix = (String)_namespaceTable.get(namespace);		if (prefix == null) {			_out.write("<");			_out.write(namespace);			_out.write(uri.getLocalName());			_out.write("> ");		}		else {			_out.write(prefix);			_out.write(":");			_out.write(uri.getLocalName());			_out.write(" ");		}	}	private void _writeBNode(BNode bNode)		throws IOException	{		// FIXME: N3 has no concept of named bNodes		_out.write("() ");	}	private void _writeLiteral(Literal lit)		throws IOException	{		// Do some character escaping on the label:		// FIXME: not nice to be dependent on class NTriplesUtil		String s = lit.getLabel();		s = NTriplesUtil.escapeString(s);		s = StringUtil.gsub("'", "\\'", s);		_out.write("\"");		_out.write(s);		_out.write("\"");		// Write the literal's language if it has any:		if (lit.getLanguage() != null) {			_out.write("-");			_out.write(lit.getLanguage());		}		_out.write(" ");	}	private void _writeIndent()		throws IOException	{		_out.write("\t");	}	private void _writeNewLine()		throws IOException	{		_out.write("\n");	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -