001/*
002 * (c) Copyright 2010 University of Bristol
003 * All rights reserved.
004 * [See end of file]
005 */
006
007package net.rootdev.javardfa.output;
008
009import java.io.OutputStream;
010import java.io.OutputStreamWriter;
011import java.io.UnsupportedEncodingException;
012import java.io.Writer;
013
014
015/**
016 * A pretty ropey turtle serialiser.
017 * Advantages: streams, no dependencies.
018 *
019 * @author pldms
020 */
021public class TurtleSink extends NTripleSink {
022
023    private String currentSubject = null;
024    private String currentPredicate = null;
025
026    public TurtleSink(OutputStream os, String... comments) throws UnsupportedEncodingException {
027        super(new OutputStreamWriter(os, "UTF-8"), comments);
028    }
029
030    public TurtleSink(Writer writer, String... comments) {
031        super(writer, comments);
032    }
033
034    @Override
035    public void end() {
036        out.println(".");
037        super.end();
038    }
039
040    @Override
041    public void addObject(String subject, String predicate, String object) {
042        emitTriple(subject, predicate, toNode(object));
043    }
044
045    @Override
046    public void addLiteral(String subject, String predicate, String lex, String lang, String datatype) {
047        emitTriple(subject, predicate, toLiteral(lex, lang, datatype));
048    }
049
050    private void emitTriple(String subject, String predicate, String objectEncoded) {
051        if (subject.equals(currentSubject)) { // We can at least ';'
052            if (predicate.equals(currentPredicate)) { // We can ','
053                out.println(",");
054                out.print("\t\t");
055                out.print(objectEncoded);
056            } else {
057                out.println(";");
058                out.print("\t");
059                out.println(toNode(predicate));
060                out.print("\t\t");
061                out.print(objectEncoded);
062                currentPredicate = predicate;
063            }
064        } else {
065            if (currentSubject != null) out.println(".");
066            out.println(toNode(subject));
067            out.print("\t");
068            out.println(toNode(predicate));
069            out.print("\t\t");
070            out.print(objectEncoded);
071            currentPredicate = predicate;
072            currentSubject = subject;
073        }
074    }
075
076    @Override
077    protected final String enc(int codepoint) {
078        return new String( new int[] {codepoint},0,1);
079    }
080
081    @Override
082    protected final String longenc(int codepoint) {
083        return new String( new int[] {codepoint},0,1);
084    }
085}
086
087/*
088 * (c) Copyright 2009 University of Bristol
089 * All rights reserved.
090 *
091 * Redistribution and use in source and binary forms, with or without
092 * modification, are permitted provided that the following conditions
093 * are met:
094 * 1. Redistributions of source code must retain the above copyright
095 *    notice, this list of conditions and the following disclaimer.
096 * 2. Redistributions in binary form must reproduce the above copyright
097 *    notice, this list of conditions and the following disclaimer in the
098 *    documentation and/or other materials provided with the distribution.
099 * 3. The name of the author may not be used to endorse or promote products
100 *    derived from this software without specific prior written permission.
101 *
102 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
103 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
104 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
105 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
106 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
107 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
108 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
109 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
110 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
111 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
112 */