Deal with illegal JCR characters
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / JcrPath.java
index 1f79434845eb84c54ce2aa9ab08bbdafb5d76113..09b3e0e7ac0c62391631f90a2ecbc2fd913d6b53 100644 (file)
@@ -51,6 +51,9 @@ public class JcrPath implements Path {
                String trimmedPath = path.substring(absolute ? 1 : 0,
                                path.charAt(path.length() - 1) == delimChar ? path.length() - 1 : path.length());
                this.path = trimmedPath.split(delimStr);
+               for (int i = 0; i < this.path.length; i++) {
+                       this.path[i] = Text.unescapeIllegalJcrChars(this.path[i]);
+               }
                this.hashCode = this.path[this.path.length - 1].hashCode();
        }
 
@@ -102,6 +105,20 @@ public class JcrPath implements Path {
                return sb.toString();
        }
 
+       public String toJcrPath() {
+               if (path == null)
+                       return "/";
+               StringBuilder sb = new StringBuilder();
+               if (isAbsolute())
+                       sb.append('/');
+               for (int i = 0; i < path.length; i++) {
+                       if (i != 0)
+                               sb.append('/');
+                       sb.append(Text.escapeIllegalJcrChars(path[i]));
+               }
+               return sb.toString();
+       }
+
        @Override
        public Path getFileName() {
                if (path == null)
@@ -288,7 +305,7 @@ public class JcrPath implements Path {
        public Node getNode() throws RepositoryException {
                if (!isAbsolute())// TODO default dir
                        throw new JcrFsException("Cannot get node from relative path");
-               String pathStr = toString();
+               String pathStr = toJcrPath();
                Session session = fs.getSession();
                // TODO synchronize on the session ?
                if (!session.itemExists(pathStr))