def _parse(self, is_source, lang_rules):
        """
        Parses Qt file and exports all entries as GenericTranslations.
        """
        def clj(s, w):
            return s[:w].replace("\n", " ").ljust(w)

        if lang_rules:
            nplural = len(lang_rules)
        else:
            nplural = self.language.get_pluralrules_numbers()

        try:
            doc = xml.dom.minidom.parseString(
                self.content.encode(self.format_encoding)
            )
        except Exception, e:
            logger.warning("QT parsing: %s" % e.message, exc_info=True)
            raise LinguistParseError(_(
                "Your file doesn't seem to contain valid xml: %s!" % e.message
            ))
        if hasattr(doc, 'doctype') and hasattr(doc.doctype, 'name'):
            if doc.doctype.name != "TS":
                raise LinguistParseError(_("Incorrect doctype!"))
        else:
            raise LinguistParseError(_("Uploaded file has no Doctype!"))
        root = doc.documentElement
        if root.tagName != "TS":
            raise LinguistParseError(_("Root element is not 'TS'"))

        # This needed to be commented out due the 'is_source' parameter.
        # When is_source=True we return the value of the <source> node as the
        # translation for the given file, instead of the <translation> node(s).
        #stringset.target_language = language
        #language = get_attribute(root, "language", die = STRICT)

        i = 1
        # There can be many <message> elements, they might have
        # 'encoding' or 'numerus' = 'yes' | 'no' attributes
        # if 'numerus' = 'yes' then 'translation' element contains 'numerusform' elements
        for context in root.getElementsByTagName("context"):
            context_name_element = _getElementByTagName(context, "name")
            if context_name_element.firstChild:
                if context_name_element.firstChild.nodeValue:
                    context_name = escape_context(
                        [context_name_element.firstChild.nodeValue])
                else:
                    context_name = []
            else:
                context_name = []

            for message in context.getElementsByTagName("message"):
                occurrences = []

                # NB! There can be zero to many <location> elements, but all
                # of them must have 'filename' and 'line' attributes
                for location in message.getElementsByTagName("location"):
                    if location.attributes.has_key("filename") and \
                        location.attributes.has_key("line"):
                        occurrences.append("%s:%i" % (
                            location.attributes["filename"].value,
                            int(location.attributes["line"].value)))
                    elif STRICT:
                        raise LinguistParseError(_("Malformed 'location' element"))

                pluralized = False
                if message.attributes.has_key("numerus") and \
                    message.attributes['numerus'].value=='yes':
                    pluralized = True

                source = _getElementByTagName(message, "source")
                try:
                    translation = _getElementByTagName(message, "translation")
                except LinguistParseError:
                    translation = None
                try:
                    ec_node = _getElementByTagName(message, "extracomment")
                    extracomment = _getText(ec_node.childNodes)
                except LinguistParseError, e:
                    extracomment = None

                # <commend> in ts files are also used to distinguish entries,
                # so we append it to the context to make the entry unique
                try:
                    c_node = _getElementByTagName(message, "comment")
                    comment_text = _getText(c_node.childNodes)
                    if comment_text:
                        comment = escape_context([comment_text])
                    else:
                        comment = []
                except LinguistParseError, e:
                    comment = []

                status = None
                if source.firstChild:
                    sourceString = _getText(source.childNodes)
                else:
                    sourceString = None # WTF?

                # Check whether the message is using logical id
                if message.attributes.has_key("id"):
                    sourceStringText = sourceString
                    sourceString = message.attributes['id'].value
                else:
                    sourceStringText = None

                same_nplural = True
                obsolete, fuzzy = False, False
                messages = []

                if is_source:
                    if translation and translation.attributes.has_key("variants") and \
                      translation.attributes['variants'].value == 'yes':
                        logger.error("Source file has unsupported"
                            " variants.")
                        raise LinguistParseError(_("Qt Linguist variants are"
                            " not yet supported."))

                    # Skip obsolete strings.
                    if translation and translation.attributes.has_key("type"):
                        status = translation.attributes["type"].value.lower()
                        if status == "obsolete":
                            continue

                    translation_text = None
                    if translation:
                        translation_text = _getText(translation.childNodes)
                    messages = [(5, translation_text or sourceStringText or sourceString)]
                    # remove unfinished/obsolete attrs from template
                    if translation and translation.attributes.has_key("type"):
                        status = translation.attributes["type"].value.lower()
                        if status == "unfinished":
                            del translation.attributes["type"]
                    if pluralized:
                        if translation:
                            try:
                                numerusforms = translation.getElementsByTagName('numerusform')
                                messages = []
                                for n,f in enumerate(numerusforms):
                                    if numerusforms[n].attributes.has_key("variants") and \
                                      numerusforms[n].attributes['variants'].value == 'yes':
                                        logger.error("Source file has unsupported"
                                            " variants.")
                                        raise LinguistParseError(_("Source file"
                                            " could not be imported: Qt Linguist"
                                            " variants are not supported."))
                                for n,f in enumerate(numerusforms):
                                    if numerusforms[n].attributes.has_key("variants") and \
                                      numerusforms[n].attributes['variants'].value == 'yes':
                                        continue
                                for n,f in enumerate(numerusforms):
                                    nf=numerusforms[n]
                                    messages.append((nplural[n], _getText(nf.childNodes)
                                        or sourceStringText or sourceString ))
                            except LinguistParseError, e:
                                pass
                        else:
                            plural_numbers = self.language.get_pluralrules_numbers()
                            for p in plural_numbers:
                                if p != 5:
                                    messages.append((p, sourceStringText or sourceString))

                elif translation and translation.firstChild:
                    # For messages with variants set to 'yes', we skip them
                    # altogether. We can't support variants at the momment...
                    if translation.attributes.has_key("variants") and \
                      translation.attributes['variants'].value == 'yes':
                        continue

                    # Skip obsolete strings.
                    if translation.attributes.has_key("type"):
                        status = translation.attributes["type"].value.lower()
                        if status == "obsolete":
                            continue

                    if translation.attributes.has_key("type"):
                        status = translation.attributes["type"].value.lower()
                        if status == "unfinished" and\
                          not pluralized:
                            suggestion = GenericTranslation(sourceString,
                                _getText(translation.childNodes),
                                context=context_name + comment,
                                occurrences= ";".join(occurrences))
                            self.suggestions.strings.append(suggestion)
                        else:
                            logger.error("Element 'translation' attribute "\
                                "'type' is neither 'unfinished' nor 'obsolete'")

                        continue

                    if not pluralized:
                        messages = [(5, _getText(translation.childNodes))]
                    else:
                        numerusforms = translation.getElementsByTagName('numerusform')
                        try:
                            for n,f  in enumerate(numerusforms):
                                if numerusforms[n].attributes.has_key("variants") and \
                                  numerusforms[n].attributes['variants'].value == 'yes':
                                    raise StopIteration
                        except __HOLE__:
                            continue
                        if nplural:
                            nplural_file = len(numerusforms)
                            if len(nplural) != nplural_file:
                                logger.error("Passed plural rules has nplurals=%s"
                                    ", but '%s' file has nplurals=%s. String '%s'"
                                    "skipped." % (nplural, self.filename,
                                     nplural_file, sourceString))
                                same_nplural = False
                        else:
                            same_nplural = False

                        if not same_nplural:
                            # If we're missing plurals, skip them altogether
                            continue

                        for n,f  in enumerate(numerusforms):
                            nf=numerusforms[n]
                            if nf.firstChild:
                                messages.append((nplural[n], _getText(nf.childNodes)))

                    # NB! If <translation> doesn't have type attribute, it means that string is finished

                if sourceString and messages:
                    for msg in messages:
                        self._add_translation_string(
                            sourceString, msg[1],
                            context = context_name + comment, rule=msg[0],
                            occurrences = ";".join(occurrences),
                            pluralized=pluralized, fuzzy=fuzzy,
                            comment=extracomment, obsolete=obsolete)
                i += 1

                if is_source:
                    if sourceString is None:
                        continue
                    if message.attributes.has_key("numerus") and \
                        message.attributes['numerus'].value=='yes' and translation:
                            numerusforms = translation.getElementsByTagName('numerusform')
                            for n,f in enumerate(numerusforms):
                                f.appendChild(doc.createTextNode(
                                        "%(hash)s_pl_%(key)s" %
                                        {
                                            'hash': hash_tag(sourceString,
                                                context_name + comment),
                                            'key': n
                                        }
                                ))
                    else:
                        if not translation:
                            translation = doc.createElement("translation")

                        # Delete all child nodes. This is usefull for xml like
                        # strings (eg html) where the translation text is split
                        # in multiple nodes.
                        translation.childNodes = []

                        translation.appendChild(doc.createTextNode(
                                ("%(hash)s_tr" % {'hash': hash_tag(
                                    sourceString, context_name + comment)})
                        ))
        return doc
dataset/ETHPy150Open rvanlaar/easy-transifex/src/transifex/transifex/resources/formats/qt.py/LinguistHandler._parse