jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From resc...@apache.org
Subject svn commit: r1330298 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/ oak-core/src/test/java/org/apache/jackrabbit/oak/namepath/ oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/
Date Wed, 25 Apr 2012 14:07:21 GMT
Author: reschke
Date: Wed Apr 25 14:07:20 2012
New Revision: 1330298

URL: http://svn.apache.org/viewvc?rev=1330298&view=rev
Log:
OAK-61: add oak->jcr conversion plus test cases, add transformation to a few experimental
places in *Impl (WIP)

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/AbstractNameMapper.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/Paths.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/namepath/PathsTest.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/AbstractNameMapper.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/AbstractNameMapper.java?rev=1330298&r1=1330297&r2=1330298&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/AbstractNameMapper.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/AbstractNameMapper.java
Wed Apr 25 14:07:20 2012
@@ -57,8 +57,7 @@ public abstract class AbstractNameMapper
             String name = jcrName.substring(pos + 1);
             String oakPrefix = getOakPrefix(pref);
             if (oakPrefix == null) {
-                // TODO
-                return null;
+                throw new IllegalArgumentException("prefix '" + pref + "' is not mapped"
);
             } else {
                 return oakPrefix + ":" + name;
             }
@@ -74,10 +73,16 @@ public abstract class AbstractNameMapper
         } else {
             String pref = oakName.substring(0, pos);
             String name = oakName.substring(pos + 1);
+
+            if (pref.startsWith("{")) {
+                throw new IllegalStateException(
+                        "invalid oak name (maybe expanded name leaked out?): "
+                                + oakName);
+            }
+
             String jcrPrefix = getJcrPrefix(pref);
             if (jcrPrefix == null) {
-                // TODO
-                return null;
+                throw new IllegalStateException("invalid oak name: " + oakName);
             } else {
                 return jcrPrefix + ":" + name;
             }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/Paths.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/Paths.java?rev=1330298&r1=1330297&r2=1330298&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/Paths.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/Paths.java
Wed Apr 25 14:07:20 2012
@@ -16,15 +16,14 @@
 */
 package org.apache.jackrabbit.oak.namepath;
 
-import org.apache.jackrabbit.mk.util.PathUtils;
-import org.apache.jackrabbit.oak.namepath.JcrNameParser.Listener;
-import org.apache.jackrabbit.oak.util.Function1;
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
 
+import org.apache.jackrabbit.mk.util.PathUtils;
+import org.apache.jackrabbit.oak.namepath.JcrNameParser.Listener;
+
 /**
  * All path in the Oak API have the following form
  * <p>
@@ -188,18 +187,18 @@ public final class Paths {
         return true;
     }
 
-    public static String toOakName(String name, final NameMapper nm) {
+    public static String toOakName(String name, final NameMapper mapper) {
         final StringBuilder element = new StringBuilder();
         
         Listener listener = new JcrNameParser.Listener() {
             @Override
             public void error(String message) {
-                // todo implement error
+                throw new RuntimeException(message);
             }
 
             @Override
             public void name(String name) {
-                String p = nm.getOakName(name);
+                String p = mapper.getOakName(name);
                 element.append(p);
             }
         };
@@ -208,14 +207,14 @@ public final class Paths {
         return element.toString();
     }
     
-    public static String toOakPath(String path, final NameMapper nm) {
+    public static String toOakPath(String jcrPath, final NameMapper mapper) {
         final List<String> elements = new ArrayList<String>();
         
         JcrPathParser.Listener listener = new JcrPathParser.Listener() {
             @Override
             public void root() {
                 if (!elements.isEmpty()) {
-                    // todo error
+                    throw new RuntimeException("/ on non-empty path");
                 }
                 elements.add("");
             }
@@ -223,7 +222,7 @@ public final class Paths {
             @Override
             public void identifier(String identifier) {
                 if (!elements.isEmpty()) {
-                    // todo error
+                    throw new RuntimeException("[identifier] on non-empty path");
                 }
                 elements.add(identifier);  // todo resolve identifier
                 // todo seal
@@ -237,7 +236,7 @@ public final class Paths {
             @Override
             public void parent() {
                 if (elements.isEmpty()) {
-                    // todo error
+                    throw new RuntimeException(".. of empty path");
                 }
                 elements.remove(elements.size() - 1);
             }
@@ -245,7 +244,7 @@ public final class Paths {
             @Override
             public void index(int index) {
                 if (index > 1) {
-                    // todo error
+                    throw new RuntimeException("index > 1");
                 }
             }
 
@@ -256,12 +255,12 @@ public final class Paths {
 
             @Override
             public void name(String name) {
-                String p = nm.getOakName(name);
+                String p = mapper.getOakName(name);
                 elements.add(p);
             }
         };
 
-        JcrPathParser.parse(path, listener);
+        JcrPathParser.parse(jcrPath, listener);
         
         StringBuilder oakPath = new StringBuilder();
         for (String element : elements) {
@@ -279,6 +278,77 @@ public final class Paths {
         return oakPath.toString();
     }
     
+    public static String toJcrPath(String oakPath, final NameMapper mapper) {
+        final List<String> elements = new ArrayList<String>();
+        
+        JcrPathParser.Listener listener = new JcrPathParser.Listener() {
+            @Override
+            public void root() {
+                if (!elements.isEmpty()) {
+                    throw new RuntimeException("/ on non-empty path");
+                }
+                elements.add("");
+            }
+
+            @Override
+            public void identifier(String identifier) {
+                if (!elements.isEmpty()) {
+                    throw new RuntimeException("[identifier] on non-empty path");
+                }
+                elements.add(identifier);  // todo resolve identifier
+                // todo seal
+            }
+
+            @Override
+            public void current() {
+                // nothing to do here
+            }
+
+            @Override
+            public void parent() {
+                if (elements.isEmpty()) {
+                    throw new RuntimeException(".. of empty path");
+                }
+                elements.remove(elements.size() - 1);
+            }
+
+            @Override
+            public void index(int index) {
+                if (index > 1) {
+                    throw new RuntimeException("index > 1");
+                }
+            }
+
+            @Override
+            public void error(String message) {
+                throw new RuntimeException(message);
+            }
+
+            @Override
+            public void name(String name) {
+                String p = mapper.getJcrName(name);
+                elements.add(p);
+            }
+        };
+
+        JcrPathParser.parse(oakPath, listener);
+        
+        StringBuilder jcrPath = new StringBuilder();
+        for (String element : elements) {
+            if (element.isEmpty()) {
+                // root
+                jcrPath.append('/');
+            }
+            else {
+                jcrPath.append(element);
+                jcrPath.append('/');
+            }
+        }
+        
+        jcrPath.deleteCharAt(jcrPath.length() - 1);
+        return jcrPath.toString();
+    }
+
     //------------------------------------------------------------< private >--- 
 
     private static Iterable<String> split(final String string, final char separator)
{

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/namepath/PathsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/namepath/PathsTest.java?rev=1330298&r1=1330297&r2=1330298&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/namepath/PathsTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/namepath/PathsTest.java
Wed Apr 25 14:07:20 2012
@@ -1,24 +1,25 @@
 /*
-* 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.
-*/
+ * 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.jackrabbit.oak.namepath;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -28,13 +29,13 @@ import org.junit.Test;
 public class PathsTest {
 
     private TestNameMapper mapper = new TestNameMapper();
-    
+
     @Test
     public void getPrefix() {
         assertEquals(null, Paths.getPrefixFromElement("foo"));
         assertEquals("foo", Paths.getPrefixFromElement("foo:bar"));
     }
-    
+
     @Test
     public void getName() {
         assertEquals("foo", Paths.getNameFromElement("foo"));
@@ -67,7 +68,7 @@ public class PathsTest {
         assertFalse(Paths.isValidPath("/p:a//r:c"));
         assertFalse(Paths.isValidPath("//"));
     }
-    
+
     @Test
     public void isValidRelativePath() {
         assertTrue(Paths.isValidPath("a/b/c"));
@@ -79,20 +80,45 @@ public class PathsTest {
         assertFalse(Paths.isValidPath("p:a/:/r:c"));
         assertFalse(Paths.isValidPath("p:a//r:c"));
     }
-    
+
     @Test
     public void testJcrToOak() {
         assertEquals("/oak-foo:bar", Paths.toOakPath("/foo:bar", mapper));
-        assertEquals("/oak-foo:bar/oak-quu:qux", Paths.toOakPath("/foo:bar/quu:qux", mapper));
+        assertEquals("/oak-foo:bar/oak-quu:qux",
+                Paths.toOakPath("/foo:bar/quu:qux", mapper));
         assertEquals("oak-foo:bar", Paths.toOakPath("foo:bar", mapper));
-        assertEquals("oak-nt:unstructured", Paths.toOakPath("{http://www.jcp.org/jcr/nt/1.0}unstructured",
mapper));
-        assertEquals("foobar/oak-jcr:content", Paths.toOakPath("foobar/{http://www.jcp.org/jcr/1.0}content",
mapper));
+        assertEquals("oak-nt:unstructured", Paths.toOakPath(
+                "{http://www.jcp.org/jcr/nt/1.0}unstructured", mapper));
+        assertEquals("foobar/oak-jcr:content", Paths.toOakPath(
+                "foobar/{http://www.jcp.org/jcr/1.0}content", mapper));
+    }
+
+    @Test
+    public void testOakToJcr() {
+        assertEquals("/jcr-foo:bar", Paths.toJcrPath("/foo:bar", mapper));
+        assertEquals("/jcr-foo:bar/jcr-quu:qux",
+                Paths.toJcrPath("/foo:bar/quu:qux", mapper));
+        assertEquals("jcr-foo:bar", Paths.toJcrPath("foo:bar", mapper));
+
+        try {
+            Paths.toJcrPath("{http://www.jcp.org/jcr/nt/1.0}unstructured",
+                    mapper);
+            fail("expanded name should not be accepted");
+        } catch (IllegalStateException expected) {
+        }
+
+        try {
+            Paths.toJcrPath("foobar/{http://www.jcp.org/jcr/1.0}content",
+                    mapper);
+            fail("expanded name should not be accepted");
+        } catch (IllegalStateException expected) {
+        }
     }
-    
+
     private class TestNameMapper extends AbstractNameMapper {
 
         private Map<String, String> uri2oakprefix = new HashMap<String, String>();
-        
+
         public TestNameMapper() {
             uri2oakprefix.put("", "");
             uri2oakprefix.put("http://www.jcp.org/jcr/1.0", "jcr");
@@ -100,7 +126,7 @@ public class PathsTest {
             uri2oakprefix.put("http://www.jcp.org/jcr/mix/1.0", "mix");
             uri2oakprefix.put("http://www.w3.org/XML/1998/namespace", "xml");
         }
-        
+
         @Override
         protected String getJcrPrefix(String oakPrefix) {
             if (oakPrefix.length() == 0) {
@@ -123,6 +149,6 @@ public class PathsTest {
         protected String getOakPrefixFromURI(String uri) {
             return "oak-" + uri2oakprefix.get(uri);
         }
-        
+
     }
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java?rev=1330298&r1=1330297&r2=1330298&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java
Wed Apr 25 14:07:20 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
+import org.apache.jackrabbit.oak.namepath.Paths;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -131,4 +132,17 @@ abstract class ItemImpl implements Item 
         return sessionContext.getValueFactory();
     }
 
+    
+    String toOakPath(String jcrPath) throws RepositoryException {
+        try {
+            return Paths.toOakPath(jcrPath, sessionContext.getNameMapper());
+        }
+        catch (IllegalArgumentException ex) {
+            throw new RepositoryException(ex);
+        }
+    }
+
+    String toJcrPath(String oakPath) {
+        return Paths.toJcrPath(oakPath, sessionContext.getNameMapper());
+    }
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java?rev=1330298&r1=1330297&r2=1330298&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
Wed Apr 25 14:07:20 2012
@@ -901,14 +901,17 @@ public class NodeImpl extends ItemImpl i
             : new NodeImpl(sessionContext, tree);
     }
     
-    private PropertyImpl getPropertyOrNull(String relPath) {
-        String absPath = Paths.concat(path(), Paths.getParentPath(relPath));
+    private PropertyImpl getPropertyOrNull(String relJcrPath) throws RepositoryException
{
+        
+        String relOakPath = toOakPath(relJcrPath);
+        
+        String absPath = Paths.concat(path(), Paths.getParentPath(relOakPath));
         Tree parent = getBranch().getTree(absPath);
         if (parent == null) {
             return null;
         }
 
-        String name = Paths.getName(relPath);
+        String name = Paths.getName(relOakPath);
         PropertyState propertyState = parent.getProperty(name);
         return propertyState == null
             ? null

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java?rev=1330298&r1=1330297&r2=1330298&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
Wed Apr 25 14:07:20 2012
@@ -78,7 +78,7 @@ public class PropertyImpl extends ItemIm
      */
     @Override
     public String getName() throws RepositoryException {
-        return name();
+        return toJcrPath(name());
     }
 
     /**

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java?rev=1330298&r1=1330297&r2=1330298&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java
Wed Apr 25 14:07:20 2012
@@ -23,6 +23,7 @@ import org.apache.jackrabbit.oak.api.Con
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.jcr.value.ValueFactoryImpl;
+import org.apache.jackrabbit.oak.namepath.NameMapper;
 
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -36,6 +37,7 @@ public interface SessionContext<T extend
     String getWorkspaceName();
     ContentSession getContentSession();
     ValueFactoryImpl getValueFactory();
+    NameMapper getNameMapper();
     LockManager getLockManager() throws RepositoryException;
     NodeTypeManager getNodeTypeManager() throws RepositoryException;
     VersionManager getVersionManager() throws RepositoryException;

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java?rev=1330298&r1=1330297&r2=1330298&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
Wed Apr 25 14:07:20 2012
@@ -21,7 +21,6 @@ import java.security.AccessControlExcept
 
 import javax.jcr.Credentials;
 import javax.jcr.InvalidItemStateException;
-import javax.jcr.NamespaceException;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.Node;
 import javax.jcr.Repository;
@@ -500,6 +499,11 @@ public class SessionImpl extends Abstrac
         }
 
         @Override
+        public NameMapper getNameMapper() {
+            return nameMapper;
+        }
+
+        @Override
         public LockManager getLockManager() throws RepositoryException {
             return getWorkspace().getLockManager();
         }



Mime
View raw message