]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/uri/URIEscape.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / uri / URIEscape.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.db.common.uri;\r
13 \r
14 import java.util.regex.Pattern;\r
15 \r
16 \r
17 class URIEscape {\r
18 \r
19     private static final String  HTTP       = "http:/";\r
20     private static final String  HTTP_SLASH = "http://";\r
21 \r
22     private static final Pattern slash = Pattern.compile("/");\r
23 \r
24     /**\r
25      * Escapes the specified name by changing spaces (' ') into underscores\r
26      * ('_').\r
27      * \r
28      * @param name name to escape\r
29      * @return\r
30      * @deprecated do not use, this is not the DB standard to URI escaping.\r
31      */\r
32     @Deprecated\r
33     public static String escapeName(String name) {\r
34         char[] chars = name.toCharArray();\r
35         boolean modified = false;\r
36         for(int i=0;i<chars.length;++i)\r
37             if(!Character.isJavaIdentifierPart(chars[i])) {\r
38                 chars[i] = '_';\r
39                 modified = true;\r
40             }\r
41         if(modified)\r
42             return new String(chars);\r
43         else\r
44             return name;\r
45     }\r
46 \r
47     public static String[] splitURI(String uri) {\r
48         if(!uri.startsWith(HTTP_SLASH))\r
49             throw new IllegalArgumentException("Uri does not begin with '" + HTTP_SLASH + "'. given uri: " + uri);\r
50         return slash.split(uri.substring(7));\r
51     }\r
52 \r
53     public static String joinURI(String[] names) {\r
54         StringBuilder b = new StringBuilder();\r
55         b.append(HTTP);\r
56         for(String name : names) {\r
57             b.append('/');\r
58             b.append(name);\r
59         }\r
60         return b.toString();\r
61     }\r
62 \r
63 }\r