001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.fcrepo.jena; 020 021import org.apache.jena.atlas.io.AWriter ; 022import org.apache.jena.atlas.io.IO ; 023import org.apache.jena.atlas.lib.CharSpace ; 024import org.apache.jena.graph.Node ; 025import org.apache.jena.graph.Triple ; 026import org.apache.jena.riot.out.NodeFormatter ; 027import org.apache.jena.riot.out.NodeFormatterNT ; 028import org.apache.jena.riot.system.StreamRDF ; 029import org.apache.jena.riot.system.StreamRDFLib ; 030import org.apache.jena.sparql.core.Quad ; 031 032/** 033 * An output of triples / quads that is streaming. It writes N-triples/N-quads. 034 */ 035 036public class CopyOfWriterStreamRDFPlain implements StreamRDF { 037 // This class is the overall structure - the NodeFormatter controls the 038 // appearance of the Nodes themselves. 039 040 // NOTE, Fedora change: remove 'final' on NodeFormatter 041 protected final AWriter out ; 042 protected NodeFormatter nodeFmt ; 043 044 /** 045 * Output tuples, using UTF8 output See {@link StreamRDFLib#writer} for 046 * ways to create a AWriter object. 047 */ 048 public CopyOfWriterStreamRDFPlain(AWriter w) { 049 this(w, CharSpace.UTF8) ; 050 } 051 052 /** 053 * Output tuples, choosing ASCII or UTF8 See 054 * {@link StreamRDFLib#writer} for ways to create a AWriter object. 055 */ 056 public CopyOfWriterStreamRDFPlain(AWriter w, CharSpace charSpace) { 057 out = w ; 058 nodeFmt = new NodeFormatterNT(charSpace) ; 059 } 060 061 @Override 062 public void start() {} 063 064 @Override 065 public void finish() { 066 IO.flush(out) ; 067 } 068 069 @Override 070 public void triple(Triple triple) { 071 Node s = triple.getSubject() ; 072 Node p = triple.getPredicate() ; 073 Node o = triple.getObject() ; 074 075 format(s) ; 076 out.print(" ") ; 077 format(p) ; 078 out.print(" ") ; 079 format(o) ; 080 out.print(" .\n") ; 081 } 082 083 @Override 084 public void quad(Quad quad) { 085 Node s = quad.getSubject() ; 086 Node p = quad.getPredicate() ; 087 Node o = quad.getObject() ; 088 Node g = quad.getGraph() ; 089 090 format(s) ; 091 out.print(" ") ; 092 format(p) ; 093 out.print(" ") ; 094 format(o) ; 095 096 if ( outputGraphSlot(g) ) { 097 out.print(" ") ; 098 format(g) ; 099 } 100 out.print(" .\n") ; 101 } 102 103 private void format(Node n) { 104 nodeFmt.format(out, n) ; 105 } 106 107 @Override 108 public void base(String base) {} 109 110 @Override 111 public void prefix(String prefix, String iri) {} 112 113 private static boolean outputGraphSlot(Node g) { 114 return (g != null && g != Quad.tripleInQuad && !Quad.isDefaultGraph(g)) ; 115 } 116}