001
014
015 package com.liferay.portal.language;
016
017 import com.liferay.portal.kernel.util.PropertiesUtil;
018 import com.liferay.portal.kernel.util.ResourceBundleThreadLocal;
019 import com.liferay.portal.kernel.util.ResourceBundleUtil;
020
021 import java.io.IOException;
022 import java.io.InputStream;
023
024 import java.util.Enumeration;
025 import java.util.HashMap;
026 import java.util.Iterator;
027 import java.util.Map;
028 import java.util.MissingResourceException;
029 import java.util.NoSuchElementException;
030 import java.util.Properties;
031 import java.util.ResourceBundle;
032 import java.util.Set;
033
034
038 public class LiferayResourceBundle extends ResourceBundle {
039
040 public LiferayResourceBundle(String string, String charsetName)
041 throws IOException {
042
043 _map = new HashMap<String, String>();
044
045 Properties properties = PropertiesUtil.load(string, charsetName);
046
047 LanguageResources.fixValues(_map, properties);
048 }
049
050 public LiferayResourceBundle(InputStream inputStream, String charsetName)
051 throws IOException {
052
053 this(null, inputStream, charsetName);
054 }
055
056 public LiferayResourceBundle(
057 ResourceBundle parentResourceBundle, InputStream inputStream,
058 String charsetName)
059 throws IOException {
060
061 setParent(parentResourceBundle);
062
063 _map = new HashMap<String, String>();
064
065 Properties properties = PropertiesUtil.load(inputStream, charsetName);
066
067 LanguageResources.fixValues(_map, properties);
068 }
069
070 @Override
071 public Object handleGetObject(String key) {
072 if (key == null) {
073 throw new NullPointerException();
074 }
075
076 String value = _map.get(key);
077
078 if ((value == null) && ResourceBundleThreadLocal.isReplace()) {
079 if (parent != null) {
080 try {
081 value = parent.getString(key);
082 }
083 catch (MissingResourceException mre) {
084 }
085 }
086
087 if (value == null) {
088 value = ResourceBundleUtil.NULL_VALUE;
089 }
090 }
091
092 return value;
093 }
094
095 @Override
096 public Enumeration<String> getKeys() {
097 final Set<String> keys = _map.keySet();
098
099 final Enumeration<String> parentKeys =
100 (parent == null) ? null : parent.getKeys();
101
102 final Iterator<String> itr = keys.iterator();
103
104 return new Enumeration<String>() {
105 String next = null;
106
107 public boolean hasMoreElements() {
108 if (next == null) {
109 if (itr.hasNext()) {
110 next = itr.next();
111 }
112 else if (parentKeys != null) {
113 while ((next == null) && parentKeys.hasMoreElements()) {
114 next = parentKeys.nextElement();
115
116 if (keys.contains(next)) {
117 next = null;
118 }
119 }
120 }
121 }
122
123 if (next != null) {
124 return true;
125 }
126 else {
127 return false;
128 }
129 }
130
131 public String nextElement() {
132 if (hasMoreElements()) {
133 String result = next;
134
135 next = null;
136
137 return result;
138 }
139 else {
140 throw new NoSuchElementException();
141 }
142 }
143 };
144 }
145
146 private Map<String, String> _map;
147
148 }