Don't depend on the "count" attribute

"count" is an optional attribute
This commit is contained in:
Debao Zhang
2013-11-25 15:12:03 +08:00
parent f89559eec1
commit 630f7895be
+203 -148
View File
@@ -708,42 +708,61 @@ bool Styles::readNumFmts(XmlStreamReader &reader)
{ {
Q_ASSERT(reader.name() == QLatin1String("numFmts")); Q_ASSERT(reader.name() == QLatin1String("numFmts"));
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
int count = attributes.value(QLatin1String("count")).toString().toInt(); bool hasCount = attributes.hasAttribute(QLatin1String("count"));
for (int i=0; i<count; ++i) { int count = hasCount ? attributes.value(QLatin1String("count")).toString().toInt() : -1;
reader.readNextStartElement();
if (reader.name() != QLatin1String("numFmt"))
return false;
QXmlStreamAttributes attributes = reader.attributes();
QSharedPointer<XlsxFormatNumberData> fmt (new XlsxFormatNumberData);
fmt->formatIndex = attributes.value(QLatin1String("numFmtId")).toString().toInt();
fmt->formatString = attributes.value(QLatin1String("formatCode")).toString();
if (fmt->formatIndex >= m_nextCustomNumFmtId)
m_nextCustomNumFmtId = fmt->formatIndex + 1;
m_customNumFmtIdMap.insert(fmt->formatIndex, fmt);
m_customNumFmtsHash.insert(fmt->formatString, fmt);
while (!(reader.name() == QLatin1String("numFmt") && reader.tokenType() == QXmlStreamReader::EndElement)) //Read utill we find the numFmts end tag or ....
reader.readNextStartElement(); while (!reader.atEnd() && !(reader.tokenType() == QXmlStreamReader::EndElement
&& reader.name() == QLatin1String("numFmts"))) {
reader.readNextStartElement();
if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("numFmt")) {
QXmlStreamAttributes attributes = reader.attributes();
QSharedPointer<XlsxFormatNumberData> fmt (new XlsxFormatNumberData);
fmt->formatIndex = attributes.value(QLatin1String("numFmtId")).toString().toInt();
fmt->formatString = attributes.value(QLatin1String("formatCode")).toString();
if (fmt->formatIndex >= m_nextCustomNumFmtId)
m_nextCustomNumFmtId = fmt->formatIndex + 1;
m_customNumFmtIdMap.insert(fmt->formatIndex, fmt);
m_customNumFmtsHash.insert(fmt->formatString, fmt);
}
}
} }
if (reader.hasError())
qWarning()<<reader.errorString();
if (hasCount && (count != m_customNumFmtIdMap.size()))
qWarning("error read custom numFmts");
return true; return true;
} }
bool Styles::readFonts(XmlStreamReader &reader) bool Styles::readFonts(XmlStreamReader &reader)
{ {
Q_ASSERT(reader.name() == QLatin1String("fonts")); Q_ASSERT(reader.name() == QLatin1String("fonts"));
QXmlStreamAttributes attrs = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
int count = attrs.value(QLatin1String("count")).toString().toInt(); bool hasCount = attributes.hasAttribute(QLatin1String("count"));
for (int i=0; i<count; ++i) { int count = hasCount ? attributes.value(QLatin1String("count")).toString().toInt() : -1;
while(!reader.atEnd() && !(reader.tokenType() == QXmlStreamReader::EndElement
&& reader.name() == QLatin1String("fonts"))) {
reader.readNextStartElement(); reader.readNextStartElement();
if (reader.name() != QLatin1String("font")) if (reader.tokenType() == QXmlStreamReader::StartElement) {
return false; if (reader.name() == QLatin1String("font")) {
Format format; Format format;
readFont(reader, format); readFont(reader, format);
m_fontsList.append(format); m_fontsList.append(format);
m_fontsHash.insert(format.fontKey(), format); m_fontsHash.insert(format.fontKey(), format);
if (format.isValid()) if (format.isValid())
format.setFontIndex(m_fontsList.size()-1); format.setFontIndex(m_fontsList.size()-1);
}
}
} }
if (reader.hasError())
qWarning()<<reader.errorString();
if (hasCount && (count != m_fontsList.size()))
qWarning("error read fonts");
return true; return true;
} }
@@ -820,18 +839,27 @@ bool Styles::readFills(XmlStreamReader &reader)
Q_ASSERT(reader.name() == QLatin1String("fills")); Q_ASSERT(reader.name() == QLatin1String("fills"));
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
int count = attributes.value(QLatin1String("count")).toString().toInt(); bool hasCount = attributes.hasAttribute(QLatin1String("count"));
for (int i=0; i<count; ++i) { int count = hasCount ? attributes.value(QLatin1String("count")).toString().toInt() : -1;
while(!reader.atEnd() && !(reader.tokenType() == QXmlStreamReader::EndElement
&& reader.name() == QLatin1String("fills"))) {
reader.readNextStartElement(); reader.readNextStartElement();
if (reader.name() != QLatin1String("fill") || reader.tokenType() != QXmlStreamReader::StartElement) if (reader.tokenType() == QXmlStreamReader::StartElement) {
return false; if (reader.name() == QLatin1String("fill")) {
Format fill; Format fill;
readFill(reader, fill); readFill(reader, fill);
m_fillsList.append(fill); m_fillsList.append(fill);
m_fillsHash.insert(fill.fillKey(), fill); m_fillsHash.insert(fill.fillKey(), fill);
if (fill.isValid()) if (fill.isValid())
fill.setFillIndex(m_fillsList.size()-1); fill.setFillIndex(m_fillsList.size()-1);
}
}
} }
if (reader.hasError())
qWarning()<<reader.errorString();
if (hasCount && (count != m_fillsList.size()))
qWarning("error read fills");
return true; return true;
} }
@@ -917,18 +945,29 @@ bool Styles::readBorders(XmlStreamReader &reader)
Q_ASSERT(reader.name() == QLatin1String("borders")); Q_ASSERT(reader.name() == QLatin1String("borders"));
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
int count = attributes.value(QLatin1String("count")).toString().toInt(); bool hasCount = attributes.hasAttribute(QLatin1String("count"));
for (int i=0; i<count; ++i) { int count = hasCount ? attributes.value(QLatin1String("count")).toString().toInt() : -1;
while(!reader.atEnd() && !(reader.tokenType() == QXmlStreamReader::EndElement
&& reader.name() == QLatin1String("borders"))) {
reader.readNextStartElement(); reader.readNextStartElement();
if (reader.name() != QLatin1String("border") || reader.tokenType() != QXmlStreamReader::StartElement) if (reader.tokenType() == QXmlStreamReader::StartElement) {
return false; if (reader.name() == QLatin1String("border")) {
Format border; Format border;
readBorder(reader, border); readBorder(reader, border);
m_bordersList.append(border); m_bordersList.append(border);
m_bordersHash.insert(border.borderKey(), border); m_bordersHash.insert(border.borderKey(), border);
if (border.isValid()) if (border.isValid())
border.setBorderIndex(m_bordersList.size()-1); border.setBorderIndex(m_bordersList.size()-1);
}
}
} }
if (reader.hasError())
qWarning()<<reader.errorString();
if (hasCount && (count != m_bordersList.size()))
qWarning("error read borders");
return true; return true;
} }
@@ -1057,123 +1096,130 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
{ {
Q_ASSERT(reader.name() == QLatin1String("cellXfs")); Q_ASSERT(reader.name() == QLatin1String("cellXfs"));
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
int count = attributes.value(QLatin1String("count")).toString().toInt(); bool hasCount = attributes.hasAttribute(QLatin1String("count"));
for (int i=0; i<count; ++i) { int count = hasCount ? attributes.value(QLatin1String("count")).toString().toInt() : -1;
while(!reader.atEnd() && !(reader.tokenType() == QXmlStreamReader::EndElement
&& reader.name() == QLatin1String("cellXfs"))) {
reader.readNextStartElement(); reader.readNextStartElement();
if (reader.name() != QLatin1String("xf")) if (reader.tokenType() == QXmlStreamReader::StartElement) {
return false; if (reader.name() == QLatin1String("xf")) {
Format format;
QXmlStreamAttributes xfAttrs = reader.attributes();
// qDebug()<<reader.name()<<reader.tokenString()<<" ........."; Format format;
// for (int i=0; i<xfAttrs.size(); ++i) QXmlStreamAttributes xfAttrs = reader.attributes();
// qDebug()<<"... "<<i<<" "<<xfAttrs[i].name()<<xfAttrs[i].value();
if (xfAttrs.hasAttribute(QLatin1String("applyNumberFormat"))) { // qDebug()<<reader.name()<<reader.tokenString()<<" .........";
int numFmtIndex = xfAttrs.value(QLatin1String("numFmtId")).toString().toInt(); // for (int i=0; i<xfAttrs.size(); ++i)
if (!m_customNumFmtIdMap.contains(numFmtIndex)) // qDebug()<<"... "<<i<<" "<<xfAttrs[i].name()<<xfAttrs[i].value();
format.setNumberFormatIndex(numFmtIndex);
else
format.setNumberFormat(numFmtIndex, m_customNumFmtIdMap[numFmtIndex]->formatString);
}
if (xfAttrs.hasAttribute(QLatin1String("applyFont"))) { if (xfAttrs.hasAttribute(QLatin1String("applyNumberFormat"))) {
int fontIndex = xfAttrs.value(QLatin1String("fontId")).toString().toInt(); int numFmtIndex = xfAttrs.value(QLatin1String("numFmtId")).toString().toInt();
if (fontIndex >= m_fontsList.size()) { if (!m_customNumFmtIdMap.contains(numFmtIndex))
qDebug("Error read styles.xml, cellXfs fontId"); format.setNumberFormatIndex(numFmtIndex);
} else { else
Format fontFormat = m_fontsList[fontIndex]; format.setNumberFormat(numFmtIndex, m_customNumFmtIdMap[numFmtIndex]->formatString);
for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
if (fontFormat.hasProperty(i))
format.setProperty(i, fontFormat.property(i));
} }
}
}
if (xfAttrs.hasAttribute(QLatin1String("applyFill"))) { if (xfAttrs.hasAttribute(QLatin1String("applyFont"))) {
int id = xfAttrs.value(QLatin1String("fillId")).toString().toInt(); int fontIndex = xfAttrs.value(QLatin1String("fontId")).toString().toInt();
if (id >= m_fillsList.size()) { if (fontIndex >= m_fontsList.size()) {
qDebug("Error read styles.xml, cellXfs fillId"); qDebug("Error read styles.xml, cellXfs fontId");
} else { } else {
Format fillFormat = m_fillsList[id]; Format fontFormat = m_fontsList[fontIndex];
for (int i=FormatPrivate::P_Fill_STARTID; i<FormatPrivate::P_Fill_ENDID; ++i) { for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
if (fillFormat.hasProperty(i)) if (fontFormat.hasProperty(i))
format.setProperty(i, fillFormat.property(i)); format.setProperty(i, fontFormat.property(i));
} }
}
}
if (xfAttrs.hasAttribute(QLatin1String("applyBorder"))) {
int id = xfAttrs.value(QLatin1String("borderId")).toString().toInt();
if (id >= m_bordersList.size()) {
qDebug("Error read styles.xml, cellXfs borderId");
} else {
Format borderFormat = m_bordersList[id];
for (int i=FormatPrivate::P_Border_STARTID; i<FormatPrivate::P_Border_ENDID; ++i) {
if (borderFormat.hasProperty(i))
format.setProperty(i, borderFormat.property(i));
}
}
}
if (xfAttrs.hasAttribute(QLatin1String("applyAlignment"))) {
reader.readNextStartElement();
if (reader.name() == QLatin1String("alignment")) {
QXmlStreamAttributes alignAttrs = reader.attributes();
if (alignAttrs.hasAttribute(QLatin1String("horizontal"))) {
static QMap<QString, Format::HorizontalAlignment> alignStringMap;
if (alignStringMap.isEmpty()) {
alignStringMap.insert(QStringLiteral("left"), Format::AlignLeft);
alignStringMap.insert(QStringLiteral("center"), Format::AlignHCenter);
alignStringMap.insert(QStringLiteral("right"), Format::AlignRight);
alignStringMap.insert(QStringLiteral("justify"), Format::AlignHJustify);
alignStringMap.insert(QStringLiteral("centerContinuous"), Format::AlignHMerge);
alignStringMap.insert(QStringLiteral("distributed"), Format::AlignHDistributed);
} }
QString str = alignAttrs.value(QLatin1String("horizontal")).toString();
if (alignStringMap.contains(str))
format.setHorizontalAlignment(alignStringMap[str]);
} }
if (alignAttrs.hasAttribute(QLatin1String("vertical"))) { if (xfAttrs.hasAttribute(QLatin1String("applyFill"))) {
static QMap<QString, Format::VerticalAlignment> alignStringMap; int id = xfAttrs.value(QLatin1String("fillId")).toString().toInt();
if (alignStringMap.isEmpty()) { if (id >= m_fillsList.size()) {
alignStringMap.insert(QStringLiteral("top"), Format::AlignTop); qDebug("Error read styles.xml, cellXfs fillId");
alignStringMap.insert(QStringLiteral("center"), Format::AlignVCenter); } else {
alignStringMap.insert(QStringLiteral("justify"), Format::AlignVJustify); Format fillFormat = m_fillsList[id];
alignStringMap.insert(QStringLiteral("distributed"), Format::AlignVDistributed); for (int i=FormatPrivate::P_Fill_STARTID; i<FormatPrivate::P_Fill_ENDID; ++i) {
if (fillFormat.hasProperty(i))
format.setProperty(i, fillFormat.property(i));
}
} }
QString str = alignAttrs.value(QLatin1String("vertical")).toString();
if (alignStringMap.contains(str))
format.setVerticalAlignment(alignStringMap[str]);
} }
if (alignAttrs.hasAttribute(QLatin1String("indent"))) { if (xfAttrs.hasAttribute(QLatin1String("applyBorder"))) {
int indent = alignAttrs.value(QLatin1String("indent")).toString().toInt(); int id = xfAttrs.value(QLatin1String("borderId")).toString().toInt();
format.setIndent(indent); if (id >= m_bordersList.size()) {
qDebug("Error read styles.xml, cellXfs borderId");
} else {
Format borderFormat = m_bordersList[id];
for (int i=FormatPrivate::P_Border_STARTID; i<FormatPrivate::P_Border_ENDID; ++i) {
if (borderFormat.hasProperty(i))
format.setProperty(i, borderFormat.property(i));
}
}
} }
if (alignAttrs.hasAttribute(QLatin1String("textRotation"))) { if (xfAttrs.hasAttribute(QLatin1String("applyAlignment"))) {
int rotation = alignAttrs.value(QLatin1String("textRotation")).toString().toInt(); reader.readNextStartElement();
format.setRotation(rotation); if (reader.name() == QLatin1String("alignment")) {
QXmlStreamAttributes alignAttrs = reader.attributes();
if (alignAttrs.hasAttribute(QLatin1String("horizontal"))) {
static QMap<QString, Format::HorizontalAlignment> alignStringMap;
if (alignStringMap.isEmpty()) {
alignStringMap.insert(QStringLiteral("left"), Format::AlignLeft);
alignStringMap.insert(QStringLiteral("center"), Format::AlignHCenter);
alignStringMap.insert(QStringLiteral("right"), Format::AlignRight);
alignStringMap.insert(QStringLiteral("justify"), Format::AlignHJustify);
alignStringMap.insert(QStringLiteral("centerContinuous"), Format::AlignHMerge);
alignStringMap.insert(QStringLiteral("distributed"), Format::AlignHDistributed);
}
QString str = alignAttrs.value(QLatin1String("horizontal")).toString();
if (alignStringMap.contains(str))
format.setHorizontalAlignment(alignStringMap[str]);
}
if (alignAttrs.hasAttribute(QLatin1String("vertical"))) {
static QMap<QString, Format::VerticalAlignment> alignStringMap;
if (alignStringMap.isEmpty()) {
alignStringMap.insert(QStringLiteral("top"), Format::AlignTop);
alignStringMap.insert(QStringLiteral("center"), Format::AlignVCenter);
alignStringMap.insert(QStringLiteral("justify"), Format::AlignVJustify);
alignStringMap.insert(QStringLiteral("distributed"), Format::AlignVDistributed);
}
QString str = alignAttrs.value(QLatin1String("vertical")).toString();
if (alignStringMap.contains(str))
format.setVerticalAlignment(alignStringMap[str]);
}
if (alignAttrs.hasAttribute(QLatin1String("indent"))) {
int indent = alignAttrs.value(QLatin1String("indent")).toString().toInt();
format.setIndent(indent);
}
if (alignAttrs.hasAttribute(QLatin1String("textRotation"))) {
int rotation = alignAttrs.value(QLatin1String("textRotation")).toString().toInt();
format.setRotation(rotation);
}
if (alignAttrs.hasAttribute(QLatin1String("wrapText")))
format.setTextWarp(true);
if (alignAttrs.hasAttribute(QLatin1String("shrinkToFit")))
format.setShrinkToFit(true);
}
} }
if (alignAttrs.hasAttribute(QLatin1String("wrapText"))) addXfFormat(format, true);
format.setTextWarp(true);
if (alignAttrs.hasAttribute(QLatin1String("shrinkToFit")))
format.setShrinkToFit(true);
} }
} }
addXfFormat(format, true);
//Find the endElement of xf
while (!(reader.tokenType() == QXmlStreamReader::EndElement && reader.name() == QLatin1String("xf")))
reader.readNextStartElement();
} }
if (reader.hasError())
qWarning()<<reader.errorString();
if (hasCount && (count != m_xf_formatsList.size()))
qWarning("error read CellXfs");
return true; return true;
} }
@@ -1181,13 +1227,22 @@ bool Styles::readDxfs(XmlStreamReader &reader)
{ {
Q_ASSERT(reader.name() == QLatin1String("dxfs")); Q_ASSERT(reader.name() == QLatin1String("dxfs"));
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
int count = attributes.value(QLatin1String("count")).toString().toInt(); bool hasCount = attributes.hasAttribute(QLatin1String("count"));
for (int i=0; i<count; ++i) { int count = hasCount ? attributes.value(QLatin1String("count")).toString().toInt() : -1;
while (!reader.atEnd() && !(reader.tokenType() == QXmlStreamReader::EndElement
&& reader.name() == QLatin1String("dxfs"))) {
reader.readNextStartElement(); reader.readNextStartElement();
if (reader.name() != QLatin1String("dxf")) if (reader.tokenType() == QXmlStreamReader::StartElement) {
return false; if (reader.name() == QLatin1String("dxf"))
readDxf(reader); readDxf(reader);
}
} }
if (reader.hasError())
qWarning()<<reader.errorString();
if (hasCount && (count != m_dxf_formatsList.size()))
qWarning("error read dxfs");
return true; return true;
} }