01: /**
02: * $Id: WSRPConsumerUtil.java,v 1.4 2004/01/24 00:16:27 sm108881 Exp $
03: * Copyright 2003 Sun Microsystems, Inc. All
04: * rights reserved. Use of this product is subject
05: * to license terms. Federal Acquisitions:
06: * Commercial Software -- Government Users
07: * Subject to Standard License Terms and
08: * Conditions.
09: *
10: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
11: * are trademarks or registered trademarks of Sun Microsystems,
12: * Inc. in the United States and other countries.
13: */package com.sun.portal.wsrp.consumer.common;
14:
15: import com.sun.portal.wsrp.common.stubs.ResourceList;
16: import com.sun.portal.wsrp.common.stubs.Resource;
17: import com.sun.portal.wsrp.common.stubs.ResourceValue;
18:
19: /**
20: * Common Utility class
21: */
22:
23: public class WSRPConsumerUtil {
24:
25: /**
26: * Convert ISO-compatible locale representation into the xml:lang format.
27: *
28: * @param locale ISO-formatted locale representation (i.e. en_US)
29: * @return xml:lang formatted locale representation (i.e. en-US)
30: */
31: static public String getXMLLocale(String locale) {
32: return locale.replace('_', '-');
33: }
34:
35: /**
36: * Lookup resource list to match the given resource name and the locale.
37: *
38: * @param resourceList resource list
39: * @param resourceName name of the resource to be looked up.
40: * @param locale locale in xml:lang format (i.e. en-US)
41: * @return matching String value or null if no match is found.
42: */
43: static public String getResource(ResourceList resourceList,
44: String resourceName, String locale) {
45:
46: //
47: // sanity check
48: //
49: if (resourceList == null || resourceName == null) {
50: return null;
51: }
52:
53: //
54: // TBD: room for optimization
55: //
56: Resource[] resources = resourceList.getResources();
57:
58: //
59: // iterate through resources to find the resource
60: // that matches the given resource name
61: //
62: for (int i = 0; i < resources.length; i++) {
63: Resource resource = resources[i];
64: if (resource.getResourceName().equals(resourceName)) {
65: //
66: // iterate through resource values to find the one
67: // that matches the given locale
68: //
69: ResourceValue[] resourceValues = resource.getValues();
70: for (int j = 0; i < resourceValues.length; i++) {
71: ResourceValue resourceValue = resourceValues[j];
72: if (resourceValue.getLang().equals(locale)) {
73: return resourceValue.getValue();
74: }
75: }
76: }
77: }
78: //
79: // no match found. return null;
80: //
81: return null;
82: }
83: }
|