LibreOffice Module writerperfect (master) 1
xmltbli.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include "xmltbli.hxx"
11
12#include "txtparai.hxx"
13#include "xmlimp.hxx"
14#include "xmltext.hxx"
15
16#include <sal/log.hxx>
17
18using namespace com::sun::star;
19
20namespace writerperfect::exp
21{
22namespace
23{
25class XMLTableRowContext : public XMLImportContext
26{
27public:
28 XMLTableRowContext(XMLImport& rImport);
29
31 CreateChildContext(const OUString& rName,
32 const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs) override;
33 int GetColumn() const;
34 void SetColumn(int nColumn);
35
36 void SAL_CALL
37 startElement(const OUString& rName,
38 const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs) override;
39 void SAL_CALL endElement(const OUString& rName) override;
40
41private:
42 int m_nColumn = 0;
43};
44
46class XMLTableCellContext : public XMLImportContext
47{
48public:
49 XMLTableCellContext(XMLImport& rImport, XMLTableRowContext& rRow);
50
52 CreateChildContext(const OUString& rName,
53 const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs) override;
54
55 void SAL_CALL
56 startElement(const OUString& rName,
57 const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs) override;
58 void SAL_CALL endElement(const OUString& rName) override;
59
60private:
61 XMLTableRowContext& m_rRow;
62};
63}
64
65XMLTableCellContext::XMLTableCellContext(XMLImport& rImport, XMLTableRowContext& rRow)
66 : XMLImportContext(rImport)
67 , m_rRow(rRow)
68{
69}
70
71rtl::Reference<XMLImportContext> XMLTableCellContext::CreateChildContext(
72 const OUString& rName, const css::uno::Reference<css::xml::sax::XAttributeList>& /*xAttribs*/)
73{
74 return CreateTextChildContext(GetImport(), rName);
75}
76
77void XMLTableCellContext::startElement(
78 const OUString& /*rName*/, const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs)
79{
80 librevenge::RVNGPropertyList aPropertyList;
81 for (sal_Int16 i = 0; i < xAttribs->getLength(); ++i)
82 {
83 const OUString& rAttributeName = xAttribs->getNameByIndex(i);
84 const OUString& rAttributeValue = xAttribs->getValueByIndex(i);
85
86 if (rAttributeName == "table:style-name")
87 FillStyles(rAttributeValue, GetImport().GetAutomaticCellStyles(),
88 GetImport().GetCellStyles(), aPropertyList);
89 else
90 {
91 OString sName = OUStringToOString(rAttributeName, RTL_TEXTENCODING_UTF8);
92 OString sValue = OUStringToOString(rAttributeValue, RTL_TEXTENCODING_UTF8);
93 aPropertyList.insert(sName.getStr(), sValue.getStr());
94 }
95 }
96 aPropertyList.insert("librevenge:column", m_rRow.GetColumn());
97 GetImport().GetGenerator().openTableCell(aPropertyList);
98 m_rRow.SetColumn(m_rRow.GetColumn() + 1);
99}
100
101void XMLTableCellContext::endElement(const OUString& /*rName*/)
102{
103 GetImport().GetGenerator().closeTableCell();
104}
105
106namespace
107{
109class XMLTableColumnContext : public XMLImportContext
110{
111public:
112 XMLTableColumnContext(XMLImport& rImport, librevenge::RVNGPropertyListVector& rColumns);
113
114 void SAL_CALL
115 startElement(const OUString& rName,
116 const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs) override;
117
118private:
119 librevenge::RVNGPropertyListVector& m_rColumns;
120};
121}
122
123XMLTableColumnContext::XMLTableColumnContext(XMLImport& rImport,
124 librevenge::RVNGPropertyListVector& rColumns)
125 : XMLImportContext(rImport)
126 , m_rColumns(rColumns)
127{
128}
129
130void XMLTableColumnContext::startElement(
131 const OUString& /*rName*/, const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs)
132{
133 librevenge::RVNGPropertyList aPropertyList;
134 for (sal_Int16 i = 0; i < xAttribs->getLength(); ++i)
135 {
136 const OUString& rAttributeName = xAttribs->getNameByIndex(i);
137 const OUString& rAttributeValue = xAttribs->getValueByIndex(i);
138
139 if (rAttributeName == "table:style-name")
140 FillStyles(rAttributeValue, GetImport().GetAutomaticColumnStyles(),
141 GetImport().GetColumnStyles(), aPropertyList);
142 }
143 m_rColumns.append(aPropertyList);
144}
145
146XMLTableRowContext::XMLTableRowContext(XMLImport& rImport)
147 : XMLImportContext(rImport)
148{
149}
150
151rtl::Reference<XMLImportContext> XMLTableRowContext::CreateChildContext(
152 const OUString& rName, const css::uno::Reference<css::xml::sax::XAttributeList>& /*xAttribs*/)
153{
154 if (rName == "table:table-cell")
155 return new XMLTableCellContext(GetImport(), *this);
156 if (rName == "table:covered-table-cell")
157 {
158 ++m_nColumn;
159 GetImport().GetGenerator().insertCoveredTableCell(librevenge::RVNGPropertyList());
160 }
161 else
162 SAL_WARN("writerperfect", "XMLTableRowContext::CreateChildContext: unhandled " << rName);
163 return nullptr;
164}
165
166void XMLTableRowContext::startElement(
167 const OUString& /*rName*/, const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs)
168{
169 librevenge::RVNGPropertyList aPropertyList;
170 for (sal_Int16 i = 0; i < xAttribs->getLength(); ++i)
171 {
172 const OUString& rAttributeName = xAttribs->getNameByIndex(i);
173 const OUString& rAttributeValue = xAttribs->getValueByIndex(i);
174
175 if (rAttributeName == "table:style-name")
176 FillStyles(rAttributeValue, GetImport().GetAutomaticRowStyles(),
177 GetImport().GetRowStyles(), aPropertyList);
178 }
179 GetImport().GetGenerator().openTableRow(aPropertyList);
180}
181
182void XMLTableRowContext::endElement(const OUString& /*rName*/)
183{
184 GetImport().GetGenerator().closeTableRow();
185}
186
187int XMLTableRowContext::GetColumn() const { return m_nColumn; }
188
189void XMLTableRowContext::SetColumn(int nColumn) { m_nColumn = nColumn; }
190
191XMLTableContext::XMLTableContext(XMLImport& rImport, bool bTopLevel)
192 : XMLImportContext(rImport)
193 , m_bTopLevel(bTopLevel)
194{
195}
196
198 const OUString& rName, const css::uno::Reference<css::xml::sax::XAttributeList>& /*xAttribs*/)
199{
200 if (rName == "table:table-column")
201 // Make sure columns are parsed before we open the table.
202 return new XMLTableColumnContext(GetImport(), m_aColumns);
203
204 if (!m_bTableOpened)
205 {
206 if (!m_aColumns.empty())
207 m_aPropertyList.insert("librevenge:table-columns", m_aColumns);
209 m_bTableOpened = true;
210 }
211
212 if (rName == "table:table-row")
213 return new XMLTableRowContext(GetImport());
214
215 SAL_WARN("writerperfect", "XMLTableContext::CreateChildContext: unhandled " << rName);
216
217 return nullptr;
218}
219
221 const OUString& /*rName*/, const css::uno::Reference<css::xml::sax::XAttributeList>& xAttribs)
222{
223 for (sal_Int16 i = 0; i < xAttribs->getLength(); ++i)
224 {
225 const OUString& rAttributeName = xAttribs->getNameByIndex(i);
226 const OUString& rAttributeValue = xAttribs->getValueByIndex(i);
227
228 if (rAttributeName == "table:style-name")
229 {
230 FillStyles(rAttributeValue, GetImport().GetAutomaticTableStyles(),
231 GetImport().GetTableStyles(), m_aPropertyList);
232 if (m_bTopLevel)
234 }
235 else
236 {
237 OString sName = OUStringToOString(rAttributeName, RTL_TEXTENCODING_UTF8);
238 OString sValue = OUStringToOString(rAttributeValue, RTL_TEXTENCODING_UTF8);
239 m_aPropertyList.insert(sName.getStr(), sValue.getStr());
240 }
241 }
242}
243
244void XMLTableContext::endElement(const OUString& /*rName*/)
245{
246 if (m_bTableOpened)
247 {
248 GetImport().GetGenerator().closeTable();
249 }
250}
251
252} // namespace writerperfect::exp
253
254/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Base class for a handler of a single XML element during ODF -> librevenge conversion.
Definition: xmlictxt.hxx:23
ODT export feeds this class to make librevenge calls.
Definition: xmlimp.hxx:69
void HandlePageSpan(const librevenge::RVNGPropertyList &rPropertyList)
Definition: xmlimp.cxx:586
librevenge::RVNGTextInterface & GetGenerator() const
Definition: xmlimp.cxx:462
librevenge::RVNGPropertyList m_aPropertyList
Definition: xmltbli.hxx:38
bool m_bTopLevel
If the context is a direct child of XMLBodyContentContext.
Definition: xmltbli.hxx:37
void SAL_CALL endElement(const OUString &rName) override
Definition: xmltbli.cxx:244
rtl::Reference< XMLImportContext > CreateChildContext(const OUString &rName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttribs) override
Definition: xmltbli.cxx:197
librevenge::RVNGPropertyListVector m_aColumns
Definition: xmltbli.hxx:39
void SAL_CALL startElement(const OUString &rName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttribs) override
Definition: xmltbli.cxx:220
OUString sName
#define SAL_WARN(area, stream)
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
void FillStyles(const OUString &rName, std::map< OUString, librevenge::RVNGPropertyList > &rAutomaticStyles, std::map< OUString, librevenge::RVNGPropertyList > &rNamedStyles, librevenge::RVNGPropertyList &rPropertyList)
Looks for rName in rAutomaticStyles (and failing that, in rNamedStyles) and fills rPropertyList based...
Definition: txtparai.cxx:623
rtl::Reference< XMLImportContext > CreateTextChildContext(XMLImport &rImport, std::u16string_view rName, bool bTopLevel)
Context factory for body text, section, table cell, etc.
Definition: xmltext.cxx:39
XMLTableRowContext & m_rRow
Definition: xmltbli.cxx:61
int m_nColumn
Definition: xmltbli.cxx:42
librevenge::RVNGPropertyListVector & m_rColumns
Definition: xmltbli.cxx:119