]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/uri/URIEscape.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/uri/URIEscape.java
new file mode 100644 (file)
index 0000000..f73d455
--- /dev/null
@@ -0,0 +1,63 @@
+/*******************************************************************************\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.db.common.uri;\r
+\r
+import java.util.regex.Pattern;\r
+\r
+\r
+class URIEscape {\r
+\r
+    private static final String  HTTP       = "http:/";\r
+    private static final String  HTTP_SLASH = "http://";\r
+\r
+    private static final Pattern slash = Pattern.compile("/");\r
+\r
+    /**\r
+     * Escapes the specified name by changing spaces (' ') into underscores\r
+     * ('_').\r
+     * \r
+     * @param name name to escape\r
+     * @return\r
+     * @deprecated do not use, this is not the DB standard to URI escaping.\r
+     */\r
+    @Deprecated\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
+    public static String[] splitURI(String uri) {\r
+        if(!uri.startsWith(HTTP_SLASH))\r
+            throw new IllegalArgumentException("Uri does not begin with '" + HTTP_SLASH + "'. given uri: " + uri);\r
+        return slash.split(uri.substring(7));\r
+    }\r
+\r
+    public static String joinURI(String[] names) {\r
+        StringBuilder b = new StringBuilder();\r
+        b.append(HTTP);\r
+        for(String name : names) {\r
+            b.append('/');\r
+            b.append(name);\r
+        }\r
+        return b.toString();\r
+    }\r
+\r
+}\r