Add session to cache with both hostname and address.
[cyberduck.git] / source / ch / cyberduck / core / i18n / RegexLocale.java
blob4e738684f570bfbce5fce71fe1b6282686360b82
1 package ch.cyberduck.core.i18n;
3 /*
4 * Copyright (c) 2002-2015 David Kocher. All rights reserved.
5 * http://cyberduck.io/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * feedback@cyberduck.io
21 import ch.cyberduck.core.Local;
22 import ch.cyberduck.core.preferences.ApplicationResourcesFinderFactory;
24 import org.apache.commons.collections.map.LRUMap;
25 import org.apache.commons.io.IOUtils;
26 import org.apache.log4j.Logger;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.LineNumberReader;
32 import java.nio.charset.Charset;
33 import java.util.Collections;
34 import java.util.Map;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
38 /**
39 * @version $Id$
41 public class RegexLocale implements Locale {
42 private static final Logger log = Logger.getLogger(RegexLocale.class);
44 private Map<Key, String> cache
45 = Collections.<Key, String>synchronizedMap(new LRUMap(1000));
47 private Local resources;
49 private String locale
50 = java.util.Locale.getDefault().getLanguage();
52 private Pattern pattern
53 = Pattern.compile("\"(.*)\"\\s*=\\s*\"(.*)\";");
55 public RegexLocale() {
56 this(ApplicationResourcesFinderFactory.get().find());
59 public RegexLocale(final Local resources) {
60 this.resources = resources;
63 @Override
64 public void setDefault(final String language) {
65 locale = language;
66 cache.clear();
69 @Override
70 public String localize(final String key, final String table) {
71 final Key lookup = new Key(table, key);
72 if(!cache.containsKey(lookup)) {
73 try {
74 this.load(table);
76 catch(IOException e) {
77 log.warn(String.format("Failure loading properties from %s.strings. %s", table, e.getMessage()));
80 if(cache.containsKey(lookup)) {
81 return cache.get(lookup);
83 return key;
86 private void load(final String table) throws IOException {
87 final LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(
88 String.format("%s/%s.lproj/%s.strings", resources.getAbsolute(), locale, table)
89 ), Charset.forName("UTF-16")));
90 try {
91 String line;
92 while((line = reader.readLine()) != null) {
93 final Matcher matcher = pattern.matcher(line);
94 if(matcher.matches()) {
95 cache.put(new Key(table, matcher.group(1)), matcher.group(2));
99 finally {
100 IOUtils.closeQuietly(reader);
104 private final class Key {
105 private String table;
106 private String key;
108 public Key(final String table, final String key) {
109 this.table = table;
110 this.key = key;
113 @Override
114 public boolean equals(final Object o) {
115 if(this == o) {
116 return true;
118 if(!(o instanceof Key)) {
119 return false;
121 final Key key1 = (Key) o;
122 if(key != null ? !key.equals(key1.key) : key1.key != null) {
123 return false;
125 if(table != null ? !table.equals(key1.table) : key1.table != null) {
126 return false;
128 return true;
131 @Override
132 public int hashCode() {
133 int result = table != null ? table.hashCode() : 0;
134 result = 31 * result + (key != null ? key.hashCode() : 0);
135 return result;