]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/URIUtil.java
Re-implement URIStringUtils escape and unescape using Unicode
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / URIUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.util;\r
13 \r
14 import java.io.UnsupportedEncodingException;\r
15 import java.nio.charset.Charset;\r
16 \r
17 /**\r
18  * <a href="http://www.simantics.org/wiki/index.php/URI">Simantics URI and identifier escape specification.\r
19  * \r
20  * @author Hannu Niemist&ouml;\r
21  */\r
22 public final class URIUtil {\r
23 \r
24     static final Charset UTF8        = Charset.forName("UTF-8");\r
25 \r
26     static final byte[]  encodeTable = new byte[128];\r
27     static final byte[]  encodeTable2 = new byte[128]; // for non-bijection filenames\r
28 \r
29     static {\r
30         for (int i = 0; i < 128; ++i) {\r
31             char c = (char) i;\r
32             if (c == ' ')\r
33                 encodeTable[i] = '_';\r
34             \r
35             else if (Character.isJavaIdentifierPart(c) && c != '_' && c != '$') {\r
36                 encodeTable[i] = (byte) i;\r
37             } else\r
38                 encodeTable[i] = -1;\r
39         }\r
40         \r
41         for (int i = 0; i < 128; ++i) {\r
42             char c = (char) i;\r
43             if (c == ' ' || c == '_' || c == '(' || c== ')')\r
44                 encodeTable2[i] = (byte) i;\r
45             else if (c == '/')\r
46                 encodeTable2[i] = (byte) '-';\r
47             else if (c == ' ')\r
48                 encodeTable2[i] = (byte) '_';\r
49             else if (c == '-' || c == '.')\r
50                 encodeTable2[i] = (byte) i;\r
51             else if (Character.isJavaIdentifierPart(c) && c != '_' && c != '$') {\r
52                 encodeTable2[i] = (byte) i;\r
53             } else\r
54                 encodeTable2[i] = -1;\r
55         }\r
56         \r
57     }\r
58 \r
59     public static byte[] encode(String str, byte escapeChar, boolean identifier) throws UnsupportedEncodingException {\r
60         byte[] bytes = str.getBytes(UTF8);\r
61 \r
62         boolean prefixWithUnderscore = identifier && bytes.length > 0 && (bytes[0] == '_' || Character.isDigit(bytes[0]));\r
63 \r
64         // First calculate the length\r
65         int length = bytes.length;\r
66         for (byte b : bytes) {\r
67             if (b < 0 || encodeTable[b] == -1)\r
68                 length += 2;\r
69         }\r
70         if (prefixWithUnderscore)\r
71             length += 1;\r
72 \r
73         // Then encode\r
74         if (length == bytes.length) {\r
75             for (int i = 0; i < length; ++i)\r
76                 bytes[i] = encodeTable[bytes[i]];\r
77             return bytes;\r
78         } else {\r
79             byte[] result = new byte[length];\r
80             int pos = 0;\r
81             if (prefixWithUnderscore) {\r
82                 result[pos++] = '_';\r
83             }\r
84             for (byte b : bytes) {\r
85                 int ib = (int) b;\r
86                 if (ib >= 0) {\r
87                     byte eb = encodeTable[ib];\r
88                     if (eb >= 0) {\r
89                         result[pos++] = eb;\r
90                         continue;\r
91                     }\r
92                 } else\r
93                     ib += 256;\r
94 \r
95                 result[pos++] = escapeChar;\r
96                 result[pos++] = (byte) Character.forDigit(ib >> 4, 16);\r
97                 result[pos++] = (byte) Character.forDigit(ib & 15, 16);\r
98             }\r
99             return result;\r
100         }\r
101     }\r
102     \r
103     public static byte[] encodeFilename(String str, byte escapeChar, boolean identifier) throws UnsupportedEncodingException {\r
104         byte[] bytes = str.getBytes(UTF8);\r
105 \r
106         boolean prefixWithUnderscore = identifier && bytes.length > 0 && (bytes[0] == '_' || Character.isDigit(bytes[0]));\r
107 \r
108         // First calculate the length\r
109         int length = bytes.length;\r
110         for (byte b : bytes) {\r
111             if (b < 0 || encodeTable2[b] == -1)\r
112                 length += 2;\r
113         }\r
114         if (prefixWithUnderscore)\r
115             length += 1;\r
116 \r
117         // Then encode\r
118         if (length == bytes.length) {\r
119             for (int i = 0; i < length; ++i)\r
120                 bytes[i] = encodeTable2[bytes[i]];\r
121             return bytes;\r
122         } else {\r
123             byte[] result = new byte[length];\r
124             int pos = 0;\r
125             if (prefixWithUnderscore) {\r
126                 result[pos++] = '_';\r
127             }\r
128             for (byte b : bytes) {\r
129                 int ib = (int) b;\r
130                 if (ib >= 0) {\r
131                     byte eb = encodeTable2[ib];\r
132                     if (eb >= 0) {\r
133                         result[pos++] = eb;\r
134                         continue;\r
135                     }\r
136                 } else\r
137                     ib += 256;\r
138 \r
139                 result[pos++] = escapeChar;\r
140                 result[pos++] = (byte) Character.forDigit(ib >> 4, 16);\r
141                 result[pos++] = (byte) Character.forDigit(ib & 15, 16);\r
142             }\r
143             return result;\r
144         }\r
145     }\r
146     \r
147     public static String encodeFilename(String str) {\r
148         try {\r
149             byte[] result = encodeFilename(str, (byte) '%', false);\r
150             return new String(result, 0, result.length);\r
151         } catch (UnsupportedEncodingException e) {\r
152             // Should never happen when using UTF-8\r
153             throw new Error(e);\r
154         }\r
155 \r
156     }\r
157 \r
158     public static String encodeURI(String str) {\r
159         try {\r
160             byte[] result = encode(str, (byte) '%', false);\r
161             return new String(result, 0, result.length);\r
162         } catch (UnsupportedEncodingException e) {\r
163             // Should never happen when using UTF-8\r
164             throw new Error(e);\r
165         }\r
166 \r
167     }\r
168 \r
169     public static String encodeIdentifier(String str) {\r
170         try {\r
171             byte[] result = encode(str, (byte) '$', true);\r
172             return new String(result, 0, result.length);\r
173         } catch (UnsupportedEncodingException e) {\r
174             // Should never happen when using UTF-8\r
175             throw new Error(e);\r
176         }\r
177 \r
178     }\r
179 \r
180     public static String decode(byte[] bytes, byte escapeChar, boolean identifier) {\r
181         int length = 0;\r
182         int startPos = 0;\r
183         {\r
184             int i = 0;\r
185             // Skip '_' prefix if necessary\r
186             if (identifier && bytes.length > 0 && bytes[0] == '_') {\r
187                 startPos = 1;\r
188                 i = 1;\r
189             }\r
190             for (; i < bytes.length; ++i) {\r
191                 byte b = bytes[i];\r
192                 if (b == escapeChar)\r
193                     i += 2;\r
194                 ++length;\r
195             }\r
196         }\r
197         int pos = 0;\r
198         byte[] result = new byte[length];\r
199         for (int i = startPos; i < bytes.length; ++i) {\r
200             byte b = bytes[i];\r
201             if (b == escapeChar) {\r
202                 int c = Character.digit((char) bytes[++i], 16);\r
203                 c *= 16;\r
204                 c += Character.digit((char) bytes[++i], 16);\r
205                 result[pos] = (byte) c;\r
206             } else {\r
207                 if (b == '_')\r
208                     result[pos] = ' ';\r
209                 else\r
210                     result[pos] = b;\r
211             }\r
212             ++pos;\r
213         }\r
214         return new String(result, UTF8);\r
215     }\r
216 \r
217     public static String decodeURI(String str) {\r
218         return decode(str.getBytes(), (byte) '%', false);\r
219     }\r
220 \r
221     public static String decodeIdentifier(String str) {\r
222         return decode(str.getBytes(), (byte) '$', true);\r
223     }\r
224     \r
225 }\r