~ the parsing function now returns the potential parsing errors/warnings
authorAlexandre Bertails <bertails@w3.org>
Thu, 02 Dec 2010 11:37:40 -0500
changeset 286 03d774916577
parent 285 25c233353fab
child 287 7eacfbcab74c
~ the parsing function now returns the potential parsing errors/warnings
rdfxml/src/main/scala/RDFXML.scala
rdfxml/src/test/scala/RDFXMLTest.scala
--- a/rdfxml/src/main/scala/RDFXML.scala	Thu Dec 02 11:36:42 2010 -0500
+++ b/rdfxml/src/main/scala/RDFXML.scala	Thu Dec 02 11:37:40 2010 -0500
@@ -39,63 +39,75 @@
 
 }
 
+sealed trait ParseErrorType
+object RDFParseError extends ParseErrorType
+object XMLParseError extends ParseErrorType
+
+sealed trait ParseErrorLevel
+object FatalError extends ParseErrorLevel
+object Error extends ParseErrorLevel
+object Warning extends ParseErrorLevel
+
+case class ParseError(
+  val message:String,
+  val errorType:ParseErrorType,
+  val level:ParseErrorLevel)
+
+object ParseError {
+  def fromSAXParseException(e:SAXParseException, level:ParseErrorLevel):ParseError = {
+    val message = e.getMessage
+    val errorType = if (e.isInstanceOf[ParseException]) RDFParseError else XMLParseError
+    new ParseError(message, errorType, level)
+  }
+}
+
 class RDFXML() {
 
   import RDFXML._
 
-  def toGraph(file:File):Graph = toGraph(new FileInputStream(file))
+  def toGraph(file:File):(Graph, List[ParseError]) = toGraph(new FileInputStream(file))
 
-  def toGraph(rdfxml:String):Graph = toGraph(new StringReader(rdfxml))
+  def toGraph(rdfxml:String):(Graph, List[ParseError]) = toGraph(new StringReader(rdfxml))
 
-  def toGraph(in:InputStream):Graph = toGraph(new BufferedReader(new InputStreamReader(in)))
+  def toGraph(in:InputStream):(Graph, List[ParseError]) = toGraph(new BufferedReader(new InputStreamReader(in)))
 
-  def toGraph(in:Reader):Graph = {
+  def toGraph(in:Reader):(Graph, List[ParseError]) = {
 
     // the accumulator for the triples
-    val triples = scala.collection.mutable.Set[Triple]()
+    var triples = Set[Triple]()
 
     // the accumulators for the problems we encounter
-    var fatalErrors = List[SAXParseException]()
-    var errors = List[SAXParseException]()
-    var warnings = List[SAXParseException]()
+    var parseErrors = List[ParseError]()
 
     // this ErrorHandler keeps track of all the problems during the parsing
     val errorHandler = new ErrorHandler {
-      def fatalError(e:SAXParseException):Unit = fatalErrors ::= e
-      def error(e:SAXParseException):Unit = errors ::= e
-      def warning(e:SAXParseException):Unit = warnings ::= e
+      def fatalError(e:SAXParseException):Unit = parseErrors ::= ParseError.fromSAXParseException(e, FatalError)
+      def error(e:SAXParseException):Unit = parseErrors ::= ParseError.fromSAXParseException(e, Error)
+      def warning(e:SAXParseException):Unit = parseErrors ::= ParseError.fromSAXParseException(e, Warning)
     }
 
     // this StatementHandler read the parsed triples
     val statementHandler = new StatementHandler {
-      def statement(s:AResource, p:AResource, o:ALiteral):Unit = {
-	val triple = Triple(SubjectNode(toNode(s)),
-			    toPredicate(p),
-			    ObjectLiteral(toLiteral(o)))
-	triples += triple
-      }
-      def statement(s:AResource, p:AResource, o:AResource):Unit = {
-	val triple = Triple(SubjectNode(toNode(s)),
-			    toPredicate(p),
-			    ObjectNode(toNode(o)))
-	triples += triple
-      }
+      def statement(s:AResource, p:AResource, o:ALiteral):Unit =
+	triples += Triple(SubjectNode(toNode(s)),
+			  toPredicate(p),
+			  ObjectLiteral(toLiteral(o)))
+      def statement(s:AResource, p:AResource, o:AResource):Unit =
+	triples += Triple(SubjectNode(toNode(s)),
+			  toPredicate(p),
+			  ObjectNode(toNode(o)))
     }
 
     // http://jena.sourceforge.net/ARP/standalone.html
 
     val arp = new ARP
-    arp.getOptions.setLaxErrorMode()
+    arp.getOptions.setStrictErrorMode
     arp.getHandlers.setErrorHandler(errorHandler)
     arp.getHandlers.setStatementHandler(statementHandler)
     arp.load(in)
 
-    // we should do something else with that...
-    val problems = fatalErrors ++ errors ++ warnings
-    if (! problems.isEmpty) println(problems)
-
-    // returns an immutable set
-    triples.toSet
+    // returns an immutable set and the potential errors
+    (triples.toSet, parseErrors)
   }
 
 }
--- a/rdfxml/src/test/scala/RDFXMLTest.scala	Thu Dec 02 11:36:42 2010 -0500
+++ b/rdfxml/src/test/scala/RDFXMLTest.scala	Thu Dec 02 11:37:40 2010 -0500
@@ -19,7 +19,7 @@
 </rdf:RDF>
 """ // "
 
-    val graph:Graph = parser.toGraph(rdfxml)
+    val (graph, _) = parser.toGraph(rdfxml)
 
     println(graph)
 
@@ -36,7 +36,7 @@
 </rdf:RDF>
 """ // "
 
-    val graph:Graph = parser.toGraph(rdfxml)
+    val (graph, _) = parser.toGraph(rdfxml)
 
     println(graph)