Search in sources :

Example 1 with OpenRDFException

use of org.openrdf.OpenRDFException 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 OpenRDFException

use of org.openrdf.OpenRDFException in project nanopub-server by tkuhn.

the class NanopubDb method getNanopub.

public Nanopub getNanopub(String artifactCode) {
    BasicDBObject query = new BasicDBObject("_id", artifactCode);
    DBCursor cursor = getNanopubCollection().find(query);
    if (!cursor.hasNext()) {
        return null;
    }
    String nanopubString = cursor.next().get("nanopub").toString();
    Nanopub np = null;
    try {
        np = new NanopubImpl(nanopubString, internalFormat);
    } catch (MalformedNanopubException ex) {
        throw new RuntimeException("Stored nanopub is not wellformed (this shouldn't happen)", ex);
    } catch (OpenRDFException ex) {
        throw new RuntimeException("Stored nanopub is corrupted (this shouldn't happen)", ex);
    }
    if (ServerConf.get().isCheckNanopubsOnGetEnabled() && !TrustyNanopubUtils.isValidTrustyNanopub(np)) {
        throw new RuntimeException("Stored nanopub is not trusty (this shouldn't happen)");
    }
    return np;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) Nanopub(org.nanopub.Nanopub) OpenRDFException(org.openrdf.OpenRDFException) MalformedNanopubException(org.nanopub.MalformedNanopubException) NanopubImpl(org.nanopub.NanopubImpl)

Aggregations

OpenRDFException (org.openrdf.OpenRDFException)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBCursor (com.mongodb.DBCursor)1 MalformedNanopubException (org.nanopub.MalformedNanopubException)1 Nanopub (org.nanopub.Nanopub)1 NanopubImpl (org.nanopub.NanopubImpl)1 Resource (org.openrdf.model.Resource)1 Statement (org.openrdf.model.Statement)1 URI (org.openrdf.model.URI)1 Value (org.openrdf.model.Value)1 RepositoryConnection (org.openrdf.repository.RepositoryConnection)1 SailException (org.openrdf.sail.SailException)1