view software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/donatus/xmlrpc/DonatusMorphologyDocumentContentHandler.java @ 0:408254cf2f1d

Erstellung
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Wed, 24 Nov 2010 17:24:23 +0100
parents
children
line wrap: on
line source

package de.mpg.mpiwg.berlin.mpdl.donatus.xmlrpc;

import org.xml.sax.*;

public class DonatusMorphologyDocumentContentHandler implements ContentHandler {
  private String docUri;
  private DonatusMorphologyDocument result;
  private String language;
  private Element currentElement;
  private DonatusLemma currentLemma;
  private DonatusVariant currentVariant;
  
  public DonatusMorphologyDocumentContentHandler(String docUri, String language) {
    this.docUri = docUri;
    this.language = language;
  }
  
  public DonatusMorphologyDocument getResult() {
    return result;
  }
  
  public void startDocument() throws SAXException {
  }

  public void endDocument() throws SAXException {
  }
  
  public void characters(char[] c, int start, int length) throws SAXException {
    if (currentElement != null && currentElement.name.equals("definition")) {
      char[] cCopy = new char[length];
      System.arraycopy(c, start, cCopy, 0, length);
      currentLemma.setDefinition(String.valueOf(cCopy));
    }
  }

  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 {
  }

  public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
    currentElement = new Element(name);
    if (name.equals("morphology")) {
      result = new DonatusMorphologyDocument(docUri);
      result.setLanguage(language);
    } else if (name.equals("lemma")) {
      String language = "";
      String form = "";
      if (attrs != null) {
        int length = attrs.getLength();
        for (int i = 0; i < length; i++) {
          String attrName = attrs.getLocalName(i);
          if (attrName.equals("form")) {
            form = attrs.getValue(i);
            if (form.matches(".*#\\d*"))
              form = form.replaceFirst("#\\d*", "");  // remove #number in the lemma form 
          } else if (attrName.equals("lang")) {
            language = attrs.getValue(i);
          }
        }
      }
      DonatusLemma morphDocLemma = result.getLemma(form);
      if (morphDocLemma == null) {
        DonatusLemma newLemma = new DonatusLemma(result, language, DonatusConstants.TYPE_DONATUS, form);
        currentLemma = newLemma;
        result.putLemma(currentLemma);
      } else {
        currentLemma = morphDocLemma;  // lemma with same form exists already in morphology document e.g. a lemma with a different #number in its form
      }
    } else if (name.equals("variant")) {
      String form = "";
      if (attrs != null) {
        int length = attrs.getLength();
        for (int i = 0; i < length; i++) {
          String attrName = attrs.getLocalName(i);
          if (attrName.equals("form"))
            form = attrs.getValue(i);
        }
      }
      DonatusVariant variant = new DonatusVariant(currentLemma, DonatusConstants.TYPE_DONATUS, form);
      currentVariant = variant;
      if (currentLemma != null)
        currentLemma.addVariant(variant);
    } else if (name.equals("analysis")) {
      String desc = "";
      String xlink = "";
      if (attrs != null) {
        int length = attrs.getLength();
        for (int i = 0; i < length; i++) {
          String attrName = attrs.getQName(i);
          if (attrName.equals("desc"))
            desc = attrs.getValue(i);
          else if (attrName.equals("xlink:type"))
            xlink = attrs.getValue(i);
        }
      }
      DonatusAnalysis analysis = new DonatusAnalysis(desc, xlink);
      if(currentVariant != null)
        currentVariant.addAnalysis(analysis);
    }
  }

  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;
    }
  }
}