void NamespaceSupport::setPrefixes(const QXmlStreamNamespaceDeclarations &declarations)
{
    for (int i = 0; i < declarations.count(); i++) {
        const QXmlStreamNamespaceDeclaration declaration = declarations.at(i);

        const QXmlName::PrefixCode prefixCode = m_namePool->allocatePrefix(declaration.prefix().toString());
        const QXmlName::NamespaceCode namespaceCode = m_namePool->allocateNamespace(declaration.namespaceUri().toString());
        m_ns.insert(prefixCode, namespaceCode);
    }
}
Ejemplo n.º 2
0
bool applyTranslationToXMIFile(const char *fileName, const QStringList &attributes, TranslationMap &translations)
{

    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
        return false;
    QXmlStreamReader reader(&file);
    QFile outFile;
    if (!outFile.open(stdout, QIODevice::WriteOnly))
        return false;
    QXmlStreamWriter writer(&outFile);
    writer.setAutoFormatting (true);
    writer.setAutoFormattingIndent(1);
    writer.setCodec(reader.documentEncoding().toLatin1().constData());

    while (!reader.atEnd())
    {
        QXmlStreamReader::TokenType type = reader.readNext();
        switch(type)
        {
        case QXmlStreamReader::ProcessingInstruction:
            writer.writeProcessingInstruction(reader.processingInstructionTarget().toString(), reader.processingInstructionData().toString());
            break;

        case QXmlStreamReader::DTD:
            writer.writeDTD(reader.text().toString());
            break;

        case QXmlStreamReader::StartDocument:
            writer.writeStartDocument(reader.documentVersion().toString());
            break;

        case QXmlStreamReader::StartElement:
        {
            writer.writeStartElement(reader.namespaceUri().toString(), reader.name().toString());
            if (reader.namespaceDeclarations().size() > 0)
            {
                QXmlStreamNamespaceDeclaration ns = reader.namespaceDeclarations().first();
                writer.writeNamespace(ns.namespaceUri().toString(), ns.prefix().toString());
            }
            QXmlStreamAttributes writerAttributes;
            for(int index = 0; index < reader.attributes().size(); index++)
            {
                QXmlStreamAttribute attr = reader.attributes()[index];
                QString name = attr.qualifiedName().toString();
                if (!attributes.contains(name)) {
                    writerAttributes.append(attr);
                    continue;
                }
                QString value = attr.value().toString();
                if (value.isEmpty()) {
                    writerAttributes.append(attr);
                    continue;
                }
                if (!translations.contains(value))
                {
                    cerr << "could not find translation for attribute '" << qPrintable(name) << "':'" << qPrintable(value) << "'" << std::endl;
                    continue;
                }
                QString newValue = translations[value];
                if (newValue.isEmpty()) {
                    writerAttributes.append(attr);
                    continue;
                }
                //cerr << name.toUtf8().data() << ":" << value.toUtf8().data() << "->" << newValue.toUtf8().data() << endl;
                QXmlStreamAttribute newAttribute(name, newValue);
                writerAttributes.append(newAttribute);
                //qDebug() << writerAttributes;
            }
            writer.writeAttributes(writerAttributes);
            //QString content = xmlReader.readElementText(QXmlStreamReader::SkipChildElements);
            //writer.writeCharacters(content);
            break;
        }

        case QXmlStreamReader::Characters:
            writer.writeCharacters(reader.text().toString());
            break;

        case QXmlStreamReader::Comment:
            writer.writeComment(reader.text().toString());
            break;

        case QXmlStreamReader::EndElement:
            writer.writeEndElement();
            break;

        case QXmlStreamReader::EndDocument:
           writer.writeEndDocument();
           break;

        default:
            break;
        }

    }
    outFile.close();
    return true;
}