from browser import document from model.tags import export_tag from model.translation import Translation 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("", "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 = entry.original_xml.querySelector("head headword lemma") headword_lemma.textContent = entry.headword if entry.headword_type is not None: headword_lemma.setAttribute("type", entry.headword_type) if entry.headword_audio is not None: headword_lemma.setAttribute("audio", entry.headword_audio) headword.appendChild(headword_lemma) head.appendChild(headword) homonymy = doc.createElement("homonymy") headword.appendChild(homonymy) for hFeature in entry.homonymy: feature = doc.createElement("homonymyFeature") feature.textContent = hFeature.value # Can't use hFeature.name, because Python has name reserver and so it becomes py_name in JS feature.setAttribute("name", hFeature["name"]) homonymy.appendChild(feature) # if({}) works uncorrectly in transcrypt if len(entry.lexical_unit) > 0 and len(entry.lexical_unit['lexemes']) > 0: lexunit = doc.createElement("lexicalUnit") if(entry.lexical_unit["id"]): lexunit.setAttribute("id", entry.lexical_unit["id"]) lexunit.setAttribute("type", entry.lexical_unit['type']) for lexeme in entry.lexical_unit["lexemes"]: lexeme_xml = doc.createElement("lexeme") if(lexeme["id"]): lexeme_xml.setAttribute("lexical_unit_lexeme_id", lexeme["id"]) lexeme_xml.textContent = lexeme["text"] if len(entry.lexical_unit["lexemes"]) > 1: component = doc.createElement('component') component.appendChild(lexeme_xml) lexunit.appendChild(component) else: lexunit.appendChild(lexeme_xml) head.appendChild(lexunit) grammar = doc.createElement("grammar") grammar_category = doc.createElement("category") grammar_category.textContent = entry.grammar grammar.appendChild(grammar_category) head.appendChild(grammar) if len(entry.measure) > 0: measure_list = doc.createElement("measureList") measure = doc.createElement("measure") measure.setAttribute("source", entry.measure["source"]) measure.setAttribute("type", entry.measure["type"]) measure.textContent = entry.measure["text"] measure_list.appendChild(measure) head.appendChild(measure_list) variants = doc.createElement("variantList") head.appendChild(variants) for v in entry.variants: variant = doc.createElement("variant") variant.textContent = v variants.appendChild(variant) relist = doc.createElement("relatedEntryList") head.appendChild(relist) for re in entry.related_entries: relateEntry = doc.createElement("relatedEntry") relateEntry.textContent = re relist.appendChild(relateEntry) head.appendChild(_export_label_list(doc, entry.labels)) comment = doc.createElement("comment") comment.textContent = entry.comment head.appendChild(comment) # now lets do body body = doc.createElement("body") entry_xml.appendChild(body) sense_list = doc.createElement("senseList") 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") sense_xml.appendChild(_export_label_list(doc, sense.labels)) if sense.id is not None: sense_xml.setAttribute("id", sense.id) definition_list = doc.createElement("definitionList") sense_xml.appendChild(definition_list) for definition in sense.definitions: definition_xml = doc.createElement("definition") definition_xml.textContent = definition["value"] definition_xml.setAttribute("type", definition["type"]) definition_list.appendChild(definition_xml) translation_container_list = doc.createElement("translationContainerList") export_translation_list(doc, sense, translation_container_list) sense_xml.appendChild(translation_container_list) example_container_list = doc.createElement("exampleContainerList") sense_xml.appendChild(example_container_list) for example in sense.examples: example_container = example.export(doc) translation_container_list = doc.createElement("translationContainerList") export_translation_list(doc, example, translation_container_list) example_container.appendChild(translation_container_list) example_container_list.appendChild(example_container) return sense_xml def export_translation_list(doc, py_parent, xml_parent): for cidx, cluster in enumerate(py_parent.translations): if len(cluster) > 0: for translation in cluster: translation_container = export_translation(doc, translation) translation_container.setAttribute("cluster", str(cidx + 1)) xml_parent.appendChild(translation_container) def export_translation(doc, translation): translation_xml = doc.createElement("translationContainer") translation_xml.appendChild(_export_label_list(doc, translation.tags)) actual_t = doc.createElement("translation") actual_t.textContent = translation.translation actual_t.setAttribute("targetLang", translation.targetLang) if translation.audio: actual_t.setAttribute("audio", translation.audio) if translation.source: actual_t.setAttribute("source", translation.source) translation_xml.appendChild(actual_t) if len(translation.explanationList) > 0 : explanationList = _export_explanation_list(doc, translation.explanationList) translation_xml.appendChild(explanationList) return translation_xml def _export_explanation_list(doc, lst): result = doc.createElement('explanationList') for explanation in lst: result.appendChild(explanation.export(doc)) return result def _export_label_list(doc, lst): result = doc.createElement("labelList") for key, value in lst: key, value = export_tag(key, value) label_el = doc.createElement("label") label_el.textContent = value label_el.setAttribute('type', key) result.appendChild(label_el) return result def _original_xml_query_selector(selector, entry, doc, parent_selector = selector.rsplit(' ', 1)[0]): query = entry.original_xml.querySelector(selector) if query is None: query = doc.createElement(selector.rsplit(' ', 1)[1]) entry.original_xml.querySelector(parent_selector).appendChild(query) return query def export_example_to_entry_xml(example, other_examples = None): parser = __new__(DOMParser()) doc = parser.parseFromString("", "text/xml") entry_xml = doc.firstChild head = doc.createElement("head") entry_xml.appendChild(head) status = doc.createElement("status") head.appendChild(status) headword = doc.createElement("headword") head.appendChild(headword) lemma = doc.createElement("lemma") lemma.textContent = " ".join(comp.text for comp in example.components) lemma.setAttribute("type", "compound") if example.inner.other_attributes['audio'] is not None: lemma.setAttribute('audio', example.inner.other_attributes['audio']) headword.appendChild(lemma) homonymy = doc.createElement("homonymy") headword.appendChild(homonymy) lexical_unit = doc.createElement("lexicalUnit") lexical_unit.setAttribute("type", "MWE") head.appendChild(lexical_unit) if example.inner.other_attributes['structure_id'] != None and len(example.components) <= 3: lexical_unit.setAttribute("id", example.inner.other_attributes['structure_id']) for comp in example.components: lexeme = doc.createElement("lexeme") lexeme.textContent = comp.text comp_xml = doc.createElement("component") lexical_unit.appendChild(comp_xml) comp_xml.appendChild(lexeme) grammar = doc.createElement("grammar") category = doc.createElement("category") grammar.appendChild(category) head.appendChild(grammar) variant_list = doc.createElement("variantList") head.appendChild(variant_list) related_entry_list = doc.createElement("relatedEntryList") head.appendChild(related_entry_list) label_list = doc.createElement("labelList") head.appendChild(label_list) comment = doc.createElement("comment") head.appendChild(comment) body = doc.createElement("body") entry_xml.appendChild(body) sense_list = doc.createElement("senseList") body.appendChild(sense_list) sense = doc.createElement("sense") sense_list.appendChild(sense) sense_label_list = doc.createElement("labelList") sense.appendChild(sense_label_list) first_translation = example.translations[0][0] if len(example.translations) > 0 and len(example.translations[0]) > 0 else Translation() first_translation_is_valid = False translation_label_list = doc.createElement("labelList") # Add labels to sense if label value isn't kontrastivno or približek else keep them in translation for key, value in first_translation.tags: key, value = export_tag(key, value) label_el = doc.createElement("label") label_list = translation_label_list if value == "kontrastivno" or value == "približek" else sense_label_list label_el.textContent = value label_el.setAttribute('type', key) label_list.appendChild(label_el) # Set definition as explanation if explanation in slo definition_list = doc.createElement("definitionList") sense.appendChild(definition_list) for explanation in first_translation.explanationList: if explanation.language == "slo": definition = doc.createElement("definition") definition.setAttribute("type", "indicator") definition.textContent = explanation.value definition_list.appendChild(definition) first_translation.explanationList.remove(explanation) translation_container_list = doc.createElement("translationContainerList") sense.appendChild(translation_container_list) translation_container = doc.createElement("translationContainer") if len(translation_label_list) > 0: translation_container.appendChild(translation_label_list) if first_translation.translation is not "": translation = doc.createElement("translation") translation_container.appendChild(translation) translation.textContent = first_translation.translation translation.setAttribute("targetLang", first_translation.targetLang) if first_translation.audio: translation.setAttribute("audio", first_translation.audio) if first_translation.source: translation.setAttribute("source", first_translation.source) first_translation_is_valid = True if len(first_translation.explanationList) > 0 : explanation_list = _export_explanation_list(doc, first_translation.explanationList) translation_container.appendChild(explanation_list) first_translation_is_valid = True if first_translation_is_valid: translation_container_list.appendChild(translation_container) example.translations[0] = example.translations[0][1:] if len(example.translations) > 0 and len(example.translations[0]) > 0 else example.translations[0] export_translation_list(doc, example, translation_container_list) if other_examples is not None: example_container_list = doc.createElement("exampleContainerList") sense.appendChild(example_container_list) for example in other_examples: example_container = example.export(doc) translation_container_list = doc.createElement("translationContainerList") export_translation_list(doc, example, translation_container_list) example_container.appendChild(translation_container_list) example_container_list.appendChild(example_container) return doc