Get the style color and number just once
[LibreOffice.git] / writerperfect / source / common / DocumentHandler.cxx
blob28730ab5ee99a936714295110eb4d54579183498
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>
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)
40 const char* p = s;
41 const char* const end = p + sz;
42 while (p != end)
44 const char* const next = librvng_utf8_next_char(p);
45 if (next > end)
47 // oops, the string is invalid
48 break;
50 if (p + 4 <= end && p + 1 == next && *p == '&')
52 // look for &amp; , &lt; , &gt; , &apos; , &quot;
53 bool escapedChar = false;
54 switch (*(p + 1))
56 case 'a':
57 if (p + 5 <= end && strncmp(p, "&amp;", 5) == 0)
59 res.append('&');
60 p += 5;
61 escapedChar = true;
63 else if (p + 6 <= end && strncmp(p, "&apos;", 6) == 0)
65 res.append('\'');
66 p += 6;
67 escapedChar = true;
69 break;
70 case 'g':
71 if (strncmp(p, "&gt;", 4) == 0)
73 res.append('>');
74 p += 4;
75 escapedChar = true;
77 break;
78 case 'l':
79 if (strncmp(p, "&lt;", 4) == 0)
81 res.append('<');
82 p += 4;
83 escapedChar = true;
85 break;
86 case 'q':
87 if (p + 6 <= end && strncmp(p, "&quot;", 6) == 0)
89 res.append('"');
90 p += 6;
91 escapedChar = true;
93 break;
94 default:
95 break;
97 if (escapedChar)
98 continue;
101 while (p != next)
103 res.append(*p);
104 ++p;
106 p = next;
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);
153 sValue
154 = OUString(decodedValue.cstr(), decodedValue.len(), RTL_TEXTENCODING_UTF8);
155 break;
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: */