Author: mir
Date: Thu Mar  4 18:39:18 2010
New Revision: 919118

URL: http://svn.apache.org/viewvc?rev=919118&view=rev
Log:
CLEREZZA-129: revised places where encoding happens

Modified:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/pom.xml
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/pom.xml?rev=919118&r1=919117&r2=919118&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/pom.xml
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/pom.xml
 Thu Mar  4 18:39:18 2010
@@ -90,9 +90,5 @@
                        <artifactId>junit</artifactId>
                        <scope>test</scope>
                </dependency>
-               <dependency>
-                       <groupId>org.apache.clerezza</groupId>
-                       <artifactId>org.apache.clerezza.utils</artifactId>
-               </dependency>
        </dependencies>
 </project>
\ No newline at end of file

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java?rev=919118&r1=919117&r2=919118&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java
 Thu Mar  4 18:39:18 2010
@@ -30,6 +30,7 @@
 import org.apache.clerezza.rdf.ontologies.HIERARCHY;
 import org.apache.clerezza.rdf.ontologies.RDF;
 import org.apache.clerezza.rdf.utils.RdfList;
+import org.apache.clerezza.utils.UriException;
 
 /**
  * This class represents a collection in a hierarchy. A collection can have
@@ -42,14 +43,15 @@
        private HierarchyService hierarchyService;
 
        CollectionNode(UriRef collectionNode, TripleCollection graph,
-                       HierarchyService hierarchyService) {
+                       HierarchyService hierarchyService) throws UriException {
                super(collectionNode, graph, hierarchyService);
-               if (!this.hasProperty(RDF.type, HIERARCHY.Collection)) {
-                       throw new IllegalArgumentException(collectionNode + " 
is not a Collection");
-               }
                this.hierarchyService = hierarchyService;
        }
 
+       boolean isValid() {
+               return this.hasProperty(RDF.type, HIERARCHY.Collection);
+       }
+
        /**
         * Adds the specified hierarchy node to this collection nodes members 
list
         * @param node the hierarchy node that is added this collection
@@ -84,7 +86,7 @@
                while (membersIter.hasNext()) {
                        UriRef uri = (UriRef) membersIter.next();
                        try {
-                               
nodes.add(hierarchyService.getHierarchyNodeWithEncodedUri(uri));
+                               
nodes.add(hierarchyService.getHierarchyNode(uri));
                        } catch (NodeDoesNotExistException ex) {
                                throw new RuntimeException(ex);
                        }
@@ -117,8 +119,12 @@
                UriRef newUri = (UriRef) replacement;
                HierarchyUtils.ensureCollectionUri(newUri);
                super.replaceWith(newUri);
-               CollectionNode movedNode = new CollectionNode(newUri, 
getGraph(),
-                               hierarchyService);
+               CollectionNode movedNode;
+               try {
+                       movedNode = new CollectionNode(newUri, getGraph(), 
hierarchyService);
+               } catch (UriException ex) {
+                       throw new IllegalArgumentException(ex);
+               }
                movedNode.updateMembers();
                return movedNode;
        }
@@ -145,16 +151,20 @@
                try {
                        CollectionNode memberCollection = new 
CollectionNode(memberUri,
                                        getGraph(), hierarchyService);
-                       UriRef newUri = new UriRef(getNode().getUnicodeString() 
+
-                               memberCollection.getName() + "/");
-                       memberCollection.replaceWith(newUri);
-                       memberCollection.updateMembers();
-               } catch(IllegalArgumentException e) {
-                       HierarchyNode memberResource = new 
HierarchyNode(memberUri,
-                                       getGraph(), hierarchyService);
-                       UriRef newUri = new UriRef(getNode().getUnicodeString() 
+
-                               memberResource.getName());
-                       memberResource.replaceWith(newUri);
+                       if (memberCollection.isValid()) {
+                               UriRef newUri = new 
UriRef(getNode().getUnicodeString()
+                                               + memberCollection.getName() + 
"/");
+                               memberCollection.replaceWith(newUri);
+                               memberCollection.updateMembers();
+                       } else {
+                               HierarchyNode memberResource = new 
HierarchyNode(memberUri,
+                                               getGraph(), hierarchyService);
+                               UriRef newUri = new 
UriRef(getNode().getUnicodeString()
+                                               + memberResource.getName());
+                               memberResource.replaceWith(newUri);
+                       }
+               } catch (UriException e) {
+                       throw new IllegalArgumentException(e);
                }
        }
 

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java?rev=919118&r1=919117&r2=919118&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
 Thu Mar  4 18:39:18 2010
@@ -19,12 +19,11 @@
 package org.apache.clerezza.platform.content.hierarchy;
 
 import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 import org.apache.clerezza.rdf.core.NonLiteral;
 import org.apache.clerezza.rdf.core.Resource;
 import org.apache.clerezza.rdf.core.TripleCollection;
 import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.utils.EncodedUriRef;
 import org.apache.clerezza.rdf.utils.GraphNode;
 import org.apache.clerezza.utils.UriException;
 import org.apache.clerezza.utils.UriUtil;
@@ -40,8 +39,8 @@
        private HierarchyService hierarchyService;
 
        HierarchyNode(UriRef hierarchyNode, TripleCollection graph,
-                       HierarchyService hierarchyService) {
-               super(hierarchyNode, graph);
+                       HierarchyService hierarchyService) throws UriException {
+               super(new EncodedUriRef(hierarchyNode), graph);
                this.hierarchyService = hierarchyService;
        }
 
@@ -76,7 +75,7 @@
                try {
                        UriRef parentCollectionUri = HierarchyUtils
                                        .extractParentCollectionUri(getNode());
-                       return 
hierarchyService.getCollectionNodeWithEncodedUri(parentCollectionUri);
+                       return 
hierarchyService.getCollectionNode(parentCollectionUri);
                } catch (NodeDoesNotExistException ex) {
                        throw new RuntimeException(ex);
                }
@@ -136,21 +135,22 @@
                }               
                String newUriString = 
newParentCollection.getNode().getUnicodeString() +
                                name;
+               String alternativeUriString = newUriString;
                if (this instanceof CollectionNode) {
                        newUriString += "/";
+               } else {
+                       alternativeUriString += "/";
                }
                UriRef newUri = new UriRef(newUriString);
-               String alternativeUriString = newUriString.endsWith("/") ? 
-                       newUriString.substring(0, newUriString.length() - 1) : 
newUriString + "/";
                UriRef alternativeUri = new UriRef(alternativeUriString);
                List<Resource> parentMembers = 
newParentCollection.getMembersRdf();
                if (parentMembers.contains(newUri) || 
parentMembers.contains(alternativeUri)) {
                        HierarchyNode existingNode = null;
                        try {
-                               existingNode = 
hierarchyService.getHierarchyNodeWithEncodedUri(newUri);
+                               existingNode = 
hierarchyService.getHierarchyNode(newUri);
                        } catch (NodeDoesNotExistException ex) {
                                try {
-                                       existingNode = 
hierarchyService.getHierarchyNodeWithEncodedUri(alternativeUri);
+                                       existingNode = 
hierarchyService.getHierarchyNode(alternativeUri);
                                } catch (NodeDoesNotExistException e) {
                                        throw new RuntimeException(ex);
                                }
@@ -186,7 +186,11 @@
                }
                UriRef newUri = (UriRef) replacement;
                super.replaceWith(newUri);
-               return new HierarchyNode(newUri, getGraph(), hierarchyService);
+               try {
+                       return new HierarchyNode(newUri, getGraph(), 
hierarchyService);
+               } catch (UriException ex) {
+                       throw new IllegalArgumentException(ex);
+               }
        }
 
        /**

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java?rev=919118&r1=919117&r2=919118&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
 Thu Mar  4 18:39:18 2010
@@ -50,7 +50,6 @@
 import org.apache.clerezza.rdf.ontologies.RDF;
 import org.apache.clerezza.rdf.utils.GraphNode;
 import org.apache.clerezza.utils.UriException;
-import org.apache.clerezza.utils.UriUtil;
 
 /**
  * The hierarchy service is an OSGi service that provides methods for managing
@@ -91,24 +90,19 @@
         * @param uri
         */
        public HierarchyNode getHierarchyNode(UriRef uri)
-                       throws NodeDoesNotExistException{               
-               try {
-                       uri = new 
UriRef(UriUtil.encodePartlyEncodedPath(uri.getUnicodeString(), "UTF-8"));
-               } catch (UriException ex) {
-                       throw new RuntimeException(ex);
-               }
-               return getHierarchyNodeWithEncodedUri(uri);
-       }
-
-       HierarchyNode getHierarchyNodeWithEncodedUri(UriRef uri)
-                       throws NodeDoesNotExistException{
+                       throws NodeDoesNotExistException {
                HierarchyNode hierarchyNode;
                try {
-                       hierarchyNode =
+                       CollectionNode collectionNode =
                                        new CollectionNode(uri, 
cgProvider.getContentGraph(), this);
-               } catch(IllegalArgumentException e) {
-                       hierarchyNode =
-                                       new HierarchyNode(uri, 
cgProvider.getContentGraph(), this);
+                       if (collectionNode.isValid()) {
+                               hierarchyNode = collectionNode;
+                       } else {
+                               hierarchyNode = new HierarchyNode(uri, 
cgProvider.getContentGraph(), this);
+                       }
+
+               } catch (UriException ex) {
+                       throw new IllegalArgumentException(ex);
                }
                checkExistence(hierarchyNode);
                return hierarchyNode;
@@ -143,22 +137,14 @@
         */
        public CollectionNode getCollectionNode(UriRef uri)
                        throws NodeDoesNotExistException{
-               try {
-                       uri = new 
UriRef(UriUtil.encodePartlyEncodedPath(uri.getUnicodeString(), "UTF-8"));
-               } catch (UriException ex) {
-                       throw new RuntimeException(ex);
-               }
-               return getCollectionNodeWithEncodedUri(uri);
-       }
-
-       CollectionNode getCollectionNodeWithEncodedUri(UriRef uri)
-                       throws NodeDoesNotExistException{
                CollectionNode collectionNode;
                try {
                        collectionNode =
                                        new CollectionNode(uri, 
cgProvider.getContentGraph(), this);
                } catch (IllegalArgumentException ex) {
                        throw new NodeDoesNotExistException(uri);
+               } catch (UriException ex) {
+                       throw new IllegalArgumentException(ex);
                }
                checkExistence(collectionNode);
                return collectionNode;
@@ -181,15 +167,14 @@
         */
        public HierarchyNode createNonCollectionNode(UriRef uri, int 
posInParent)
                        throws NodeAlreadyExistsException {
+               HierarchyUtils.ensureNonCollectionUri(uri);
+               handleRootOfUri(uri);
+               HierarchyNode hierarchyNode;
                try {
-                       uri = new 
UriRef(UriUtil.encodePath(uri.getUnicodeString(), "UTF-8"));
+                       hierarchyNode = new HierarchyNode(uri, 
cgProvider.getContentGraph(), this);
                } catch (UriException ex) {
-                       throw new RuntimeException(ex);
+                       throw new IllegalArgumentException(ex);
                }
-               HierarchyUtils.ensureNonCollectionUri(uri);
-               handleRootOfUri(uri);
-               HierarchyNode hierarchyNode = new HierarchyNode(uri,
-                               cgProvider.getContentGraph(), this);
                addToParent(hierarchyNode, posInParent);
                addCreationProperties(hierarchyNode);
                return hierarchyNode;
@@ -333,16 +318,15 @@
         */
        public CollectionNode createCollectionNode(UriRef uri, int posInParent)
                        throws NodeAlreadyExistsException {
-               try {
-                       uri = new 
UriRef(UriUtil.encodePartlyEncodedPath(uri.getUnicodeString(), "UTF-8"));
-               } catch (UriException ex) {
-                       throw new RuntimeException(ex);
-               }
                HierarchyUtils.ensureCollectionUri(uri);
                handleRootOfUri(uri);
-               addCollectionTypeTriple(uri);
-               CollectionNode collectionNode = new CollectionNode(uri,
-                               cgProvider.getContentGraph(), this);
+               CollectionNode collectionNode;
+               try {
+                       collectionNode = new CollectionNode(uri, 
cgProvider.getContentGraph(), this);
+               } catch (UriException ex) {
+                       throw new IllegalArgumentException(ex);
+               } 
+               addCollectionTypeTriple(collectionNode);
                addToParent(collectionNode, posInParent);
                addCreationProperties(collectionNode);
                return collectionNode;
@@ -394,10 +378,8 @@
                return createCollectionNode(uri);
        }
 
-       private void addCollectionTypeTriple(UriRef uri) {
-               Triple collectionTypeTriple = new TripleImpl(uri, RDF.type,
-                               HIERARCHY.Collection);
-               cgProvider.getContentGraph().add(collectionTypeTriple);
+       private void addCollectionTypeTriple(CollectionNode node) {
+               node.addProperty(RDF.type, HIERARCHY.Collection);
        }
 
        /**
@@ -442,8 +424,13 @@
        }
 
        private void addRoot(UriRef baseUri) {
-               addCollectionTypeTriple(baseUri);
-               roots.add(new CollectionNode(baseUri, 
cgProvider.getContentGraph(), this));
+               try {
+                       CollectionNode node = new CollectionNode(baseUri, 
cgProvider.getContentGraph(), this);
+                       addCollectionTypeTriple(node);
+                       roots.add(node);
+               } catch (UriException ex) {
+                       throw new IllegalArgumentException(ex);
+               }
        }
 
        protected void deactivate(ComponentContext componentContext) {

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java?rev=919118&r1=919117&r2=919118&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
 Thu Mar  4 18:39:18 2010
@@ -37,7 +37,6 @@
 import org.apache.clerezza.rdf.ontologies.HIERARCHY;
 import org.apache.clerezza.rdf.ontologies.PLATFORM;
 import org.apache.clerezza.rdf.ontologies.RDF;
-import org.apache.clerezza.utils.UriUtil;
 
 
 /**
@@ -63,8 +62,10 @@
        private UriRef newRootTest = new UriRef("http://newRoot/test/";);
        private UriRef newRoot2Resource = new 
UriRef("http://newRoot2/resource";);
        private UriRef newRoot2 = new UriRef("http://newRoot2/";);
-       private UriRef encodedResource = new UriRef("http://localhost:8282/t 
+");
-       private UriRef encodedCollection = new UriRef("http://localhost:8282/t 
+/");
+       private UriRef unencodedResource = new UriRef("http://localhost:8282/t 
+");
+       private UriRef encodedResource = new 
UriRef("http://localhost:8282/t%20%2B";);
+       private UriRef unencodedCollection = new 
UriRef("http://localhost:8282/t +/");
+       private UriRef encodedCollection = new 
UriRef("http://localhost:8282/t%20%2B/";);
 
         
 
@@ -135,19 +136,17 @@
        @Test
        public void nonCollectionNodeCreationWithEncodedCharacters() throws 
Exception{
                HierarchyService hierarchyService = getHierarchyService();
-               hierarchyService.createNonCollectionNode(encodedResource, 0);
-        HierarchyNode encodedNode = 
hierarchyService.getHierarchyNode(encodedResource);
-               
Assert.assertEquals(UriUtil.encodePath(encodedResource.getUnicodeString(), 
"UTF-8"),
-                               encodedNode.getNode().getUnicodeString());
+               hierarchyService.createNonCollectionNode(unencodedResource, 0);
+        HierarchyNode encodedNode = 
hierarchyService.getHierarchyNode(unencodedResource);
+               Assert.assertEquals(encodedResource, encodedNode.getNode());
        }
 
        @Test
        public void collectionNodeCreationWithEncodedCharacters() throws 
Exception{
                HierarchyService hierarchyService = getHierarchyService();
-               hierarchyService.createCollectionNode(encodedCollection, 0);
-        CollectionNode encodedNode = 
hierarchyService.getCollectionNode(encodedCollection);
-               
Assert.assertEquals(UriUtil.encodePath(encodedCollection.getUnicodeString(), 
"UTF-8"),
-                               encodedNode.getNode().getUnicodeString());
+               hierarchyService.createCollectionNode(unencodedCollection, 0);
+        CollectionNode encodedNode = 
hierarchyService.getCollectionNode(unencodedCollection);
+               Assert.assertEquals(encodedCollection, encodedNode.getNode());
        }
 
        @Test
@@ -195,6 +194,7 @@
                Assert.assertEquals(barFooNode, barList.get(0));
                List<HierarchyNode> barFooList = barFooNode.getMembers();
                Assert.assertEquals(1, barFooList.size());
+               System.out.println(barFooList.get(0).toString());
                HierarchyNode barFooResourceNode = 
hierarchyService.getHierarchyNode(barFooResource);
                Assert.assertEquals(barFooResourceNode, barFooList.get(0));
        }


Reply via email to