view software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/app/MorphFileReaderContentHandler.java @ 19:4a3641ae14d2

Erstellung
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Wed, 09 Nov 2011 15:32:05 +0100
parents
children
line wrap: on
line source

package de.mpg.mpiwg.berlin.mpdl.lt.morph.app;

import java.util.Hashtable;

import org.xml.sax.*;

import de.mpg.mpiwg.berlin.mpdl.lt.morph.app.Form;
import de.mpg.mpiwg.berlin.mpdl.lt.morph.app.Lemma;

public class MorphFileReaderContentHandler implements ContentHandler {
  private Hashtable<String, Form> forms;
  private Hashtable<String, Lemma> lemmas;
  private Element currentElement;
  private Form currentForm;
  
  public MorphFileReaderContentHandler(Hashtable<String, Form> forms, Hashtable<String, Lemma> lemmas) {
    this.forms = forms;
    this.lemmas = lemmas;
  }
  
  public void startDocument() throws SAXException {
  }

  public void endDocument() throws SAXException {
  }
  
  public void characters(char[] c, int start, int length) throws SAXException {
    if (currentElement != null) {
      String elemName = currentElement.name;
      if (currentForm != null) {
        char[] cCopy = new char[length];
        System.arraycopy(c, start, cCopy, 0, length);
        String charactersStr = String.valueOf(cCopy);
        if (charactersStr != null && ! (charactersStr.trim().equals(""))) {
          if (elemName.equals("provider")) {
            currentForm.setProvider(charactersStr);
          } else if (elemName.equals("language")) {
            currentForm.setLanguage(charactersStr);
          } else if (elemName.equals("form-name")) {
            currentForm.setFormName(charactersStr);
          } else if (elemName.equals("lemma-name")) {
            currentForm.setLemmaName(charactersStr);
          } else if (elemName.equals("pos")) {
            currentForm.setPos(charactersStr);
          } else if (elemName.equals("tense")) {
            currentForm.setTense(charactersStr);
          } else if (elemName.equals("voice")) {
            currentForm.setVoice(charactersStr);
          } else if (elemName.equals("casus")) {
            currentForm.setCasus(charactersStr);
          } else if (elemName.equals("number")) {
            currentForm.setNumber(charactersStr);
          } else if (elemName.equals("mood")) {
            currentForm.setMood(charactersStr);
          } else if (elemName.equals("person")) {
            currentForm.setPerson(charactersStr);
          } else if (elemName.equals("gender")) {
            currentForm.setGender(charactersStr);
          } else if (elemName.equals("definite")) {
            currentForm.setDefinite(charactersStr);
          }
        }
      }
    } 
  }

  public void ignorableWhitespace(char[] c, int start, int length) throws SAXException {
  }

  public void processingInstruction(String target, String data) throws SAXException {
  }

  public void setDocumentLocator(org.xml.sax.Locator arg1) {
  }

  public void endPrefixMapping(String prefix) throws SAXException {
  }

  public void skippedEntity(String name) throws SAXException {
  }

  public void endElement(String uri, String localName, String name) throws SAXException {
    currentElement = null;
    if (name.equals("form")) {
      String provider = currentForm.getProvider();
      String language = currentForm.getLanguage();
      String formName = currentForm.getFormName();
      String lemmaName = currentForm.getLemmaName();
      String formKey = language + "###" + formName;
      forms.put(formKey, currentForm);
      String lemmaKey = language + "###" + lemmaName;
      Lemma lemma = lemmas.get(lemmaKey);
      if(lemma == null) {
        Lemma l = new Lemma(provider, language, lemmaName);
        l.addForm(currentForm);
        lemmas.put(lemmaKey, l);
      } else {
        lemma.addForm(currentForm);
      }
      currentForm = null;
    }
  }

  public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
    currentElement = new Element(name);
    if (name.equals("form")) {
      currentForm = new Form();
    }
  }

  public void startPrefixMapping(String prefix, String uri) throws SAXException {
  }
  
  private class Element {
    String name;
    String value;
    
    Element(String name) {
      this.name = name;
    }

    Element(String name, String value) {
      this.name = name;
      this.value = value;
    }
  }
}