Coverage Report - org.apache.any23.vocab.RDFSchemaUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
RDFSchemaUtils
0%
0/53
0%
0/20
3.333
RDFSchemaUtils$VocabularyFormat
0%
0/4
N/A
3.333
 
 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.vocab;
 19  
 
 20  
 import org.apache.any23.io.nquads.NQuadsWriter;
 21  
 import org.apache.any23.rdf.RDFUtils;
 22  
 import org.apache.any23.util.DiscoveryUtils;
 23  
 import org.apache.any23.util.StringUtils;
 24  
 import org.openrdf.model.URI;
 25  
 import org.openrdf.model.vocabulary.RDF;
 26  
 import org.openrdf.model.vocabulary.RDFS;
 27  
 import org.openrdf.rio.RDFHandlerException;
 28  
 import org.openrdf.rio.RDFWriter;
 29  
 import org.openrdf.rio.ntriples.NTriplesWriter;
 30  
 import org.openrdf.rio.rdfxml.RDFXMLWriter;
 31  
 
 32  
 import java.io.ByteArrayOutputStream;
 33  
 import java.io.PrintStream;
 34  
 import java.lang.reflect.Constructor;
 35  
 import java.util.List;
 36  
 import java.util.Map;
 37  
 
 38  
 /**
 39  
  * This class provides a set of methods for generating
 40  
  * <a href="http://www.w3.org/TR/rdf-schema/">RDF Schema</a>.
 41  
  *
 42  
  * @author Michele Mostarda (mostarda@fbk.eu)
 43  
  */
 44  
 public class RDFSchemaUtils {
 45  
 
 46  0
     private static final String RDF_XML_SEPARATOR = StringUtils.multiply('=', 100);
 47  
     
 48  
     /**
 49  
      * Supported formats for vocabulary serialization.
 50  
      */
 51  0
     public enum VocabularyFormat {
 52  0
         NQuads,
 53  0
         NTriples,
 54  0
         RDFXML
 55  
     }
 56  
 
 57  
     /**
 58  
      * Serializes a vocabulary composed of the given <code>namespace</code>,
 59  
      * <code>resources</code> and <code>properties</code>.
 60  
      *
 61  
      * @param namespace vocabulary namespace.
 62  
      * @param classes list of classes.
 63  
      * @param properties list of properties.
 64  
      * @param comments map of resource comments.
 65  
      * @param writer writer to print out the RDF Schema triples.
 66  
      * @throws RDFHandlerException
 67  
      */
 68  
     public static void serializeVocabulary(
 69  
             URI namespace,
 70  
             URI[] classes,
 71  
             URI[] properties,
 72  
             Map<URI,String> comments,
 73  
             RDFWriter writer
 74  
     ) throws RDFHandlerException {
 75  0
         writer.startRDF();
 76  0
         for(URI clazz : classes) {
 77  0
             writer.handleStatement( RDFUtils.quad(clazz, RDF.TYPE, RDFS.CLASS, namespace) );
 78  0
             writer.handleStatement( RDFUtils.quad(clazz, RDFS.MEMBER, namespace, namespace) );
 79  0
             final String comment = comments.get(clazz);
 80  0
             if(comment != null)
 81  0
                 writer.handleStatement( RDFUtils.quad(clazz, RDFS.COMMENT, RDFUtils.literal(comment), namespace) );
 82  
         }
 83  0
         for(URI property : properties) {
 84  0
             writer.handleStatement(RDFUtils.quad(property, RDF.TYPE, RDF.PROPERTY, namespace));
 85  0
             writer.handleStatement(RDFUtils.quad(property, RDFS.MEMBER, namespace, namespace));
 86  0
             final String comment = comments.get(property);
 87  0
             if(comment != null)
 88  0
                 writer.handleStatement( RDFUtils.quad(property, RDFS.COMMENT, RDFUtils.literal(comment), namespace) );
 89  
         }
 90  0
         writer.endRDF();
 91  0
     }
 92  
 
 93  
     /**
 94  
      * Serializes the given <code>vocabulary</code> to triples over the given <code>writer</code>.
 95  
      *
 96  
      * @param vocabulary vocabulary to be serialized.
 97  
      * @param writer output writer.
 98  
      * @throws RDFHandlerException
 99  
      */
 100  
     public static void serializeVocabulary(Vocabulary vocabulary, RDFWriter writer)
 101  
     throws RDFHandlerException {
 102  0
         serializeVocabulary(
 103  
                 vocabulary.getNamespace(),
 104  
                 vocabulary.getClasses(),
 105  
                 vocabulary.getProperties(),
 106  
                 vocabulary.getComments(),
 107  
                 writer
 108  
         );
 109  0
     }
 110  
 
 111  
     /**
 112  
      * Serializes the given <code>vocabulary</code> to <i>NQuads</i> over the given output stream.
 113  
      *
 114  
      * @param vocabulary vocabulary to be serialized.
 115  
      * @param format output format for vocabulary.
 116  
      * @param willFollowAnother if <code>true</code> another vocab will be printed in the same stream.
 117  
      * @param ps output stream.
 118  
      * @throws RDFHandlerException
 119  
      */
 120  
     public static void serializeVocabulary(
 121  
             Vocabulary vocabulary,
 122  
             VocabularyFormat format,
 123  
             boolean willFollowAnother,
 124  
             PrintStream ps
 125  
     ) throws RDFHandlerException {
 126  
         final RDFWriter rdfWriter;
 127  0
         if(format == VocabularyFormat.RDFXML) {
 128  0
             rdfWriter = new RDFXMLWriter(ps);
 129  0
             if(willFollowAnother)
 130  0
                 ps.print("\n");
 131  0
                 ps.print(RDF_XML_SEPARATOR);
 132  0
                 ps.print("\n");
 133  0
         } else if(format == VocabularyFormat.NTriples) {
 134  0
             rdfWriter = new NTriplesWriter(ps);
 135  0
         } else if(format == VocabularyFormat.NQuads) {
 136  0
             rdfWriter = new NQuadsWriter(ps);
 137  
         }
 138  
         else {
 139  0
             throw new IllegalArgumentException("Unsupported format " + format);
 140  
         }
 141  0
         serializeVocabulary(vocabulary, rdfWriter);
 142  0
     }
 143  
 
 144  
     /**
 145  
      * Serialized the given <code>vocabulary</code> to <i>NQuads</i> and return them as string.
 146  
      *
 147  
      * @param vocabulary vocabulary to be serialized.
 148  
      * @param format output format for vocabulary.
 149  
      * @return string contained serialization.
 150  
      * @throws RDFHandlerException
 151  
      */
 152  
     public static String serializeVocabulary(Vocabulary vocabulary, VocabularyFormat format)
 153  
     throws RDFHandlerException {
 154  0
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
 155  0
         final PrintStream ps = new PrintStream(baos);
 156  0
         serializeVocabulary(vocabulary, format, false, ps);
 157  0
         ps.close();
 158  0
         return baos.toString();
 159  
     }
 160  
 
 161  
     /**
 162  
      * Serializes all the vocabularies to <i>NQuads</i> over the given output stream.
 163  
      *
 164  
      * @param format output format for vocabularies.
 165  
      * @param ps output print stream.
 166  
      */
 167  
     public static void serializeVocabularies(VocabularyFormat format, PrintStream ps) {
 168  0
         final Class vocabularyClass = Vocabulary.class;
 169  0
         final List<Class> vocabularies = DiscoveryUtils.getClassesInPackage(
 170  
                 vocabularyClass.getPackage().getName(),
 171  
                 vocabularyClass
 172  
         );
 173  0
         int currentIndex = 0;
 174  0
         for (Class vocabClazz : vocabularies) {
 175  
             final Vocabulary instance;
 176  
             try {
 177  0
                 final Constructor constructor = vocabClazz.getDeclaredConstructor();
 178  0
                 constructor.setAccessible(true);
 179  0
                 instance = (Vocabulary) constructor.newInstance();
 180  0
             } catch (Exception e) {
 181  0
                 throw new RuntimeException("Error while instantiating vocabulary class " + vocabClazz, e);
 182  0
             }
 183  
             try {
 184  0
                 serializeVocabulary(instance, format, currentIndex < vocabularies.size() - 2, ps);
 185  0
             } catch (RDFHandlerException rdfhe) {
 186  0
                 throw new RuntimeException("Error while serializing vocabulary.", rdfhe);
 187  0
             }
 188  0
         }
 189  0
     }
 190  
 
 191  0
     private RDFSchemaUtils() {}
 192  
 
 193  
 }