use of org.openrdf.repository.RepositoryConnection in project backstage by zepheira.
the class Database method exportRDFa.
public String exportRDFa(int limit, String title) {
try {
RepositoryConnection con = getRepository().getConnection();
RepositoryResult<Statement> result = con.getStatements(null, null, null, false);
String out = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><style>span {padding:4px}</style><title>" + title + "</title></head><body>";
int itemCount = 0;
Resource prevS = null;
URI prevP = null;
Value prevO = null;
while (result.hasNext() && itemCount <= limit) {
Statement st = result.next();
String currLabel = null;
Resource s = st.getSubject();
String sStr = s.toString();
// strip "http://127.0.0.1/" prefix from subj/pred
if (sStr.startsWith("http://127.0.0.1/")) {
sStr = sStr.substring(17);
}
URI p = st.getPredicate();
String pStr = p.toString();
if (pStr.startsWith("http://127.0.0.1/")) {
pStr = pStr.substring(17);
}
Value o = st.getObject();
String oStr = o.stringValue();
// Determine if our object and subjects are URI
boolean objectIsURI = false;
try {
oStr = (new java.net.URL(oStr).toString());
objectIsURI = true;
} catch (Exception e) {
objectIsURI = false;
}
boolean subjectIsURI = false;
try {
sStr = (new java.net.URL(sStr).toString());
subjectIsURI = true;
} catch (Exception e) {
subjectIsURI = false;
}
// FIXME. Use heuristic to determine if property is an image type. See above re fix.
boolean objectIsImage = false;
if (oStr.endsWith("png") || oStr.endsWith("jpg") || oStr.endsWith("gif") || oStr.endsWith("svg") || oStr.endsWith("jpeg")) {
objectIsImage = true;
}
// arrives before we need it.
if (pStr.equals("http://www.w3.org/2000/01/rdf-schema#label")) {
currLabel = oStr;
}
// group by subject, count as an item
if (s != prevS) {
itemCount++;
if (itemCount > limit)
break;
if (subjectIsURI) {
out += "<p about=\"" + sStr + "\"" + ">\n";
} else {
out += "<p about=\"#" + sStr + "\"" + ">\n";
}
}
// skip externally irrelevant triples or those we handle elsewhere
if (!pStr.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") && !pStr.equals("http://simile.mit.edu/2006/11/exhibit#id") && !pStr.equals("http://www.w3.org/2000/01/rdf-schema#label")) {
if (objectIsURI) {
// if we don't have a label, use subject
if (currLabel == null) {
currLabel = sStr;
}
if (objectIsImage) {
out += "<img property=\"" + pStr + "\" src=\"" + oStr + "\" alt=\"" + currLabel + "\" />";
} else {
out += "<a property=\"" + pStr + "\" href=\"" + oStr + "\">" + currLabel + "</a>";
}
} else {
out += "<span property=\"" + pStr + "\">" + oStr + "</span>\n";
}
}
prevS = s;
prevP = p;
prevO = o;
}
return out + "</body></html>";
} catch (OpenRDFException e) {
return "<html><head><title=\"Error\"></head><body>" + e.toString() + "</body></html>";
}
}
use of org.openrdf.repository.RepositoryConnection in project gocd by gocd.
the class SesameGraphTest method checkWhenAddStatementWithBooleanObjectExplodesItThrowsAShineRuntimeException.
@Test(expected = ShineRuntimeException.class)
public void checkWhenAddStatementWithBooleanObjectExplodesItThrowsAShineRuntimeException() throws RepositoryException {
RDFProperty property = new RDFProperty("http://www.example.com/ontology#foo");
RepositoryConnection badConnection = mock(RepositoryConnection.class);
doThrow(new RepositoryException("")).when(badConnection).add((org.openrdf.model.Resource) any(), (org.openrdf.model.URI) any(), (org.openrdf.model.Literal) any());
org.openrdf.model.ValueFactory stubValueFactory = mock(org.openrdf.model.ValueFactory.class);
when(badConnection.getValueFactory()).thenReturn(stubValueFactory);
SesameURIReference stubSubject = mock(SesameURIReference.class);
when(stubSubject.getSesameNativeResource()).thenReturn(null);
URIReference stubPredicate = mock(URIReference.class);
when(stubPredicate.getURIText()).thenReturn("");
SesameGraph badGraph = new SesameGraph(badConnection, null);
badGraph.addStatement(stubSubject, property, false);
}
use of org.openrdf.repository.RepositoryConnection in project gocd by gocd.
the class SesameGraphTest method checkWhenAddTriplesFromGraphExplodesItThrowsShineRuntimeException.
@Test(expected = ShineRuntimeException.class)
public void checkWhenAddTriplesFromGraphExplodesItThrowsShineRuntimeException() throws RepositoryException {
RepositoryConnection badConnection = mock(RepositoryConnection.class);
doThrow(new RepositoryException("")).when(badConnection).getStatements((org.openrdf.model.Resource) any(), (org.openrdf.model.URI) any(), (org.openrdf.model.Value) any(), anyBoolean());
SesameGraph otherGraph = new SesameGraph(badConnection, null);
SesameGraph badGraph = new SesameGraph(badConnection, null);
badGraph.addTriplesFromGraph(otherGraph);
}
use of org.openrdf.repository.RepositoryConnection in project gocd by gocd.
the class SesameGraphTest method checkWhenAddStatementWithIntObjectExplodesItThrowsAShineRuntimeException.
@Test(expected = ShineRuntimeException.class)
public void checkWhenAddStatementWithIntObjectExplodesItThrowsAShineRuntimeException() throws RepositoryException {
RDFProperty property = new RDFProperty("http://www.example.com/ontology#foo");
RepositoryConnection badConnection = mock(RepositoryConnection.class);
doThrow(new RepositoryException("")).when(badConnection).add((org.openrdf.model.Resource) any(), (org.openrdf.model.URI) any(), (org.openrdf.model.Literal) any());
org.openrdf.model.ValueFactory stubValueFactory = mock(org.openrdf.model.ValueFactory.class);
when(badConnection.getValueFactory()).thenReturn(stubValueFactory);
SesameURIReference stubSubject = mock(SesameURIReference.class);
when(stubSubject.getSesameNativeResource()).thenReturn(null);
URIReference stubPredicate = mock(URIReference.class);
when(stubPredicate.getURIText()).thenReturn("");
SesameGraph badGraph = new SesameGraph(badConnection, null);
badGraph.addStatement(stubSubject, property, 3);
}
use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.
the class RyaAccumuloSailFactoryTest method testAddStatement.
@Ignore
@Test
public void testAddStatement() throws Exception {
SailRepositoryFactory f = new SailRepositoryFactory();
Repository r = f.getRepository(getConfig());
r.initialize();
RepositoryConnection rc = r.getConnection();
ValueFactory vf = rc.getValueFactory();
Statement s = vf.createStatement(vf.createURI("u:a"), vf.createURI("u:b"), vf.createURI("u:c"));
assertFalse(rc.hasStatement(s, false));
rc.add(s);
Assert.assertTrue(rc.hasStatement(s, false));
rc.close();
}
Aggregations