001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.ws.scout.registry.infomodel;
018:
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Locale;
024: import java.util.Map;
025:
026: import javax.xml.registry.JAXRException;
027: import javax.xml.registry.infomodel.InternationalString;
028: import javax.xml.registry.infomodel.LocalizedString;
029:
030: /**
031: * For futher details, look into the JAXR API Javadoc.
032: *
033: * @author Anil Saldhana <anil@apache.org>
034: */
035: public class InternationalStringImpl implements InternationalString {
036: /**
037: * Maintains an Hashmap of locale to string value
038: */
039: private final Map<MapKey, LocalizedString> map = new HashMap<MapKey, LocalizedString>();
040:
041: public InternationalStringImpl() {
042: }
043:
044: public InternationalStringImpl(String str) {
045: Locale locale = Locale.getDefault();
046: map
047: .put(new MapKey(locale,
048: LocalizedString.DEFAULT_CHARSET_NAME),
049: new LocalizedStringImpl(locale, str,
050: LocalizedString.DEFAULT_CHARSET_NAME));
051:
052: }
053:
054: public InternationalStringImpl(Locale locale, String str,
055: String charsetName) {
056: MapKey mapKey = new MapKey(locale, charsetName);
057: map.put(mapKey, new LocalizedStringImpl(locale, str,
058: charsetName));
059: }
060:
061: public void addLocalizedString(LocalizedString localizedString)
062: throws JAXRException {
063: MapKey mapKey = new MapKey(localizedString);
064: map.put(mapKey, localizedString);
065: }
066:
067: public void addLocalizedStrings(Collection collection)
068: throws JAXRException {
069: for (Iterator i = collection.iterator(); i.hasNext();) {
070: LocalizedString localizedString = (LocalizedString) i
071: .next();
072: map.put(new MapKey(localizedString), localizedString);
073: }
074: }
075:
076: public Collection<LocalizedString> getLocalizedStrings()
077: throws JAXRException {
078: return Collections.unmodifiableCollection(map.values());
079: }
080:
081: public String getValue() throws JAXRException {
082: return getValue(Locale.getDefault());
083: }
084:
085: public void setValue(String str) throws JAXRException {
086: setValue(Locale.getDefault(), str);
087: }
088:
089: public String getValue(Locale locale) throws JAXRException {
090: LocalizedString localizedString = (LocalizedString) map
091: .get(new MapKey(locale,
092: LocalizedString.DEFAULT_CHARSET_NAME));
093: return localizedString != null ? localizedString.getValue()
094: : null;
095: }
096:
097: public void setValue(Locale locale, String value)
098: throws JAXRException {
099: map
100: .put(new MapKey(locale,
101: LocalizedString.DEFAULT_CHARSET_NAME),
102: new LocalizedStringImpl(locale, value,
103: LocalizedString.DEFAULT_CHARSET_NAME));
104: }
105:
106: public void removeLocalizedString(LocalizedString localizedString)
107: throws JAXRException {
108: map.remove(new MapKey(localizedString));
109: }
110:
111: public void removeLocalizedStrings(Collection collection)
112: throws JAXRException {
113: for (Iterator i = collection.iterator(); i.hasNext();) {
114: removeLocalizedString((LocalizedString) i.next());
115: }
116: }
117:
118: public LocalizedString getLocalizedString(Locale locale,
119: String charset) throws JAXRException {
120: return (LocalizedString) map.get(new MapKey(locale, charset));
121: }
122:
123: private static class MapKey {
124: private final Locale locale;
125: private final String charsetName;
126:
127: public MapKey(Locale locale, String charsetName) {
128: this .locale = locale;
129: this .charsetName = charsetName;
130: }
131:
132: public MapKey(LocalizedString localizedString)
133: throws JAXRException {
134: this .locale = localizedString.getLocale();
135: this .charsetName = localizedString.getCharsetName();
136: }
137:
138: public boolean equals(Object o) {
139: if (this == o)
140: return true;
141: if (!(o instanceof MapKey))
142: return false;
143: final MapKey mapKey = (MapKey) o;
144: if (!charsetName.equals(mapKey.charsetName))
145: return false;
146: if (!locale.equals(mapKey.locale))
147: return false;
148: return true;
149: }
150:
151: public int hashCode() {
152: int result;
153: result = locale.hashCode();
154: result = 29 * result + charsetName.hashCode();
155: return result;
156: }
157:
158: public String toString() {
159: StringBuffer buf = new StringBuffer(32);
160: buf.append('[').append(locale).append(',').append(
161: charsetName).append(']');
162: return buf.toString();
163: }
164: }
165: }
|