LibreOffice Module writerperfect (master) 1
DocumentHandler.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 Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * For further information visit http://libwpd.sourceforge.net
8 */
9
10#include <DocumentHandler.hxx>
11
12#include <string.h>
13
14#include <com/sun/star/uno/Reference.hxx>
15#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
16#include <com/sun/star/xml/sax/XAttributeList.hpp>
18#include <xmloff/xmlimp.hxx>
19
20namespace writerperfect
21{
22const unsigned char librvng_utf8_skip_data[256]
23 = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
24 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
25 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
26 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
27 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
28 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
29 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
30 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
31 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 };
32
33static const char* librvng_utf8_next_char(const char* p)
34{
35 return p + librvng_utf8_skip_data[*reinterpret_cast<unsigned char const*>(p)];
36}
37
38static void unescapeXML(const char* s, const unsigned long sz, librevenge::RVNGString& res)
39{
40 const char* p = s;
41 const char* const end = p + sz;
42 while (p != end)
43 {
44 const char* const next = librvng_utf8_next_char(p);
45 if (next > end)
46 {
47 // oops, the string is invalid
48 break;
49 }
50 if (p + 4 <= end && p + 1 == next && *p == '&')
51 {
52 // look for &amp; , &lt; , &gt; , &apos; , &quot;
53 bool escapedChar = false;
54 switch (*(p + 1))
55 {
56 case 'a':
57 if (p + 5 <= end && strncmp(p, "&amp;", 5) == 0)
58 {
59 res.append('&');
60 p += 5;
61 escapedChar = true;
62 }
63 else if (p + 6 <= end && strncmp(p, "&apos;", 6) == 0)
64 {
65 res.append('\'');
66 p += 6;
67 escapedChar = true;
68 }
69 break;
70 case 'g':
71 if (strncmp(p, "&gt;", 4) == 0)
72 {
73 res.append('>');
74 p += 4;
75 escapedChar = true;
76 }
77 break;
78 case 'l':
79 if (strncmp(p, "&lt;", 4) == 0)
80 {
81 res.append('<');
82 p += 4;
83 escapedChar = true;
84 }
85 break;
86 case 'q':
87 if (p + 6 <= end && strncmp(p, "&quot;", 6) == 0)
88 {
89 res.append('"');
90 p += 6;
91 escapedChar = true;
92 }
93 break;
94 default:
95 break;
96 }
97 if (escapedChar)
98 continue;
99 }
100
101 while (p != next)
102 {
103 res.append(*p);
104 ++p;
105 }
106 p = next;
107 }
108}
109
111using com::sun::star::xml::sax::XDocumentHandler;
112
114 : mxHandler(xHandler)
115{
116 if (SvXMLImport* pFastHandler = dynamic_cast<SvXMLImport*>(mxHandler.get()))
117 mxHandler.set(new SvXMLLegacyToFastDocHandler(pFastHandler));
118}
119
120void DocumentHandler::startDocument() { mxHandler->startDocument(); }
121
122void DocumentHandler::endDocument() { mxHandler->endDocument(); }
123
124void DocumentHandler::startElement(const char* psName,
125 const librevenge::RVNGPropertyList& xPropList)
126{
128 librevenge::RVNGPropertyList::Iter i(xPropList);
129 for (i.rewind(); i.next();)
130 {
131 // filter out librevenge elements
132 if (strncmp(i.key(), "librevenge", 10) != 0)
133 {
134 size_t keyLength = strlen(i.key());
135 OUString sName(i.key(), keyLength, RTL_TEXTENCODING_UTF8);
136 OUString sValue(i()->getStr().cstr(), i()->getStr().len(), RTL_TEXTENCODING_UTF8);
137
138 // libodfgen xml-encodes some attribute's value, so check if the value is encoded or not
139 for (int j = 0; j < 9; ++j)
140 {
141 // list of the encoded attributes followed by their lengths
142 static char const* listEncoded[9]
143 = { "draw:name", "svg:font-family", "style:condition",
144 "style:num-prefix", "style:num-suffix", "table:formula",
145 "text:bullet-char", "text:label", "xlink:href" };
146 static size_t const listEncodedLength[9] = { 9, 15, 15, 16, 16, 13, 16, 10, 10 };
147 if (keyLength == listEncodedLength[j]
148 && strncmp(i.key(), listEncoded[j], keyLength) == 0)
149 {
150 librevenge::RVNGString decodedValue("");
151 unescapeXML(i()->getStr().cstr(),
152 static_cast<unsigned long>(i()->getStr().len()), decodedValue);
153 sValue
154 = OUString(decodedValue.cstr(), decodedValue.len(), RTL_TEXTENCODING_UTF8);
155 break;
156 }
157 }
158 pAttrList->AddAttribute(sName, sValue);
159 }
160 }
161
162 OUString sElementName(psName, strlen(psName), RTL_TEXTENCODING_UTF8);
163 mxHandler->startElement(sElementName, pAttrList);
164}
165
166void DocumentHandler::endElement(const char* psName)
167{
168 OUString sElementName(psName, strlen(psName), RTL_TEXTENCODING_UTF8);
169 mxHandler->endElement(sElementName);
170}
171
172void DocumentHandler::characters(const librevenge::RVNGString& sCharacters)
173{
174 OUString sCharU16(sCharacters.cstr(), strlen(sCharacters.cstr()), RTL_TEXTENCODING_UTF8);
175 mxHandler->characters(sCharU16);
176}
177}
178
179/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPRIVATE void endElement(const char *psName) override
SAL_DLLPRIVATE void endDocument() override
SAL_DLLPRIVATE void startElement(const char *psName, const librevenge::RVNGPropertyList &xPropList) override
SAL_DLLPRIVATE void startDocument() override
css::uno::Reference< css::xml::sax::XDocumentHandler > mxHandler
DocumentHandler(css::uno::Reference< css::xml::sax::XDocumentHandler > const &xHandler)
SAL_DLLPRIVATE void characters(const librevenge::RVNGString &sCharacters) override
OUString sName
void * p
int i
end
const unsigned char librvng_utf8_skip_data[256]
static const char * librvng_utf8_next_char(const char *p)
static void unescapeXML(const char *s, const unsigned long sz, librevenge::RVNGString &res)
rtl::Reference< FragmentHandler > mxHandler