001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.settings;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018    import com.liferay.portal.kernel.util.StringUtil;
019    
020    import java.io.Serializable;
021    
022    import java.util.Collection;
023    import java.util.HashMap;
024    import java.util.Locale;
025    import java.util.Map;
026    import java.util.Set;
027    
028    import javax.xml.stream.XMLOutputFactory;
029    import javax.xml.stream.XMLStreamException;
030    import javax.xml.stream.XMLStreamWriter;
031    
032    /**
033     * @author Iv??n Zaera
034     */
035    public class LocalizedValuesMap implements Map<Locale, String>, Serializable {
036    
037            public LocalizedValuesMap(
038                    String key, Locale defaultLocale, Locale... availableLocales) {
039    
040                    _key = key;
041                    _defaultLocale = defaultLocale;
042                    _availableLocales = availableLocales;
043            }
044    
045            @Override
046            public void clear() {
047                    _map.clear();
048            }
049    
050            @Override
051            public boolean containsKey(Object key) {
052                    return _map.containsKey(key);
053            }
054    
055            @Override
056            public boolean containsValue(Object value) {
057                    return _map.containsValue(value);
058            }
059    
060            @Override
061            public Set<Map.Entry<Locale, String>> entrySet() {
062                    return _map.entrySet();
063            }
064    
065            @Override
066            public boolean equals(Object object) {
067                    return _map.equals(object);
068            }
069    
070            @Override
071            public String get(Object key) {
072                    return _map.get(key);
073            }
074    
075            public Locale[] getAvailableLocales() {
076                    return _availableLocales;
077            }
078    
079            public Locale getDefaultLocale() {
080                    return _defaultLocale;
081            }
082    
083            public String getDefaultValue() {
084                    return get(_defaultLocale);
085            }
086    
087            public String getKey() {
088                    return _key;
089            }
090    
091            public String getLocalizationXml() {
092                    XMLStreamWriter xmlStreamWriter = null;
093    
094                    try {
095                            UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
096    
097                            XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
098    
099                            xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(
100                                    unsyncStringWriter);
101    
102                            xmlStreamWriter.writeStartDocument();
103    
104                            xmlStreamWriter.writeStartElement("root");
105    
106                            xmlStreamWriter.writeAttribute(
107                                    "available-locales", StringUtil.merge(_availableLocales));
108                            xmlStreamWriter.writeAttribute(
109                                    "default-locale", _defaultLocale.toString());
110    
111                            for (Locale locale : _availableLocales) {
112                                    String value = get(locale);
113    
114                                    if (value != null) {
115                                            xmlStreamWriter.writeStartElement(_key);
116    
117                                            xmlStreamWriter.writeAttribute(
118                                                    "language-id", locale.toString());
119    
120                                            xmlStreamWriter.writeCharacters(value);
121    
122                                            xmlStreamWriter.writeEndElement();
123                                    }
124                            }
125    
126                            xmlStreamWriter.writeEndElement();
127    
128                            xmlStreamWriter.writeEndDocument();
129    
130                            return unsyncStringWriter.toString();
131                    }
132                    catch (XMLStreamException xmlse) {
133                            throw new RuntimeException(xmlse);
134                    }
135                    finally {
136                            _close(xmlStreamWriter);
137                    }
138            }
139    
140            @Override
141            public int hashCode() {
142                    return _map.hashCode();
143            }
144    
145            @Override
146            public boolean isEmpty() {
147                    return _map.isEmpty();
148            }
149    
150            @Override
151            public Set<Locale> keySet() {
152                    return _map.keySet();
153            }
154    
155            @Override
156            public String put(Locale key, String value) {
157                    return _map.put(key, value);
158            }
159    
160            @Override
161            public void putAll(Map<? extends Locale, ? extends String> map) {
162                    _map.putAll(map);
163            }
164    
165            @Override
166            public String remove(Object key) {
167                    return _map.remove(key);
168            }
169    
170            @Override
171            public int size() {
172                    return _map.size();
173            }
174    
175            @Override
176            public Collection<String> values() {
177                    return _map.values();
178            }
179    
180            private void _close(XMLStreamWriter xmlStreamWriter) {
181                    if (xmlStreamWriter != null) {
182                            try {
183                                    xmlStreamWriter.close();
184                            }
185                            catch (XMLStreamException xmlse) {
186                            }
187                    }
188            }
189    
190            private final Locale[] _availableLocales;
191            private final Locale _defaultLocale;
192            private final String _key;
193            private final Map<Locale, String> _map = new HashMap<Locale, String>();
194    
195    }