Search in sources :

Example 1 with RepositoryConnection

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>";
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) OpenRDFException(org.openrdf.OpenRDFException) URI(org.openrdf.model.URI) SailException(org.openrdf.sail.SailException) OpenRDFException(org.openrdf.OpenRDFException) Value(org.openrdf.model.Value)

Example 2 with RepositoryConnection

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);
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) Test(org.junit.Test)

Example 3 with RepositoryConnection

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);
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) Test(org.junit.Test)

Example 4 with RepositoryConnection

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);
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) Test(org.junit.Test)

Example 5 with RepositoryConnection

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();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Repository(org.openrdf.repository.Repository) Statement(org.openrdf.model.Statement) ValueFactory(org.openrdf.model.ValueFactory) SailRepositoryFactory(org.openrdf.repository.sail.config.SailRepositoryFactory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

RepositoryConnection (org.openrdf.repository.RepositoryConnection)71 TupleQuery (org.openrdf.query.TupleQuery)27 URI (org.openrdf.model.URI)24 RepositoryException (org.openrdf.repository.RepositoryException)20 Test (org.junit.Test)15 Statement (org.openrdf.model.Statement)11 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)10 Literal (org.openrdf.model.Literal)9 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 StatementImpl (org.openrdf.model.impl.StatementImpl)6 Update (org.openrdf.query.Update)6 InputStream (java.io.InputStream)5 Value (org.openrdf.model.Value)5 Repository (org.openrdf.repository.Repository)5 SailRepository (org.openrdf.repository.sail.SailRepository)5 RyaSailRepository (org.apache.rya.rdftriplestore.RyaSailRepository)4 Resource (org.openrdf.model.Resource)4 InputStreamReader (java.io.InputStreamReader)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3