]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/util/URIStringUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / URIStringUtils.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/URIStringUtils.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/URIStringUtils.java
new file mode 100644 (file)
index 0000000..dde498a
--- /dev/null
@@ -0,0 +1,528 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/* The following copyright is attached because marked parts of the following code are\r
+ * copied and modified from Jena 2.4.\r
+ */\r
+/*\r
+ *  (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006 Hewlett-Packard Development Company, LP\r
+ *  All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ *    notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ *    notice, this list of conditions and the following disclaimer in the\r
+ *    documentation and/or other materials provided with the distribution.\r
+ * 3. The name of the author may not be used to endorse or promote products\r
+ *    derived from this software without specific prior written permission.\r
+\r
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+ * * Id: URIref.java,v 1.5 2006/03/22 13:52:49 andy_seaborne Exp\r
+\r
+   AUTHOR:  Jeremy J. Carroll\r
+ */\r
+\r
+package org.simantics.databoard.util;\r
+\r
+import java.nio.charset.Charset;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+\r
+/**\r
+ * Contains utility methods for handling URI Strings in the context of ProCore\r
+ * and the Simantics platform. This includes URI escaping and unescaping and\r
+ * namespace/local name separation and joining.\r
+ * \r
+ * <p>\r
+ * URI's in this context are assumed to be formed as follows:\r
+ * \r
+ * <pre>\r
+ * &lt;namespace part&gt;#&lt;local name part&gt;\r
+ * </pre>\r
+ * \r
+ * <p>\r
+ * The implementation of {@link #escape(String)} and {@link #unescape(String)}\r
+ * is copied and modified from Jena's com.hp.hpl.jena.util.URIref.\r
+ * </p>\r
+ * \r
+ * @see <a href="http://en.wikipedia.org/wiki/Percent-encoding">Percent-encoding</a>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class URIStringUtils {\r
+\r
+    /**\r
+     * The character '/' is used as a path separator in URI namespace parts in ProCore.\r
+     */\r
+    public static final char NAMESPACE_PATH_SEPARATOR  = '/';\r
+\r
+    /**\r
+     * The '#' character is used to separate the local name and namespace parts\r
+     * of an URI, for example <code>http://www.example.org#localName</code>.\r
+     */\r
+    public static final char NAMESPACE_LOCAL_SEPARATOR = '#';\r
+\r
+    /**\r
+     * Checks that only one separator character ({@link #NAMESPACE_LOCAL_SEPARATOR})\r
+     * between namespace and localname exists in the specified URI and returns\r
+     * its index.\r
+     * \r
+     * @param uri the URI to search from\r
+     * @return the character index of the separator ranging from 0 to uri.length()-1\r
+     * @throws IllegalArgumentException if no {@link #NAMESPACE_LOCAL_SEPARATOR}\r
+     *         is found in the specified URI\r
+     */\r
+    private static int assertSingleSeparatorPosition(String uri) {\r
+        int sharpIndex = uri.indexOf(NAMESPACE_LOCAL_SEPARATOR);\r
+        if (sharpIndex == -1) {\r
+            throw new IllegalArgumentException("URI '" + uri + "' does not contain any '" + NAMESPACE_LOCAL_SEPARATOR + "' separator characters");\r
+        }\r
+        int nextSharpIndex = uri.indexOf(NAMESPACE_LOCAL_SEPARATOR, sharpIndex + 1);\r
+        if (nextSharpIndex != -1) {\r
+            throw new IllegalArgumentException("URI '" + uri + "' contains multiple '" + NAMESPACE_LOCAL_SEPARATOR + "' separator characters");\r
+        }\r
+        return sharpIndex;\r
+    }\r
+\r
+    /**\r
+     * Checks that only one separator character (\r
+     * {@link #NAMESPACE_LOCAL_SEPARATOR}) between namespace and localname\r
+     * exists in the specified URI and returns its index. This version does not\r
+     * throw an exception when the separator is not found.\r
+     * \r
+     * @param uri the URI to search from\r
+     * @return the character index of the separator ranging from 0 to\r
+     *         uri.length()-1 or -1 if no separator was found.\r
+     */\r
+    private static int singleSeparatorPosition(String uri) {\r
+        int sharpIndex = uri.indexOf(NAMESPACE_LOCAL_SEPARATOR);\r
+        if (sharpIndex == -1) {\r
+            return -1;\r
+        }\r
+        int nextSharpIndex = uri.indexOf(NAMESPACE_LOCAL_SEPARATOR, sharpIndex + 1);\r
+        if (nextSharpIndex != -1) {\r
+            return -1;\r
+        }\r
+        return sharpIndex;\r
+    }\r
+\r
+    /**\r
+     * Splits the specified URI into a namespace and a local name and returns\r
+     * the namespace.\r
+     * \r
+     * <p>\r
+     * Assumes that namespaces are always separated by\r
+     * {@link #NAMESPACE_LOCAL_SEPARATOR} characters.\r
+     * </p>\r
+     * \r
+     * @param uri the URI to split, must be non-null\r
+     * @return the namespace part of the specified URI\r
+     * @throws IllegalArgumentException for URIs without a\r
+     *         {@link #NAMESPACE_LOCAL_SEPARATOR}\r
+     * @throws NullPointerException for <code>null</code> URIs\r
+     */\r
+    public static String getNamespace(String uri) {\r
+        if (uri == null)\r
+            throw new NullPointerException("null uri");\r
+        int separatorIndex = assertSingleSeparatorPosition(uri);\r
+        return uri.substring(0, separatorIndex);\r
+    }\r
+    \r
+    public static String getRVIParent(String uri) {\r
+        int childSeparator = uri.lastIndexOf(URIStringUtils.NAMESPACE_PATH_SEPARATOR);\r
+        int propertySeparator = uri.lastIndexOf(URIStringUtils.NAMESPACE_LOCAL_SEPARATOR);\r
+        int separator = Math.max(childSeparator, propertySeparator);\r
+        return uri.substring(0, separator);\r
+    }\r
+    \r
+\r
+    /**\r
+     * Splits the specified URI into a namespace and a local name and returns\r
+     * the local name.\r
+     * \r
+     * <p>\r
+     * Assumes that namespaces are always separated by\r
+     * {@link #NAMESPACE_LOCAL_SEPARATOR} characters.\r
+     * </p>\r
+     * \r
+     * @param uri the URI to split, must be non-null\r
+     * @return the local name part of the specified URI\r
+     * @throws IllegalArgumentException for URIs without a\r
+     *         {@link #NAMESPACE_LOCAL_SEPARATOR}\r
+     * @throws NullPointerException for <code>null</code> URIs\r
+     */\r
+    public static String getLocalName(String uri) {\r
+        if (uri == null)\r
+            throw new NullPointerException("null uri");\r
+        int separatorIndex = assertSingleSeparatorPosition(uri);\r
+        return uri.substring(separatorIndex + 1);\r
+    }\r
+\r
+    public static String escapeName(String name) {\r
+        char[] chars = name.toCharArray();\r
+        boolean modified = false;\r
+        for(int i=0;i<chars.length;++i)\r
+            if(!Character.isJavaIdentifierPart(chars[i])) {\r
+                chars[i] = '_';\r
+                modified = true;\r
+            }\r
+        if(modified)\r
+            return new String(chars);\r
+        else\r
+            return name;\r
+    }\r
+\r
+    final private static int HTTP_POSITION = "http://".length();\r
+\r
+    public static String[] splitURI(String uri) {\r
+        int nextPathSeparator = uri.lastIndexOf(URIStringUtils.NAMESPACE_PATH_SEPARATOR);\r
+        if (nextPathSeparator == -1) return null;\r
+        if (nextPathSeparator == HTTP_POSITION - 1) {\r
+            if(uri.startsWith("http://")) return new String[] { "http://", uri.substring(HTTP_POSITION, uri.length()) };\r
+            else return null;\r
+        }\r
+        return new String[] {\r
+                uri.substring(0, nextPathSeparator),\r
+                uri.substring(nextPathSeparator + 1, uri.length())\r
+        };\r
+    }\r
+    \r
+    public static List<String> splitURISCL(String uri) {\r
+        String[] result = splitURI(uri);\r
+        ArrayList<String> list = new ArrayList<String>(result.length);\r
+        for(String s : result) list.add(s);\r
+        return list;\r
+    }\r
+\r
+    /**\r
+     * Splits the specified URI into a namespace and a local name and returns\r
+     * them both separately as an array.\r
+     * \r
+     * @param uri the URI to split, must be non-null\r
+     * @return [0] = namespace, [1] = local name or <code>null</code> if the URI\r
+     *         cannot be split.\r
+     * @throws NullPointerException for <code>null</code> URIs\r
+     */\r
+    public static String[] trySplitNamespaceAndLocalName(String uri) {\r
+        if (uri == null)\r
+            throw new NullPointerException("null uri");\r
+        int separatorIndex = singleSeparatorPosition(uri);\r
+        return separatorIndex == -1 ?\r
+                null\r
+                : new String[] { uri.substring(0, separatorIndex), uri.substring(separatorIndex + 1) };\r
+    }\r
+\r
+    /**\r
+     * Splits the specified URI into a namespace and a local name and returns\r
+     * them both separately as an array.\r
+     * \r
+     * @param uri the URI to split, must be non-null\r
+     * @return [0] = namespace, [1] = local name\r
+     * @throws IllegalArgumentException for URIs without a\r
+     *         {@link #NAMESPACE_LOCAL_SEPARATOR}\r
+     * @throws NullPointerException for <code>null</code> URIs\r
+     */\r
+    public static String[] splitNamespaceAndLocalName(String uri) {\r
+        if (uri == null)\r
+            throw new NullPointerException("null uri");\r
+        int separatorIndex = assertSingleSeparatorPosition(uri);\r
+        return new String[] { uri.substring(0, separatorIndex), uri.substring(separatorIndex + 1) };\r
+    }\r
+\r
+    /**\r
+     * Converts a unicode string into an RFC 2396 compliant URI, using %NN\r
+     * escapes where appropriate, including the\r
+     * {@link #NAMESPACE_PATH_SEPARATOR} character.\r
+     * \r
+     * @param localName the string to escape\r
+     * @return the escaped string\r
+     * @throws NullPointerException for <code>null</code> URIs\r
+     */\r
+    public static String escapeURI(String localName) {\r
+        if (localName == null)\r
+            throw new NullPointerException("null local name");\r
+        String result = encode(localName);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * Add a suffix path to a namespace string, i.e. join the strings to\r
+     * together with the {@link #NAMESPACE_PATH_SEPARATOR} character in between.\r
+     * \r
+     * @param namespace the namespace to append to\r
+     * @param suffix the suffix to append\r
+     * @return the joined namespace\r
+     */\r
+    public static String appendURINamespace(String namespace, String suffix) {\r
+        //return namespace + NAMESPACE_PATH_SEPARATOR + suffix;\r
+        return new StringBuffer(namespace.length() + 1 + suffix.length())\r
+        .append(namespace)\r
+        .append(NAMESPACE_PATH_SEPARATOR)\r
+        .append(suffix)\r
+        .toString();\r
+    }\r
+\r
+    /**\r
+     * Join a namespace and a localname to form an URI with\r
+     * {@link #NAMESPACE_LOCAL_SEPARATOR}.\r
+     * \r
+     * @param namespace the namespace part to join\r
+     * @param localName the localname part to join\r
+     * @return the joined URI\r
+     */\r
+    public static String makeURI(String namespace, String localName) {\r
+        //return namespace + NAMESPACE_LOCAL_SEPARATOR + escapeURI(localName);\r
+        String escapedLocalName = escapeURI(localName);\r
+        return new StringBuffer(namespace.length() + 1 + escapedLocalName.length())\r
+        .append(namespace)\r
+        .append(NAMESPACE_LOCAL_SEPARATOR)\r
+        .append(escapedLocalName)\r
+        .toString();\r
+    }\r
+\r
+    /**\r
+     * Convert a Unicode string, first to UTF-8 and then to an RFC 2396\r
+     * compliant URI with optional fragment identifier using %NN escape\r
+     * mechanism as appropriate. The '%' character is assumed to already\r
+     * indicated an escape byte. The '%' character must be followed by two\r
+     * hexadecimal digits.\r
+     * \r
+     * <p>\r
+     * Meant to be used for encoding URI local name parts if it is desired to\r
+     * have '/' characters in the local name without creating a new namespace.\r
+     * For example these two URI's:<br/>\r
+     * \r
+     * <code>\r
+     * http://foo.bar.com/foo/bar/org%2Fcom<br/>\r
+     * http://foo.bar.com/foo/bar/net%2Fcom<br/>\r
+     * </code>\r
+     * \r
+     * have the same namespace <code>http://foo.bar.com/foo/bar/</code> and\r
+     * different local names <code>org%2Fcom</code> and <code>net%2Fcom</code>\r
+     * or <code>org/com</code> and <code>net/com</code> in unescaped form.\r
+     * </p>\r
+     * \r
+     * @param unicode The uri, in characters specified by RFC 2396 + '#'\r
+     * @return The corresponding Unicode String\r
+     */\r
+    public static String escape(String unicode) {\r
+        return encode(unicode);\r
+    }\r
+\r
+\r
+    final private static Charset UTF8 = Charset.forName("UTF-8");\r
+    final private static Charset ASCII = Charset.forName("US-ASCII");\r
+\r
+    /* Copied and modified from Jena 2.4 com.hp.hpl.jena.util.URIref */\r
+    private static String encode(String unicode) {\r
+        boolean needsEscapes = needsEscaping(unicode);\r
+        if (!needsEscapes)\r
+            return unicode;\r
+\r
+        byte utf8[] = unicode.getBytes(UTF8);\r
+        byte rsltAscii[] = new byte[utf8.length * 6];\r
+        int in = 0;\r
+        int out = 0;\r
+        while (in < utf8.length) {\r
+            switch (utf8[in]) {\r
+                case (byte)'a': case (byte)'b': case (byte)'c': case (byte)'d': case (byte)'e': case (byte)'f': case (byte)'g': case (byte)'h': case (byte)'i': case (byte)'j': case (byte)'k': case (byte)'l': case (byte)'m': case (byte)'n': case (byte)'o': case (byte)'p': case (byte)'q': case (byte)'r': case (byte)'s': case (byte)'t': case (byte)'u': case (byte)'v': case (byte)'w': case (byte)'x': case (byte)'y': case (byte)'z':\r
+                case (byte)'A': case (byte)'B': case (byte)'C': case (byte)'D': case (byte)'E': case (byte)'F': case (byte)'G': case (byte)'H': case (byte)'I': case (byte)'J': case (byte)'K': case (byte)'L': case (byte)'M': case (byte)'N': case (byte)'O': case (byte)'P': case (byte)'Q': case (byte)'R': case (byte)'S': case (byte)'T': case (byte)'U': case (byte)'V': case (byte)'W': case (byte)'X': case (byte)'Y': case (byte)'Z':\r
+                case (byte)'0': case (byte)'1': case (byte)'2': case (byte)'3': case (byte)'4': case (byte)'5': case (byte)'6': case (byte)'7': case (byte)'8': case (byte)'9':\r
+                case (byte)';': case (byte)'?': case (byte)':': case (byte)'@': case (byte)'=': case (byte)'+': case (byte)'$': case (byte)',':\r
+                case (byte)'-': case (byte)'_': case (byte)'.': case (byte)'!': case (byte)'~': case (byte)'*': case (byte)'\'': case (byte)'(': case (byte)')':\r
+                case (byte)'[': case (byte)']':\r
+                    rsltAscii[out] = utf8[in];\r
+                    out++;\r
+                    in++;\r
+                    break;\r
+                case (byte)' ':\r
+                    rsltAscii[out++] = (byte) '%';\r
+                    rsltAscii[out++] = '2';\r
+                    rsltAscii[out++] = '0';\r
+                    in++;\r
+                    break;\r
+                case (byte) '%':\r
+                    // [lehtonen] NOTE: all input needs to be escaped, i.e. "%01" should result in "%2501", not "%01".\r
+                    // escape+unescape is a bijection, not an idempotent operation. \r
+                    // Fall through to to escape '%' as '%25'\r
+                case (byte) '#':\r
+                case (byte) '/':\r
+                    // Fall through to escape '/'\r
+                case (byte)'&':\r
+                    // Fall through to escape '&' characters to avoid them\r
+                    // being interpreted as SGML entities.\r
+                default:\r
+                    rsltAscii[out++] = (byte) '%';\r
+                    // Get rid of sign ...\r
+                    int c = (utf8[in]) & 255;\r
+                    rsltAscii[out++] = hexEncode(c / 16);\r
+                    rsltAscii[out++] = hexEncode(c % 16);\r
+                    in++;\r
+                    break;\r
+            }\r
+        }\r
+        return new String(rsltAscii, 0, out, ASCII);\r
+    }\r
+\r
+    /*\r
+     * RFC 3986 section 2.2 Reserved Characters (January 2005)\r
+     * !*'();:@&=+$,/?#[]\r
+     */\r
+    private static boolean needsEscaping(String unicode) {\r
+        int len = unicode.length();\r
+        for (int i = 0; i < len; ++i) {\r
+            switch (unicode.charAt(i)) {\r
+                case (byte)'!':\r
+                case (byte)'*':\r
+                case (byte)'\'':\r
+                case (byte)'(':\r
+                case (byte)')':\r
+                case (byte)';':\r
+                case (byte)':':\r
+                case (byte)'@':\r
+                case (byte)'=': \r
+                case (byte)'+':\r
+                case (byte)'$':\r
+                case (byte)',':\r
+                case (byte)'?':\r
+                case (byte)'~':\r
+                case (byte)'[':\r
+                case (byte)']':\r
+                    break;\r
+                case (byte)' ':\r
+                case (byte) '#':\r
+                case (byte) '%':\r
+                case (byte) '/':\r
+                case (byte)'&':\r
+                    return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    private static boolean needsUnescaping(String unicode) {\r
+        return unicode.indexOf('%') > -1;\r
+    }\r
+\r
+    /**\r
+     * Convert a URI, in US-ASCII, with escaped characters taken from UTF-8, to\r
+     * the corresponding Unicode string. On ill-formed input the results are\r
+     * undefined, specifically if the unescaped version is not a UTF-8 String,\r
+     * some String will be returned.\r
+     * \r
+     * @param uri the uri, in characters specified by RFC 2396 + '#'.\r
+     * @return the corresponding Unicode String.\r
+     * @exception IllegalArgumentException if a % hex sequence is ill-formed.\r
+     */\r
+    public static String unescape(String uri) {\r
+        try {\r
+            if (!needsUnescaping(uri))\r
+                return uri;\r
+\r
+            byte ascii[] = uri.getBytes("US-ASCII");\r
+            byte utf8[] = new byte[ascii.length];\r
+            int in = 0;\r
+            int out = 0;\r
+            while ( in < ascii.length ) {\r
+                if (ascii[in] == (byte) '%') {\r
+                    in++;\r
+                    utf8[out++] = (byte) (hexDecode(ascii[in]) * 16 | hexDecode(ascii[in + 1]));\r
+                    in += 2;\r
+                } else {\r
+                    utf8[out++] = ascii[in++];\r
+                }\r
+            }\r
+            return new String(utf8, 0, out, "UTF-8");\r
+        } catch (IllegalArgumentException e) {\r
+            throw new IllegalArgumentException("Problem while unescaping string: " + uri, e);\r
+        } catch (java.io.UnsupportedEncodingException e) {\r
+            throw new Error("The JVM is required to support UTF-8 and US-ASCII encodings.");\r
+        } catch (ArrayIndexOutOfBoundsException ee) {\r
+            throw new IllegalArgumentException("Incomplete Hex escape sequence in " + uri);\r
+        }\r
+    }\r
+\r
+    /* Copied from Jena 2.4 com.hp.hpl.jena.util.URIref */\r
+    private static byte hexEncode(int i) {\r
+        if (i < 10)\r
+            return (byte) ('0' + i);\r
+        else\r
+            return (byte)('A' + i - 10);\r
+    }\r
+\r
+    /* Copied from Jena 2.4 com.hp.hpl.jena.util.URIref */\r
+    private static int hexDecode(byte b) {\r
+        switch (b) {\r
+            case (byte)'a': case (byte)'b': case (byte)'c': case (byte)'d': case (byte)'e': case (byte)'f':\r
+                return ((b) & 255) - 'a' + 10;\r
+            case (byte)'A': case (byte)'B': case (byte)'C': case (byte)'D': case (byte)'E': case (byte)'F':\r
+                return b - (byte) 'A' + 10;\r
+            case (byte)'0': case (byte)'1': case (byte)'2': case (byte)'3': case (byte)'4': case (byte)'5': case (byte)'6': case (byte)'7': case (byte)'8': case (byte)'9':\r
+                return b - (byte) '0';\r
+            default:\r
+                throw new IllegalArgumentException("Bad Hex escape character: " + ((b)&255) );\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Some simple tests.\r
+     * @param args\r
+     */\r
+    public static void main(String[] args) {\r
+        String s;\r
+        s = "http://www.vtt.fi%2FSome- %25 Namespace/Jotain";\r
+        System.out.println(String.format("escape+unescape: %s -> %s -> %s", s, escape(s), unescape(escape(s))));\r
+        s = "http://www.vtt.fi%2FPSK";\r
+        System.out.println(String.format("unescape: %s -> %s", s, unescape(s)));\r
+        s = "http://www.vtt.fi%2FSome-Namespace/Jotain / Muuta";\r
+        System.out.println(String.format("escape: %s -> %s", s, escape(s)));\r
+        s = "Jotain / Muuta";\r
+        System.out.println(String.format("escape: %s -> %s", s, escape(s)));\r
+\r
+        System.out.println("escapeURI: " + escapeURI("foo/bar/org%2Fnet"));\r
+        System.out.println("escapeURI('...#...'): " + escapeURI("foo/bar#org%2Fnet"));\r
+        s = makeURI("http://foo.bar.com/foo/bar", "baz/guuk/org%2Fnet");\r
+        System.out.println("escapeURI: " + s);\r
+        System.out.println("getNamespace: " + getNamespace(s));\r
+        System.out.println("getLocalName: " + getLocalName(s));\r
+\r
+        testEscape("/", "%2F");\r
+        testEscape("#", "%23");\r
+        testEscape("%", "%25");\r
+        testEscape("%01", "%2501");\r
+        testEscape("%GG", "%25GG");\r
+    }\r
+\r
+    private static void testEscape(String unescaped, String expectedEscaped) {\r
+        String esc = escape(unescaped);\r
+        String unesc = unescape(esc);\r
+        System.out.format("escape('%s')='%s', unescape('%s')='%s'\n", unescaped, esc, esc, unesc);\r
+        if (!esc.equals(expectedEscaped))\r
+            throw new AssertionError("escape('" + unescaped + "') was expected to return '" + expectedEscaped + "' but returned '" + esc + "'");\r
+        if (!unesc.equals(unescaped))\r
+            throw new AssertionError("unescape(escape('" + unescaped + "'))=unescape(" + esc + ") was expected to return '" + unescaped + "' but returned '" + unesc + "'");\r
+    }\r
+\r
+}\r