comparison software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/db/DBMorphWriterContentHandler.java @ 19:4a3641ae14d2

Erstellung
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Wed, 09 Nov 2011 15:32:05 +0100
parents
children
comparison
equal deleted inserted replaced
18:dc5e9fcb3fdc 19:4a3641ae14d2
1 package de.mpg.mpiwg.berlin.mpdl.lt.morph.db;
2
3 import java.util.Hashtable;
4
5 import org.xml.sax.*;
6
7 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
8 import de.mpg.mpiwg.berlin.mpdl.lt.morph.app.Form;
9 import de.mpg.mpiwg.berlin.mpdl.lt.morph.app.Lemma;
10
11 public class DBMorphWriterContentHandler implements ContentHandler {
12 private DBMorphHandler dbMorphHandler;
13 private Element currentElement;
14 private Form form;
15 private Lemma lemma;
16 private Hashtable<String, Form> forms;
17
18 public DBMorphWriterContentHandler(DBMorphHandler dbMorphHandler) {
19 this.dbMorphHandler = dbMorphHandler;
20 }
21
22 public void startDocument() throws SAXException {
23 forms = new Hashtable<String, Form>();
24 }
25
26 public void endDocument() throws SAXException {
27 forms = null;
28 }
29
30 // TODO setPos etc. ersetzen durch addPos etc.
31 public void characters(char[] c, int start, int length) throws SAXException {
32 if (currentElement != null) {
33 String elemName = currentElement.name;
34 if (form != null) {
35 char[] cCopy = new char[length];
36 System.arraycopy(c, start, cCopy, 0, length);
37 String charactersStr = String.valueOf(cCopy);
38 if (charactersStr != null && ! (charactersStr.trim().equals(""))) {
39 if (elemName.equals("provider")) {
40 form.addProvider(charactersStr);
41 lemma.addProvider(charactersStr);
42 } else if (elemName.equals("language")) {
43 form.addLanguage(charactersStr);
44 lemma.addLanguage(charactersStr);
45 } else if (elemName.equals("form-name")) {
46 form.addFormName(charactersStr);
47 } else if (elemName.equals("lemma-name")) {
48 form.addLemmaName(charactersStr);
49 lemma.addLemmaName(charactersStr);
50 } else if (elemName.equals("pos")) {
51 form.addPos(charactersStr);
52 } else if (elemName.equals("tense")) {
53 form.addTense(charactersStr);
54 } else if (elemName.equals("voice")) {
55 form.addVoice(charactersStr);
56 } else if (elemName.equals("casus")) {
57 form.addCasus(charactersStr);
58 } else if (elemName.equals("number")) {
59 form.addNumber(charactersStr);
60 } else if (elemName.equals("mood")) {
61 form.addMood(charactersStr);
62 } else if (elemName.equals("person")) {
63 form.addPerson(charactersStr);
64 } else if (elemName.equals("gender")) {
65 form.addGender(charactersStr);
66 } else if (elemName.equals("definite")) {
67 form.addDefinite(charactersStr);
68 }
69 }
70 }
71 }
72 }
73
74 public void ignorableWhitespace(char[] c, int start, int length) throws SAXException {
75 }
76
77 public void processingInstruction(String target, String data) throws SAXException {
78 }
79
80 public void setDocumentLocator(org.xml.sax.Locator arg1) {
81 }
82
83 public void endPrefixMapping(String prefix) throws SAXException {
84 }
85
86 public void skippedEntity(String name) throws SAXException {
87 }
88
89 public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
90 currentElement = new Element(name, "");
91 if (localName.equals("form")) {
92 form = new Form();
93 lemma = new Lemma();
94 }
95 }
96
97 public void endElement(String uri, String localName, String name) throws SAXException {
98 currentElement = null;
99 if (localName.equals("form")) {
100 String keyStr = form.getFormName();
101 forms.put(keyStr, form);
102 write(form, lemma);
103 form = null;
104 lemma = null;
105 }
106 }
107
108 public void startPrefixMapping(String prefix, String uri) throws SAXException {
109 }
110
111 private void write(Form form, Lemma lemma) throws SAXException {
112 try {
113 dbMorphHandler.writeFormLemma(form, lemma);
114 dbMorphHandler.writeLemmaForm(lemma, form);
115 } catch (ApplicationException e) {
116 throw new SAXException(e);
117 }
118 }
119
120 private class Element {
121 String name;
122 String value;
123
124 Element(String name) {
125 this.name = name;
126 }
127
128 Element(String name, String value) {
129 this.name = name;
130 this.value = value;
131 }
132 }
133 }