nss: upgrade to release 3.73
[LibreOffice.git] / writerperfect / source / common / DocumentHandler.cxx
blob18141503390920195d9706c5c7a0c572eac1a4f4
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/.
7 * For further information visit http://libwpd.sourceforge.net
8 */
10 #include <DocumentHandler.hxx>
12 #include <string.h>
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)
41 const char* p = s;
42 const char* const end = p + sz;
43 while (p != end)
45 const char* const next = librvng_utf8_next_char(p);
46 if (next > end)
48 // oops, the string is invalid
49 break;
51 if (p + 4 <= end && p + 1 == next && *p == '&')
53 // look for &amp; , &lt; , &gt; , &apos; , &quot;
54 bool escapedChar = false;
55 switch (*(p + 1))
57 case 'a':
58 if (p + 5 <= end && strncmp(p, "&amp;", 5) == 0)
60 res.append('&');
61 p += 5;
62 escapedChar = true;
64 else if (p + 6 <= end && strncmp(p, "&apos;", 6) == 0)
66 res.append('\'');
67 p += 6;
68 escapedChar = true;
70 break;
71 case 'g':
72 if (strncmp(p, "&gt;", 4) == 0)
74 res.append('>');
75 p += 4;
76 escapedChar = true;
78 break;
79 case 'l':
80 if (strncmp(p, "&lt;", 4) == 0)
82 res.append('<');
83 p += 4;
84 escapedChar = true;
86 break;
87 case 'q':
88 if (p + 6 <= end && strncmp(p, "&quot;", 6) == 0)
90 res.append('"');
91 p += 6;
92 escapedChar = true;
94 break;
95 default:
96 break;
98 if (escapedChar)
99 continue;
102 while (p != next)
104 res.append(*p);
105 ++p;
107 p = next;
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);
156 sValue
157 = OUString(decodedValue.cstr(), decodedValue.len(), RTL_TEXTENCODING_UTF8);
158 break;
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: */