Browse Source

Use namespaceUri instead of qualifiedName

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

86
src/xlsx/xlsxdocpropscore.cpp

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

Loading…
Cancel
Save