]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/util/URIUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / URIUtil.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/URIUtil.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/URIUtil.java
new file mode 100644 (file)
index 0000000..b67e297
--- /dev/null
@@ -0,0 +1,225 @@
+/*******************************************************************************\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
+package org.simantics.databoard.util;\r
+\r
+import java.io.UnsupportedEncodingException;\r
+import java.nio.charset.Charset;\r
+\r
+/**\r
+ * <a href="http://www.simantics.org/wiki/index.php/URI">Simantics URI and identifier escape specification.\r
+ * \r
+ * @author Hannu Niemist&ouml;\r
+ */\r
+public final class URIUtil {\r
+\r
+    static final Charset UTF8        = Charset.forName("UTF-8");\r
+\r
+    static final byte[]  encodeTable = new byte[128];\r
+    static final byte[]  encodeTable2 = new byte[128]; // for non-bijection filenames\r
+\r
+    static {\r
+        for (int i = 0; i < 128; ++i) {\r
+            char c = (char) i;\r
+            if (c == ' ')\r
+                encodeTable[i] = '_';\r
+            \r
+            else if (Character.isJavaIdentifierPart(c) && c != '_' && c != '$') {\r
+                encodeTable[i] = (byte) i;\r
+            } else\r
+                encodeTable[i] = -1;\r
+        }\r
+        \r
+        for (int i = 0; i < 128; ++i) {\r
+            char c = (char) i;\r
+            if (c == ' ' || c == '_' || c == '(' || c== ')')\r
+                encodeTable2[i] = (byte) i;\r
+            else if (c == '/')\r
+                encodeTable2[i] = (byte) '-';\r
+            else if (c == ' ')\r
+                encodeTable2[i] = (byte) '_';\r
+            else if (c == '-' || c == '.')\r
+                encodeTable2[i] = (byte) i;\r
+            else if (Character.isJavaIdentifierPart(c) && c != '_' && c != '$') {\r
+                encodeTable2[i] = (byte) i;\r
+            } else\r
+                encodeTable2[i] = -1;\r
+        }\r
+        \r
+    }\r
+\r
+    public static byte[] encode(String str, byte escapeChar, boolean identifier) throws UnsupportedEncodingException {\r
+        byte[] bytes = str.getBytes(UTF8);\r
+\r
+        boolean prefixWithUnderscore = identifier && bytes.length > 0 && (bytes[0] == '_' || Character.isDigit(bytes[0]));\r
+\r
+        // First calculate the length\r
+        int length = bytes.length;\r
+        for (byte b : bytes) {\r
+            if (b < 0 || encodeTable[b] == -1)\r
+                length += 2;\r
+        }\r
+        if (prefixWithUnderscore)\r
+            length += 1;\r
+\r
+        // Then encode\r
+        if (length == bytes.length) {\r
+            for (int i = 0; i < length; ++i)\r
+                bytes[i] = encodeTable[bytes[i]];\r
+            return bytes;\r
+        } else {\r
+            byte[] result = new byte[length];\r
+            int pos = 0;\r
+            if (prefixWithUnderscore) {\r
+                result[pos++] = '_';\r
+            }\r
+            for (byte b : bytes) {\r
+                int ib = (int) b;\r
+                if (ib >= 0) {\r
+                    byte eb = encodeTable[ib];\r
+                    if (eb >= 0) {\r
+                        result[pos++] = eb;\r
+                        continue;\r
+                    }\r
+                } else\r
+                    ib += 256;\r
+\r
+                result[pos++] = escapeChar;\r
+                result[pos++] = (byte) Character.forDigit(ib >> 4, 16);\r
+                result[pos++] = (byte) Character.forDigit(ib & 15, 16);\r
+            }\r
+            return result;\r
+        }\r
+    }\r
+    \r
+    public static byte[] encodeFilename(String str, byte escapeChar, boolean identifier) throws UnsupportedEncodingException {\r
+        byte[] bytes = str.getBytes(UTF8);\r
+\r
+        boolean prefixWithUnderscore = identifier && bytes.length > 0 && (bytes[0] == '_' || Character.isDigit(bytes[0]));\r
+\r
+        // First calculate the length\r
+        int length = bytes.length;\r
+        for (byte b : bytes) {\r
+            if (b < 0 || encodeTable2[b] == -1)\r
+                length += 2;\r
+        }\r
+        if (prefixWithUnderscore)\r
+            length += 1;\r
+\r
+        // Then encode\r
+        if (length == bytes.length) {\r
+            for (int i = 0; i < length; ++i)\r
+                bytes[i] = encodeTable2[bytes[i]];\r
+            return bytes;\r
+        } else {\r
+            byte[] result = new byte[length];\r
+            int pos = 0;\r
+            if (prefixWithUnderscore) {\r
+                result[pos++] = '_';\r
+            }\r
+            for (byte b : bytes) {\r
+                int ib = (int) b;\r
+                if (ib >= 0) {\r
+                    byte eb = encodeTable2[ib];\r
+                    if (eb >= 0) {\r
+                        result[pos++] = eb;\r
+                        continue;\r
+                    }\r
+                } else\r
+                    ib += 256;\r
+\r
+                result[pos++] = escapeChar;\r
+                result[pos++] = (byte) Character.forDigit(ib >> 4, 16);\r
+                result[pos++] = (byte) Character.forDigit(ib & 15, 16);\r
+            }\r
+            return result;\r
+        }\r
+    }\r
+    \r
+    public static String encodeFilename(String str) {\r
+        try {\r
+            byte[] result = encodeFilename(str, (byte) '%', false);\r
+            return new String(result, 0, result.length);\r
+        } catch (UnsupportedEncodingException e) {\r
+            // Should never happen when using UTF-8\r
+            throw new Error(e);\r
+        }\r
+\r
+    }\r
+\r
+    public static String encodeURI(String str) {\r
+        try {\r
+            byte[] result = encode(str, (byte) '%', false);\r
+            return new String(result, 0, result.length);\r
+        } catch (UnsupportedEncodingException e) {\r
+            // Should never happen when using UTF-8\r
+            throw new Error(e);\r
+        }\r
+\r
+    }\r
+\r
+    public static String encodeIdentifier(String str) {\r
+        try {\r
+            byte[] result = encode(str, (byte) '$', true);\r
+            return new String(result, 0, result.length);\r
+        } catch (UnsupportedEncodingException e) {\r
+            // Should never happen when using UTF-8\r
+            throw new Error(e);\r
+        }\r
+\r
+    }\r
+\r
+    public static String decode(byte[] bytes, byte escapeChar, boolean identifier) {\r
+        int length = 0;\r
+        int startPos = 0;\r
+        {\r
+            int i = 0;\r
+            // Skip '_' prefix if necessary\r
+            if (identifier && bytes.length > 0 && bytes[0] == '_') {\r
+                startPos = 1;\r
+                i = 1;\r
+            }\r
+            for (; i < bytes.length; ++i) {\r
+                byte b = bytes[i];\r
+                if (b == escapeChar)\r
+                    i += 2;\r
+                ++length;\r
+            }\r
+        }\r
+        int pos = 0;\r
+        byte[] result = new byte[length];\r
+        for (int i = startPos; i < bytes.length; ++i) {\r
+            byte b = bytes[i];\r
+            if (b == escapeChar) {\r
+                int c = Character.digit((char) bytes[++i], 16);\r
+                c *= 16;\r
+                c += Character.digit((char) bytes[++i], 16);\r
+                result[pos] = (byte) c;\r
+            } else {\r
+                if (b == '_')\r
+                    result[pos] = ' ';\r
+                else\r
+                    result[pos] = b;\r
+            }\r
+            ++pos;\r
+        }\r
+        return new String(result, UTF8);\r
+    }\r
+\r
+    public static String decodeURI(String str) {\r
+        return decode(str.getBytes(), (byte) '%', false);\r
+    }\r
+\r
+    public static String decodeIdentifier(String str) {\r
+        return decode(str.getBytes(), (byte) '$', true);\r
+    }\r
+    \r
+}\r