Skip to content
Permalink
Browse files
CLEREZZA-1011: Fiexd Handler and added tests, using released dependec…
…ies to allow release
  • Loading branch information
retog committed Jul 8, 2016
1 parent 2506180 commit 03f02db652cf20c03c8fc3786e3ba4655f340951
Showing 5 changed files with 126 additions and 8 deletions.
@@ -84,12 +84,12 @@
<dependency>
<groupId>org.apache.clerezza.commons-rdf</groupId>
<artifactId>commons-rdf-api</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.apache.clerezza.commons-rdf</groupId>
<artifactId>commons-rdf-impl-utils</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
@@ -102,5 +102,17 @@
<version>1.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.core</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.jena.serializer</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -15,8 +15,11 @@
*/
package org.apache.clerezza.commons.rdf.impl.sparql;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -95,7 +98,7 @@ final public static class SparqlsResultsHandler extends DefaultHandler {
private final List<Map<String, RDFTerm>> results = new ArrayList<>();
private boolean readingValue;
private String lang; //the xml:lang attribute of a literal
private String value;
private StringWriter valueWriter;
private Map<String, BlankNode> bNodeMap = new HashMap<>();
private static final IRI XSD_STRING = new IRI("http://www.w3.org/2001/XMLSchema#string");
private static final IRI RDF_LANG_STRING = new IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
@@ -144,6 +147,7 @@ public void startElement(String namespaceURI,
}
lang = atts.getValue("http://www.w3.org/XML/1998/namespace", "lang");
readingValue = true;
valueWriter = new StringWriter();
}
}

@@ -154,7 +158,7 @@ public void startElement(String namespaceURI,
@Override
public void characters(char[] chars, int start, int length) throws SAXException {
if (readingValue) {
value = new String(chars, start, length);
valueWriter.write(chars, start, length);
//System.err.println(value + start + ", " + length);
}
}
@@ -180,13 +184,15 @@ public void endElement(String namespaceURI,
final Language language = lang == null? null : new Language(lang);;
switch (b) {
case uri:
rdfTerm = new IRI(value);
rdfTerm = new IRI(valueWriter.toString());
valueWriter = null;
break;
case bnode:
rdfTerm = getBNode(value);
rdfTerm = getBNode(valueWriter.toString());
valueWriter = null;
break;
case literal:
final String lf = value;
final String lf = valueWriter.toString();
rdfTerm = new AbstractLiteral() {

@Override
@@ -387,6 +387,7 @@ private String createQuery(final BlankNodeOrIRI filterSubject, final IRI filterP
@Override
protected int performSize() {
try {
//TODO replace this with count
return sparqlClient.queryResultSet("SELECT * WHERE { ?s ?p ?o}").size();
} catch (IOException ex) {
throw new RuntimeException(ex);
@@ -399,7 +400,7 @@ private String asSparqlTerm(IRI iri) {

private String asSparqlTerm(Literal literal) {
//TODO langauge and datatype
return "\"" + literal.getLexicalForm() + "\"";
return "\"" + literal.getLexicalForm().replace("\n", "\\n").replace("\"", "\\\"") + "\"";
}

private String asSparqlTerm(BlankNode bnode) {
@@ -0,0 +1,94 @@
/*
* Copyright 2015 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.clerezza.commons.rdf.impl.sparql;

import com.hp.hpl.jena.query.DatasetAccessor;
import com.hp.hpl.jena.query.DatasetAccessorFactory;
import java.io.IOException;
import java.net.ServerSocket;
import org.apache.jena.fuseki.EmbeddedFusekiServer;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.clerezza.commons.rdf.Graph;
import org.apache.clerezza.commons.rdf.IRI;
import org.apache.clerezza.commons.rdf.Language;
import org.apache.clerezza.commons.rdf.Literal;
import org.apache.clerezza.commons.rdf.RDFTerm;
import org.apache.clerezza.commons.rdf.Triple;
import org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl;
import org.apache.clerezza.rdf.core.serializedform.Serializer;
import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
* @author reto
*/
public class DadminTest {

final static int serverPort = findFreePort();
static EmbeddedFusekiServer server;

@BeforeClass
public static void prepare() throws IOException {
final String serviceURI = "http://localhost:" + serverPort + "/ds/data";
final DatasetAccessor accessor = DatasetAccessorFactory.createHTTP(serviceURI);
final InputStream in = DadminTest.class.getResourceAsStream("dadmin.ttl");
final Model m = ModelFactory.createDefaultModel();
String base = "http://example.org/";
m.read(in, base, "TURTLE");
server = EmbeddedFusekiServer.memTDB(serverPort, "/ds");//dataSet.getAbsolutePath());
server.start();
System.out.println("Started fuseki on port " + serverPort);
accessor.putModel(m);
}

@AfterClass
public static void cleanup() {
server.stop();
}

@Test
public void graphSize() {
final Graph graph = new SparqlGraph("http://localhost:" + serverPort + "/ds/query");
Assert.assertEquals("Graph not of the exepected size", 1, graph.size());
}

@Test
public void dump() {
final Graph graph = new SparqlGraph("http://localhost:" + serverPort + "/ds/query");
Serializer serializer = Serializer.getInstance();
serializer.serialize(System.out, graph, SupportedFormat.TURTLE);
}

public static int findFreePort() {
int port = 0;
try (ServerSocket server = new ServerSocket(0);) {
port = server.getLocalPort();
} catch (Exception e) {
throw new RuntimeException("unable to find a free port");
}
return port;
}

}
@@ -0,0 +1,5 @@
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ns1: <http://schema.org/> .

_:b10521
ns1:articleBody "\n <li><a href=\"http://data.admin.ch/datasets\" title=\"Available Data\">Data</a></li>\n <li><a href=\"http://data.admin.ch/apps\" title=\"Applications\">Applications</a></li>\n <li><a href=\"http://data.admin.ch/sparql\" title=\"SPARQL Endpoint\">SPARQL</a></li>\n <li><a title=\"About the Portal\" href=\"http://data.admin.ch/about\">About the Portal</a></li>\n <li><a title=\"Contact Us\" href=\"http://data.admin.ch/contact\">Contact</a></li>\n" .

0 comments on commit 03f02db

Please sign in to comment.