clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From enrid...@apache.org
Subject [1/2] git commit: CLEREZZA-872 Prepared literals escaping non ascii chars. Added a dedicated test. Specified charset=UTF-8 in JDBC connection string.
Date Thu, 06 Feb 2014 22:39:46 GMT
Updated Branches:
  refs/heads/master 58a2670e1 -> 40de7d4f5


CLEREZZA-872 Prepared literals escaping non ascii chars. Added a dedicated test. Specified
charset=UTF-8 in JDBC connection string.


Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/2af17b9e
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/2af17b9e
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/2af17b9e

Branch: refs/heads/master
Commit: 2af17b9e974d6015ee670379ed5a7b97c32f6bc4
Parents: 3bb78d8
Author: enridaga <enridaga@apache.org>
Authored: Thu Feb 6 22:37:07 2014 +0000
Committer: enridaga <enridaga@apache.org>
Committed: Thu Feb 6 22:37:07 2014 +0000

----------------------------------------------------------------------
 .../rdf/virtuoso/storage/VirtuosoMGraph.java    | 36 ++++++++++++++++----
 .../access/VirtuosoWeightedProvider.java        |  2 +-
 .../java/rdf/virtuoso/storage/RdfIOTest.java    | 28 ++++++++++++++-
 .../java/rdf/virtuoso/storage/TestUtils.java    |  2 +-
 4 files changed, 59 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/2af17b9e/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
index 33ef98c..b4116d3 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
@@ -359,7 +359,7 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
 	protected boolean performAdd(Triple triple) {
 		logger.debug("performAdd(Triple {})", triple);
 		String sql = getAddSQLStatement(triple);
-		logger.info("Executing SQL: {}", sql);
+//		logger.info("Executing SQL: {}", sql);
 		// logger.info("--- {} ", sql);
 		writeLock.lock();
 		VirtuosoConnection connection = null;
@@ -394,6 +394,9 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
 			}
 		}
 		if (e != null) {
+			logger.error("S {}", triple.getSubject());
+			logger.error("P {}", triple.getPredicate());
+			logger.error("O {}", triple.getObject());
 			throw new RuntimeException(e);
 		}
 		return true;
@@ -620,10 +623,31 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
 	private String toVirtTypedLiteral(TypedLiteral object) {
 		logger.debug("toVirtTypedLiteral(TypedLiteral {})", object);
 		UriRef dt = object.getDataType();
-		String literal = object.getLexicalForm().replaceAll("\"", "\\\\\"");
-		return new StringBuilder().append('"').append(literal).append('"')
+		String literal = object.getLexicalForm();//.replaceAll("\"", "\\\\\"");
+		return new StringBuilder().append('"').append('"').append('"').append(prepareString(literal)).append('"').append('"').append('"')
 				.append("^^").append(toVirtIri(dt)).toString();
 	}
+	
+	private StringBuilder prepareString(String str) {
+		  StringBuilder retStr = new StringBuilder();
+		  for(int i=0; i<str.length(); i++) {
+		    int cp = Character.codePointAt(str, i);
+		    int charCount = Character.charCount(cp);
+		    if (charCount > 1) {
+		      i += charCount - 1; // 2.
+		      if (i >= str.length()) {
+		        throw new IllegalArgumentException("truncated unexpectedly");
+		      }
+		    }
+
+		    if (cp < 128) {
+		      retStr.appendCodePoint(cp);
+		    } else {
+		      retStr.append(String.format("\\u%04x", cp));
+		    }
+		  }
+		  return retStr;
+		}
 
 	/**
 	 * Returns a string to be used in SQL statements.
@@ -634,9 +658,9 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
 	private String toVirtPlainLiteral(PlainLiteral object) {
 		logger.debug("toVirtPlainLiteral(PlainLiteral {})", object);
 		Language lang = object.getLanguage();
-		String literal = object.getLexicalForm().replaceAll("\"", "\\\\\"");
-		StringBuilder sb = new StringBuilder().append('"').append(literal)
-				.append('"');
+		String literal = object.getLexicalForm();//.replaceAll("\"", "\\\\\"");
+		StringBuilder sb = new StringBuilder().append('"').append('"').append('"').append(prepareString(literal))
+				.append('"').append('"').append('"');
 		if (lang == null) {
 			return sb.toString();
 		} else {

http://git-wip-us.apache.org/repos/asf/clerezza/blob/2af17b9e/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
index affed28..d97556a 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
@@ -175,7 +175,7 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
 
 				// Build connection string
 				 connStr = new StringBuilder().append("jdbc:virtuoso://")
-						.append(host).append(":").append(port).toString();
+						.append(host).append(":").append(port).append("/CHARSET=UTF-8").toString();
 				
 				// Check connection
 				VirtuosoConnection connection = getConnection(connStr, user, pwd);

http://git-wip-us.apache.org/repos/asf/clerezza/blob/2af17b9e/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
index 7860395..6f5826b 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
@@ -110,6 +110,7 @@ public class RdfIOTest {
 				.equals(new TripleImpl(subject, predicate, object)));
 		Assert.assertTrue(read.getObject() instanceof TypedLiteral);
 		TypedLiteral l = (TypedLiteral) read.getObject();
+		Assert.assertEquals(l.getLexicalForm(), "lorem ipsum");
 		Assert.assertEquals(l.getDataType(), new UriRef(XSD + "string"));
 
 	}
@@ -334,7 +335,7 @@ public class RdfIOTest {
 		Assert.assertEquals(n, 1);
 
 	}
-	
+
 	@Test
 	public void sysconfigTest(){
 		if (TestUtils.SKIP) {
@@ -357,4 +358,29 @@ public class RdfIOTest {
             throw new RuntimeException(ex);
         }
 	}
+	
+	@Test
+	public void testUnicodeChars() throws ClassNotFoundException, SQLException {
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		log.info("Text an xsd:string");
+		
+		String s = "lorem ipsum è é £ ò ç à ù β ät ü ä";
+		TypedLiteral object = new TypedLiteralImpl(s, new UriRef(
+				XSD + "string"));
+		UriRef subject = new UriRef("urn:io-test:reto");
+		UriRef predicate = new UriRef("urn:io-test:hasText");
+
+		Triple read = writeAndRead(subject, predicate, object);
+		log.info("o: {} :: {}", object, read.getObject());
+		Assert.assertTrue(read
+				.equals(new TripleImpl(subject, predicate, object)));
+		Assert.assertTrue(read.getObject() instanceof TypedLiteral);
+		TypedLiteral l = (TypedLiteral) read.getObject();
+		Assert.assertEquals(l.getLexicalForm(), s);
+		Assert.assertEquals(l.getDataType(), new UriRef(XSD + "string"));
+
+	}
 }

http://git-wip-us.apache.org/repos/asf/clerezza/blob/2af17b9e/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
index ecd4670..a720e4d 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
@@ -108,7 +108,7 @@ public class TestUtils {
 		cb.append("jdbc:virtuoso://");
 		cb.append(host);
 		cb.append(":");
-		cb.append(port);
+		cb.append(port).append("/CHARSET=UTF-8");
 		jdbcConnectionString = cb.toString();
 //		Class.forName(VirtuosoWeightedProvider.DRIVER);
 		log.info("Create provider");


Mime
View raw message