| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public class RDFSchemaUtils { |
| 45 | |
|
| 46 | 0 | private static final String RDF_XML_SEPARATOR = StringUtils.multiply('=', 100); |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | public enum VocabularyFormat { |
| 52 | 0 | NQuads, |
| 53 | 0 | NTriples, |
| 54 | 0 | RDFXML |
| 55 | |
} |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 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 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 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 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 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 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 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 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 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 | |
} |