Browse Source

Use namespaceUri instead of qualifiedName

master
Debao Zhang 11 years ago
parent
commit
1b57b6f042
  1. 21
      src/xlsx/xlsxdocpropsapp.cpp
  2. 86
      src/xlsx/xlsxdocpropscore.cpp

21
src/xlsx/xlsxdocpropsapp.cpp

@ -82,37 +82,38 @@ QStringList DocPropsApp::propertyNames() const
void DocPropsApp::saveToXmlFile(QIODevice *device)
{
QXmlStreamWriter writer(device);
QString vt = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes");
writer.writeStartDocument(QStringLiteral("1.0"), true);
writer.writeStartElement(QStringLiteral("Properties"));
writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"));
writer.writeAttribute(QStringLiteral("xmlns:vt"), QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"));
writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"));
writer.writeNamespace(vt, QStringLiteral("vt"));
writer.writeTextElement(QStringLiteral("Application"), QStringLiteral("Microsoft Excel"));
writer.writeTextElement(QStringLiteral("DocSecurity"), QStringLiteral("0"));
writer.writeTextElement(QStringLiteral("ScaleCrop"), QStringLiteral("false"));
writer.writeStartElement(QStringLiteral("HeadingPairs"));
writer.writeStartElement(QStringLiteral("vt:vector"));
writer.writeStartElement(vt, QStringLiteral("vector"));
writer.writeAttribute(QStringLiteral("size"), QString::number(m_headingPairsList.size()*2));
writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("variant"));
typedef QPair<QString,int> PairType; //Make foreach happy
foreach (PairType pair, m_headingPairsList) {
writer.writeStartElement(QStringLiteral("vt:variant"));
writer.writeTextElement(QStringLiteral("vt:lpstr"), pair.first);
writer.writeStartElement(vt, QStringLiteral("variant"));
writer.writeTextElement(vt, QStringLiteral("lpstr"), pair.first);
writer.writeEndElement(); //vt:variant
writer.writeStartElement(QStringLiteral("vt:variant"));
writer.writeTextElement(QStringLiteral("vt:i4"), QString::number(pair.second));
writer.writeStartElement(vt, QStringLiteral("variant"));
writer.writeTextElement(vt, QStringLiteral("i4"), QString::number(pair.second));
writer.writeEndElement(); //vt:variant
}
writer.writeEndElement();//vt:vector
writer.writeEndElement();//HeadingPairs
writer.writeStartElement(QStringLiteral("TitlesOfParts"));
writer.writeStartElement(QStringLiteral("vt:vector"));
writer.writeStartElement(vt, QStringLiteral("vector"));
writer.writeAttribute(QStringLiteral("size"), QString::number(m_titlesOfPartsList.size()));
writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("lpstr"));
foreach (QString title, m_titlesOfPartsList)
writer.writeTextElement(QStringLiteral("vt:lpstr"), title);
writer.writeTextElement(vt, QStringLiteral("lpstr"), title);
writer.writeEndElement();//vt:vector
writer.writeEndElement();//TitlesOfParts
@ -151,7 +152,7 @@ DocPropsApp DocPropsApp::loadFromXmlFile(QIODevice *device)
if (reader.name() == QStringLiteral("Manager")) {
props.setProperty(QStringLiteral("manager"), reader.readElementText());
} else if (reader.qualifiedName() == QStringLiteral("Company")) {
} else if (reader.name() == QStringLiteral("Company")) {
props.setProperty(QStringLiteral("company"), reader.readElementText());
}
}

86
src/xlsx/xlsxdocpropscore.cpp

@ -75,46 +75,50 @@ QStringList DocPropsCore::propertyNames() const
void DocPropsCore::saveToXmlFile(QIODevice *device)
{
QXmlStreamWriter writer(device);
const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
const QString dcmitype = QStringLiteral("http://purl.org/dc/dcmitype/");
const QString xsi = QStringLiteral("http://www.w3.org/2001/XMLSchema-instance");
writer.writeStartDocument(QStringLiteral("1.0"), true);
writer.writeStartElement(QStringLiteral("cp:coreProperties"));
writer.writeAttribute(QStringLiteral("xmlns:cp"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties"));
writer.writeAttribute(QStringLiteral("xmlns:dc"), QStringLiteral("http://purl.org/dc/elements/1.1/"));
writer.writeAttribute(QStringLiteral("xmlns:dcterms"), QStringLiteral("http://purl.org/dc/terms/"));
writer.writeAttribute(QStringLiteral("xmlns:dcmitype"), QStringLiteral("http://purl.org/dc/dcmitype/"));
writer.writeAttribute(QStringLiteral("xmlns:xsi"), QStringLiteral("http://www.w3.org/2001/XMLSchema-instance"));
writer.writeNamespace(cp, QStringLiteral("cp"));
writer.writeNamespace(dc, QStringLiteral("dc"));
writer.writeNamespace(dcterms, QStringLiteral("dcterms"));
writer.writeNamespace(dcmitype, QStringLiteral("dcmitype"));
writer.writeNamespace(xsi, QStringLiteral("xsi"));
if (m_properties.contains(QStringLiteral("title")))
writer.writeTextElement(QStringLiteral("dc:title"), m_properties[QStringLiteral("title")]);
writer.writeTextElement(dc, QStringLiteral("title"), m_properties[QStringLiteral("title")]);
if (m_properties.contains(QStringLiteral("subject")))
writer.writeTextElement(QStringLiteral("dc:subject"), m_properties[QStringLiteral("subject")]);
writer.writeTextElement(dc, QStringLiteral("subject"), m_properties[QStringLiteral("subject")]);
writer.writeTextElement(QStringLiteral("dc:creator"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
writer.writeTextElement(dc, QStringLiteral("creator"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
if (m_properties.contains(QStringLiteral("keywords")))
writer.writeTextElement(QStringLiteral("cp:keywords"), m_properties[QStringLiteral("keywords")]);
writer.writeTextElement(cp, QStringLiteral("keywords"), m_properties[QStringLiteral("keywords")]);
if (m_properties.contains(QStringLiteral("description")))
writer.writeTextElement(QStringLiteral("dc:description"), m_properties[QStringLiteral("description")]);
writer.writeTextElement(dc, QStringLiteral("description"), m_properties[QStringLiteral("description")]);
writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
writer.writeTextElement(cp, QStringLiteral("lastModifiedBy"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
writer.writeStartElement(QStringLiteral("dcterms:created"));
writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF"));
writer.writeStartElement(dcterms, QStringLiteral("created"));
writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
writer.writeCharacters(m_properties.contains(QStringLiteral("created")) ? m_properties[QStringLiteral("created")] : QDateTime::currentDateTime().toString(Qt::ISODate));
writer.writeEndElement();//dcterms:created
writer.writeStartElement(QStringLiteral("dcterms:modified"));
writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF"));
writer.writeStartElement(dcterms, QStringLiteral("modified"));
writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
writer.writeEndElement();//dcterms:created
if (m_properties.contains(QStringLiteral("category")))
writer.writeTextElement(QStringLiteral("cp:category"), m_properties[QStringLiteral("category")]);
writer.writeTextElement(cp, QStringLiteral("category"), m_properties[QStringLiteral("category")]);
if (m_properties.contains(QStringLiteral("status")))
writer.writeTextElement(QStringLiteral("cp:contentStatus"), m_properties[QStringLiteral("status")]);
writer.writeTextElement(cp, QStringLiteral("contentStatus"), m_properties[QStringLiteral("status")]);
writer.writeEndElement(); //cp:coreProperties
writer.writeEndDocument();
@ -134,34 +138,38 @@ DocPropsCore DocPropsCore::loadFromXmlFile(QIODevice *device)
{
DocPropsCore props;
QXmlStreamReader reader(device);
const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
while(!reader.atEnd()) {
QXmlStreamReader::TokenType token = reader.readNext();
if (token == QXmlStreamReader::StartElement) {
if (reader.qualifiedName() == QLatin1String("cp:coreProperties"))
continue;
QString text = reader.readElementText();
if (reader.qualifiedName() == QStringLiteral("dc:subject")) {
props.setProperty(QStringLiteral("subject"), text);
} else if (reader.qualifiedName() == QStringLiteral("dc:title")) {
props.setProperty(QStringLiteral("title"), text);
} else if (reader.qualifiedName() == QStringLiteral("dc:creator")) {
props.setProperty(QStringLiteral("creator"), text);
} else if (reader.qualifiedName() == QStringLiteral("dc:description")) {
props.setProperty(QStringLiteral("description"), text);
} else if (reader.qualifiedName() == QStringLiteral("cp:keywords")) {
props.setProperty(QStringLiteral("keywords"), text);
} else if (reader.qualifiedName() == QStringLiteral("dcterms:created")) {
props.setProperty(QStringLiteral("created"), text);
} else if (reader.qualifiedName() == QStringLiteral("cp:category")) {
props.setProperty(QStringLiteral("category"), text);
} else if (reader.qualifiedName() == QStringLiteral("cp:contentStatus")) {
props.setProperty(QStringLiteral("status"), text);
const QStringRef nsUri = reader.namespaceUri();
const QStringRef name = reader.name();
qDebug()<<nsUri<<name;
if (name == QStringLiteral("subject") && nsUri == dc) {
props.setProperty(QStringLiteral("subject"), reader.readElementText());
} else if (name == QStringLiteral("title") && nsUri == dc) {
props.setProperty(QStringLiteral("title"), reader.readElementText());
} else if (name == QStringLiteral("creator") && nsUri == dc) {
props.setProperty(QStringLiteral("creator"), reader.readElementText());
} else if (name == QStringLiteral("description") && nsUri == dc) {
props.setProperty(QStringLiteral("description"), reader.readElementText());
} else if (name == QStringLiteral("keywords") && nsUri == cp) {
props.setProperty(QStringLiteral("keywords"), reader.readElementText());
} else if (name == QStringLiteral("created") && nsUri == dcterms) {
props.setProperty(QStringLiteral("created"), reader.readElementText());
} else if (name == QStringLiteral("category") && nsUri == cp) {
props.setProperty(QStringLiteral("category"), reader.readElementText());
} else if (name == QStringLiteral("contentStatus") && nsUri == cp) {
props.setProperty(QStringLiteral("status"), reader.readElementText());
}
}
if (reader.hasError()) {
qDebug()<<"Error when read doc props core file.";
qDebug()<<"Error when read doc props core file."<<reader.errorString();
}
}

Loading…
Cancel
Save