summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2021-12-08 20:37:41 -0500
committerKohei Yoshida <kohei.yoshida@gmail.com>2021-12-08 20:38:37 -0500
commit82a5da70dae6b96a2f63dc1cc000aeae1fc1c276 (patch)
tree72a55b9cd5bde1603ad4e70599c851ee624c1c56
parent13fd8c214a34e7817afe493068f00d47a073ab09 (diff)
downloadorcus-82a5da70dae6b96a2f63dc1cc000aeae1fc1c276.tar.gz
Add a test case for ignore invalid sub structure.
Also tweak the validator to allow elements when no rules are defined. This is related to #138.
-rw-r--r--src/liborcus/xml_context_base.cpp45
-rw-r--r--src/liborcus/xml_element_validator.cpp4
-rw-r--r--src/orcus_test_xls_xml.cpp1
-rw-r--r--test/xls-xml/invalid-sub-structure/check.txt25
-rw-r--r--test/xls-xml/invalid-sub-structure/input.xml135
5 files changed, 189 insertions, 21 deletions
diff --git a/src/liborcus/xml_context_base.cpp b/src/liborcus/xml_context_base.cpp
index d64975d7..fa96babc 100644
--- a/src/liborcus/xml_context_base.cpp
+++ b/src/liborcus/xml_context_base.cpp
@@ -72,30 +72,33 @@ bool xml_context_base::evaluate_child_element(xmlns_id_t ns, xml_token_t name) c
xml_element_validator::result res = m_elem_validator.validate(parent, child);
- switch (res)
+ if (get_config().debug)
{
- case xml_element_validator::result::child_invalid:
+ switch (res)
{
- std::ostringstream os;
- print_element(os, child);
- os << " cannot be a child element of ";
- print_element(os, parent);
- warn(os.str());
- break;
+ case xml_element_validator::result::child_invalid:
+ {
+ std::ostringstream os;
+ print_element(os, child);
+ os << " cannot be a child element of ";
+ print_element(os, parent);
+ warn(os.str());
+ break;
+ }
+ case xml_element_validator::result::parent_unknown:
+ {
+ std::ostringstream os;
+ os << "parent ";
+ print_element(os, parent);
+ os << " does not have any rules defined (child: ";
+ print_element(os, child);
+ os << ')';
+ warn(os.str());
+ break;
+ }
+ case xml_element_validator::result::child_valid:
+ break;
}
- case xml_element_validator::result::parent_unknown:
- {
- std::ostringstream os;
- os << "parent ";
- print_element(os, parent);
- os << " does not have any rules defined (child: ";
- print_element(os, child);
- os << ')';
- warn(os.str());
- break;
- }
- case xml_element_validator::result::child_valid:
- break;
}
return res != xml_element_validator::result::child_invalid;
diff --git a/src/liborcus/xml_element_validator.cpp b/src/liborcus/xml_element_validator.cpp
index 812e0431..ceb632cd 100644
--- a/src/liborcus/xml_element_validator.cpp
+++ b/src/liborcus/xml_element_validator.cpp
@@ -42,6 +42,10 @@ void xml_element_validator::init(const rule* rules, std::size_t n_rules)
xml_element_validator::result xml_element_validator::validate(
const xml_token_pair_t& parent, const xml_token_pair_t& child) const
{
+ if (m_rules.empty())
+ // No rules are defined. Allow everything.
+ return result::child_valid;
+
auto it = m_rules.find(parent);
if (it == m_rules.end())
// No rules for this parent.
diff --git a/src/orcus_test_xls_xml.cpp b/src/orcus_test_xls_xml.cpp
index 1d672519..0fd0c643 100644
--- a/src/orcus_test_xls_xml.cpp
+++ b/src/orcus_test_xls_xml.cpp
@@ -51,6 +51,7 @@ std::vector<const char*> dirs = {
SRCDIR"/test/xls-xml/formula-cells-1/",
SRCDIR"/test/xls-xml/formula-cells-2/",
SRCDIR"/test/xls-xml/formula-cells-3/",
+ SRCDIR"/test/xls-xml/invalid-sub-structure/",
SRCDIR"/test/xls-xml/leading-whitespace/",
SRCDIR"/test/xls-xml/merged-cells/",
SRCDIR"/test/xls-xml/named-colors/",
diff --git a/test/xls-xml/invalid-sub-structure/check.txt b/test/xls-xml/invalid-sub-structure/check.txt
new file mode 100644
index 00000000..935cf220
--- /dev/null
+++ b/test/xls-xml/invalid-sub-structure/check.txt
@@ -0,0 +1,25 @@
+Num/1/0:numeric:1.1
+Num/1/2:numeric:2.1
+Num/1/4:numeric:3.1
+Num/2/0:numeric:1.2
+Num/2/2:numeric:2.2
+Num/2/4:numeric:3.2
+Num/3/0:numeric:1.3
+Num/3/2:numeric:2.3
+Num/3/4:numeric:3.3
+Num/6/2:numeric:5
+Num/6/3:numeric:6
+Num/6/4:numeric:7
+Text/0/0:string:"A"
+Text/1/0:string:"B"
+Text/1/1:string:"D"
+Text/2/0:string:"C"
+Text/2/1:string:"E"
+Text/2/2:string:"G"
+Text/3/1:string:"F"
+Text/3/2:string:"H"
+Text/4/2:string:"I"
+Text/6/3:string:"Andy"
+Text/7/3:string:"Bruce"
+Text/8/3:string:"Charlie"
+Text/9/3:string:"David"
diff --git a/test/xls-xml/invalid-sub-structure/input.xml b/test/xls-xml/invalid-sub-structure/input.xml
new file mode 100644
index 00000000..16787a56
--- /dev/null
+++ b/test/xls-xml/invalid-sub-structure/input.xml
@@ -0,0 +1,135 @@
+<?xml version="1.0"?>
+<?mso-application progid="Excel.Sheet"?>
+<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
+ xmlns:o="urn:schemas-microsoft-com:office:office"
+ xmlns:x="urn:schemas-microsoft-com:office:excel"
+ xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
+ xmlns:html="http://www.w3.org/TR/REC-html40">
+ <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
+ <Author>Kohei Yoshida</Author>
+ <LastAuthor>Kohei Yoshida</LastAuthor>
+ <Created>2012-12-11T23:16:00Z</Created>
+ <LastSaved>2012-12-11T23:19:30Z</LastSaved>
+ <Company>Novell, Inc.</Company>
+ <Version>16.00</Version>
+ </DocumentProperties>
+ <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
+ <AllowPNG/>
+ </OfficeDocumentSettings>
+ <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
+ <WindowHeight>8775</WindowHeight>
+ <WindowWidth>20880</WindowWidth>
+ <WindowTopX>360</WindowTopX>
+ <WindowTopY>75</WindowTopY>
+ <ProtectStructure>False</ProtectStructure>
+ <ProtectWindows>False</ProtectWindows>
+ </ExcelWorkbook>
+ <Styles>
+ <Style ss:ID="Default" ss:Name="Normal">
+ <Alignment ss:Vertical="Bottom"/>
+ <Borders/>
+ <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
+ <Interior/>
+ <NumberFormat/>
+ <Protection/>
+ </Style>
+ </Styles>
+ <Worksheet ss:Name="Num">
+ <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="7" x:FullColumns="1"
+ x:FullRows="1" ss:DefaultRowHeight="15">
+ <Row ss:Index="2">
+ <Cell><Data ss:Type="Number">1.1000000000000001</Data></Cell>
+ <Cell ss:Index="3"><Data ss:Type="Number">2.1</Data></Cell>
+ <Cell ss:Index="5"><Data ss:Type="Number">3.1</Data></Cell>
+ </Row>
+ <Row>
+ <Cell><Data ss:Type="Number">1.2</Data></Cell>
+ <Cell ss:Index="3"><Data ss:Type="Number">2.2000000000000002</Data></Cell>
+ <Cell ss:Index="5"><Data ss:Type="Number">3.2</Data></Cell>
+ </Row>
+ <Row>
+ <Cell><Data ss:Type="Number">1.3</Data></Cell>
+ <Cell ss:Index="3"><Data ss:Type="Number">2.2999999999999998</Data></Cell>
+ <Cell ss:Index="5"><Data ss:Type="Number">3.3</Data></Cell>
+ </Row>
+ <Row ss:Index="7">
+ <Cell ss:Index="3"><Data ss:Type="Number">5</Data></Cell>
+ <Cell><Data ss:Type="Number">6</Data></Cell>
+ <Cell><Data ss:Type="Number">7</Data></Cell>
+ </Row>
+ </Table>
+ <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
+ <PageSetup>
+ <Header x:Margin="0.3"/>
+ <Footer x:Margin="0.3"/>
+ <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
+ </PageSetup>
+ <Selected/>
+ <Panes>
+ <Pane>
+ <Number>3</Number>
+ <ActiveRow>6</ActiveRow>
+ <ActiveCol>5</ActiveCol>
+ </Pane>
+ </Panes>
+ <ProtectObjects>False</ProtectObjects>
+ <ProtectScenarios>False</ProtectScenarios>
+ </WorksheetOptions>
+ </Worksheet>
+ <Worksheet ss:Name="Text">
+ <Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="10" x:FullColumns="1"
+ x:FullRows="1" ss:DefaultRowHeight="15">
+ <Row>
+ <Cell><Data ss:Type="String">A</Data></Cell>
+ </Row>
+ <Row>
+ <Cell><Data ss:Type="String">B</Data></Cell>
+ <Cell><Data ss:Type="String">D</Data></Cell>
+ </Row>
+ <Row>
+ <Cell><Data ss:Type="String">C</Data></Cell>
+ <Cell><Data ss:Type="String">E</Data></Cell>
+ <Cell><Data ss:Type="String">G</Data></Cell>
+ </Row>
+ <Row>
+ <Cell ss:Index="2"><Data ss:Type="String">F</Data></Cell>
+ <Cell><Data ss:Type="String">H</Data></Cell>
+ </Row>
+ <Row>
+ <Cell ss:Index="3"><Data ss:Type="String">I</Data></Cell>
+ </Row>
+ <Row ss:Index="7">
+ <Cell ss:Index="4"><Data ss:Type="String">Andy</Data></Cell>
+ </Row>
+ <Row>
+ <Cell ss:Index="4"><Data ss:Type="String">Bruce</Data></Cell>
+ </Row>
+ <Row>
+ <Cell ss:Index="4"><Data ss:Type="String">Charlie</Data></Cell>
+ </Row>
+ <Row>
+ <Cell ss:Index="4"><Data ss:Type="String">David</Data></Cell>
+ </Row>
+ </Table>
+ <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
+ <PageSetup>
+ <Header x:Margin="0.3"/>
+ <Footer x:Margin="0.3"/>
+ <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
+ </PageSetup>
+ <Panes>
+ <Pane>
+ <Number>3</Number>
+ <ActiveRow>8</ActiveRow>
+ <ActiveCol>3</ActiveCol>
+ </Pane>
+ </Panes>
+ <ProtectObjects>False</ProtectObjects>
+ <ProtectScenarios>False</ProtectScenarios>
+ </WorksheetOptions>
+ </Worksheet>
+ <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
+ <PageSetup/>
+ <Panes/>
+ </WorksheetOptions>
+</Workbook>