1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 import java.util.HashMap;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
19 import org.eclipse.ui.IViewPart;
20 import org.simantics.browsing.ui.common.views.IViewArguments;
23 * Utilities for parsing what is specified in {@link IViewArguments}.
25 public class ViewArgumentUtils {
27 private static final Pattern argumentPattern = Pattern.compile("([^:&=]*)=([^:&=]*)");
29 public static Map<String, String> parseViewArguments(IViewPart part) {
30 String secondaryId = part.getViewSite().getSecondaryId();
31 Map<String, String> result = ViewArgumentUtils.decodeArguments(secondaryId);
35 public static Map<String, String> decodeArguments(String secondaryId) {
36 Map<String, String> result = new HashMap<String, String>();
37 if (secondaryId == null)
40 String[] args = secondaryId.split("&");
41 for (String arg : args) {
42 Matcher m = argumentPattern.matcher(arg);
44 String key = m.group(1);
45 String value = m.group(2);
48 value = unescape(value);
50 result.put(key, value);
56 public static String encodeArguments(Map<String, String> args) {
57 StringBuilder sb = new StringBuilder();
58 boolean notFirst = false;
59 for (Map.Entry<String, String> e : args.entrySet()) {
63 String key = e.getKey();
64 String value = e.getValue();
65 sb.append(escape(key));
67 sb.append(escape(value));
72 private static String escape(String s) {
73 return s.replace("&", "%26").replace(":", "%3A").replace("=", "%3D");
76 private static String unescape(String s) {
77 return s.replace("%26", "&").replace("%3A", ":").replace("%3D", "=");