Coverage Report - org.apache.any23.io.nquads.NQuadsWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
NQuadsWriter
0%
0/94
0%
0/28
2.5
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *  http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.any23.io.nquads;
 19  
 
 20  
 import org.openrdf.model.BNode;
 21  
 import org.openrdf.model.Literal;
 22  
 import org.openrdf.model.Resource;
 23  
 import org.openrdf.model.Statement;
 24  
 import org.openrdf.model.URI;
 25  
 import org.openrdf.model.Value;
 26  
 import org.openrdf.rio.RDFFormat;
 27  
 import org.openrdf.rio.RDFHandlerException;
 28  
 import org.openrdf.rio.RDFWriter;
 29  
 import org.openrdf.rio.ntriples.NTriplesUtil;
 30  
 
 31  
 import java.io.IOException;
 32  
 import java.io.OutputStream;
 33  
 import java.io.OutputStreamWriter;
 34  
 import java.io.Writer;
 35  
 import java.util.HashMap;
 36  
 import java.util.Map;
 37  
 
 38  
 /**
 39  
  * <i>N-Quads</i> implementation of an {@link org.openrdf.rio.RDFWriter}.
 40  
  * See the format specification <a href="http://sw.deri.org/2008/07/n-quads/">here</a>.
 41  
  *
 42  
  * @author Michele Mostarda (mostarda@fbk.eu)
 43  
  */
 44  
 public class NQuadsWriter implements RDFWriter {
 45  
 
 46  
     /**
 47  
      * The table maintaining namespaces.
 48  
      */
 49  
     private Map<String,String> namespaceTable;
 50  
 
 51  
     /**
 52  
      * The output writer.
 53  
      */
 54  
     private Writer writer;
 55  
 
 56  
     /**
 57  
      * Maintain the started status.
 58  
      */
 59  0
     private boolean started = false;
 60  
 
 61  
     public NQuadsWriter(OutputStream os) {
 62  0
         this( new OutputStreamWriter(os) );
 63  0
     }
 64  
 
 65  0
     public NQuadsWriter(Writer w) {
 66  0
         if(w == null) {
 67  0
             throw new NullPointerException("the writer cannot be null.");
 68  
         }
 69  0
         writer = w;
 70  0
     }
 71  
 
 72  
     public RDFFormat getRDFFormat() {
 73  0
         return NQuads.FORMAT;
 74  
     }
 75  
 
 76  
     public void startRDF() throws RDFHandlerException {
 77  0
         if(started) {
 78  0
             throw new IllegalStateException("Parsing already started.");
 79  
         }
 80  0
         started = true;
 81  0
     }
 82  
 
 83  
     public void endRDF() throws RDFHandlerException {
 84  0
         if(!started) {
 85  0
             throw new IllegalStateException("Parsing never started.");
 86  
         }
 87  
 
 88  
         try {
 89  0
             writer.flush();
 90  0
         } catch (IOException ioe) {
 91  0
             throw new RDFHandlerException("Error while flushing writer.", ioe);
 92  
         } finally {
 93  0
             started = false;
 94  0
             if(namespaceTable != null) {
 95  0
                 namespaceTable.clear();
 96  
             }
 97  
         }
 98  0
     }
 99  
 
 100  
     public void handleNamespace(String ns, String uri) throws RDFHandlerException {
 101  0
         if(!started) {
 102  0
             throw new IllegalStateException("Parsing never started.");
 103  
         }
 104  
 
 105  0
         if(namespaceTable == null) {
 106  0
             namespaceTable = new HashMap<String, String>();
 107  
         }
 108  0
         namespaceTable.put(ns, NTriplesUtil.escapeString(uri) );
 109  0
     }
 110  
 
 111  
     public void handleStatement(Statement statement) throws RDFHandlerException {
 112  0
         if(!started) {
 113  0
             throw new IllegalStateException("Cannot handle statement without start parsing first.");
 114  
         }
 115  
 
 116  
         try {
 117  0
             printSubject(statement);
 118  0
             printSpace();
 119  0
             printPredicate(statement);
 120  0
             printSpace();
 121  0
             printObject(statement);
 122  0
             printSpace();
 123  0
             printGraph(statement);
 124  0
             printCloseStatement();
 125  0
         } catch (IOException ioe) {
 126  0
             throw new RDFHandlerException("An error occurred while printing statement.", ioe);
 127  0
         }
 128  0
     }
 129  
 
 130  
     public void handleComment(String comment) throws RDFHandlerException {
 131  
         try {
 132  0
             writer.write("# ");
 133  0
             writer.write(comment);
 134  0
             writer.append('\n');
 135  0
         } catch (IOException ioe) {
 136  0
             throw new RDFHandlerException("An error occurred while printing comment.", ioe);
 137  0
         }
 138  0
     }
 139  
 
 140  
     /**
 141  
      * Prints out a space.
 142  
      *
 143  
      * @throws IOException
 144  
      */
 145  
     private void printSpace() throws IOException {
 146  0
         writer.append(' ');
 147  0
     }
 148  
 
 149  
     /**
 150  
      * Prints out the close statement.
 151  
      * 
 152  
      * @throws IOException
 153  
      */
 154  
     private void printCloseStatement() throws IOException {
 155  0
         writer.append(" .\n");
 156  0
     }
 157  
 
 158  
     /**
 159  
      * Prints out a URI string, replacing the existing prefix if found.
 160  
      * 
 161  
      * @param uri the URI to print.
 162  
      * @throws IOException
 163  
      */
 164  
     private void printURI(URI uri) throws IOException {
 165  0
         final String uriString = uri.stringValue();
 166  0
         int splitIdx = 0;
 167  0
         String namespace = null;
 168  0
         if(namespaceTable != null) {
 169  0
             splitIdx = uriString.indexOf(':');
 170  0
             if (splitIdx > 0) {
 171  0
                 String prefix = uriString.substring(0, splitIdx);
 172  0
                 namespace = namespaceTable.get(prefix);
 173  
             }
 174  
         }
 175  
 
 176  0
         if (namespace != null) {
 177  0
             writer.append('<');
 178  0
             writer.append(namespace);
 179  0
             writer.append( NTriplesUtil.escapeString(uriString.substring(splitIdx)) );
 180  0
             writer.append('>');
 181  
         } else {
 182  0
             writer.append('<');
 183  0
             writer.append( NTriplesUtil.escapeString(uriString) );
 184  0
             writer.append('>');
 185  
         }
 186  0
     }
 187  
 
 188  
     /**
 189  
      * Prints out the bnode.
 190  
      *
 191  
      * @param b bnode value.
 192  
      * @throws IOException
 193  
      */
 194  
     private void printBNode(BNode b) throws IOException {
 195  0
         writer.append( NTriplesUtil.toNTriplesString(b) );
 196  0
     }
 197  
 
 198  
     /**
 199  
      * Prints out the resource.
 200  
      *
 201  
      * @param r resource value.
 202  
      * @throws java.io.IOException
 203  
      */
 204  
     private void printResource(Resource r) throws IOException {
 205  0
         if(r instanceof BNode) {
 206  0
             printBNode((BNode) r);
 207  0
         } else if(r instanceof URI) {
 208  0
             printURI((URI) r);
 209  
         } else {
 210  0
             throw new IllegalStateException();
 211  
         }
 212  0
     }
 213  
 
 214  
     /**
 215  
      * Prints out a literal value.
 216  
      *
 217  
      * @param l literal value.
 218  
      * @throws java.io.IOException
 219  
      */
 220  
     private void printLiteral(Literal l) throws IOException {
 221  0
         writer.append( NTriplesUtil.toNTriplesString(l) );
 222  0
     }
 223  
 
 224  
     /**
 225  
      * Prints out the subject.
 226  
      * 
 227  
      * @param s
 228  
      * @throws IOException
 229  
      */
 230  
     private void printSubject(Statement s) throws IOException {
 231  0
         printResource( s.getSubject() );
 232  0
     }
 233  
 
 234  
     /**
 235  
      * Prints out the predicate.
 236  
      *
 237  
      * @param s
 238  
      * @throws IOException
 239  
      */
 240  
     private void printPredicate(Statement s) throws IOException {
 241  0
         printURI( s.getPredicate() );
 242  0
     }
 243  
 
 244  
     /**
 245  
      * Prints out the object, handling all the logic to manage
 246  
      * the literal data type / language attribute.
 247  
      *
 248  
      * @param s
 249  
      * @throws IOException
 250  
      */
 251  
     private void printObject(Statement s) throws IOException {
 252  0
         Value v = s.getObject();
 253  0
         if(v instanceof Resource) {
 254  0
             printResource((Resource) v);
 255  0
             return;
 256  
         }
 257  0
         printLiteral( (Literal) v );
 258  0
     }
 259  
 
 260  
     /**
 261  
      * Prints out the graph.
 262  
      *
 263  
      * @param s
 264  
      * @throws IOException
 265  
      */
 266  
     private void printGraph(Statement s) throws IOException {
 267  0
         Resource graph = s.getContext();
 268  0
         if(graph != null) {
 269  0
             printResource( s.getContext() );
 270  
         }
 271  0
     }
 272  
 
 273  
 }