Skip to content

Instantly share code, notes, and snippets.

@sidharthkuruvila
Created July 19, 2011 17:00
Show Gist options
  • Save sidharthkuruvila/1093078 to your computer and use it in GitHub Desktop.
Save sidharthkuruvila/1093078 to your computer and use it in GitHub Desktop.
Scala Traversable for jena's TurtleParser
import com.hp.hpl.jena.{ n3, graph }
import n3.turtle.TurtleEventHandler
import n3.turtle.parser.TurtleParser
import graph.Triple
import java.io.Reader
/**
* Wrapper to convert a TurtleParser into a Traversable
*/
class NTripleTraversable(parser:TurtleParser, inputFileName: String) extends Traversable[Triple] {
def this(reader: Reader, inputFileName: String){
this(new BadUrlsTurtleParser(reader), inputFileName)
}
def foreach[U](f: Triple => U): Unit = {
val extractor = new TurtleEventHandler {
def triple(line: Int, col: Int, triple: Triple) = f(triple)
def prefix(line: Int, col: Int, prefix: String, iri: String) = ni
def startFormula(line: Int, col: Int) = ni
def endFormula(line: Int, col: Int) = ni
private def ni = throw new RuntimeException("Not implemented");
}
parser.setEventHandler(extractor)
parser.setBaseURI("file:///" + inputFileName)
parser.parse()
}
}
/**
* A turtle parser that does not throw TurtleParseExceptions when it comes
* across malformed urls
*/
class BadUrlsTurtleParser(reader: Reader) extends TurtleParser(reader) {
override def resolveIRI(iriStr: String, line: Int, column: Int): String = {
try {
super.resolveIRI(iriStr, line, column)
} catch {
case ex: com.hp.hpl.jena.n3.turtle.TurtleParseException => {
println("bad iri:" + iriStr)
iriStr
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment