]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ViewArgumentUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ViewArgumentUtils.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ViewArgumentUtils.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ViewArgumentUtils.java
new file mode 100644 (file)
index 0000000..1e7baee
--- /dev/null
@@ -0,0 +1,80 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 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.browsing.ui.swt;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.eclipse.ui.IViewPart;\r
+import org.simantics.browsing.ui.common.views.IViewArguments;\r
+\r
+/**\r
+ * Utilities for parsing what is specified in {@link IViewArguments}.\r
+ */\r
+public class ViewArgumentUtils {\r
+\r
+    private static final Pattern argumentPattern = Pattern.compile("([^:&=]*)=([^:&=]*)");\r
+\r
+    public static Map<String, String> parseViewArguments(IViewPart part) {\r
+        String secondaryId = part.getViewSite().getSecondaryId();\r
+        Map<String, String> result = ViewArgumentUtils.decodeArguments(secondaryId);\r
+        return result;\r
+    }    \r
+\r
+    public static Map<String, String> decodeArguments(String secondaryId) {\r
+        Map<String, String> result = new HashMap<String, String>();\r
+        if (secondaryId == null)\r
+            return result;\r
+\r
+        String[] args = secondaryId.split("&");\r
+        for (String arg : args) {\r
+            Matcher m = argumentPattern.matcher(arg);\r
+            if (m.matches()) {\r
+                String key = m.group(1);\r
+                String value = m.group(2);\r
+\r
+                key = unescape(key);\r
+                value = unescape(value);\r
+\r
+                result.put(key, value);\r
+            }\r
+        }\r
+        return result;\r
+    }\r
+\r
+    public static String encodeArguments(Map<String, String> args) {\r
+        StringBuilder sb = new StringBuilder();\r
+        boolean notFirst = false;\r
+        for (Map.Entry<String, String> e : args.entrySet()) {\r
+            if (notFirst)\r
+                sb.append("&");\r
+            notFirst = true;\r
+            String key = e.getKey();\r
+            String value = e.getValue();\r
+            sb.append(escape(key));\r
+            sb.append("=");\r
+            sb.append(escape(value));\r
+        }\r
+        return sb.toString();\r
+    }\r
+\r
+    private static String escape(String s) {\r
+        return s.replace("&", "%26").replace(":", "%3A").replace("=", "%3D");\r
+    }\r
+\r
+    private static String unescape(String s) {\r
+        return s.replace("%26", "&").replace("%3A", ":").replace("%3D", "=");\r
+    }\r
+\r
+}\r