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