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>
18 #include <xmloff/attrlist.hxx>
19 #include <xmloff/xmlimp.hxx>
21 namespace writerperfect
23 const unsigned char librvng_utf8_skip_data
[256]
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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
30 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,
31 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,
32 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 };
34 static const char* librvng_utf8_next_char(const char* p
)
36 return p
+ librvng_utf8_skip_data
[*reinterpret_cast<unsigned char const*>(p
)];
39 static void unescapeXML(const char* s
, const unsigned long sz
, librevenge::RVNGString
& res
)
42 const char* const end
= p
+ sz
;
45 const char* const next
= librvng_utf8_next_char(p
);
48 // oops, the string is invalid
51 if (p
+ 4 <= end
&& p
+ 1 == next
&& *p
== '&')
53 // look for & , < , > , ' , "
54 bool escapedChar
= false;
58 if (p
+ 5 <= end
&& strncmp(p
, "&", 5) == 0)
64 else if (p
+ 6 <= end
&& strncmp(p
, "'", 6) == 0)
72 if (strncmp(p
, ">", 4) == 0)
80 if (strncmp(p
, "<", 4) == 0)
88 if (p
+ 6 <= end
&& strncmp(p
, """, 6) == 0)
111 using com::sun::star::uno::Reference
;
112 using com::sun::star::xml::sax::XAttributeList
;
113 using com::sun::star::xml::sax::XDocumentHandler
;
115 DocumentHandler::DocumentHandler(Reference
<XDocumentHandler
> const& xHandler
)
116 : mxHandler(xHandler
)
118 if (SvXMLImport
* pFastHandler
= dynamic_cast<SvXMLImport
*>(mxHandler
.get()))
119 mxHandler
.set(new SvXMLLegacyToFastDocHandler(pFastHandler
));
122 void DocumentHandler::startDocument() { mxHandler
->startDocument(); }
124 void DocumentHandler::endDocument() { mxHandler
->endDocument(); }
126 void DocumentHandler::startElement(const char* psName
,
127 const librevenge::RVNGPropertyList
& xPropList
)
129 SvXMLAttributeList
* pAttrList
= new SvXMLAttributeList();
130 Reference
<XAttributeList
> xAttrList(pAttrList
);
131 librevenge::RVNGPropertyList::Iter
i(xPropList
);
132 for (i
.rewind(); i
.next();)
134 // filter out librevenge elements
135 if (strncmp(i
.key(), "librevenge", 10) != 0)
137 size_t keyLength
= strlen(i
.key());
138 OUString
sName(i
.key(), keyLength
, RTL_TEXTENCODING_UTF8
);
139 OUString
sValue(i()->getStr().cstr(), i()->getStr().len(), RTL_TEXTENCODING_UTF8
);
141 // libodfgen xml-encodes some attribute's value, so check if the value is encoded or not
142 for (int j
= 0; j
< 9; ++j
)
144 // list of the encoded attributes followed by their lengths
145 static char const* listEncoded
[9]
146 = { "draw:name", "svg:font-family", "style:condition",
147 "style:num-prefix", "style:num-suffix", "table:formula",
148 "text:bullet-char", "text:label", "xlink:href" };
149 static size_t const listEncodedLength
[9] = { 9, 15, 15, 16, 16, 13, 16, 10, 10 };
150 if (keyLength
== listEncodedLength
[j
]
151 && strncmp(i
.key(), listEncoded
[j
], keyLength
) == 0)
153 librevenge::RVNGString
decodedValue("");
154 unescapeXML(i()->getStr().cstr(),
155 static_cast<unsigned long>(i()->getStr().len()), decodedValue
);
157 = OUString(decodedValue
.cstr(), decodedValue
.len(), RTL_TEXTENCODING_UTF8
);
161 pAttrList
->AddAttribute(sName
, sValue
);
165 OUString
sElementName(psName
, strlen(psName
), RTL_TEXTENCODING_UTF8
);
166 mxHandler
->startElement(sElementName
, xAttrList
);
169 void DocumentHandler::endElement(const char* psName
)
171 OUString
sElementName(psName
, strlen(psName
), RTL_TEXTENCODING_UTF8
);
172 mxHandler
->endElement(sElementName
);
175 void DocumentHandler::characters(const librevenge::RVNGString
& sCharacters
)
177 OUString
sCharU16(sCharacters
.cstr(), strlen(sCharacters
.cstr()), RTL_TEXTENCODING_UTF8
);
178 mxHandler
->characters(sCharU16
);
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */