001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.providers.window.util;
014:
015: import java.net.URLEncoder;
016: import java.net.URLDecoder;
017:
018: import java.util.List;
019: import java.util.ArrayList;
020: import java.util.Enumeration;
021: import java.util.Map;
022: import java.util.Set;
023: import java.util.HashMap;
024: import java.util.Hashtable;
025: import java.util.Iterator;
026: import java.util.Collections;
027: import java.util.StringTokenizer;
028:
029: import com.sun.portal.desktop.util.Integers;
030:
031: import com.sun.portal.providers.context.ProviderContext;
032: import com.sun.portal.providers.context.ProviderContextException;
033:
034: import com.sun.portal.providers.ProviderException;
035:
036: public class Util {
037:
038: private static final String PARAM_DELIMITER = "|";
039: private static final String KEY_DELIMITER = "=";
040: private static final String ARRAY_DELIMITER = ";";
041:
042: /**
043: * Convert a Map to String representation.
044: */
045: public static String getStringFromMap(Map paramMap) {
046: String paramString = "";
047: StringBuffer sb = new StringBuffer();
048: for (Iterator i = paramMap.keySet().iterator(); i.hasNext();) {
049: String key = (String) i.next();
050: sb.append(key).append(KEY_DELIMITER);
051: try {
052: String[] value = (String[]) paramMap.get(key);
053: for (int j = 0; j < value.length; j++) {
054: sb.append(URLEncoder.encode(value[j]));
055: if ((j + 1) < value.length) {
056: sb.append(ARRAY_DELIMITER);
057: }
058: }
059: } catch (ClassCastException cce) {
060: //getProviderContext().debugWarning("Util.getStringFromMap(): RenderParam value should be a String array", cce);
061: }
062: if (i.hasNext()) {
063: sb.append(PARAM_DELIMITER);
064: }
065: }
066: paramString = sb.toString();
067:
068: return paramString;
069: }
070:
071: /**
072: * Convert a String to Map representation
073: */
074: public static Map getMapFromString(String paramString) {
075: Map paramMap = new HashMap();
076: StringTokenizer paramtokenizer = new StringTokenizer(
077: paramString, PARAM_DELIMITER);
078: while (paramtokenizer.hasMoreTokens()) {
079: String keyValue = paramtokenizer.nextToken();
080: StringTokenizer keyTokenizer = new StringTokenizer(
081: keyValue, KEY_DELIMITER);
082: if (keyTokenizer.countTokens() == 2) {
083: String key = keyTokenizer.nextToken();
084: String value = keyTokenizer.nextToken();
085: //if (value.indexOf(ARRAY_DELIMITER) != -1) {
086: StringTokenizer valueTokenizer = new StringTokenizer(
087: value, ARRAY_DELIMITER);
088: int noTokens = valueTokenizer.countTokens();
089: String[] valueArray = new String[noTokens];
090: for (int i = 0; i < noTokens; i++) {
091: valueArray[i] = URLDecoder.decode(valueTokenizer
092: .nextToken());
093: }
094: paramMap.put(key, valueArray);
095: //} else {
096: // paramMap.put(key, value);
097: //}
098:
099: }
100:
101: }
102: return paramMap;
103: }
104: }
|