01: /*
02: * Copyright 2002 Sun Microsystems, Inc. All
03: * rights reserved. Use of this product is subject
04: * to license terms. Federal Acquisitions:
05: * Commercial Software -- Government Users
06: * Subject to Standard License Terms and
07: * Conditions.
08: *
09: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
10: * are trademarks or registered trademarks of Sun Microsystems,
11: * Inc. in the United States and other countries.
12: */
13: package com.sun.portal.wsrp.common;
14:
15: import java.util.ArrayList;
16: import java.util.List;
17: import java.util.Iterator;
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import com.sun.portal.wsrp.common.stubs.*;
22:
23: public class WSRPUtility {
24:
25: static private String[] EMPTY_STRING_ARRAY = new String[0];
26:
27: //*****************************************************************
28: //
29: // NamedStrings conversion
30: //
31: //***************************************************************
32:
33: /**
34: * Convert java Maps to NamedString array used by WSRP.
35: */
36: public static NamedString[] convertToNamedStrings(Map params,
37: List excludes) {
38: ArrayList namedStringList = new ArrayList();
39: Iterator keys = params.keySet().iterator();
40: while (keys.hasNext()) {
41: String key = (String) keys.next();
42: if (excludes != null && !excludes.contains(key)) {
43: String[] value = (String[]) params.get(key);
44: for (int i = 0; i < value.length; i++) {
45: NamedString namedString = new NamedString(key,
46: value[i]);
47: namedStringList.add(namedString);
48: }
49: }
50: }
51: NamedString[] namedStrings = new NamedString[namedStringList
52: .size()];
53: namedStringList.toArray(namedStrings);
54: return namedStrings;
55: }
56:
57: /**
58: * Convert NamedString arrays used by WSRP to java Maps.
59: */
60: public static Map convertFromNamedString(NamedString[] strings) {
61:
62: Map map1 = null;
63: Map map2 = null;
64:
65: if (strings != null) {
66: map1 = new HashMap();
67:
68: // First round, just construct the map with array list as the values
69: for (int i = 0; i < strings.length; i++) {
70: String name = strings[i].getName();
71: List list = null;
72: if (map1.containsKey(name)) {
73: list = (List) map1.get(name);
74: } else {
75: list = new ArrayList();
76: }
77: list.add(strings[i].getValue());
78: map1.put(name, list);
79: }
80:
81: // Second round, convert the array lists back into string arrays
82: Iterator keys = map1.keySet().iterator();
83: map2 = new HashMap();
84: while (keys.hasNext()) {
85: String name = (String) keys.next();
86: List values = (List) map1.get(name);
87: map2.put(name, values.toArray(EMPTY_STRING_ARRAY));
88: }
89: }
90: return map2;
91: }
92: }
|