2019-11-11 22:04:45 +00:00
|
|
|
from browser import document
|
|
|
|
|
|
|
|
|
|
|
|
def export_to_xml(model):
|
|
|
|
xml_document = export_entry(model.entry)
|
|
|
|
serializer = __new__(XMLSerializer())
|
|
|
|
return serializer.serializeToString(xml_document);
|
|
|
|
|
|
|
|
|
|
|
|
def export_entry(entry):
|
|
|
|
parser = __new__(DOMParser())
|
|
|
|
doc = parser.parseFromString("<entry />", "text/xml")
|
|
|
|
|
|
|
|
entry_xml = doc.firstChild
|
|
|
|
|
|
|
|
# create head
|
|
|
|
head = doc.createElement("head")
|
|
|
|
entry_xml.appendChild(head)
|
|
|
|
|
|
|
|
status = doc.createElement("status")
|
|
|
|
status.textContent = entry.status
|
|
|
|
head.appendChild(status)
|
|
|
|
|
|
|
|
headword = doc.createElement("headword")
|
|
|
|
headword_lemma = doc.createElement("lemma")
|
|
|
|
headword_lemma.textContent = entry.headword
|
|
|
|
headword.appendChild(headword_lemma)
|
|
|
|
head.appendChild(headword)
|
|
|
|
|
|
|
|
grammar = doc.createElement("grammar")
|
|
|
|
grammar_category = doc.createElement("category")
|
|
|
|
grammar_category.textContent = entry.grammar
|
|
|
|
grammar.appendChild(grammar_category)
|
|
|
|
head.appendChild(grammar)
|
|
|
|
|
|
|
|
comment = doc.createElement("comment")
|
|
|
|
comment.textContent = entry.comment
|
|
|
|
head.appendChild(comment)
|
|
|
|
|
|
|
|
# now lets do body
|
|
|
|
body = doc.createElement("body")
|
|
|
|
entry_xml.appendChild(body)
|
|
|
|
|
2019-11-13 22:16:28 +00:00
|
|
|
sense_list = doc.createElement("senseList")
|
2019-11-11 22:04:45 +00:00
|
|
|
body.appendChild(sense_list)
|
|
|
|
|
|
|
|
for sense in entry.senses:
|
|
|
|
sense_list.appendChild(export_sense(doc, sense))
|
|
|
|
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
def export_sense(doc, sense):
|
|
|
|
sense_xml = doc.createElement("sense")
|
|
|
|
|
|
|
|
label_list = doc.createElement("labelList")
|
|
|
|
sense_xml.appendChild(label_list)
|
|
|
|
|
2019-11-11 23:34:52 +00:00
|
|
|
for label in sense.labels:
|
|
|
|
label_xml = doc.createElement("label")
|
|
|
|
label_xml.textContent = label
|
|
|
|
label_list.appendChild(label_xml)
|
2019-11-11 22:04:45 +00:00
|
|
|
|
|
|
|
definition_list = doc.createElement("definitionList")
|
|
|
|
sense_xml.appendChild(definition_list)
|
|
|
|
|
|
|
|
definition = doc.createElement("definition")
|
|
|
|
definition.textContent = sense.definition
|
|
|
|
definition.setAttribute("type", "indicator")
|
|
|
|
definition_list.appendChild(definition)
|
|
|
|
|
|
|
|
translation_container_list = doc.createElement("translationContainerList")
|
|
|
|
sense_xml.appendChild(translation_container_list)
|
|
|
|
|
|
|
|
for cidx, cluster in enumerate(sense.translations):
|
|
|
|
for translation in cluster:
|
|
|
|
translation_container = export_translation(doc, translation)
|
|
|
|
translation_container.setAttribute("cluster", str(cidx + 1))
|
|
|
|
translation_container_list.appendChild(translation_container)
|
|
|
|
|
2019-11-13 22:16:28 +00:00
|
|
|
example_container_list = doc.createElement("exampleContainerList")
|
|
|
|
sense_xml.appendChild(example_container_list)
|
|
|
|
|
|
|
|
for example in sense.examples:
|
|
|
|
example_container = doc.createElement("exampleContainer")
|
|
|
|
example_container_list.appendChild(example_container)
|
|
|
|
|
|
|
|
example_xml = doc.createElement("example")
|
|
|
|
example_xml.textContent = example.example
|
|
|
|
example_container.appendChild(example_xml)
|
|
|
|
|
|
|
|
translation_container = doc.createElement("translationContainer")
|
|
|
|
example_container.appendChild(translation_container)
|
|
|
|
|
|
|
|
translation = doc.createElement("translation")
|
|
|
|
translation.textContent = example.translation
|
|
|
|
translation_container.appendChild(translation)
|
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
return sense_xml
|
|
|
|
|
|
|
|
def export_translation(doc, translation):
|
|
|
|
translation_xml = doc.createElement("translationContainer")
|
|
|
|
|
|
|
|
actual_t = doc.createElement("translation")
|
|
|
|
actual_t.textContent = translation.translation
|
|
|
|
translation_xml.appendChild(actual_t)
|
|
|
|
|
|
|
|
tags = doc.createElement("tagsContainer")
|
|
|
|
translation_xml.appendChild(tags)
|
|
|
|
|
|
|
|
for tagname, tagvalue in translation.tags.items():
|
|
|
|
name_el = doc.createElement("type")
|
|
|
|
name_el.textContent = tagname
|
|
|
|
value_el = doc.createElement("value")
|
|
|
|
value_el.textContent = tagvalue
|
|
|
|
|
|
|
|
tag_el = doc.createElement("tag")
|
|
|
|
tag_el.appendChild(name_el)
|
|
|
|
tag_el.appendChild(value_el)
|
|
|
|
|
|
|
|
tags.appendChild(tag_el)
|
|
|
|
|
|
|
|
return translation_xml
|
|
|
|
|
|
|
|
|