1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
7 * For further information visit http://libwpd.sourceforge.net
10 #include <DocumentHandler.hxx>
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>
17 #include <comphelper/attributelist.hxx>
18 #include <xmloff/xmlimp.hxx>
20 namespace writerperfect
22 const 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 };
33 static const char* librvng_utf8_next_char(const char* p
)
35 return p
+ librvng_utf8_skip_data
[*reinterpret_cast<unsigned char const*>(p
)];
38 static void unescapeXML(const char* s
, const unsigned long sz
, librevenge::RVNGString
& res
)
41 const char* const end
= p
+ sz
;
44 const char* const next
= librvng_utf8_next_char(p
);
47 // oops, the string is invalid
50 if (p
+ 4 <= end
&& p
+ 1 == next
&& *p
== '&')
52 // look for & , < , > , ' , "
53 bool escapedChar
= false;
57 if (p
+ 5 <= end
&& strncmp(p
, "&", 5) == 0)
63 else if (p
+ 6 <= end
&& strncmp(p
, "'", 6) == 0)
71 if (strncmp(p
, ">", 4) == 0)
79 if (strncmp(p
, "<", 4) == 0)
87 if (p
+ 6 <= end
&& strncmp(p
, """, 6) == 0)
110 using com::sun::star::uno::Reference
;
111 using com::sun::star::xml::sax::XDocumentHandler
;
113 DocumentHandler::DocumentHandler(Reference
<XDocumentHandler
> const& xHandler
)
114 : mxHandler(xHandler
)
116 if (SvXMLImport
* pFastHandler
= dynamic_cast<SvXMLImport
*>(mxHandler
.get()))
117 mxHandler
.set(new SvXMLLegacyToFastDocHandler(pFastHandler
));
120 void DocumentHandler::startDocument() { mxHandler
->startDocument(); }
122 void DocumentHandler::endDocument() { mxHandler
->endDocument(); }
124 void DocumentHandler::startElement(const char* psName
,
125 const librevenge::RVNGPropertyList
& xPropList
)
127 rtl::Reference
<comphelper::AttributeList
> pAttrList
= new comphelper::AttributeList();
128 librevenge::RVNGPropertyList::Iter
i(xPropList
);
129 for (i
.rewind(); i
.next();)
131 // filter out librevenge elements
132 if (strncmp(i
.key(), "librevenge", 10) != 0)
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
);
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
)
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)
150 librevenge::RVNGString
decodedValue("");
151 unescapeXML(i()->getStr().cstr(),
152 static_cast<unsigned long>(i()->getStr().len()), decodedValue
);
154 = OUString(decodedValue
.cstr(), decodedValue
.len(), RTL_TEXTENCODING_UTF8
);
158 pAttrList
->AddAttribute(sName
, sValue
);
162 OUString
sElementName(psName
, strlen(psName
), RTL_TEXTENCODING_UTF8
);
163 mxHandler
->startElement(sElementName
, pAttrList
);
166 void DocumentHandler::endElement(const char* psName
)
168 OUString
sElementName(psName
, strlen(psName
), RTL_TEXTENCODING_UTF8
);
169 mxHandler
->endElement(sElementName
);
172 void DocumentHandler::characters(const librevenge::RVNGString
& sCharacters
)
174 OUString
sCharU16(sCharacters
.cstr(), strlen(sCharacters
.cstr()), RTL_TEXTENCODING_UTF8
);
175 mxHandler
->characters(sCharU16
);
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */