Author: reto
Date: Wed Feb 16 00:34:31 2011
New Revision: 1071114

URL: http://svn.apache.org/viewvc?rev=1071114&view=rev
Log:
CLEREZZA-414: fixed encryption of negative hasCodes

Added:
    incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/
    
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/
    
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/
    
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/rdf/
    
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/rdf/storage/
    
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/rdf/storage/externalizer/
    
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraphTest.java
Modified:
    
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/main/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraph.java

Modified: 
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/main/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraph.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/main/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraph.java?rev=1071114&r1=1071113&r2=1071114&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/main/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraph.java
 (original)
+++ 
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/main/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraph.java
 Wed Feb 16 00:34:31 2011
@@ -52,7 +52,7 @@ class ExternalizingMGraph extends Abstra
 
        private final MGraph baseGraph;
        private final File dataDir;
-       private static final UriRef base64Uri =
+       static final UriRef base64Uri =
                        new 
UriRef("http://www.w3.org/2001/XMLSchema#base64Binary";);
        //not using a known uri-scheme (such as urn:hash) to avoid collission 
with Uris in the graph
        private static final String UriHashPrefix = "urn:x-litrep:";
@@ -120,7 +120,7 @@ class ExternalizingMGraph extends Abstra
                return false;
        }
 
-       private UriRef replace(TypedLiteral literal) {
+       UriRef replace(TypedLiteral literal) {
                FileOutputStream out = null;
                try {
                        final byte[] serializedLiteral = 
serializeLiteral(literal);
@@ -161,19 +161,29 @@ class ExternalizingMGraph extends Abstra
                }
        }
 
+       TypedLiteral getLiteralForUri(String uriString) {
+               String base16Hash = uriString.substring(UriHashPrefix.length());
+               return getLiteralForHash(base16Hash);
+       }
+
        private TypedLiteral getLiteralForHash(String base16Hash) {
                return new ReplacementLiteral(base16Hash);
 
 
        }
 
-       private String toBase16(byte[] bytes) {
+       String toBase16(byte[] bytes) {
                StringBuilder sb = new StringBuilder();
                for (byte b : bytes) {
-                       if (b < 16) {
+                       if ((b < 16) && (b > 0)) {
                                sb.append('0');
                        }
-                       sb.append(Integer.toHexString(b));
+                       String integerHex = Integer.toHexString(b);
+                       if (b < 0) {
+                               sb.append(integerHex.substring(6));
+                       } else {
+                               sb.append(integerHex);
+                       }
                }
                return sb.toString();
        }
@@ -208,9 +218,8 @@ class ExternalizingMGraph extends Abstra
                                if (object instanceof UriRef) {
                                        String uriString = ((UriRef) 
object).getUnicodeString();
                                        if 
(uriString.startsWith(UriHashPrefix)) {
-                                               String base16Hash = 
uriString.substring(UriHashPrefix.length());
                                                return new 
TripleImpl(triple.getSubject(), triple.getPredicate(),
-                                                               
getLiteralForHash(base16Hash));
+                                                               
getLiteralForUri(uriString));
                                        }
                                }
                                return triple;
@@ -232,6 +241,25 @@ class ExternalizingMGraph extends Abstra
                return hash;
        }
 
+       int parseHexInt(String hexInt) {
+               int[] hashBytes = new int[4];
+               hashBytes[0] = Integer.parseInt(hexInt.substring(0,2), 16);
+               hashBytes[1] = Integer.parseInt(hexInt.substring(2,4), 16);
+               hashBytes[2] = Integer.parseInt(hexInt.substring(4,6), 16);
+               hashBytes[3] = Integer.parseInt(hexInt.substring(6,8), 16);
+               return getIntFromBytes(hashBytes);
+       }
+
+       private int getIntFromBytes(int[] bytes) {
+               int result = 0;
+               int shift = (bytes.length*8);
+               for (int b : bytes) {
+                       shift -= 8;
+                       result |= (0xFF & b) << shift;
+               }
+               return result;
+       }
+
        private class ReplacementLiteral implements TypedLiteral {
 
                private String lexicalForm;
@@ -239,10 +267,10 @@ class ExternalizingMGraph extends Abstra
                final private String base16Hash;
                private boolean initialized = false;
                final private int hash;
-
                private ReplacementLiteral(String base16Hash) {
                        this.base16Hash = base16Hash;
-                       hash = Integer.parseInt(base16Hash.substring(0,8), 16);
+                       hash = parseHexInt(base16Hash.substring(0, 8));
+                       
                }
 
                private synchronized void initialize() {

Added: 
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraphTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraphTest.java?rev=1071114&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraphTest.java
 (added)
+++ 
incubator/clerezza/trunk/parent/rdf.storage.externalizer/src/test/java/org/apache/clerezza/rdf/storage/externalizer/ExternalizingMGraphTest.java
 Wed Feb 16 00:34:31 2011
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.clerezza.rdf.storage.externalizer;
+
+import java.io.File;
+import org.apache.clerezza.rdf.core.TypedLiteral;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * @author reto
+ */
+public class ExternalizingMGraphTest {
+
+       @Test
+       public void replaceLiteral() throws Exception {
+               File dataDir = File.createTempFile("test", "externalizer");
+               dataDir.delete();
+               dataDir.mkdir();
+               ExternalizingMGraph graph = new ExternalizingMGraph(new 
SimpleMGraph(), dataDir);
+               TypedLiteral lit = new TypedLiteralImpl("jkjkj", 
ExternalizingMGraph.base64Uri);
+               UriRef replacement = graph.replace(lit);
+               TypedLiteral reconstructed = 
graph.getLiteralForUri(replacement.getUnicodeString());
+               Assert.assertEquals(replacement, graph.replace(reconstructed));
+       }
+
+       @Test
+       public void base16Ints() throws Exception {
+               File dataDir = File.createTempFile("test", "externalizer");
+               dataDir.delete();
+               dataDir.mkdir();
+               ExternalizingMGraph graph = new ExternalizingMGraph(new 
SimpleMGraph(), dataDir);
+               //int value = -1291264412;
+               int value = -0x10;
+               byte[] bytes = new byte[4];
+               bytes[0] = (byte) (0xFF & (value >>> 24));
+        bytes[1] = (byte) (0xFF & (value >>> 16));
+        bytes[2] = (byte) (0xFF & (value >>> 8));
+        bytes[3] = (byte) (0xFF & value);
+               Assert.assertEquals(value, 
graph.parseHexInt(graph.toBase16(bytes)));
+       }
+
+}


Reply via email to