Mercurial > hg > mpdl-group
comparison software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/dict/app/Lexicon.java @ 19:4a3641ae14d2
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 09 Nov 2011 15:32:05 +0100 |
parents | |
children | 7d6d969b10cf |
comparison
equal
deleted
inserted
replaced
18:dc5e9fcb3fdc | 19:4a3641ae14d2 |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.lt.dict.app; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.Collections; | |
5 import java.util.Enumeration; | |
6 import java.util.Hashtable; | |
7 | |
8 import org.apache.commons.lang3.StringEscapeUtils; | |
9 | |
10 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
11 import de.mpg.mpiwg.berlin.mpdl.lt.general.Language; | |
12 import de.mpg.mpiwg.berlin.mpdl.lt.text.transcode.Transcoder; | |
13 | |
14 public class Lexicon implements Comparable<Lexicon> { | |
15 private String name; | |
16 private String sourceLang; | |
17 private String description; | |
18 private String queryUrl; | |
19 private String type; // local or remote | |
20 private Hashtable<String, LexiconEntry> entries; | |
21 | |
22 public Lexicon(String name, String sourceLanguage) { | |
23 this.name = name; | |
24 this.sourceLang = sourceLanguage; | |
25 this.type = "local"; // default is local | |
26 this.entries = new Hashtable<String, LexiconEntry>(); | |
27 } | |
28 | |
29 public int compareTo(Lexicon l) { | |
30 return name.compareTo(l.name); | |
31 } | |
32 | |
33 public String getName() { | |
34 return name; | |
35 } | |
36 | |
37 public void setName(String name) { | |
38 this.name = name; | |
39 } | |
40 | |
41 public String getSourceLanguage() { | |
42 return sourceLang; | |
43 } | |
44 | |
45 public String getDescription() { | |
46 return description; | |
47 } | |
48 | |
49 public void setDescription(String description) { | |
50 this.description = description; | |
51 } | |
52 | |
53 public String getQueryUrl() { | |
54 return queryUrl; | |
55 } | |
56 | |
57 public void setQueryUrl(String queryUrl) { | |
58 this.queryUrl = queryUrl; | |
59 } | |
60 | |
61 public String getType() { | |
62 return type; | |
63 } | |
64 | |
65 public void setType(String type) { | |
66 this.type = type; | |
67 } | |
68 | |
69 public boolean isLocalLexicon() { | |
70 boolean isLocal = false; | |
71 if (type != null && type.equals("local")) | |
72 isLocal = true; | |
73 return isLocal; | |
74 } | |
75 | |
76 public boolean isBetacodeLexicon() { | |
77 boolean isBetacode = false; | |
78 if (name.equals("autenrieth") || name.equals("bonitz") || name.equals("lsj")) | |
79 isBetacode = true; | |
80 return isBetacode; | |
81 } | |
82 | |
83 public boolean isBuckwalterLexicon() { | |
84 boolean isBuckwalter = false; | |
85 if (name.equals("salmone")) | |
86 isBuckwalter = true; | |
87 return isBuckwalter; | |
88 } | |
89 | |
90 public ArrayList<LexiconEntry> getEntries() { | |
91 ArrayList<LexiconEntry> result = new ArrayList<LexiconEntry>(); | |
92 if (entries != null) { | |
93 Enumeration<String> entryKeys = entries.keys(); | |
94 while(entryKeys.hasMoreElements()) { | |
95 String entryKey = entryKeys.nextElement(); | |
96 LexiconEntry le = entries.get(entryKey); | |
97 result.add(le); | |
98 } | |
99 } | |
100 Collections.sort(result); | |
101 if (result.isEmpty()) | |
102 return null; | |
103 else | |
104 return result; | |
105 } | |
106 | |
107 public LexiconEntry getDynamicEntry(String formName) throws ApplicationException { | |
108 LexiconEntry lexEntry = new LexiconEntry(name, formName, null); | |
109 String linkForm = formName; | |
110 if (Language.getInstance().isGreek(sourceLang)) { | |
111 linkForm = Transcoder.getInstance().transcodeFromUnicode2BetaCode(formName); | |
112 } | |
113 if (name.equals("linyutan")) { | |
114 linkForm = Transcoder.getInstance().encodeBig5(formName); | |
115 } | |
116 String remoteUrl = queryUrl + linkForm; | |
117 lexEntry.setRemoteUrl(remoteUrl); | |
118 return lexEntry; | |
119 } | |
120 | |
121 public boolean isEmpty() { | |
122 if (entries == null || entries.isEmpty()) | |
123 return true; | |
124 else | |
125 return false; | |
126 } | |
127 | |
128 public void addEntry(LexiconEntry newEntry) { | |
129 if (entries == null) | |
130 this.entries = new Hashtable<String, LexiconEntry>(); | |
131 entries.put(newEntry.getFormName(), newEntry); | |
132 } | |
133 | |
134 public void addEntries(ArrayList<LexiconEntry> newEntries) { | |
135 if (entries == null) | |
136 this.entries = new Hashtable<String, LexiconEntry>(); | |
137 for (int i=0; i<newEntries.size(); i++) { | |
138 LexiconEntry newEntry = newEntries.get(i); | |
139 entries.put(newEntry.getFormName(), newEntry); | |
140 } | |
141 } | |
142 | |
143 /* | |
144 * without lexicon entries (non-Javadoc) | |
145 * @see java.lang.Object#clone() | |
146 */ | |
147 public Lexicon clone() { | |
148 Lexicon lex = new Lexicon(name, sourceLang); | |
149 lex.description = description; | |
150 lex.entries = new Hashtable<String, LexiconEntry>(); | |
151 lex.queryUrl = queryUrl; | |
152 lex.type = type; | |
153 return lex; | |
154 } | |
155 | |
156 public String toXmlString() { | |
157 String result = ""; | |
158 result = result + "<dictionary>"; | |
159 result = result + "<name>" + name + "</name>"; | |
160 result = result + "<description>" + description + "</description>"; | |
161 result = result + "<entries>"; | |
162 for (int i=0; i<entries.size(); i++) { | |
163 result = result + "<entry>"; | |
164 LexiconEntry entry = getEntries().get(i); | |
165 result = result + "<form>" + entry.getFormName() + "</form>"; | |
166 if (isLocalLexicon()) { | |
167 result = result + "<xml-valid>"; | |
168 String xmlValid = "false"; | |
169 if (entry.isXmlValid()) | |
170 xmlValid = "true"; | |
171 result = result + xmlValid; | |
172 result = result + "</xml-valid>"; | |
173 result = result + "<content>"; | |
174 if (entry.isXmlValid()) { | |
175 String repairedEntry = entry.getRepairedEntry(); | |
176 repairedEntry = repairedEntry.replaceAll("<repaired-entry>", ""); | |
177 repairedEntry = repairedEntry.replaceAll("</repaired-entry>", ""); | |
178 result = result + repairedEntry; // unicode content of the original entry | |
179 } else { | |
180 result = result + "<remark>This dictionary entry has no valid XML/HTML content in database so a text version of this entry is shown</remark>"; | |
181 String originalEntry = entry.getOriginalEntry(); // original content: not valid and e.g. in Betacode | |
182 originalEntry = originalEntry.replaceAll("<original-entry>", ""); | |
183 originalEntry = originalEntry.replaceAll("</original-entry>", ""); | |
184 originalEntry = StringEscapeUtils.escapeXml(originalEntry); // create text version of the invalid xml content | |
185 result = result + originalEntry; | |
186 } | |
187 result = result + "</content>"; | |
188 } | |
189 if (entry.getRemoteUrl() != null) | |
190 result = result + "<remoteUrl>" + entry.getRemoteUrl() + "</remoteUrl>"; | |
191 result = result + "</entry>"; | |
192 } | |
193 result = result + "</entries>"; | |
194 result = result + "</dictionary>"; | |
195 return result; | |
196 } | |
197 | |
198 } |