diff 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
line wrap: on
line diff
--- a/software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/db/DBMorphHandler.java	Wed Dec 14 13:57:09 2011 +0100
+++ b/software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/db/DBMorphHandler.java	Tue Nov 27 12:35:19 2012 +0100
@@ -5,7 +5,6 @@
 import java.io.StringReader;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
-import java.util.Hashtable;
 
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
@@ -33,6 +32,12 @@
     this.dbDirectory = dbDir;
   }
   
+  public void startReadOnly() throws ApplicationException {
+    morphDbEnv = new DbEnvMorph();
+    morphDbEnv.setDataDir(dbDirectory);
+    morphDbEnv.initReadOnly(); // open databases in read only mode
+  }
+  
   public void start() throws ApplicationException {
     morphDbEnv = new DbEnvMorph();
     morphDbEnv.setDataDir(dbDirectory);
@@ -134,39 +139,16 @@
       DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey);
       DatabaseEntry foundFormValue = new DatabaseEntry();
       OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundFormValue, LockMode.DEFAULT);
+      StringBuilder formsXmlString = new StringBuilder();
+      formsXmlString.append("<forms>");
       while (operationStatus == OperationStatus.SUCCESS) {
         byte[] foundFormValueBytes = foundFormValue.getData();
         String foundFormValueStr = new String(foundFormValueBytes, "utf-8");
-        Form f = parseXmlFormString(foundFormValueStr);
-        retForms.add(f);
+        formsXmlString.append(foundFormValueStr);
         operationStatus = cursor.getNextDup(dbEntryKey, foundFormValue, LockMode.DEFAULT);
       }
-      cursor.close();
-    } catch (DatabaseException e) {
-      throw new ApplicationException(e);
-    } catch (UnsupportedEncodingException e) {
-      throw new ApplicationException(e);
-    }
-    return retForms;
-  }
-  
-  // TODO diese Methode wird nicht verwendet bis jetzt
-  public Hashtable<String, Form> readForms() throws ApplicationException {
-    Hashtable<String, Form> retForms = new Hashtable<String, Form>();
-    try {
-      Database lemmaDB = morphDbEnv.getLemmaDB();
-      Cursor cursor = lemmaDB.openCursor(null, null);
-      DatabaseEntry dbEntryKey = new DatabaseEntry();
-      DatabaseEntry foundFormValue = new DatabaseEntry();
-      OperationStatus operationStatus = cursor.getFirst(dbEntryKey, foundFormValue, LockMode.DEFAULT);
-      while (operationStatus == OperationStatus.SUCCESS) {
-        byte[] foundFormValueBytes = foundFormValue.getData();
-        String foundFormValueStr = new String(foundFormValueBytes, "utf-8");
-        Form f = parseXmlFormString(foundFormValueStr);
-        String formHashKey = f.getLanguage() + "###" + f.getFormName();
-        retForms.put(formHashKey, f);
-        operationStatus = cursor.getNext(dbEntryKey, foundFormValue, LockMode.DEFAULT);
-      }
+      formsXmlString.append("</forms>");
+      retForms = parseXmlFormsString(formsXmlString.toString());
       cursor.close();
     } catch (DatabaseException e) {
       throw new ApplicationException(e);
@@ -177,7 +159,7 @@
   }
   
   public ArrayList<Lemma> readLemmas(String language, String formName) throws ApplicationException {
-    ArrayList<Lemma> retForms = new ArrayList<Lemma>();
+    ArrayList<Lemma> retLemmas = new ArrayList<Lemma>();
     String lang = Language.getInstance().getLanguageId(language);
     String hashKey = lang + "###" + formName;
     try {
@@ -191,7 +173,7 @@
         byte[] foundLemmaValueBytes = foundLemmaValue.getData();
         String foundLemmaValueStr = new String(foundLemmaValueBytes, "utf-8");
         Lemma l = parseXmlLemmaString(foundLemmaValueStr);
-        retForms.add(l);
+        retLemmas.add(l);
         operationStatus = cursor.getNextDup(dbEntryKey, foundLemmaValue, LockMode.DEFAULT);
       }
       cursor.close();
@@ -200,11 +182,11 @@
     } catch (UnsupportedEncodingException e) {
       throw new ApplicationException(e);
     }
-    return retForms;
+    return retLemmas;
   }
   
-  private Form parseXmlFormString(String xmlString) throws ApplicationException {
-    Form form = null;
+  private ArrayList<Form> parseXmlFormsString(String xmlString) throws ApplicationException {
+    ArrayList<Form> forms = null;
     try {
       XMLReader xmlParser = new SAXParser();
       SimpleMorphContentHandler morphContentHandler = new SimpleMorphContentHandler();
@@ -212,13 +194,13 @@
       Reader reader = new StringReader(xmlString);
       InputSource input = new InputSource(reader);
       xmlParser.parse(input);
-      form = morphContentHandler.getForm();
+      forms = morphContentHandler.getForms();
     } catch (SAXException e) {
       throw new ApplicationException(e);
     } catch (IOException e) {
       throw new ApplicationException(e);
     }
-    return form;
+    return forms;
   }
 
   private Lemma parseXmlLemmaString(String xmlString) throws ApplicationException {