You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by tr...@apache.org on 2005/07/05 11:58:15 UTC

svn commit: r209253 - in /incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit: JcrConstants.java util/Text.java

Author: tripod
Date: Tue Jul  5 02:58:14 2005
New Revision: 209253

URL: http://svn.apache.org/viewcvs?rev=209253&view=rev
Log:
- adding well known constants (not name space aware!)
- adding some useful text methods

Added:
    incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/JcrConstants.java   (with props)
Modified:
    incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/util/Text.java

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/JcrConstants.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/JcrConstants.java?rev=209253&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/JcrConstants.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/JcrConstants.java Tue Jul  5 02:58:14 2005
@@ -0,0 +1,301 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed 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;
+
+/**
+ * This Interface defines some jcr constants
+ */
+public interface JcrConstants {
+    /**
+     * jcr:autoCreated
+     */
+    public static final String JCR_AUTOCREATED = "jcr:autoCreated";
+    /**
+     * jcr:baseVersion
+     */
+    public static final String JCR_BASEVERSION = "jcr:baseVersion";
+    /**
+     * jcr:child
+     */
+    public static final String JCR_CHILD = "jcr:child";
+    /**
+     * jcr:childNodeDefinition
+     */
+    public static final String JCR_CHILDNODEDEFINITION = "jcr:childNodeDefinition";
+    /**
+     * jcr:content
+     */
+    public static final String JCR_CONTENT = "jcr:content";
+    /**
+     * jcr:created
+     */
+    public static final String JCR_CREATED = "jcr:created";
+    /**
+     * jcr:data
+     */
+    public static final String JCR_DATA = "jcr:data";
+    /**
+     * jcr:defaultPrimaryType
+     */
+    public static final String JCR_DEFAULTPRIMARYTYPE = "jcr:defaultPrimaryType";
+    /**
+     * jcr:defaultValues
+     */
+    public static final String JCR_DEFAULTVALUES = "jcr:defaultValues";
+    /**
+     * jcr:encoding
+     */
+    public static final String JCR_ENCODING = "jcr:encoding";
+    /**
+     * jcr:frozenMixinTypes
+     */
+    public static final String JCR_FROZENMIXINTYPES = "jcr:frozenMixinTypes";
+    /**
+     * jcr:frozenNode
+     */
+    public static final String JCR_FROZENNODE = "jcr:frozenNode";
+    /**
+     * jcr:frozenPrimaryType
+     */
+    public static final String JCR_FROZENPRIMARYTYPE = "jcr:frozenPrimaryType";
+    /**
+     * jcr:frozenUuid
+     */
+    public static final String JCR_FROZENUUID = "jcr:frozenUuid";
+    /**
+     * jcr:hasOrderableChildNodes
+     */
+    public static final String JCR_HASORDERABLECHILDNODES = "jcr:hasOrderableChildNodes";
+    /**
+     * jcr:isCheckedOut
+     */
+    public static final String JCR_ISCHECKEDOUT = "jcr:isCheckedOut";
+    /**
+     * jcr:isMixin
+     */
+    public static final String JCR_ISMIXIN = "jcr:isMixin";
+    /**
+     * jcr:language
+     */
+    public static final String JCR_LANGUAGE = "jcr:language";
+    /**
+     * jcr:lastModified
+     */
+    public static final String JCR_LASTMODIFIED = "jcr:lastModified";
+    /**
+     * jcr:lockIsDeep
+     */
+    public static final String JCR_LOCKISDEEP = "jcr:lockIsDeep";
+    /**
+     * jcr:lockOwner
+     */
+    public static final String JCR_LOCKOWNER = "jcr:lockOwner";
+    /**
+     * jcr:mandatory
+     */
+    public static final String JCR_MANDATORY = "jcr:mandatory";
+    /**
+     * jcr:mergeFailed
+     */
+    public static final String JCR_MERGEFAILED = "jcr:mergeFailed";
+    /**
+     * jcr:mimeType
+     */
+    public static final String JCR_MIMETYPE = "jcr:mimeType";
+    /**
+     * jcr:mixinTypes
+     */
+    public static final String JCR_MIXINTYPES = "jcr:mixinTypes";
+    /**
+     * jcr:multiple
+     */
+    public static final String JCR_MULTIPLE = "jcr:multiple";
+    /**
+     * jcr:name
+     */
+    public static final String JCR_NAME = "jcr:name";
+    /**
+     * jcr:nodeTypeName
+     */
+    public static final String JCR_NODETYPENAME = "jcr:nodeTypeName";
+    /**
+     * jcr:onParentVersion
+     */
+    public static final String JCR_ONPARENTVERSION = "jcr:onParentVersion";
+    /**
+     * jcr:predecessors
+     */
+    public static final String JCR_PREDECESSORS = "jcr:predecessors";
+    /**
+     * jcr:primaryItemName
+     */
+    public static final String JCR_PRIMARYITEMNAME = "jcr:primaryItemName";
+    /**
+     * jcr:primaryType
+     */
+    public static final String JCR_PRIMARYTYPE = "jcr:primaryType";
+    /**
+     * jcr:propertyDefinition
+     */
+    public static final String JCR_PROPERTYDEFINITION = "jcr:propertyDefinition";
+    /**
+     * jcr:protected
+     */
+    public static final String JCR_PROTECTED = "jcr:protected";
+    /**
+     * jcr:requiredPrimaryTypes
+     */
+    public static final String JCR_REQUIREDPRIMARYTYPES = "jcr:requiredPrimaryTypes";
+    /**
+     * jcr:requiredType
+     */
+    public static final String JCR_REQUIREDTYPE = "jcr:requiredType";
+    /**
+     * jcr:rootVersion
+     */
+    public static final String JCR_ROOTVERSION = "jcr:rootVersion";
+    /**
+     * jcr:sameNameSiblings
+     */
+    public static final String JCR_SAMENAMESIBLINGS = "jcr:sameNameSiblings";
+    /**
+     * jcr:statement
+     */
+    public static final String JCR_STATEMENT = "jcr:statement";
+    /**
+     * jcr:successors
+     */
+    public static final String JCR_SUCCESSORS = "jcr:successors";
+    /**
+     * jcr:supertypes
+     */
+    public static final String JCR_SUPERTYPES = "jcr:supertypes";
+    /**
+     * jcr:system
+     */
+    public static final String JCR_SYSTEM = "jcr:system";
+    /**
+     * jcr:uuid
+     */
+    public static final String JCR_UUID = "jcr:uuid";
+    /**
+     * jcr:valueConstraints
+     */
+    public static final String JCR_VALUECONSTRAINTS = "jcr:valueConstraints";
+    /**
+     * jcr:versionHistory
+     */
+    public static final String JCR_VERSIONHISTORY = "jcr:versionHistory";
+    /**
+     * jcr:versionLabels
+     */
+    public static final String JCR_VERSIONLABELS = "jcr:versionLabels";
+    /**
+     * jcr:versionStorage
+     */
+    public static final String JCR_VERSIONSTORAGE = "jcr:versionStorage";
+    /**
+     * jcr:versionableUuid
+     */
+    public static final String JCR_VERSIONABLEUUID = "jcr:versionableUuid";
+
+    /**
+     * Pseudo property jcr:path used with query results
+     */
+    public static final String JCR_PATH = "jcr:path";
+    /**
+     * Pseudo property jcr:score used with query results
+     */
+    public static final String JCR_SCORE = "jcr:score";
+
+    /**
+     * mix:lockable
+     */
+    public static final String MIX_LOCKABLE = "mix:lockable";
+    /**
+     * mix:referenceable
+     */
+    public static final String MIX_REFERENCEABLE = "mix:referenceable";
+    /**
+     * mix:versionable
+     */
+    public static final String MIX_VERSIONABLE = "mix:versionable";
+    /**
+     * nt:base
+     */
+    public static final String NT_BASE = "nt:base";
+    /**
+     * nt:childNodeDefinition
+     */
+    public static final String NT_CHILDNODEDEFINITION = "nt:childNodeDefinition";
+    /**
+     * nt:file
+     */
+    public static final String NT_FILE = "nt:file";
+    /**
+     * nt:folder
+     */
+    public static final String NT_FOLDER = "nt:folder";
+    /**
+     * nt:frozenNode
+     */
+    public static final String NT_FROZENNODE = "nt:frozenNode";
+    /**
+     * nt:hierarchyNode
+     */
+    public static final String NT_HIERARCHYNODE = "nt:hierarchyNode";
+    /**
+     * nt:linkedFile
+     */
+    public static final String NT_LINKEDFILE = "nt:linkedFile";
+    /**
+     * nt:nodeType
+     */
+    public static final String NT_NODETYPE = "nt:nodeType";
+    /**
+     * nt:propertyDefinition
+     */
+    public static final String NT_PROPERTYDEFINITION = "nt:propertyDefinition";
+    /**
+     * nt:query
+     */
+    public static final String NT_QUERY = "nt:query";
+    /**
+     * nt:resource
+     */
+    public static final String NT_RESOURCE = "nt:resource";
+    /**
+     * nt:unstructured
+     */
+    public static final String NT_UNSTRUCTURED = "nt:unstructured";
+    /**
+     * nt:version
+     */
+    public static final String NT_VERSION = "nt:version";
+    /**
+     * nt:versionHistory
+     */
+    public static final String NT_VERSIONHISTORY = "nt:versionHistory";
+    /**
+     * nt:versionLabels
+     */
+    public static final String NT_VERSIONLABELS = "nt:versionLabels";
+    /**
+     * nt:versionedChild
+     */
+    public static final String NT_VERSIONEDCHILD = "nt:versionedChild";
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/JcrConstants.java
------------------------------------------------------------------------------
    svn = 

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/JcrConstants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/util/Text.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/util/Text.java?rev=209253&r1=209252&r2=209253&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/util/Text.java (original)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/util/Text.java Tue Jul  5 02:58:14 2005
@@ -17,9 +17,11 @@
 package org.apache.jackrabbit.util;
 
 import java.io.UnsupportedEncodingException;
+import java.io.ByteArrayOutputStream;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
+import java.util.BitSet;
 
 /**
  * This Class provides some text related utilities
@@ -159,6 +161,23 @@
     }
 
     /**
+     * Concatenates all strings in the string array using the specified delimiter.
+     * @param arr
+     * @param delim
+     * @return
+     */
+    public static String implode(String[] arr, String delim) {
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < arr.length; i++) {
+            if (i > 0) {
+                buf.append(delim);
+            }
+            buf.append(arr[i]);
+        }
+        return buf.toString();
+    }
+
+    /**
      * Replaces all occurences of <code>oldString</code> in <code>text</code>
      * with <code>newString</code>.
      *
@@ -243,4 +262,293 @@
             return buf.toString();
         }
     }
+
+    /**
+     * The list of characters that are not encoded by the <code>escape()</code>
+     * and <code>unescape()</code> METHODS. They contains the characters as
+     * defined 'unreserved' in section 2.3 of the RFC 2396 'URI genric syntax':
+     * <p/>
+     * <pre>
+     * unreserved  = alphanum | mark
+     * mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
+     * </pre>
+     */
+    public static BitSet URISave;
+
+    /**
+     * Same as {@link #URISave} but also contains the '/'
+     */
+    public static BitSet URISaveEx;
+
+    static {
+        URISave = new BitSet(256);
+        int i;
+        for (i = 'a'; i <= 'z'; i++) {
+            URISave.set(i);
+        }
+        for (i = 'A'; i <= 'Z'; i++) {
+            URISave.set(i);
+        }
+        for (i = '0'; i <= '9'; i++) {
+            URISave.set(i);
+        }
+        URISave.set('-');
+        URISave.set('_');
+        URISave.set('.');
+        URISave.set('!');
+        URISave.set('~');
+        URISave.set('*');
+        URISave.set('\'');
+        URISave.set('(');
+        URISave.set(')');
+
+        URISaveEx = (BitSet) URISave.clone();
+        URISaveEx.set('/');
+    }
+
+    /**
+     * Does an URL encoding of the <code>string</code> using the
+     * <code>escape</code> character. The characters that don't need encoding
+     * are those defined 'unreserved' in section 2.3 of the 'URI genric syntax'
+     * RFC 2396, but without the escape character.
+     *
+     * @param string the string to encode.
+     * @param escape the escape character.
+     * @return the escaped string
+     * @throws NullPointerException if <code>string</code> is <code>null</code>.
+     */
+    public static String escape(String string, char escape) {
+        return escape(string, escape, false);
+    }
+
+    /**
+     * Does an URL encoding of the <code>string</code> using the
+     * <code>escape</code> character. The characters that don't need encoding
+     * are those defined 'unreserved' in section 2.3 of the 'URI genric syntax'
+     * RFC 2396, but without the escape character. If <code>isPath</code> is
+     * <code>true</code>, additionally the slash '/' is ignored, too.
+     *
+     * @param string the string to encode.
+     * @param escape the escape character.
+     * @param isPath if <code>true</code>, the string is treated as path
+     * @return the escaped string
+     * @throws NullPointerException if <code>string</code> is <code>null</code>.
+     */
+    public static String escape(String string, char escape, boolean isPath) {
+        try {
+            BitSet validChars = isPath ? URISaveEx : URISave;
+            byte[] bytes = string.getBytes("utf-8");
+            StringBuffer out = new StringBuffer(bytes.length);
+            for (int i = 0; i < bytes.length; i++) {
+                int c = bytes[i] & 0xff;
+                if (validChars.get(c) && c != escape) {
+                    out.append((char) c);
+                } else {
+                    out.append(escape);
+                    out.append(hexTable[(c >> 4) & 0x0f]);
+                    out.append(hexTable[(c) & 0x0f]);
+                }
+            }
+            return out.toString();
+        } catch (UnsupportedEncodingException e) {
+            throw new InternalError(e.toString());
+        }
+    }
+
+    /**
+     * Does a URL encoding of the <code>string</code>. The characters that
+     * don't need encoding are those defined 'unreserved' in section 2.3 of
+     * the 'URI genric syntax' RFC 2396.
+     *
+     * @param string the string to encode
+     * @return the escaped string
+     * @throws NullPointerException if <code>string</code> is <code>null</code>.
+     */
+    public static String escape(String string) {
+        return escape(string, '%');
+    }
+
+    /**
+     * Does a URL encoding of the <code>path</code>. The characters that
+     * don't need encoding are those defined 'unreserved' in section 2.3 of
+     * the 'URI genric syntax' RFC 2396. In contrast to the
+     * {@link #escape(String)} method, not the entire path string is escaped,
+     * but every individual part (i.e. the slashes are not escaped).
+     *
+     * @param path the path to encode
+     * @return the escaped path
+     * @throws NullPointerException if <code>path</code> is <code>null</code>.
+     */
+    public static String escapePath(String path) {
+        return escape(path, '%', true);
+    }
+
+    /**
+     * Does a URL decoding of the <code>string</code> using the
+     * <code>escape</code> character. Please note that in opposite to the
+     * {@link java.net.URLDecoder} it does not transform the + into spaces.
+     *
+     * @param string the string to decode
+     * @param escape the escape character
+     * @return the decoded string
+     * @throws NullPointerException           if <code>string</code> is <code>null</code>.
+     * @throws ArrayIndexOutOfBoundsException if not enough character follow an
+     *                                        escape character
+     * @throws IllegalArgumentException       if the 2 characters following the escape
+     *                                        character do not represent a hex-number.
+     */
+    public static String unescape(String string, char escape) {
+        ByteArrayOutputStream out = new ByteArrayOutputStream(string.length());
+        for (int i = 0; i < string.length(); i++) {
+            char c = string.charAt(i);
+            if (c == escape) {
+                try {
+                    out.write(Integer.parseInt(string.substring(i + 1, i + 3), 16));
+                } catch (NumberFormatException e) {
+                    throw new IllegalArgumentException();
+                }
+                i += 2;
+            } else {
+                out.write(c);
+            }
+        }
+
+        try {
+            return new String(out.toByteArray(), "utf-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new InternalError(e.toString());
+        }
+    }
+
+    /**
+     * Does a URL decoding of the <code>string</code>. Please note that in
+     * opposite to the {@link java.net.URLDecoder} it does not transform the +
+     * into spaces.
+     *
+     * @param string the string to decode
+     * @return the decoded string
+     * @throws NullPointerException           if <code>string</code> is <code>null</code>.
+     * @throws ArrayIndexOutOfBoundsException if not enough character follow an
+     *                                        escape character
+     * @throws IllegalArgumentException       if the 2 characters following the escape
+     *                                        character do not represent a hex-number.
+     */
+    public static String unescape(String string) {
+        return unescape(string, '%');
+    }
+
+    /**
+     * Returns the name part of the path
+     *
+     * @param path the path
+     * @return the name part
+     */
+    public static String getName(String path) {
+        int pos = path.lastIndexOf('/');
+        return pos >= 0 ? path.substring(pos + 1) : "";
+    }
+
+    /**
+     * Returns the name part of the path, delimited by the given <code>delim</code>
+     *
+     * @param path the path
+     * @param delim the delimiter
+     * @return the name part
+     */
+    public static String getName(String path, char delim) {
+        int pos = path.lastIndexOf(delim);
+        return pos >= 0 ? path.substring(pos + 1) : "";
+    }
+
+    /**
+     * Determines, if two paths denote hierarchical siblins.
+     *
+     * @param p1 first path
+     * @param p2 second path
+     * @return true if on same level, false otherwise
+     */
+    public static boolean isSibling(String p1, String p2) {
+        int pos1 = p1.lastIndexOf('/');
+        int pos2 = p2.lastIndexOf('/');
+        return (pos1 == pos2 && pos1 >= 0 && p1.regionMatches(0, p2, 0, pos1));
+    }
+
+    /**
+     * Determines if the <code>descendant</code> path is hierarchical a
+     * descendant of <code>path</code>.
+     *
+     * @param path     the current path
+     * @param descendant the potential descendant
+     * @return <code>true</code> if the <code>descendant</code> is a descendant;
+     *         <code>false</code> otherwise.
+     */
+    public static boolean isDescendant(String path, String descendant) {
+        return !path.equals(descendant) &&
+                descendant.startsWith(path) &&
+                descendant.charAt(path.length()) == '/';
+    }
+
+    /**
+     * Determines if the <code>descendant</code> path is hierarchical a
+     * descendant of <code>path</code> or equal to it.
+     *
+     * @param path       the path to check
+     * @param descendant the potential descendant
+     * @return <code>true</code> if the <code>descendant</code> is a descendant
+     *         or equal; <code>false</code> otherwise.
+     */
+    public static boolean isDescendantOrEqual(String path, String descendant) {
+        if (path.equals(descendant)) {
+            return true;
+        } else {
+            String pattern = path.endsWith("/") ? path : path + "/";
+            return descendant.startsWith(pattern);
+        }
+    }
+
+    /**
+     * Returns the n<sup>th</sup> relative parent of the path, where n=level.
+     * <p>Example:<br>
+     * <code>
+     * Text.getRelativeParent("/foo/bar/test", 1) == "/foo/bar"
+     * </code>
+     *
+     * @param path the path of the page
+     * @param level  the level of the parent
+     */
+    public static String getRelativeParent(String path, int level) {
+        int idx = path.length();
+        while (level > 0) {
+            idx = path.lastIndexOf('/', idx - 1);
+            if (idx < 0) {
+                return "";
+            }
+            level--;
+        }
+        return (idx == 0) ? "/" : path.substring(0, idx);
+    }
+
+    /**
+     * Returns the n<sup>th</sup> absolute parent of the path, where n=level.
+     * <p>Example:<br>
+     * <code>
+     * Text.getAbsoluteParent("/foo/bar/test", 1) == "/foo/bar"
+     * </code>
+     *
+     * @param path the path of the page
+     * @param level  the level of the parent
+     */
+    public static String getAbsoluteParent(String path, int level) {
+        int idx = 0;
+        int len = path.length();
+        while (level >= 0 && idx < len) {
+            idx = path.indexOf('/', idx + 1);
+            if (idx < 0) {
+                idx = len;
+            }
+            level--;
+        }
+        return level >= 0 ? "" : path.substring(0, idx);
+    }
+
 }