use of org.openrdf.model.Statement 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.model.Statement in project stanbol by apache.
the class AbstractSesameBackend method listObjectsInternal.
protected Collection<Value> listObjectsInternal(RepositoryConnection connection, Resource subject, org.openrdf.model.URI property, boolean includeInferred, Resource... context) throws RepositoryException {
ValueFactory valueFactory = connection.getValueFactory();
Set<Value> result = new HashSet<Value>();
RepositoryResult<Statement> qResult = connection.getStatements(merge(subject, connection.getValueFactory()), merge(property, connection.getValueFactory()), null, includeInferred, context);
try {
while (qResult.hasNext()) {
result.add(qResult.next().getObject());
}
} finally {
qResult.close();
}
return result;
}
use of org.openrdf.model.Statement in project stanbol by apache.
the class RdfIndexingSource method extractRepresentation.
/**
* Extracts all {@link Statement}s part of the Representation. If
* {@link #followBNodeState} this is called recursively for {@link Statement}s
* where the value is an {@link BNode}.
*/
protected void extractRepresentation(RepositoryConnection con, Model model, Resource node, Set<BNode> visited) throws RepositoryException {
//we need all the outgoing relations and also want to follow bNodes until
//the next UriRef. However we are not interested in incoming relations!
RepositoryResult<Statement> outgoing = con.getStatements(node, null, null, includeInferred, contexts);
Statement statement;
Set<BNode> bnodes = followBNodeState ? new HashSet<BNode>() : null;
while (outgoing.hasNext()) {
statement = outgoing.next();
model.add(statement);
if (followBNodeState) {
Value object = statement.getObject();
if (object instanceof BNode && !visited.contains(object)) {
bnodes.add((BNode) object);
}
}
//else do not follow values beeing BNodes
}
outgoing.close();
if (followBNodeState) {
for (BNode bnode : bnodes) {
visited.add(bnode);
//TODO: recursive calls could cause stackoverflows with wired graphs
extractRepresentation(con, model, bnode, visited);
}
}
}
use of org.openrdf.model.Statement in project stanbol by apache.
the class SesameYard method extractRepresentation.
/**
* Recursive Method internally doing all the work for
* {@link #createRepresentationGraph(UriRef, TripleCollection)}
* @param con the repository connection to read the data from
* @param model The model to add the statements retrieved
* @param node the current node. Changes in recursive calls as it follows
* @param visited holding all the visited BNodes to avoid cycles. Other nodes
* need not be added because this implementation would not follow it anyway
* outgoing relations if the object is a {@link BNode} instance.
* @throws RepositoryException
*/
private void extractRepresentation(RepositoryConnection con, Model model, Resource node, Set<BNode> visited) throws RepositoryException {
//we need all the outgoing relations and also want to follow bNodes until
//the next UriRef. However we are not interested in incoming relations!
RepositoryResult<Statement> outgoing = con.getStatements(node, null, null, includeInferred, contexts);
Statement statement;
Set<BNode> bnodes = new HashSet<BNode>();
while (outgoing.hasNext()) {
statement = outgoing.next();
model.add(statement);
Value object = statement.getObject();
if (object instanceof BNode && !visited.contains(object)) {
bnodes.add((BNode) object);
}
}
outgoing.close();
for (BNode bnode : bnodes) {
visited.add(bnode);
//TODO: recursive calls could cause stackoverflows with wired graphs
extractRepresentation(con, model, bnode, visited);
}
}
use of org.openrdf.model.Statement in project stanbol by apache.
the class RdfRepresentation method removeAllNaturalText.
@Override
public void removeAllNaturalText(String field, String... languages) throws IllegalArgumentException {
if (field == null) {
throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
} else if (field.isEmpty()) {
throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
}
ValueTypeFilter<Literal> vtf = new ValueTypeFilter<Literal>(languages);
Iterator<Statement> statements = model.filter(subject, sesameFactory.createURI(field), null).iterator();
while (statements.hasNext()) {
Statement statement = statements.next();
if (vtf.evaluate(statement.getObject())) {
statements.remove();
}
}
}
Aggregations