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