-/*******************************************************************************\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ö\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.databoard.util;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+
+/**
+ * <a href="http://www.simantics.org/wiki/index.php/URI">Simantics URI and identifier escape specification.
+ *
+ * @author Hannu Niemistö
+ */
+public final class URIUtil {
+
+ static final Charset UTF8 = Charset.forName("UTF-8");
+
+ static final byte[] encodeTable = new byte[128];
+ static final byte[] encodeTable2 = new byte[128]; // for non-bijection filenames
+
+ static {
+ for (int i = 0; i < 128; ++i) {
+ char c = (char) i;
+ if (c == ' ')
+ encodeTable[i] = '_';
+
+ else if (Character.isJavaIdentifierPart(c) && c != '_' && c != '$') {
+ encodeTable[i] = (byte) i;
+ } else
+ encodeTable[i] = -1;
+ }
+
+ for (int i = 0; i < 128; ++i) {
+ char c = (char) i;
+ if (c == ' ' || c == '_' || c == '(' || c== ')')
+ encodeTable2[i] = (byte) i;
+ else if (c == '/')
+ encodeTable2[i] = (byte) '-';
+ else if (c == ' ')
+ encodeTable2[i] = (byte) '_';
+ else if (c == '-' || c == '.')
+ encodeTable2[i] = (byte) i;
+ else if (Character.isJavaIdentifierPart(c) && c != '_' && c != '$') {
+ encodeTable2[i] = (byte) i;
+ } else
+ encodeTable2[i] = -1;
+ }
+
+ }
+
+ public static byte[] encode(String str, byte escapeChar, boolean identifier) throws UnsupportedEncodingException {
+ byte[] bytes = str.getBytes(UTF8);
+
+ boolean prefixWithUnderscore = identifier && bytes.length > 0 && (bytes[0] == '_' || Character.isDigit(bytes[0]));
+
+ // First calculate the length
+ int length = bytes.length;
+ for (byte b : bytes) {
+ if (b < 0 || encodeTable[b] == -1)
+ length += 2;
+ }
+ if (prefixWithUnderscore)
+ length += 1;
+
+ // Then encode
+ if (length == bytes.length) {
+ for (int i = 0; i < length; ++i)
+ bytes[i] = encodeTable[bytes[i]];
+ return bytes;
+ } else {
+ byte[] result = new byte[length];
+ int pos = 0;
+ if (prefixWithUnderscore) {
+ result[pos++] = '_';
+ }
+ for (byte b : bytes) {
+ int ib = (int) b;
+ if (ib >= 0) {
+ byte eb = encodeTable[ib];
+ if (eb >= 0) {
+ result[pos++] = eb;
+ continue;
+ }
+ } else
+ ib += 256;
+
+ result[pos++] = escapeChar;
+ result[pos++] = (byte) Character.forDigit(ib >> 4, 16);
+ result[pos++] = (byte) Character.forDigit(ib & 15, 16);
+ }
+ return result;
+ }
+ }
+
+ public static byte[] encodeFilename(String str, byte escapeChar, boolean identifier) throws UnsupportedEncodingException {
+ byte[] bytes = str.getBytes(UTF8);
+
+ boolean prefixWithUnderscore = identifier && bytes.length > 0 && (bytes[0] == '_' || Character.isDigit(bytes[0]));
+
+ // First calculate the length
+ int length = bytes.length;
+ for (byte b : bytes) {
+ if (b < 0 || encodeTable2[b] == -1)
+ length += 2;
+ }
+ if (prefixWithUnderscore)
+ length += 1;
+
+ // Then encode
+ if (length == bytes.length) {
+ for (int i = 0; i < length; ++i)
+ bytes[i] = encodeTable2[bytes[i]];
+ return bytes;
+ } else {
+ byte[] result = new byte[length];
+ int pos = 0;
+ if (prefixWithUnderscore) {
+ result[pos++] = '_';
+ }
+ for (byte b : bytes) {
+ int ib = (int) b;
+ if (ib >= 0) {
+ byte eb = encodeTable2[ib];
+ if (eb >= 0) {
+ result[pos++] = eb;
+ continue;
+ }
+ } else
+ ib += 256;
+
+ result[pos++] = escapeChar;
+ result[pos++] = (byte) Character.forDigit(ib >> 4, 16);
+ result[pos++] = (byte) Character.forDigit(ib & 15, 16);
+ }
+ return result;
+ }
+ }
+
+ public static String encodeFilename(String str) {
+ try {
+ byte[] result = encodeFilename(str, (byte) '%', false);
+ return new String(result, 0, result.length);
+ } catch (UnsupportedEncodingException e) {
+ // Should never happen when using UTF-8
+ throw new Error(e);
+ }
+
+ }
+
+ public static String encodeURI(String str) {
+ try {
+ byte[] result = encode(str, (byte) '%', false);
+ return new String(result, 0, result.length);
+ } catch (UnsupportedEncodingException e) {
+ // Should never happen when using UTF-8
+ throw new Error(e);
+ }
+
+ }
+
+ public static String encodeIdentifier(String str) {
+ try {
+ byte[] result = encode(str, (byte) '$', true);
+ return new String(result, 0, result.length);
+ } catch (UnsupportedEncodingException e) {
+ // Should never happen when using UTF-8
+ throw new Error(e);
+ }
+
+ }
+
+ public static String decode(byte[] bytes, byte escapeChar, boolean identifier) {
+ int length = 0;
+ int startPos = 0;
+ {
+ int i = 0;
+ // Skip '_' prefix if necessary
+ if (identifier && bytes.length > 0 && bytes[0] == '_') {
+ startPos = 1;
+ i = 1;
+ }
+ for (; i < bytes.length; ++i) {
+ byte b = bytes[i];
+ if (b == escapeChar)
+ i += 2;
+ ++length;
+ }
+ }
+ int pos = 0;
+ byte[] result = new byte[length];
+ for (int i = startPos; i < bytes.length; ++i) {
+ byte b = bytes[i];
+ if (b == escapeChar) {
+ int c = Character.digit((char) bytes[++i], 16);
+ c *= 16;
+ c += Character.digit((char) bytes[++i], 16);
+ result[pos] = (byte) c;
+ } else {
+ if (b == '_')
+ result[pos] = ' ';
+ else
+ result[pos] = b;
+ }
+ ++pos;
+ }
+ return new String(result, UTF8);
+ }
+
+ public static String decodeURI(String str) {
+ return decode(str.getBytes(), (byte) '%', false);
+ }
+
+ public static String decodeIdentifier(String str) {
+ return decode(str.getBytes(), (byte) '$', true);
+ }
+
+}