Last active
August 29, 2015 13:56
-
-
Save white-gecko/8849251 to your computer and use it in GitHub Desktop.
This is a substitution for Jena's Model.read() method which seams to take for ever in some cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map.Entry; | |
public class AcceptHeader { | |
private HashMap<String, Float> datatypes; | |
public AcceptHeader() { | |
datatypes = new HashMap<String, Float>(); | |
} | |
public AcceptHeader(String datatypeString) throws Exception { | |
this(); | |
addDatatypeString(datatypeString); | |
} | |
public void setDatatypeString(String datatypeString) throws Exception { | |
datatypes.clear(); | |
addDatatypeString(datatypeString); | |
} | |
public void addDatatypeString(String datatypeString) throws Exception { | |
this.datatypes.putAll(parseDatatypeString(datatypeString)); | |
} | |
public void addDatatype(String datatype) { | |
datatypes.put(datatype, new Float(1)); | |
} | |
public void addDatatype(String datatype, double quality) { | |
datatypes.put(datatype, (float)quality); | |
} | |
public void addDatatype(String datatype, float quality) { | |
datatypes.put(datatype, quality); | |
} | |
public float checkContentType(String contentType) { | |
return datatypes.get(contentType); | |
} | |
public HashMap<String, Float> parseDatatypeString (String datatypeString) throws Exception { | |
HashMap<String, Float> datatypes = new HashMap<String, Float>(); | |
if (datatypeString != null && datatypeString.length() > 0) { | |
String[] datatypeSplit = datatypeString.split(","); | |
for (String type : datatypeSplit) { | |
String[] tuple = type.split(";"); | |
if (tuple.length == 2) { | |
datatypes.put(tuple[0], Float.valueOf(tuple[1])); | |
} else if (tuple.length == 1) { | |
datatypes.put(tuple[0], new Float(1)); | |
} else { | |
throw new Exception("Couldn't parse the given datatypeString: \"" + datatypeString + "\""); | |
} | |
} | |
} | |
return datatypes; | |
} | |
public String toString() { | |
Iterator<Entry<String, Float>> it = datatypes.entrySet().iterator(); | |
StringBuilder datatypeString = new StringBuilder(); | |
while (it.hasNext()) { | |
Entry<String, Float> curr = it.next(); | |
datatypeString.append(curr.getKey() + "; q=" + curr.getValue()); | |
if (it.hasNext()) { | |
datatypeString.append(","); | |
} | |
} | |
return datatypeString.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ie.deri.hcls.catalogue; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.net.URLEncoder; | |
import java.util.HashSet; | |
import java.util.Set; | |
import org.apache.jena.atlas.web.HttpException; | |
import com.hp.hpl.jena.rdf.model.Model; | |
import com.hp.hpl.jena.rdf.model.ModelFactory; | |
import com.hp.hpl.jena.rdf.model.NodeIterator; | |
import com.hp.hpl.jena.rdf.model.Property; | |
import com.hp.hpl.jena.rdf.model.RDFNode; | |
import com.hp.hpl.jena.rdf.model.Resource; | |
public class LinkeddataHelper { | |
public static Model readModel(String modelUrl) { | |
Model tempModel = ModelFactory.createDefaultModel(); | |
try { | |
URL url = new URL(modelUrl); | |
URLConnection connection = url.openConnection(); | |
AcceptHeader acceptHeader = new AcceptHeader(); | |
acceptHeader.addDatatype("text/turtle", 1.0); | |
acceptHeader.addDatatype("application/x-turtle", 1.0); | |
acceptHeader.addDatatype("application/rdf+xml", 1.0); | |
acceptHeader.addDatatype("text/n3", 1.0); | |
acceptHeader.addDatatype("text/plain", 0.5); // NTRIPLE | |
connection.addRequestProperty("Accept", acceptHeader.toString()); | |
String contentType = connection.getContentType(); | |
// Remove everything after a semicolon. It could be charset information | |
int splitIndex = contentType.indexOf(";"); | |
if (splitIndex > 0) { | |
contentType = contentType.substring(0, splitIndex); | |
} | |
String fileSuffix = null; | |
float quality = acceptHeader.checkContentType(contentType); | |
if (quality == 0) { | |
return null; | |
} else if (quality < 1) { | |
int dotPos = modelUrl.lastIndexOf('.'); | |
if (dotPos > 0) { | |
fileSuffix = modelUrl.substring(dotPos); | |
if (fileSuffix == ".rdf" || fileSuffix == ".n3" || fileSuffix == ".nt" | |
|| fileSuffix == ".ttl" || fileSuffix == ".turtle" || fileSuffix == ".xml") { | |
} else { | |
return null; | |
} | |
} | |
} | |
String lang = null; | |
switch (contentType) { | |
case "text/turtle": | |
case "application/x-turtle": | |
lang = "TURTLE"; | |
break; | |
case "application/rdf+xml": | |
lang = "RDF/XML"; | |
break; | |
case "text/n3": | |
lang = "N3"; | |
break; | |
default: | |
switch (fileSuffix) { | |
case ".ttl": | |
case ".turtle": | |
lang = "TURTLE"; | |
break; | |
case ".rdf": | |
case ".xml": | |
lang = "RDF/XML"; | |
break; | |
case ".n3": | |
lang = "N3"; | |
break; | |
case ".nt": | |
lang = "N-TRIPLE"; | |
break; | |
} | |
} | |
tempModel.read(connection.getInputStream(), modelUrl, lang); | |
return tempModel; | |
} catch (MalformedURLException e1) { | |
e1.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static Model getResource(Resource resource) throws IOException, HttpException { | |
String resourceUri = resource.getURI(); | |
return readModel(resourceUri); | |
} | |
public static Set<RDFNode> getResourceValue(Resource resource, Property property) | |
throws IOException, HttpException { | |
Model tempModel = getResource(resource); | |
if (tempModel == null) { | |
return null; | |
} | |
NodeIterator objectIterator = tempModel.listObjectsOfProperty(resource, property); | |
Set<RDFNode> nodes = new HashSet<RDFNode>(); | |
while (objectIterator.hasNext()) { | |
nodes.add(objectIterator.next()); | |
} | |
return nodes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment