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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\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
19 import org.eclipse.ui.IViewPart;
\r
20 import org.simantics.browsing.ui.common.views.IViewArguments;
\r
23 * Utilities for parsing what is specified in {@link IViewArguments}.
\r
25 public class ViewArgumentUtils {
\r
27 private static final Pattern argumentPattern = Pattern.compile("([^:&=]*)=([^:&=]*)");
\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
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
40 String[] args = secondaryId.split("&");
\r
41 for (String arg : args) {
\r
42 Matcher m = argumentPattern.matcher(arg);
\r
44 String key = m.group(1);
\r
45 String value = m.group(2);
\r
47 key = unescape(key);
\r
48 value = unescape(value);
\r
50 result.put(key, value);
\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
63 String key = e.getKey();
\r
64 String value = e.getValue();
\r
65 sb.append(escape(key));
\r
67 sb.append(escape(value));
\r
69 return sb.toString();
\r
72 private static String escape(String s) {
\r
73 return s.replace("&", "%26").replace(":", "%3A").replace("=", "%3D");
\r
76 private static String unescape(String s) {
\r
77 return s.replace("%26", "&").replace("%3A", ":").replace("%3D", "=");
\r