1 |
/* |
2 |
* Copyright (c) 2005- Shinji Kashihara. |
3 |
* All rights reserved. This program are made available under |
4 |
* the terms of the Eclipse Public License v1.0 which accompanies |
5 |
* this distribution, and is available at epl-v10.html. |
6 |
*/ |
7 |
package jp.sourceforge.mergedoc.pleiades.aspect.resource; |
8 |
|
9 |
import static jp.sourceforge.mergedoc.pleiades.aspect.resource.CacheFileNames.*; |
10 |
import static jp.sourceforge.mergedoc.pleiades.resource.FileNames.*; |
11 |
|
12 |
import java.io.File; |
13 |
|
14 |
import jp.sourceforge.mergedoc.pleiades.Pleiades; |
15 |
import jp.sourceforge.mergedoc.pleiades.aspect.advice.JointPoint; |
16 |
import jp.sourceforge.mergedoc.pleiades.log.Logger; |
17 |
import jp.sourceforge.mergedoc.pleiades.resource.AbstractTranslationDictionary; |
18 |
import jp.sourceforge.mergedoc.pleiades.resource.Mnemonics; |
19 |
import jp.sourceforge.mergedoc.pleiades.resource.Property; |
20 |
import jp.sourceforge.mergedoc.pleiades.resource.PropertySet; |
21 |
import jp.sourceforge.mergedoc.pleiades.resource.Strings; |
22 |
import jp.sourceforge.mergedoc.pleiades.resource.TranslationString; |
23 |
|
24 |
/** |
25 |
* ������������������������������������������������������������������������������������������ |
26 |
* Eclipse ������������������������������������������������������������ |
27 |
* <p> |
28 |
* @author cypher256 |
29 |
*/ |
30 |
public class DynamicTranslationDictionary extends AbstractTranslationDictionary { |
31 |
|
32 |
/** ��������� */ |
33 |
private static final Logger log = Logger.getLogger(DynamicTranslationDictionary.class); |
34 |
|
35 |
/** ��������������������������������������������������������� */ |
36 |
private static final File cacheFile = Pleiades.getResourceFile(TRANS_CACHE_PROP); |
37 |
|
38 |
/** ��������������������������������������������������������� */ |
39 |
private static DynamicTranslationDictionary instance; |
40 |
|
41 |
/** |
42 |
* ��������������������������������������������������� |
43 |
* <p> |
44 |
* @return ������������������������������ |
45 |
*/ |
46 |
public static DynamicTranslationDictionary getInstance() { |
47 |
|
48 |
if (instance != null) { |
49 |
return instance; |
50 |
} |
51 |
if (log.isDebugEnabled()) { |
52 |
TraceableTranslationDictionary tracer = TraceableTranslationDictionary.getInstance(); |
53 |
if (tracer != null) { |
54 |
instance = tracer; |
55 |
} |
56 |
} |
57 |
if (instance == null) { |
58 |
instance = new DynamicTranslationDictionary(); |
59 |
} |
60 |
return instance; |
61 |
} |
62 |
|
63 |
// ------------------------------------------------------------------------- |
64 |
|
65 |
/** ������������������������������������������ */ |
66 |
private PropertySet cacheProp; |
67 |
|
68 |
/** ������������������������������������������ */ |
69 |
private CallHierarchyExplorer callHierarchy = CallHierarchyExplorer.getInstance(); |
70 |
|
71 |
/** |
72 |
* ��������������������������������������������������� |
73 |
*/ |
74 |
protected DynamicTranslationDictionary() { |
75 |
} |
76 |
|
77 |
/** |
78 |
* ������������������������������������������ |
79 |
*/ |
80 |
@Override |
81 |
protected boolean load() { |
82 |
|
83 |
cacheProp = new ValueHashPropertySet(); |
84 |
|
85 |
if (Pleiades.getPleiadesOption().isClean() || !cacheFile.exists()) { |
86 |
|
87 |
// -clean ������������������������������������ |
88 |
super.load(); |
89 |
cacheProp.load(validateExists(TRANS_MULTIBYTES_KEY_PROP)); |
90 |
} else { |
91 |
// -clean ��������������������������������������������������������� |
92 |
try { |
93 |
cacheProp.load(cacheFile); |
94 |
} catch (RuntimeException e) { |
95 |
|
96 |
// ������������������������������������ |
97 |
log.warn("��������������������� %s ������������������������������... - %s", cacheFile, e.toString()); |
98 |
cacheFile.delete(); |
99 |
cacheProp.clear(); |
100 |
cacheProp.load(validateExists(TRANS_MULTIBYTES_KEY_PROP)); |
101 |
} |
102 |
} |
103 |
return true; |
104 |
} |
105 |
|
106 |
/** |
107 |
* ��������������������������������� |
108 |
* ��������������������������������������������������������������������������� |
109 |
*/ |
110 |
public void shutdown() { |
111 |
|
112 |
isLoadedDefault = true; |
113 |
|
114 |
if (Pleiades.getPleiadesOption().isClean() || !cacheFile.exists()) { |
115 |
|
116 |
cacheProp.store(cacheFile, "������������������������������������������"); |
117 |
|
118 |
} else { |
119 |
|
120 |
PropertySet cachePropOld = new PropertySet(cacheFile); |
121 |
|
122 |
if (cacheProp.size() > cachePropOld.size()) { |
123 |
cacheProp.store(cacheFile, "������������������������������������������"); |
124 |
log.info("������������������������������������������������������������������"); |
125 |
} else { |
126 |
log.info("������������������������������������������������������������������������"); |
127 |
} |
128 |
} |
129 |
} |
130 |
|
131 |
/** |
132 |
* ������������������������������������������������������������������������������������ |
133 |
* ������������������������������������������������������ |
134 |
* <p> |
135 |
* @param enWithMnemonic ��������������������������� (������������������������) |
136 |
* @param jointPoint ������������������������������ |
137 |
* @return ������������������������������ (������������������������)��� |
138 |
*/ |
139 |
public String lookup(String enWithMnemonic, JointPoint jointPoint) { |
140 |
|
141 |
// ��������������������������������������� (������������������������������������ UI ���������������������������) |
142 |
if ( |
143 |
enWithMnemonic.equals("") || |
144 |
Mnemonics.hasJaMnemonic(enWithMnemonic) || |
145 |
|
146 |
// ��������������������������������������������������� 1 ������������������������ |
147 |
// ������������������������������������������������������������������ 1 ������������������ |
148 |
enWithMnemonic.length() + 1 < Strings.getBytes(enWithMnemonic, "Windows-31J").length |
149 |
) { |
150 |
return enWithMnemonic; |
151 |
} |
152 |
|
153 |
// ��������������������������������� |
154 |
String en = Mnemonics.removeEnMnemonic(enWithMnemonic); |
155 |
|
156 |
// ��������������������������� value ��������� (������������) ������������������������������ |
157 |
if (cacheProp.containsValue(en.trim())) { |
158 |
return enWithMnemonic; |
159 |
} |
160 |
|
161 |
// ��������������������������������������������������� |
162 |
String ja = lookupInternal(en, jointPoint); |
163 |
|
164 |
// ��������������������� (������������������������������������������������������������������������) |
165 |
if (!en.equals(ja) && callHierarchy.isExcludeTranslation(en, jointPoint)) { |
166 |
|
167 |
// ������������������ Javadoc���JsDoc ������������������������ Package������������������������������������ |
168 |
// ��������������������������������� 2009.02.05 |
169 |
// ----- |
170 |
// ��������������������������������������������������������������� (���: Debug���������...)��� |
171 |
// ������������������������������������������������������������������������������������������ 2009.02.11 |
172 |
if (en.equals("Debug")) { |
173 |
return enWithMnemonic; |
174 |
} |
175 |
ja = en; |
176 |
} |
177 |
|
178 |
// ������������������������������������������������������������������������������������������������������������ |
179 |
ja = convertMnemonicEnToJa(enWithMnemonic, en, ja); |
180 |
|
181 |
return ja; |
182 |
} |
183 |
|
184 |
/** |
185 |
* ������������������������������������������������������������������������������������ |
186 |
* ��������������������������������������������� |
187 |
* <p> |
188 |
* @param en ��������������������������� |
189 |
* @param jointPoint ������������������������������ |
190 |
* @return ������������������������������������������������������������ en ������������������������ |
191 |
*/ |
192 |
public String lookupIgnoreMnemonic(String en, JointPoint jointPoint) { |
193 |
|
194 |
// ������������������������������������������������������������������������������������������������ |
195 |
|
196 |
// ��������������������������������������������������� |
197 |
String ja = lookupInternal(en, jointPoint); |
198 |
|
199 |
// ��������������������� (������������������������������������������������������������������������) |
200 |
if (!en.equals(ja) && callHierarchy.isExcludeTranslation(en, jointPoint)) { |
201 |
ja = en; |
202 |
} |
203 |
return ja; |
204 |
} |
205 |
|
206 |
/** |
207 |
* ������������������������������������������������������������������������������������������������������������ |
208 |
* <p> |
209 |
* @param en ��������������������������������������� |
210 |
* @param jointPoint ������������������������������ (���������������) |
211 |
* @return ������������������������������������������������������������������������ en ������������������������ |
212 |
*/ |
213 |
public String lookupHelp(String en, JointPoint jointPoint) { |
214 |
return getValueForHelp(en); |
215 |
} |
216 |
|
217 |
/** |
218 |
* ��������������������������������� |
219 |
* <p> |
220 |
* @param en ��������������������������������������������������������� |
221 |
* @param jointPoint ������������������������������ |
222 |
* @return ������������������������������������������������������������������������������������������ en ������������������������ |
223 |
*/ |
224 |
protected String lookupInternal(String en, JointPoint jointPoint) { |
225 |
|
226 |
// ������������������������������������������������������ |
227 |
String ja = cacheProp.get(en); |
228 |
if (ja != null) { |
229 |
if (ja.equals("")) { |
230 |
ja = en; |
231 |
} |
232 |
} else { |
233 |
|
234 |
// ������������������������������������������������ |
235 |
ja = super.getValueByRegex(new TranslationString(en)); |
236 |
if (ja == null) { |
237 |
|
238 |
// ������������ (������������������������������������) ��������� |
239 |
if (en.length() != en.getBytes().length) { |
240 |
ja = en; |
241 |
|
242 |
// ������������������������������������������������������ |
243 |
} else { |
244 |
if (super.load()) { |
245 |
log.debug("������������������������������������������������������������������: " + Property.escapeKey(en)); |
246 |
} |
247 |
ja = getValue(en); |
248 |
} |
249 |
// ��������������������� |
250 |
cacheProp.put(en, en.equals(ja) ? "" : ja); |
251 |
} |
252 |
} |
253 |
|
254 |
// DEBUG |
255 |
//if (en.equals("Pleiades")) { |
256 |
// String s = System.currentTimeMillis() + "���" + en + "���������" + ja + "���"; |
257 |
// log.debug(new Exception(s), "������������������������������������������������ jointPoint:" + jointPoint); |
258 |
// return s; |
259 |
//} |
260 |
|
261 |
return ja; |
262 |
} |
263 |
|
264 |
/** |
265 |
* ��������������������������������������������������������� |
266 |
* super.getValue ���������������������������������this.getValue ��������������������������� |
267 |
* ������������������������������������������ |
268 |
* <p> |
269 |
* @param enTs ��������������������� |
270 |
* @return ������������������������������������������������������������������������������������������ null��� |
271 |
*/ |
272 |
@Override |
273 |
protected String getValueByRegex(TranslationString enTs) { |
274 |
return null; |
275 |
} |
276 |
|
277 |
/** |
278 |
* ������������������������������������������������������������������<br> |
279 |
* ������&x ��� (&X) |
280 |
* <p> |
281 |
* @param enWithMnemonic ��������������������������� ������������������������������ |
282 |
* @param en ��������������������������� ������������������������������ |
283 |
* @param ja ������������������������������������������������������������ |
284 |
* @return ������������������������������������������������������������ |
285 |
*/ |
286 |
protected String convertMnemonicEnToJa(String enWithMnemonic, String en, String ja) { |
287 |
|
288 |
if (Pleiades.getPleiadesOption().isNoMnemonic()) { |
289 |
return ja; |
290 |
} |
291 |
String jaWithMnemonic = Mnemonics.convertMnemonicEnToJa(enWithMnemonic, en, ja); |
292 |
return jaWithMnemonic; |
293 |
} |
294 |
} |