comparison software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/db/DBMorphHandler.java @ 23:e845310098ba

diverse Korrekturen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Nov 2012 12:35:19 +0100
parents 4a3641ae14d2
children
comparison
equal deleted inserted replaced
22:6a45a982c333 23:e845310098ba
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.io.Reader; 4 import java.io.Reader;
5 import java.io.StringReader; 5 import java.io.StringReader;
6 import java.io.UnsupportedEncodingException; 6 import java.io.UnsupportedEncodingException;
7 import java.util.ArrayList; 7 import java.util.ArrayList;
8 import java.util.Hashtable;
9 8
10 import org.xml.sax.InputSource; 9 import org.xml.sax.InputSource;
11 import org.xml.sax.SAXException; 10 import org.xml.sax.SAXException;
12 import org.xml.sax.XMLReader; 11 import org.xml.sax.XMLReader;
13 12
31 30
32 public DBMorphHandler(String dbDir) { 31 public DBMorphHandler(String dbDir) {
33 this.dbDirectory = dbDir; 32 this.dbDirectory = dbDir;
34 } 33 }
35 34
35 public void startReadOnly() throws ApplicationException {
36 morphDbEnv = new DbEnvMorph();
37 morphDbEnv.setDataDir(dbDirectory);
38 morphDbEnv.initReadOnly(); // open databases in read only mode
39 }
40
36 public void start() throws ApplicationException { 41 public void start() throws ApplicationException {
37 morphDbEnv = new DbEnvMorph(); 42 morphDbEnv = new DbEnvMorph();
38 morphDbEnv.setDataDir(dbDirectory); 43 morphDbEnv.setDataDir(dbDirectory);
39 morphDbEnv.init(); // open databases in read/write mode 44 morphDbEnv.init(); // open databases in read/write mode
40 } 45 }
132 Cursor cursor = lemmaDB.openCursor(null, null); 137 Cursor cursor = lemmaDB.openCursor(null, null);
133 byte[] bHashKey = hashKey.getBytes("utf-8"); 138 byte[] bHashKey = hashKey.getBytes("utf-8");
134 DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey); 139 DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey);
135 DatabaseEntry foundFormValue = new DatabaseEntry(); 140 DatabaseEntry foundFormValue = new DatabaseEntry();
136 OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundFormValue, LockMode.DEFAULT); 141 OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundFormValue, LockMode.DEFAULT);
142 StringBuilder formsXmlString = new StringBuilder();
143 formsXmlString.append("<forms>");
137 while (operationStatus == OperationStatus.SUCCESS) { 144 while (operationStatus == OperationStatus.SUCCESS) {
138 byte[] foundFormValueBytes = foundFormValue.getData(); 145 byte[] foundFormValueBytes = foundFormValue.getData();
139 String foundFormValueStr = new String(foundFormValueBytes, "utf-8"); 146 String foundFormValueStr = new String(foundFormValueBytes, "utf-8");
140 Form f = parseXmlFormString(foundFormValueStr); 147 formsXmlString.append(foundFormValueStr);
141 retForms.add(f);
142 operationStatus = cursor.getNextDup(dbEntryKey, foundFormValue, LockMode.DEFAULT); 148 operationStatus = cursor.getNextDup(dbEntryKey, foundFormValue, LockMode.DEFAULT);
143 } 149 }
150 formsXmlString.append("</forms>");
151 retForms = parseXmlFormsString(formsXmlString.toString());
144 cursor.close(); 152 cursor.close();
145 } catch (DatabaseException e) { 153 } catch (DatabaseException e) {
146 throw new ApplicationException(e); 154 throw new ApplicationException(e);
147 } catch (UnsupportedEncodingException e) { 155 } catch (UnsupportedEncodingException e) {
148 throw new ApplicationException(e); 156 throw new ApplicationException(e);
149 } 157 }
150 return retForms; 158 return retForms;
151 } 159 }
152 160
153 // TODO diese Methode wird nicht verwendet bis jetzt
154 public Hashtable<String, Form> readForms() throws ApplicationException {
155 Hashtable<String, Form> retForms = new Hashtable<String, Form>();
156 try {
157 Database lemmaDB = morphDbEnv.getLemmaDB();
158 Cursor cursor = lemmaDB.openCursor(null, null);
159 DatabaseEntry dbEntryKey = new DatabaseEntry();
160 DatabaseEntry foundFormValue = new DatabaseEntry();
161 OperationStatus operationStatus = cursor.getFirst(dbEntryKey, foundFormValue, LockMode.DEFAULT);
162 while (operationStatus == OperationStatus.SUCCESS) {
163 byte[] foundFormValueBytes = foundFormValue.getData();
164 String foundFormValueStr = new String(foundFormValueBytes, "utf-8");
165 Form f = parseXmlFormString(foundFormValueStr);
166 String formHashKey = f.getLanguage() + "###" + f.getFormName();
167 retForms.put(formHashKey, f);
168 operationStatus = cursor.getNext(dbEntryKey, foundFormValue, LockMode.DEFAULT);
169 }
170 cursor.close();
171 } catch (DatabaseException e) {
172 throw new ApplicationException(e);
173 } catch (UnsupportedEncodingException e) {
174 throw new ApplicationException(e);
175 }
176 return retForms;
177 }
178
179 public ArrayList<Lemma> readLemmas(String language, String formName) throws ApplicationException { 161 public ArrayList<Lemma> readLemmas(String language, String formName) throws ApplicationException {
180 ArrayList<Lemma> retForms = new ArrayList<Lemma>(); 162 ArrayList<Lemma> retLemmas = new ArrayList<Lemma>();
181 String lang = Language.getInstance().getLanguageId(language); 163 String lang = Language.getInstance().getLanguageId(language);
182 String hashKey = lang + "###" + formName; 164 String hashKey = lang + "###" + formName;
183 try { 165 try {
184 Database formDB = morphDbEnv.getFormDB(); 166 Database formDB = morphDbEnv.getFormDB();
185 Cursor cursor = formDB.openCursor(null, null); 167 Cursor cursor = formDB.openCursor(null, null);
189 OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundLemmaValue, LockMode.DEFAULT); 171 OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundLemmaValue, LockMode.DEFAULT);
190 while (operationStatus == OperationStatus.SUCCESS) { 172 while (operationStatus == OperationStatus.SUCCESS) {
191 byte[] foundLemmaValueBytes = foundLemmaValue.getData(); 173 byte[] foundLemmaValueBytes = foundLemmaValue.getData();
192 String foundLemmaValueStr = new String(foundLemmaValueBytes, "utf-8"); 174 String foundLemmaValueStr = new String(foundLemmaValueBytes, "utf-8");
193 Lemma l = parseXmlLemmaString(foundLemmaValueStr); 175 Lemma l = parseXmlLemmaString(foundLemmaValueStr);
194 retForms.add(l); 176 retLemmas.add(l);
195 operationStatus = cursor.getNextDup(dbEntryKey, foundLemmaValue, LockMode.DEFAULT); 177 operationStatus = cursor.getNextDup(dbEntryKey, foundLemmaValue, LockMode.DEFAULT);
196 } 178 }
197 cursor.close(); 179 cursor.close();
198 } catch (DatabaseException e) { 180 } catch (DatabaseException e) {
199 throw new ApplicationException(e); 181 throw new ApplicationException(e);
200 } catch (UnsupportedEncodingException e) { 182 } catch (UnsupportedEncodingException e) {
201 throw new ApplicationException(e); 183 throw new ApplicationException(e);
202 } 184 }
203 return retForms; 185 return retLemmas;
204 } 186 }
205 187
206 private Form parseXmlFormString(String xmlString) throws ApplicationException { 188 private ArrayList<Form> parseXmlFormsString(String xmlString) throws ApplicationException {
207 Form form = null; 189 ArrayList<Form> forms = null;
208 try { 190 try {
209 XMLReader xmlParser = new SAXParser(); 191 XMLReader xmlParser = new SAXParser();
210 SimpleMorphContentHandler morphContentHandler = new SimpleMorphContentHandler(); 192 SimpleMorphContentHandler morphContentHandler = new SimpleMorphContentHandler();
211 xmlParser.setContentHandler(morphContentHandler); 193 xmlParser.setContentHandler(morphContentHandler);
212 Reader reader = new StringReader(xmlString); 194 Reader reader = new StringReader(xmlString);
213 InputSource input = new InputSource(reader); 195 InputSource input = new InputSource(reader);
214 xmlParser.parse(input); 196 xmlParser.parse(input);
215 form = morphContentHandler.getForm(); 197 forms = morphContentHandler.getForms();
216 } catch (SAXException e) { 198 } catch (SAXException e) {
217 throw new ApplicationException(e); 199 throw new ApplicationException(e);
218 } catch (IOException e) { 200 } catch (IOException e) {
219 throw new ApplicationException(e); 201 throw new ApplicationException(e);
220 } 202 }
221 return form; 203 return forms;
222 } 204 }
223 205
224 private Lemma parseXmlLemmaString(String xmlString) throws ApplicationException { 206 private Lemma parseXmlLemmaString(String xmlString) throws ApplicationException {
225 Lemma lemma = null; 207 Lemma lemma = null;
226 try { 208 try {