1 | |
package de.glossmaker.gloss.datastructure; |
2 | |
|
3 | |
import java.io.BufferedReader; |
4 | |
import java.io.FileInputStream; |
5 | |
import java.io.FileNotFoundException; |
6 | |
import java.io.FileWriter; |
7 | |
import java.io.IOException; |
8 | |
import java.io.InputStreamReader; |
9 | |
import java.io.PrintWriter; |
10 | |
import java.util.HashMap; |
11 | |
import java.util.Iterator; |
12 | |
import java.util.Map.Entry; |
13 | |
import java.util.HashSet; |
14 | |
import java.util.StringTokenizer; |
15 | |
import java.util.TreeSet; |
16 | |
import java.util.regex.Matcher; |
17 | |
import java.util.regex.Pattern; |
18 | |
|
19 | |
import org.apache.log4j.Logger; |
20 | |
|
21 | |
import de.glossmaker.common.io.UnicodeInputStream; |
22 | |
import de.glossmaker.gloss.language.Translation; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class GlossItems implements IItems { |
30 | 2 | private static final Logger mLogger = Logger.getLogger(GlossItems.class); |
31 | |
private static final String LINEFEET = "\r\n"; |
32 | 24 | private TreeSet<GlossItem> mGlossItems = null; |
33 | 24 | private String mFileName = null; |
34 | 24 | private String mConsistencyLog = null; |
35 | 24 | private Translation mTranslation = null; |
36 | |
|
37 | 24 | public GlossItems() { |
38 | 24 | mGlossItems = new TreeSet<GlossItem>(new GlossItemComparator()); |
39 | 24 | mTranslation = Translation.getInstance(); |
40 | 24 | } |
41 | |
|
42 | |
public boolean add(IItem item) { |
43 | 26 | if (mGlossItems.contains(item) == false) { |
44 | 22 | return mGlossItems.add((GlossItem) item); |
45 | |
} else { |
46 | 4 | return false; |
47 | |
} |
48 | |
} |
49 | |
|
50 | |
public boolean remove(IItem item) { |
51 | 0 | boolean result = false; |
52 | |
Iterator<GlossItem> iterator; |
53 | 0 | iterator = mGlossItems.iterator(); |
54 | 0 | while(iterator.hasNext() && result == false) { |
55 | 0 | GlossItem glossItem = (GlossItem) iterator.next(); |
56 | 0 | if (glossItem.equals((GlossItem)item)) { |
57 | 0 | iterator.remove(); |
58 | 0 | result = true; |
59 | |
} |
60 | 0 | } |
61 | |
|
62 | 0 | return result; |
63 | |
} |
64 | |
|
65 | |
public String toString() { |
66 | 0 | String result = ""; |
67 | 0 | for(GlossItem item : mGlossItems) { |
68 | 0 | result += "heading=" + item.getHeading() + ", key=" + item.getKey() + |
69 | |
", word=" + item.getWord() + ", definition=" + item.getDefinition() + "\n"; |
70 | |
} |
71 | 0 | return result; |
72 | |
} |
73 | |
|
74 | |
public boolean remove(String key) { |
75 | 2 | boolean result = false; |
76 | |
Iterator<GlossItem> iterator; |
77 | 2 | iterator = mGlossItems.iterator(); |
78 | 4 | while(iterator.hasNext() && result == false) { |
79 | 2 | GlossItem item = (GlossItem) iterator.next(); |
80 | 2 | if (item.getKey().equals(key)) { |
81 | 2 | iterator.remove(); |
82 | 2 | result = true; |
83 | |
} |
84 | 2 | } |
85 | |
|
86 | 2 | return result; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
public void clear() { |
91 | 1 | mGlossItems.clear(); |
92 | 1 | mFileName = null; |
93 | 1 | } |
94 | |
|
95 | |
public int size() { |
96 | 18 | return mGlossItems.size(); |
97 | |
} |
98 | |
|
99 | |
public GlossItem get(int index) { |
100 | 15 | GlossItem result = null; |
101 | 15 | int i = 0; |
102 | 15 | for(GlossItem item : mGlossItems) { |
103 | 18 | if (i == index) { |
104 | 14 | result = item; |
105 | 14 | break; |
106 | |
} |
107 | 4 | i++; |
108 | |
} |
109 | 15 | return result; |
110 | |
} |
111 | |
|
112 | |
public GlossItem getItem(String key) { |
113 | 0 | GlossItem result = null; |
114 | 0 | for(GlossItem item : mGlossItems) { |
115 | 0 | if (item.getKey().equals(key)) { |
116 | 0 | result = item; |
117 | 0 | break; |
118 | |
} |
119 | |
} |
120 | |
|
121 | 0 | return result; |
122 | |
} |
123 | |
|
124 | |
public boolean existsKey(String key) { |
125 | 5 | boolean result = false; |
126 | 5 | for(GlossItem item : mGlossItems) { |
127 | 7 | if (item.getKey().equals(key)) { |
128 | 3 | result = true; |
129 | 3 | break; |
130 | |
} |
131 | |
} |
132 | 5 | return result; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
private void generateItems(StringBuffer buffer) { |
137 | 8 | Pattern p = null; |
138 | 8 | Matcher m = null; |
139 | 8 | String key = null; |
140 | 8 | String word = null; |
141 | 8 | String definition = null; |
142 | 8 | String heading = null; |
143 | 8 | StringTokenizer st = new StringTokenizer(buffer.toString(), "@"); |
144 | 21 | while (st.hasMoreTokens()) { |
145 | 13 | key = ""; |
146 | 13 | word = ""; |
147 | 13 | heading = ""; |
148 | 13 | definition = ""; |
149 | 13 | String zeile = st.nextToken(); |
150 | |
|
151 | 13 | p = Pattern.compile("[g|G][d|D][ |\t]*\\{([^,.]*)[,]"); |
152 | 13 | m = p.matcher(zeile); |
153 | 13 | if (m.find()) { |
154 | 12 | key = m.group(1); |
155 | |
} |
156 | |
|
157 | 13 | p = Pattern.compile("[w|W][o|O][r|R][d|D][ |\t]*=[ |\t]*[\"](.*)[\"]"); |
158 | 13 | m = p.matcher(zeile); |
159 | 13 | if (m.find()) { |
160 | 12 | word = m.group(1); |
161 | 12 | word = word.substring(0, word.length()); |
162 | |
} |
163 | 13 | if (word.length() < 1) { |
164 | 1 | word = key; |
165 | |
} |
166 | |
|
167 | 13 | p = Pattern.compile("[h|H][e|E][a|A][d|D][i|I][n|N][g|G][ |\t]*=[ |\t]*[\"](.*)[\"]"); |
168 | 13 | m = p.matcher(zeile); |
169 | 13 | if (m.find()) { |
170 | 12 | heading = m.group(1); |
171 | 12 | if (heading != null && heading.contains("\"")) { |
172 | 0 | heading = heading.substring(0, heading.length()); |
173 | |
} |
174 | |
} |
175 | |
|
176 | 13 | p = Pattern.compile("[d|D][e|E][f|F][i|I][n|N][i|I][t|T][i|I][o|O][n|N]" + |
177 | |
"[ |\t]*=[ |\t]*[\"](.*)[\\}]", |
178 | |
Pattern.MULTILINE | Pattern.DOTALL); |
179 | 13 | m = p.matcher(zeile); |
180 | 13 | if (m.find()) { |
181 | 12 | definition = m.group(1); |
182 | 12 | if (definition.startsWith("\"")) { |
183 | 0 | definition = definition.substring(1, definition.length()); |
184 | |
} |
185 | 12 | int index = definition.indexOf("\""); |
186 | 12 | if (index > 0) { |
187 | 12 | definition = definition.substring(0, index); |
188 | |
} |
189 | |
} |
190 | |
|
191 | 13 | if (!key.equals("") && !word.equals("") && !heading.equals("") && !definition.equals("")) { |
192 | 12 | GlossItem item = new GlossItem(key, heading, word, definition); |
193 | 12 | mGlossItems.add(item); |
194 | |
} |
195 | 13 | } |
196 | 8 | } |
197 | |
|
198 | |
public StringBuffer readFromFile(String fileName) { |
199 | 8 | FileInputStream fis = null; |
200 | 8 | String encoding = "ISO-8859-1"; |
201 | |
try { |
202 | 8 | fis = new FileInputStream(fileName); |
203 | 8 | UnicodeInputStream uis = new UnicodeInputStream(fis, "ISO-8859-1"); |
204 | 8 | encoding = uis.getEncoding(); |
205 | 0 | } catch (FileNotFoundException e) { |
206 | 0 | mLogger.error(e); |
207 | 8 | } |
208 | 8 | mFileName = fileName; |
209 | 8 | mGlossItems.clear(); |
210 | 8 | StringBuffer result = new StringBuffer(); |
211 | |
try { |
212 | 8 | BufferedReader in = new BufferedReader(new InputStreamReader( |
213 | |
new FileInputStream(fileName), encoding)); |
214 | 8 | String zeile = null; |
215 | 74 | while ((zeile = in.readLine()) != null) { |
216 | 66 | result.append(zeile); |
217 | 66 | result.append(System.getProperty("line.separator")); |
218 | |
} |
219 | 8 | in.close(); |
220 | 0 | } catch (IOException e) { |
221 | 0 | mLogger.error(e); |
222 | 8 | } |
223 | |
|
224 | 8 | generateItems(result); |
225 | |
|
226 | 8 | return result; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
public void writeToFile(String fileName) { |
232 | |
try { |
233 | 6 | mFileName = fileName; |
234 | 6 | FileWriter outFile = new FileWriter(fileName); |
235 | 6 | PrintWriter out = new PrintWriter(outFile); |
236 | 6 | for(GlossItem item : mGlossItems) { |
237 | 6 | out.print("@gd{" + item.getKey() + "," + System.getProperty("line.separator")); |
238 | 6 | out.print(" heading = \"" + item.getHeading() + "\"," + System.getProperty("line.separator")); |
239 | 6 | out.print(" word = \"" + item.getWord() + "\"," + System.getProperty("line.separator")); |
240 | 6 | out.print(" definition = \"" + item.getDefinition() + "\"" + System.getProperty("line.separator")); |
241 | 6 | out.println("}"); |
242 | |
} |
243 | 6 | out.close(); |
244 | 0 | } catch (IOException e){ |
245 | 0 | mLogger.error(e); |
246 | 6 | } |
247 | 6 | } |
248 | |
|
249 | |
public String getFileName() { |
250 | 1 | return mFileName; |
251 | |
} |
252 | |
|
253 | |
public boolean isGlossReferencesConsistent() { |
254 | 2 | boolean result = true; |
255 | 2 | HashMap<String, String> ref = new HashMap<String, String>(); |
256 | 2 | for(GlossItem item : mGlossItems) { |
257 | 5 | Pattern p = Pattern.compile("[\\\\]gloss\\{([^\\}.]*)\\}"); |
258 | 5 | Matcher m = p.matcher(item.getDefinition()); |
259 | 8 | while(m.find()) { |
260 | 3 | ref.put(m.group(1), item.getKey()); |
261 | |
} |
262 | 5 | } |
263 | 2 | for(Entry<String, String> entry : ref.entrySet()) { |
264 | 3 | if (existsKey(entry.getKey()) == false) { |
265 | 1 | result = false; |
266 | 1 | String st = mTranslation.getValue("datastructure.items.key_not_exists", entry.getKey(), |
267 | |
entry.getValue()); |
268 | 1 | if (mConsistencyLog == null) { |
269 | 1 | mConsistencyLog = st; |
270 | |
} else { |
271 | 0 | mConsistencyLog += LINEFEET + st; |
272 | |
} |
273 | 3 | } |
274 | |
} |
275 | |
|
276 | 2 | return result; |
277 | |
} |
278 | |
|
279 | |
public String getConsistencyLog() { |
280 | 0 | return mConsistencyLog; |
281 | |
} |
282 | |
|
283 | |
|
284 | |
public int getHeadingCount(String heading) { |
285 | 3 | int result = 0; |
286 | 3 | for(GlossItem item : mGlossItems) { |
287 | 9 | if (heading.equals(item.getHeading())) { |
288 | 3 | result++; |
289 | |
} |
290 | |
} |
291 | 3 | return result; |
292 | |
} |
293 | |
|
294 | |
|
295 | |
public int getHeadingElementCount() { |
296 | 1 | HashSet<String> values = new HashSet<String>(); |
297 | 1 | for(GlossItem item : mGlossItems) { |
298 | 2 | values.add(item.getHeading()); |
299 | |
} |
300 | 1 | return values.size(); |
301 | |
} |
302 | |
|
303 | |
public String getHeading(int index) { |
304 | 4 | String result = null; |
305 | 4 | TreeSet<String> values = new TreeSet<String>(); |
306 | 4 | for(GlossItem item : mGlossItems) { |
307 | 8 | values.add(item.getHeading()); |
308 | |
} |
309 | 6 | for(int i = 0; i < values.size(); i++) { |
310 | 6 | if (i == index) { |
311 | 4 | result = (String) values.toArray()[i]; |
312 | 4 | break; |
313 | |
} |
314 | |
} |
315 | 4 | return result; |
316 | |
} |
317 | |
|
318 | |
public GlossItem get(String heading, int index) { |
319 | 0 | TreeSet<GlossItem> values = new TreeSet<GlossItem>(); |
320 | 0 | for(GlossItem item : mGlossItems) { |
321 | 0 | if (heading.equals(item.getHeading())) { |
322 | 0 | values.add(item); |
323 | |
} |
324 | |
} |
325 | 0 | if (index < values.size()) { |
326 | 0 | return (GlossItem) values.toArray()[index]; |
327 | |
} else { |
328 | 0 | return (GlossItem) values.toArray()[0]; |
329 | |
} |
330 | |
} |
331 | |
|
332 | |
public GlossItem get(String key) { |
333 | 3 | GlossItem result = null; |
334 | 3 | for(GlossItem item : mGlossItems) { |
335 | 4 | if (key.equals(item.getKey())) { |
336 | 3 | result = item; |
337 | 3 | break; |
338 | |
} |
339 | |
} |
340 | 3 | return result; |
341 | |
} |
342 | |
|
343 | |
public GlossItems clone() { |
344 | 1 | GlossItems items = new GlossItems(); |
345 | 1 | items.mFileName = mFileName; |
346 | 3 | for (int i = 0; i < size(); i++) { |
347 | 2 | GlossItem item = get(i).clone(); |
348 | 2 | items.add(item); |
349 | |
} |
350 | 1 | return items; |
351 | |
} |
352 | |
|
353 | |
@Override |
354 | |
public IItem getItem(int index) { |
355 | 2 | IItem result = null; |
356 | 2 | int i = 0; |
357 | 2 | for(GlossItem item : mGlossItems) { |
358 | 4 | if (i == index) { |
359 | 2 | result = item; |
360 | |
} |
361 | 4 | i++; |
362 | |
} |
363 | |
|
364 | 2 | return result; |
365 | |
} |
366 | |
|
367 | |
} |