]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ViewArgumentUtils.java
Merge remote-tracking branch 'origin/svn' commit 'ccc1271c9d6657fb9dcf4cf3cb115fa0c8c...
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ViewArgumentUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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.browsing.ui.swt;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 import java.util.regex.Matcher;\r
17 import java.util.regex.Pattern;\r
18 \r
19 import org.eclipse.ui.IViewPart;\r
20 import org.simantics.browsing.ui.common.views.IViewArguments;\r
21 \r
22 /**\r
23  * Utilities for parsing what is specified in {@link IViewArguments}.\r
24  */\r
25 public class ViewArgumentUtils {\r
26 \r
27     private static final Pattern argumentPattern = Pattern.compile("([^:&=]*)=([^:&=]*)");\r
28 \r
29     public static Map<String, String> parseViewArguments(IViewPart part) {\r
30         String secondaryId = part.getViewSite().getSecondaryId();\r
31         Map<String, String> result = ViewArgumentUtils.decodeArguments(secondaryId);\r
32         return result;\r
33     }    \r
34 \r
35     public static Map<String, String> decodeArguments(String secondaryId) {\r
36         Map<String, String> result = new HashMap<String, String>();\r
37         if (secondaryId == null)\r
38             return result;\r
39 \r
40         String[] args = secondaryId.split("&");\r
41         for (String arg : args) {\r
42             Matcher m = argumentPattern.matcher(arg);\r
43             if (m.matches()) {\r
44                 String key = m.group(1);\r
45                 String value = m.group(2);\r
46 \r
47                 key = unescape(key);\r
48                 value = unescape(value);\r
49 \r
50                 result.put(key, value);\r
51             }\r
52         }\r
53         return result;\r
54     }\r
55 \r
56     public static String encodeArguments(Map<String, String> args) {\r
57         StringBuilder sb = new StringBuilder();\r
58         boolean notFirst = false;\r
59         for (Map.Entry<String, String> e : args.entrySet()) {\r
60             if (notFirst)\r
61                 sb.append("&");\r
62             notFirst = true;\r
63             String key = e.getKey();\r
64             String value = e.getValue();\r
65             sb.append(escape(key));\r
66             sb.append("=");\r
67             sb.append(escape(value));\r
68         }\r
69         return sb.toString();\r
70     }\r
71 \r
72     private static String escape(String s) {\r
73         return s.replace("&", "%26").replace(":", "%3A").replace("=", "%3D");\r
74     }\r
75 \r
76     private static String unescape(String s) {\r
77         return s.replace("%26", "&").replace("%3A", ":").replace("%3D", "=");\r
78     }\r
79 \r
80 }\r