Author: reto
Date: Sat May  8 19:02:05 2010
New Revision: 942448

URL: http://svn.apache.org/viewvc?rev=942448&view=rev
Log:
CLEREZZA-20: fixed version of patch daniel 2010-05-29

Added:
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializer.java
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializerTest.java
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerGraphTest.java
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/Talks.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/agenda_62.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/amp-in-url-test001.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/css3deps.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/datatypes-test001.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/datatypes-test002.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/elvisimp.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/images.xrdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/libby.foaf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/rdf-charmod-literals-test001.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/rdf-charmod-uris-test001.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/rdf-charmod-uris-test002.rdf
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/resources/org/apache/clerezza/rdf/stable/serializer/xml-canon-test001.rdf
Removed:
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/RandomGraph.java
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerProviderTest.java
Modified:
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/pom.xml
    
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerProvider.java

Modified: 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/pom.xml?rev=942448&r1=942447&r2=942448&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/pom.xml
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/pom.xml
 Sat May  8 19:02:05 2010
@@ -28,6 +28,17 @@
                        <scope>test</scope>
                </dependency>
                <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       
<artifactId>org.apache.clerezza.rdf.jena.parser</artifactId>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       <artifactId>org.apache.clerezza.rdf.utils</artifactId>
+                <version>0.13-incubating-SNAPSHOT</version>
+                <type>bundle</type>
+               </dependency>
+               <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <scope>test</scope>
@@ -40,3 +51,4 @@
                </dependency>
        </dependencies>
 </project>
+

Added: 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializer.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializer.java?rev=942448&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializer.java
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializer.java
 Sat May  8 19:02:05 2010
@@ -0,0 +1,186 @@
+/*
+ * 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.stable.serializer;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.HashMap;
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.Literal;
+import org.apache.clerezza.rdf.core.PlainLiteral;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.TypedLiteral;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A serializer that can serialze RDF Graphs into N-Triples format.
+ *
+ * NOTE: This is a special purpose serializer to be used with
+ * {...@link 
org.apache.clerezza.rdf.stable.serializer.StableSerializerProvider}.
+ * Instances are assumed to be short-lived and not used concurrently.
+ *
+ * @author Daniel Spicar ([email protected])
+ */
+class NTriplesSerializer {
+
+       private final Logger logger = LoggerFactory.getLogger(getClass());
+
+       private long genSymCounter = 0;
+       private HashMap<BNode, String> bNodeLabels;
+
+       /**
+        * Resets the counter to zero that constitutes
+        * the numerical part of blank node labels.
+        */
+       void resetGenSymCounter() {
+               genSymCounter = 0;
+       }
+
+       /**
+        * Serializes a given TripleCollection using the N-Triples format.
+        *
+        * @param os
+        *                              An outputstream.
+        * @param tc
+        *                              the triples of the graph to be 
serialized.
+        */
+       void serialize(OutputStream os, TripleCollection tc) {
+               try {
+                       bNodeLabels = new HashMap<BNode, String>(tc.size() / 2);
+
+                       for (Triple t : tc) {
+                               if (t.getSubject() instanceof BNode) {
+                                       os.write(serializeBNode((BNode) 
t.getSubject()).getBytes());
+                               } else {
+                                       os.write(serializeUriRef(
+                                                       (UriRef) 
t.getSubject()).getBytes());
+                               }
+
+                               os.write((t.getPredicate().toString() + " 
").getBytes());
+
+                               if (t.getObject() instanceof BNode) {
+                                       os.write(serializeBNode((BNode) 
t.getObject()).getBytes());
+                                       os.write(".\n".getBytes());
+                               } else {
+                                       if (t.getObject() instanceof Literal) {
+                                               
os.write((serializeLiteral((Literal) t.getObject()) +
+                                                               
".\n").getBytes());
+                                       } else {
+                                               
os.write((serializeUriRef((UriRef) t.getObject()) +
+                                                               
".\n").getBytes());
+                                       }
+                               }
+                       }
+               } catch (IOException ex) {
+                       logger.error("Exception while serializing graph: {}", 
ex);
+               }
+       }
+
+       private String serializeUriRef(UriRef uriRef) {
+               StringBuffer sb = new StringBuffer("<");
+               escapeUtf8ToUsAscii(uriRef.getUnicodeString(), sb, true);
+               sb.append("> ");
+
+               return sb.toString() ;
+       }
+
+       private String serializeBNode(BNode bNode) {
+               if (bNodeLabels.containsKey(bNode)) {
+                       return bNodeLabels.get(bNode) + " ";
+               } else {
+                       String label = "_:b" + genSymCounter++;
+                       bNodeLabels.put(bNode, label);
+                       return label + " ";
+               }
+       }
+       
+       private String serializeLiteral(Literal literal) {
+               StringBuffer sb = new StringBuffer("\"");
+               escapeUtf8ToUsAscii(literal.getLexicalForm(), sb, false);
+               sb = sb.append("\"");
+
+               if(literal instanceof TypedLiteral) {
+                       TypedLiteral typedLiteral = (TypedLiteral) literal;
+                       sb.append("^^<");
+                       escapeUtf8ToUsAscii(
+                                       
typedLiteral.getDataType().getUnicodeString(), sb, false);
+                       sb.append(">");
+               } else if(literal instanceof PlainLiteral) {
+                       PlainLiteral plainLiteral = (PlainLiteral) literal;
+                       if(plainLiteral.getLanguage() != null &&
+                                       
!plainLiteral.getLanguage().toString().equals("")) {
+
+                               sb.append("@");
+                               
sb.append(plainLiteral.getLanguage().toString());
+                       }
+               }
+
+               sb.append(" ");
+
+               return sb.toString() ;
+       }
+
+       private void escapeUtf8ToUsAscii(String input, StringBuffer sb, boolean 
uri) {
+
+               for (int i = 0; i < input.length(); ++i) {
+                       char c = input.charAt(i);
+                       int val = (int) c;
+                       if (c == '\t') {
+                               sb.append("\\t");
+                       } else if (c == '\n') {
+                               sb.append("\\n");
+                       } else if (c == '\r') {
+                               sb.append("\\r");
+                       } else if (c == '"') {
+                               sb.append("\\\"");
+                       } else if (c == '\\') {
+                               sb.append("\\\\");
+                       } else if ((val >= 0x0 && val <= 0x8) || (val >= 0xB && 
val <= 0xC) 
+                                       || (val >= 0xE && val <= 0x1F)
+                                       || (val >= 0x7F && val <= 0xFFFF)) {
+                               sb.append("\\u");
+                               sb.append(getIntegerHashString(val, 4));
+                       } else if (val >= 0x10000 && val <= 0x10FFFF) {
+                               sb.append("\\U");
+                               sb.append(getIntegerHashString(val, 8));
+                       } else {
+                               if (uri && (c == '>' || c == '<')) {
+                                       sb.append("\\u");
+                                       sb.append(getIntegerHashString(val, 4));
+                               } else {
+                                       sb.append(c);
+                               }
+                       }
+               }
+       }
+
+       private String getIntegerHashString(int val, int length) {
+               StringBuffer sb = new StringBuffer();
+               String hex = Integer.toHexString(val);
+               for(int i = 0; i < length - hex.length(); ++i) {
+                       sb.append("0");
+               }
+               sb.append(hex);
+
+               return sb.toString().toUpperCase();
+       }
+}

Modified: 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerProvider.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerProvider.java?rev=942448&r1=942447&r2=942448&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerProvider.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/main/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerProvider.java
 Sat May  8 19:02:05 2010
@@ -27,11 +27,13 @@ import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.Graph;
 import org.apache.clerezza.rdf.core.Literal;
 import org.apache.clerezza.rdf.core.Triple;
 import org.apache.clerezza.rdf.core.TripleCollection;
@@ -39,9 +41,9 @@ import org.apache.clerezza.rdf.core.UriR
 import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
 import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
 import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
+import org.apache.clerezza.rdf.utils.GraphNode;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
@@ -80,12 +82,6 @@ public class StableSerializerProvider im
                        "(0 = no limit).", intValue=0)
        public static final String MAX_LABELING_ITERATIONS = 
"max_labeling_iterations";
 
-       public static final String PARSER_FILTER =
-                       "(supportedFormat=" + SupportedFormat.N3 +")";
-
-       @Reference(target=PARSER_FILTER)
-       SerializingProvider serializer;
-
        private int maxLabelingIterations = -1;
 
        private final Logger logger = LoggerFactory.getLogger(getClass());
@@ -102,67 +98,80 @@ public class StableSerializerProvider im
 
                try {
                        List<String> lines = new Vector<String>();
+                       List<MSG> msgs = decomposeGraphToMSGs(tc);
+                       NTriplesSerializer serializer = new 
NTriplesSerializer();
 
-                       for (TripleCollection msg : decomposeGraphToMSGs(tc)) {
-                               ByteArrayOutputStream baos = new 
ByteArrayOutputStream();
+                       computeMsgHashes(msgs, "MD5");
 
-                               serializer.
-                                               serialize(baos, msg, 
formatIdentifier);
+                       for (MSG msg : msgs) {
+                               ByteArrayOutputStream baos = new 
ByteArrayOutputStream();
+                               serializer.serialize(baos, msg.tc);
                                BufferedReader serializedGraph =
                                                new BufferedReader(new 
StringReader(baos.toString()));
-                               lines.addAll(labelBlankNodes(serializedGraph,
-                                               getMDHexString(msg, "MD5")));
+                               lines.addAll(labelBlankNodes(serializedGraph, 
msg.hash));
                        }
+
                        Collections.sort(lines);
                        for (String l : lines) {
                                os.write((l + "\n").getBytes());
                        }
                } catch (IOException ex) {
-                       logger.error("Exception while parsing serilaized graph: 
{}", ex);
+                       logger.error("Exception while parsing serialized graph: 
{}", ex);
                } catch (NoSuchAlgorithmException ex) {
                        logger.error("Exception while trying to generate graph 
hash: {}", ex);
                }
        }
 
-       private List<TripleCollection> decomposeGraphToMSGs(TripleCollection 
tc) {
+       private List<MSG> decomposeGraphToMSGs(TripleCollection tc) {
 
                TripleCollection tmp = new SimpleMGraph();
                tmp.addAll(tc);
 
-               List<TripleCollection> msgSet = new Vector<TripleCollection>();
+               List<MSG> msgSet = new Vector<MSG>();
 
                while (tmp.size() > 0) {
                        Triple triple = tmp.iterator().next();
-                       TripleCollection msg = new SimpleMGraph();
+                       TripleCollection msgTc = new SimpleMGraph();
 
-                       fillMSG(triple, tmp, msg);
+                       boolean containsBNode = fillMSG(triple, tmp, msgTc);
+                       MSG msg = new MSG(msgTc);
+                       msg.containsBlankNodes = containsBNode;
                        msgSet.add(msg);
                }
 
                return msgSet;
        }
 
-       private void fillMSG(Triple triple, TripleCollection tc,
+       private boolean fillMSG(Triple triple, TripleCollection tc,
                        TripleCollection msg) {
 
-               msg.add(triple);
-               tc.remove(triple);
+               boolean containsBNode = false;
 
                if (triple.getSubject() instanceof BNode) {
-                       Iterator<Triple> it = tc.filter(null, null, 
triple.getSubject());
-                       while (it.hasNext()) {
-                               fillMSG(it.next(), tc, msg);
-                       }
-               }
-               if (triple.getObject() instanceof BNode) {
-                       Iterator<Triple> it =
-                                       tc.filter((BNode) triple.getObject(), 
null, null);
-                       while (it.hasNext()) {
-                               fillMSG(it.next(), tc, msg);
+                       containsBNode = true;
+                       GraphNode gn = new GraphNode(triple.getSubject(), tc);
+                       Graph context = gn.getNodeContext();
+                       msg.addAll(context);
+                       tc.removeAll(context);
+               } else {
+                       if ((triple.getObject() instanceof BNode)
+                                       && (triple.getSubject() != 
triple.getObject())) {
+                               containsBNode = true;
+                               GraphNode gn = new 
GraphNode(triple.getObject(), tc);
+                               Graph context = gn.getNodeContext();
+                               msg.addAll(context);
+                               tc.removeAll(context);
+                       } else {
+                               msg.add(triple);
+                               tc.remove(triple);
                        }
                }
+
+               return containsBNode;
        }
 
+
+
        private List<String> labelBlankNodes(BufferedReader serializedGraph,
                        String prefix) throws IOException {
 
@@ -209,8 +218,8 @@ public class StableSerializerProvider im
                        Map<String, Long> labels, long genSymCounter, long 
comments,
                        String prefix) {
 
-               for (int i = 2; i <= lines.size(); ++i) {
-                       StringBuilder previousLine = new 
StringBuilder(lines.get(i - 2));
+               for (int i = 1; i <= lines.size(); ++i) {
+                       StringBuilder previousLine = i > 1 ?new 
StringBuilder(lines.get(i - 2)) : new StringBuilder();
                        StringBuilder currentLine = new 
StringBuilder(lines.get(i - 1));
                        StringBuilder nextLine = new StringBuilder();
                        if (i < lines.size()) {
@@ -321,7 +330,7 @@ public class StableSerializerProvider im
 
                int index = currentLine.lastIndexOf("#_:");
                String identifier =
-                               currentLine.substring(index + 1, 
currentLine.length());
+                               currentLine.substring(index + 1, 
currentLine.length()).trim();
                currentLine.delete(index, currentLine.length());
                if (!labels.containsKey(identifier)) {
                        labels.put(identifier, genSymCounter++);
@@ -384,38 +393,71 @@ public class StableSerializerProvider im
                return line.substring(0, line.lastIndexOf(" .") + 2);
        }
 
-       private String getMDHexString(TripleCollection tc, String algorithm)
+       private void computeMsgHashes(List<MSG> msgs, String algorithm)
                        throws NoSuchAlgorithmException {
 
                MessageDigest md = MessageDigest.getInstance(algorithm);
-               StringBuffer input = new StringBuffer();
-               for (Triple t : tc) {
-                       if (!(t.getSubject() instanceof BNode)) {
-                               input.append(((UriRef) 
t.getSubject()).hashCode());
-                       }
-                       input.append(t.getPredicate().hashCode());
-                       if (!(t.getObject() instanceof BNode)) {
-                               if (t.getObject() instanceof Literal) {
-                                       input.append(((Literal) 
t.getObject()).getLexicalForm());
-                               } else {
-                                       input.append(((UriRef) 
t.getObject()).hashCode());
+               HashSet<String> computedHashes = new 
HashSet<String>(msgs.size());
+
+               for(MSG msg : msgs) {
+                       if(!msg.containsBlankNodes) {
+                               //hash is needed only for b-node labelling
+                               continue;
+                       }
+                       StringBuffer input = new StringBuffer();
+                       for (Triple t : msg.tc) {
+                               if (!(t.getSubject() instanceof BNode)) {
+                                       input.append(((UriRef) 
t.getSubject()).hashCode());
+                               }
+                               input.append(t.getPredicate().hashCode());
+                               if (!(t.getObject() instanceof BNode)) {
+                                       if (t.getObject() instanceof Literal) {
+                                               input.append(((Literal) 
t.getObject()).
+                                                               
toString().hashCode());
+                                       } else {
+                                               input.append(((UriRef) 
t.getObject()).hashCode());
+                                       }
                                }
                        }
+                       md.update(input.toString().getBytes());
+
+                       String hexString;
+                       if(computedHashes.add((hexString = 
getHashHexString(md.digest())))){
+                               msg.hash = hexString;
+                       } else {
+                               md.update(String.valueOf(
+                                               
System.currentTimeMillis()).getBytes());
+                               while(!computedHashes.add(
+                                               (hexString = 
getHashHexString(md.digest())))) {
+                                       md.update(String.valueOf(
+                                                       
System.currentTimeMillis()).getBytes());
+                               }
+                               msg.hash = hexString;
+                       }
                }
+       }
 
-               md.update(input.toString().getBytes());
-               byte[] hash = md.digest();
 
+       private String getHashHexString(byte[] hash) {
                StringBuffer hexString = new StringBuffer();
                for (int i = 0; i < hash.length; i++) {
                        String hex = Integer.toHexString(0xFF & hash[i]);
                        if (hex.length() == 1) {
                                hexString.append('0');
                        }
-
                        hexString.append(hex);
                }
-
                return hexString.toString();
        }
+
+       private static class MSG {
+
+               final TripleCollection tc;
+               String hash = null;
+               boolean containsBlankNodes = false;
+
+               MSG(TripleCollection tc) {
+                       this.tc = tc;
+               }
+       }
 }
\ No newline at end of file

Added: 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializerTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializerTest.java?rev=942448&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializerTest.java
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/NTriplesSerializerTest.java
 Sat May  8 19:02:05 2010
@@ -0,0 +1,90 @@
+/*
+ * 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.stable.serializer;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+import java.util.Collection;
+import junit.framework.Assert;
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
+import org.apache.clerezza.rdf.jena.parser.JenaParserProvider;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/**
+ *
+ * @author daniel
+ */
+...@runwith(Parameterized.class)
+public class NTriplesSerializerTest {
+
+       private String inputFileName;
+       private String format;
+
+       public NTriplesSerializerTest(String inputFileName, String format) {
+               this.inputFileName = inputFileName;
+               this.format = format;
+       }
+
+       @Parameterized.Parameters
+       public static Collection inputFileNames() {
+               return Arrays.asList(new Object[][] {
+                       {"amp-in-url-test001.rdf", "application/rdf+xml"},
+                       {"datatypes-test001.rdf", "application/rdf+xml"},
+                       {"datatypes-test002.rdf", "application/rdf+xml"},
+                       {"rdf-charmod-literals-test001.rdf", 
"application/rdf+xml"},
+                       {"rdf-charmod-uris-test001.rdf", "application/rdf+xml"},
+                       {"rdf-charmod-uris-test002.rdf", "application/rdf+xml"},
+                       {"xml-canon-test001.rdf", "application/rdf+xml"},
+                       {"css3deps.rdf", "application/rdf+xml"},
+                       {"agenda_62.rdf", "application/rdf+xml"},
+                       {"Talks.rdf", "application/rdf+xml"},
+                       {"elvisimp.rdf", "application/rdf+xml"},
+                       //{"images.xrdf", "application/rdf+xml"}, //large
+                       {"libby.foaf", "application/rdf+xml"}
+               });
+       }
+
+       @Test
+       public void RDFTestCases() {
+               NTriplesSerializer nts = new NTriplesSerializer();
+
+               ParsingProvider parser = new JenaParserProvider();
+               Graph deserializedGraphOld = parser.parse(
+                               getClass().getResourceAsStream(inputFileName), 
format);
+
+               TripleCollection tc = new SimpleMGraph();
+               tc.addAll(deserializedGraphOld);
+
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               nts.serialize(baos, tc);
+
+               ByteArrayInputStream bais = new 
ByteArrayInputStream(baos.toByteArray());
+
+               Graph deserializedGraphNew = parser.parse(bais, "text/rdf+nt");
+
+               Assert.assertEquals(deserializedGraphNew, deserializedGraphOld);
+       }
+
+}

Added: 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerGraphTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerGraphTest.java?rev=942448&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerGraphTest.java
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-20/org.apache.clerezza.rdf.stable.serializer/src/test/java/org/apache/clerezza/rdf/stable/serializer/StableSerializerGraphTest.java
 Sat May  8 19:02:05 2010
@@ -0,0 +1,88 @@
+/*
+ * 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.stable.serializer;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+import java.util.Collection;
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.apache.clerezza.rdf.core.serializedform.Parser;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/**
+ *
+ * @author daniel
+ */
+...@runwith(Parameterized.class)
+public class StableSerializerGraphTest {
+       private String inputFileName;
+       private String format;
+
+       public StableSerializerGraphTest(String inputFileName, String format) {
+               this.inputFileName = inputFileName;
+               this.format = format;
+       }
+
+       @Parameterized.Parameters
+       public static Collection inputFileNames() {
+               return Arrays.asList(new Object[][]{
+                                       {"amp-in-url-test001.rdf", 
"application/rdf+xml"},
+                                       {"datatypes-test001.rdf", 
"application/rdf+xml"},
+                                       {"datatypes-test002.rdf", 
"application/rdf+xml"},
+                                       {"rdf-charmod-literals-test001.rdf", 
"application/rdf+xml"},
+                                       {"rdf-charmod-uris-test001.rdf", 
"application/rdf+xml"},
+                                       {"rdf-charmod-uris-test002.rdf", 
"application/rdf+xml"},
+                                       {"xml-canon-test001.rdf", 
"application/rdf+xml"},
+                                       {"css3deps.rdf", "application/rdf+xml"},
+                                       {"agenda_62.rdf", 
"application/rdf+xml"},
+                                       {"Talks.rdf", "application/rdf+xml"},
+                                       {"elvisimp.rdf", 
"application/rdf+xml"}, 
+                                       //{"images.xrdf", 
"application/rdf+xml"}, //large
+                                       {"libby.foaf", "application/rdf+xml"}
+                               });
+       }
+
+       @Test
+       public void RDFTestCases() {
+               StableSerializerProvider ssp = new StableSerializerProvider();
+
+               Parser parser = Parser.getInstance();
+               Graph deserializedGraphOld = parser.parse(
+                               getClass().getResourceAsStream(inputFileName), 
format);
+
+               TripleCollection tc = new SimpleMGraph();
+               tc.addAll(deserializedGraphOld);
+
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               ssp.serialize(baos, tc, "text/rdf+nt");
+
+               ByteArrayInputStream bais = new 
ByteArrayInputStream(baos.toByteArray());
+
+               Graph deserializedGraphNew = parser.parse(bais, "text/rdf+nt");
+
+               Assert.assertEquals(deserializedGraphOld, deserializedGraphNew);
+
+       }
+}


Reply via email to