public Iterable<Triple> parse(final String content) throws IOException { TripleSink sink = getTripleSink(); TripleProducer producer = getProducer(content); producer.injectTriples(sink); return sink.triples(); } }
protected final RDFModelParser getParser() { return new RDFModelParser( getConfiguration().getBase(), getConfiguration().getFormat(), UnmarshallOptions.style(getConfiguration()), UnmarshallOptions.ordering(getConfiguration())); }
@Override public <T> Unmarshaller<T> newUnmarshaller(Format format, T source) { Unmarshaller<T> unmarshaller=null; if(CharSequence.class.isInstance(source)) { @SuppressWarnings("unchecked") Unmarshaller<T> tmp = (Unmarshaller<T>) new CharSequenceUnmarshaller(); unmarshaller=tmp; } else if(InputStream.class.isInstance(source)) { @SuppressWarnings("unchecked") Unmarshaller<T> tmp = (Unmarshaller<T>) new InputStreamUnmarshaller(); unmarshaller=tmp; } else if(Reader.class.isInstance(source)) { @SuppressWarnings("unchecked") Unmarshaller<T> tmp = (Unmarshaller<T>) new ReaderUnmarshaller(); unmarshaller=tmp; } else { unmarshaller=new UnavailableUnmarshaller<T>(format, source); } return unmarshaller; }
@Override public <T> Marshaller<T> newMarshaller(Format format, T output) { Marshaller<T> marshaller=null; if(Writer.class.isInstance(output)) { @SuppressWarnings("unchecked") Marshaller<T> tmp = (Marshaller<T>)new WriterMarshaller(); marshaller=tmp; } else if(OutputStream.class.isInstance(output)) { @SuppressWarnings("unchecked") Marshaller<T> tmp = (Marshaller<T>)new OutputStreamMarshaller(); marshaller=tmp; } else { marshaller=new UnavailableMarshaller<T>(format, output); } return marshaller; }
@Override public void marshall(Iterable<Triple> triples, OutputStream target) throws IOException { String output = new RDFModelFormater(getConfiguration().getBase(),getConfiguration().getNamespaces(),getConfiguration().getFormat()).format(triples); IOUtils.write(output, target); }
@Override public void marshall(Iterable<Triple> triples, Writer target) throws IOException { String output = new RDFModelFormater(getConfiguration().getBase(),getConfiguration().getNamespaces(),getConfiguration().getFormat()).format(triples); target.write(output); }
private void importRepository(RepositoryConnection connection, TripleSink sink) throws RepositoryException { RepositoryResult<Statement> statements = null; try { statements=connection.getStatements(null, null, null, false); SesameModelParser tripleParser=new SesameModelParser(getNamespaces(connection)); while(statements.hasNext()) { sink.addTriple(tripleParser.parseStatement(statements.next())); } } finally { closeQuietly(statements, "Could not close results after parsing statements"); } }
private TripleSink getTripleSink() { TripleSink sink=null; switch(ordering) { case KEEP_TRIPLE_ORDER: sink=new UnorderedTripleSink(); break; case SORT_TRIPLES: sink=new SortingTripleSink(); break; default: throw new AssertionError("Unsupported ordering '"+ordering+"'"); } return sink; }
public TripleFormater(URI baseURI) { this.baseURI = baseURI; this.valueFactory = new MemValueFactory(); this.subjectFormater = new SubjectFormater(); this.objectFormater = new ObjectFormater(); }
private void populateRepository(Iterable<Triple> triples, RDFHandler handler) throws RDFHandlerException { RDFModelFormater.TripleFormater translator = new TripleFormater(baseURI); for(Triple t:triples) { handler.handleStatement(translator.formatTriple(t)); } }
public String format(Iterable<Triple> triples) throws IOException { StringWriter writer=new StringWriter(); try { exportTriples(triples, createWriter(writer)); return writer.toString(); } catch (RDFHandlerException e) { throw new IOException(e); } }
protected String exportRepository(RepositoryConnection connection) throws RepositoryException, RDFHandlerException { StringWriter writer=new StringWriter(); RDFWriter rdfWriter=Rio.createWriter(getFormat(),writer); if(rdfWriter instanceof TurtleWriter) { rdfWriter=new RebasingTurtleWriter(writer); } connection.export(rdfWriter); return writer.toString(); }
private void exportTriples(Iterable<Triple> triples, RDFHandler handler) throws RDFHandlerException { handler.startRDF(); addNamespaces(handler); populateRepository(triples, handler); handler.endRDF(); }
@Override public void handleStatement(Statement st) throws RDFHandlerException { this.getStatements().add(st); }
private static boolean isValidTransformer(Class<?> transformableClass, Class<?> transformerImplementationClass) { boolean result=false; if(Transformer.class.isAssignableFrom(transformerImplementationClass)) { TypeVariable<?> targetVariable = Transformer.class.getTypeParameters()[0]; Class<?> seed=transformerImplementationClass; while(!result && seed!=null) { TypeVariable<?>[] typeParameters = seed.getTypeParameters(); for(int i=0;i<typeParameters.length && !result ; i++) { result=checkVariable(typeParameters[i], targetVariable, transformableClass); } seed=seed.getSuperclass(); } } return result; }
@Override public void handleNamespace(String prefix, String uri) throws RDFHandlerException { this.getNamespaces().addPrefix(prefix,uri); }
private <T> Transformer<T> getTransformerFromAnnotation(Class<? extends T> transformable) { Transformer<T> result=null; for (Class<?> clz=transformable; clz!=null && result==null; clz=clz.getSuperclass()) { Transformable annotation = clz.getAnnotation(Transformable.class); if(annotation!=null) { result=tryAnnotation(annotation.transformer(), transformable, clz); } } return result; }