Skip to content

Commit

Permalink
Created new project: Rendering. See issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
enridaga committed Oct 17, 2015
1 parent c43bc6b commit 4b49f70
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 0 deletions.
94 changes: 94 additions & 0 deletions rendering/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>basil</groupId>
<artifactId>parent</artifactId>
<version>0.4.0-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<artifactId>rendering</artifactId>
<packaging>jar</packaging>

<name>BASIL :: Rendering</name>

<description>Tool for Building web Apis SImpLy on top of sparql endpoints</description>

<dependencies>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- Binding for Log4J -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<!-- Log4j API and Core implementation required for binding -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<!-- Dependencies -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>

<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package uk.ac.open.kmi.basil.rendering;

import java.io.OutputStream;
import java.util.Iterator;

import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.atlas.iterator.Transform;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.system.StreamOps;
import org.apache.jena.riot.system.StreamRDF;
import org.apache.jena.riot.system.StreamRDFWriter;
import org.apache.jena.riot.system.StreamRDFWriterFactory;

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.util.iterator.WrappedIterator;

public class RDFStreamer {
static {
StreamRDFWriter.register(Lang.RDFXML, RDFFormat.RDFXML);
StreamRDFWriter.register(RDFFormat.RDFXML, new StreamRDFXMLWriterFactory());
}

public static void stream(OutputStream os, ResultSet rs, RDFFormat format, Transform<QuerySolution, Iterator<Triple>> adapter) {
StreamRDF stream = StreamRDFWriter.getWriterStream(os, format);
Iterator<Triple> iter = WrappedIterator.createIteratorIterator(Iter.map(rs, adapter));
stream.start();
StreamOps.sendTriplesToStream(iter, stream);
stream.finish();
}

static class StreamRDFXMLWriterFactory implements StreamRDFWriterFactory{
@Override
public StreamRDF create(OutputStream output, RDFFormat format) {
return new WriterStreamRDFXML(output);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package uk.ac.open.kmi.basil.rendering;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.jena.atlas.iterator.Transform;

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;

public class SimpleTripleAdapter implements Transform<QuerySolution,Iterator<Triple>> {
private int rowIndex = 0;
private String instanceNS;
private String instanceNamePrefix;
private String propertyNamePrefix;
public SimpleTripleAdapter(String instanceNS) {
this(instanceNS, "row");
}

public SimpleTripleAdapter(String instanceNS, String instanceNamePrefix) {
this(instanceNS, instanceNamePrefix, "col");
}

public SimpleTripleAdapter(String instanceNS, String instanceNamePrefix, String propertyNamePrefix) {
this.instanceNamePrefix = instanceNamePrefix;
this.instanceNS = instanceNS;
this.propertyNamePrefix = propertyNamePrefix;
}

@Override
public Iterator<Triple> convert(QuerySolution qs) {
rowIndex++;
Resource subject = ResourceFactory.createResource(new StringBuilder().append(instanceNS).append(instanceNamePrefix).append(rowIndex).toString());
Property property;
List<Triple> list = new ArrayList<Triple>();
Iterator<String> cn = qs.varNames();
while (cn.hasNext()) {
String c = cn.next();
property = ResourceFactory.createProperty(new StringBuilder().append(instanceNS).append(propertyNamePrefix).append(c).toString());
list.add(new Triple(subject.asNode(), property.asNode(), qs.get(c).asNode()));
}
return list.iterator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package uk.ac.open.kmi.basil.rendering;

import java.io.OutputStream;
import java.util.List;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.jena.riot.writer.WriterStreamRDFBlocks;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.graph.impl.LiteralLabel;

class WriterStreamRDFXML extends WriterStreamRDFBlocks {
private boolean preamble = true;
public WriterStreamRDFXML(OutputStream os) {
super(os);
}

private Pair<String, String> ns(String uri) {
char c = '#';
if (uri.lastIndexOf(c) == -1) {
c = '/';
}
return new ImmutablePair<String, String>(uri.substring(0, uri.lastIndexOf(c) + 1), uri.substring(uri.lastIndexOf(c) + 1));
}

private void openDescription(String subject){
if(preamble){
openRdf();
preamble = false;
}
// Subject
out.print("\n<rdf:Description rdf:about=\"");
out.print(subject);
out.print("\">");
}

private void closeDescription(){
out.print("\n</rdf:Description>");
}
@Override
protected void finalizeRun() {
closeRdf();
super.finalizeRun();
}
private void openRdf(){
out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">");
}

private void closeRdf(){
out.print("\n</rdf:RDF>");
}

@Override
protected void printBatchTriples(Node s, List<Triple> triples) {
openDescription(s.getURI());
for(Triple triple : triples){
// Property
// XMLNS
out.print("\n\t<p:");
Pair<String, String> pp = ns(triple.getPredicate().getURI());
out.print(pp.getRight());
out.print(" xmlns:p=\"");
out.print(pp.getLeft());
out.print("\"");
// Object
if (triple.getObject().isURI()) {
out.print(' ');
out.print("rdf:resource=\"");
out.print(triple.getObject().getURI());
out.print("\"/>");
} else if (triple.getObject().isBlank()) {
out.print(' ');
out.print("rdf:nodeID=\"");
out.print(triple.getObject().getBlankNodeLabel());
out.print("\"/>");
} else if (triple.getObject().isLiteral()) {
LiteralLabel l = triple.getObject().getLiteral();
if (!l.language().equals("")) {
// Lang
out.print(' ');
out.print("xml:lang=\"");
out.print(l.language());
out.print("\">");
out.print(l.getLexicalForm());
out.print("</");
out.print(pp.getRight());
out.print(">");
} else if (l.getDatatypeURI() != null) {
// Lang
out.print(' ');
out.print("rdf:datatype=\"");
out.print(l.getDatatypeURI());
out.print("\">");
out.print(l.getLexicalForm());
out.print("</p:");
out.print(pp.getRight());
out.print(">");
}else{
out.print(">");
out.print(l.getLexicalForm());
out.print("</");
out.print(pp.getRight());
out.print(">");
}
}
}
closeDescription();
}

// @Override
// protected void reset() {
//
// };

}
Loading

0 comments on commit 4b49f70

Please sign in to comment.