001: package net.sourceforge.squirrel_sql.plugins.i18n;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.net.MalformedURLException;
009: import java.net.URL;
010: import java.util.Locale;
011: import java.util.Properties;
012: import java.util.Enumeration;
013: import java.util.zip.ZipEntry;
014: import java.util.zip.ZipFile;
015:
016: public class I18nProps extends Object {
017:
018: private File _file;
019: private File _zipFile;
020: private String _entryName;
021: private String _name;
022:
023: public I18nProps(File file, URL[] sourceUrls) {
024: _file = file;
025: initName(sourceUrls);
026: }
027:
028: public I18nProps(File zipFile, String entryName, URL[] sourceUrls) {
029: _zipFile = zipFile;
030: _entryName = entryName;
031: initName(sourceUrls);
032: }
033:
034: private void initName(URL[] sourceUrls) {
035:
036: for (int i = 0; i < sourceUrls.length; i++) {
037: String classPathEntry = sourceUrls[i].getPath().replaceAll(
038: "%20", " ");
039: if (getPath().startsWith(classPathEntry)) {
040: _name = getPath().substring(classPathEntry.length());
041: return;
042: }
043:
044: }
045: _name = getPath();
046: }
047:
048: public String getPath() {
049: try {
050: if (null != _file) {
051: return _file.toURL().getPath().replaceAll("%20", " ");
052: } else {
053: return new File(_zipFile.toString() + File.separator
054: + _entryName).toURL().getPath().replaceAll(
055: "%20", " ");
056: }
057: } catch (MalformedURLException e) {
058: throw new RuntimeException(e);
059: }
060: }
061:
062: public String getName() {
063: return _name;
064: }
065:
066: Properties getProperties() {
067: try {
068: Properties ret = new Properties();
069: InputStream is = getInputStream();
070: ret.load(is);
071: is.close();
072: return ret;
073: } catch (IOException e) {
074: throw new RuntimeException(e);
075: }
076: }
077:
078: private InputStream getInputStream() {
079: try {
080: if (null != _file) {
081: return new FileInputStream(_file);
082: } else {
083: ZipFile zf = new ZipFile(_zipFile);
084:
085: ZipEntry entry = zf.getEntry(_entryName);
086:
087: return zf.getInputStream(entry);
088: }
089: } catch (IOException e) {
090: throw new RuntimeException(e);
091: }
092: }
093:
094: public void removeProps(Properties toRemoveFrom) {
095: Properties toRemove = getProperties();
096:
097: for (Object key : toRemove.keySet()) {
098: toRemoveFrom.remove(key);
099: }
100: }
101:
102: public void copyTo(File toCopyTo) {
103: try {
104: InputStream is = getInputStream();
105:
106: FileOutputStream fos = new FileOutputStream(toCopyTo);
107:
108: int buf = is.read();
109: while (-1 != buf) {
110: fos.write(buf);
111: buf = is.read();
112:
113: }
114:
115: fos.flush();
116: fos.close();
117: is.close();
118: } catch (IOException e) {
119: throw new RuntimeException(e);
120: }
121: }
122:
123: public String getLocalizedFileName(Locale loc) {
124: String name = new File(getPath()).getPath().substring(
125: getPath().lastIndexOf(File.separator) + 1);
126: return new File(name.substring(0, name
127: .lastIndexOf(".properties"))
128: + "_" + loc + ".properties").getName();
129: }
130:
131: public String getUnlocalizedPath(Locale loc) {
132: String path = getPath().substring(
133: 0,
134: new File(getPath()).getPath().lastIndexOf(
135: File.separator));
136: String name = getPath().substring(
137: new File(getPath()).getPath().lastIndexOf(
138: File.separator));
139: return path + name.replaceAll("_" + loc.toString(), "");
140: }
141:
142: public static Locale parseLocaleFromPropsFileName(
143: String propsFileName) {
144: if (false == propsFileName.endsWith(".properties")) {
145: return null;
146: }
147:
148: String buf = propsFileName.substring(0, propsFileName.length()
149: - ".properties".length());
150:
151: Locale[] availableLocales = Locale.getAvailableLocales();
152:
153: for (int i = 0; i < availableLocales.length; i++) {
154: if (buf.endsWith("_" + availableLocales[i].toString())) {
155: return availableLocales[i];
156: }
157: }
158:
159: return null;
160:
161: }
162:
163: public Properties getTranslateableProperties() {
164: String name;
165:
166: if (null == _entryName) {
167: name = _file.getName();
168: } else {
169: if (-1 == _entryName.lastIndexOf(File.separator)) {
170: name = _entryName;
171: } else {
172: name = _entryName.substring(_entryName
173: .lastIndexOf(File.separator));
174: }
175: }
176:
177: if (name.startsWith("I18nStrings")) {
178: return getProperties();
179: } else {
180: // These files contain images etc. We try to filter out these props.
181: Properties ret = getProperties();
182:
183: // Do not change this way to iterate.
184: // Changing to ret.keySet().iterator() caused bug #1787731
185: // The save way would be not to use ret.remove(key) inside the loop.
186: for (Enumeration e = ret.keys(); e.hasMoreElements();) {
187: String key = (String) e.nextElement();
188:
189: if (key.endsWith(".image")
190: || key.endsWith(".rolloverimage")
191: || key.endsWith(".disabledimage")
192: || key.endsWith(".frameIcon")
193: || key.endsWith(".file")
194: || key.endsWith(".images")
195: || key.endsWith(".accelerator")
196: || key.equals("path.defaults")) {
197: ret.remove(key);
198: }
199:
200: if (key.endsWith(".noi18n")
201: && ret.getProperty(key)
202: .equalsIgnoreCase("true")) {
203: ret.remove(key);
204: ret.remove(key.substring(0, key.length()
205: - ".noi18n".length()));
206: }
207: }
208:
209: return ret;
210:
211: }
212:
213: }
214: }
|