From 1e909a13b31e646fc87b81db06f382ffecfb173f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr>
Date: Mon, 22 Mar 2010 20:18:40 +0000
Subject: [PATCH] - fixed a reentrance bug of InputStream/Reader when using
 parseString

---
 .../exmo/align/parser/AlignmentParser.java      | 17 +++++++++++++----
 .../inrialpes/exmo/align/parser/RDFParser.java  |  3 ---
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/fr/inrialpes/exmo/align/parser/AlignmentParser.java b/src/fr/inrialpes/exmo/align/parser/AlignmentParser.java
index 55ccb3dc..67a47db4 100644
--- a/src/fr/inrialpes/exmo/align/parser/AlignmentParser.java
+++ b/src/fr/inrialpes/exmo/align/parser/AlignmentParser.java
@@ -26,6 +26,7 @@ import java.io.StringReader;
 import java.io.Reader;
 import java.io.InputStream;
 import java.io.File;
+import java.io.ByteArrayInputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.lang.Integer;
@@ -155,14 +156,16 @@ public class AlignmentParser {
      * This dispatch is ridiculous, but that's life
      */
     private Alignment callParser( XMLParser p, Object o ) throws AlignmentException {
-	if ( o instanceof String ) return p.parse((String)o);
+	if ( o instanceof URI ) return p.parse( ((URI)o).toString() );
+	if ( o instanceof String ) return p.parse( new ByteArrayInputStream( ((String)o).getBytes() ) );
 	if ( o instanceof Reader ) return p.parse((Reader)o);
 	if ( o instanceof InputStream ) return p.parse((InputStream)o);
 	throw new AlignmentException( "AlignmentParser: cannot parse :"+o );
     }
 
     private Alignment callParser( RDFParser p, Object o ) throws AlignmentException {
-	if ( o instanceof String ) return p.parse((String)o);
+	if ( o instanceof URI ) return p.parse( ((URI)o).toString() );
+	if ( o instanceof String ) return p.parse( new ByteArrayInputStream( ((String)o).getBytes() ) );
 	if ( o instanceof Reader ) return p.parse((Reader)o);
 	if ( o instanceof InputStream ) return p.parse((InputStream)o);
 	throw new AlignmentException( "AlignmentParser: cannot parse :"+o );
@@ -173,7 +176,9 @@ public class AlignmentParser {
      * @param s String the string to parse
      */
     public Alignment parseString( String s ) throws AlignmentException {
-	parse( new StringReader( s ) );
+	// JE: The problem here is that InputStream are consumed by parsers
+	// So they must be opened again! Like Readers...
+	callParser( s );
 	return alignment;
     }
 
@@ -192,7 +197,11 @@ public class AlignmentParser {
      */
     public Alignment parse( String uri ) throws AlignmentException {
 	this.uri = uri; // should be obsoloted
-	callParser( uri );
+	try {
+	    callParser( new URI( uri ) );
+	} catch (URISyntaxException urisex) {
+	    throw new AlignmentException( "Invalid URI : "+uri, urisex );
+	}
 	return alignment;
     }
 
diff --git a/src/fr/inrialpes/exmo/align/parser/RDFParser.java b/src/fr/inrialpes/exmo/align/parser/RDFParser.java
index 8aa2f365..cf634ba3 100644
--- a/src/fr/inrialpes/exmo/align/parser/RDFParser.java
+++ b/src/fr/inrialpes/exmo/align/parser/RDFParser.java
@@ -202,8 +202,6 @@ public class RDFParser {
 	if (is == null) throw new AlignmentException("The reader must not be null");
 	Model align = ModelFactory.createDefaultModel();
 	align.read( is, null );
-	// Apparently this does not really works
-	//align.write(System.out);
 	return parse( align );
     }
     
@@ -211,7 +209,6 @@ public class RDFParser {
 	if (is == null) throw new AlignmentException("The inputstream must not be null");
 	Model align = ModelFactory.createDefaultModel();
 	align.read( is, null );
-	//debug	align.write(System.out);
 	return parse( align );
     }
 
-- 
GitLab