]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/uri/URIEscape.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / uri / URIEscape.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.uri;
13
14 import java.util.regex.Pattern;
15
16
17 class URIEscape {
18
19     private static final String  HTTP       = "http:/";
20     private static final String  HTTP_SLASH = "http://";
21
22     private static final Pattern slash = Pattern.compile("/");
23
24     /**
25      * Escapes the specified name by changing spaces (' ') into underscores
26      * ('_').
27      * 
28      * @param name name to escape
29      * @return
30      * @deprecated do not use, this is not the DB standard to URI escaping.
31      */
32     @Deprecated
33     public static String escapeName(String name) {
34         char[] chars = name.toCharArray();
35         boolean modified = false;
36         for(int i=0;i<chars.length;++i)
37             if(!Character.isJavaIdentifierPart(chars[i])) {
38                 chars[i] = '_';
39                 modified = true;
40             }
41         if(modified)
42             return new String(chars);
43         else
44             return name;
45     }
46
47     public static String[] splitURI(String uri) {
48         if(!uri.startsWith(HTTP_SLASH))
49             throw new IllegalArgumentException("Uri does not begin with '" + HTTP_SLASH + "'. given uri: " + uri);
50         return slash.split(uri.substring(7));
51     }
52
53     public static String joinURI(String[] names) {
54         StringBuilder b = new StringBuilder();
55         b.append(HTTP);
56         for(String name : names) {
57             b.append('/');
58             b.append(name);
59         }
60         return b.toString();
61     }
62
63 }