Bump version to 5.0-14
[LibreOffice.git] / writerperfect / source / common / DocumentHandler.cxx
blob3ea4d053e4a67d48bd1b547a3b7bb1afe08ae2ce
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>
20 namespace writerperfect
23 static const unsigned char librvng_utf8_skip_data[256] =
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,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,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,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,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,1,1,1,
30 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,1,1,1,
31 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
32 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
35 #define librvng_utf8_next_char(p) ((p) + librvng_utf8_skip_data[*reinterpret_cast<unsigned char const *>(p)])
37 static void unescapeXML(const char *s, const unsigned long sz, librevenge::RVNGString &res)
39 const char *p = s;
40 const char *const end = p + sz;
41 while (p != end)
43 const char *const next = librvng_utf8_next_char(p);
44 if (next > end)
46 // oops, the string is invalid
47 break;
49 if (p+4 <= end && p+1==next && *p=='&')
51 // look for &amp; , &lt; , &gt; , &apos; , &quot;
52 bool escapedChar=false;
53 switch (*(p+1))
55 case 'a':
56 if (p+5 <= end && strncmp(p,"&amp;",5)==0)
58 res.append('&');
59 p+=5;
60 escapedChar=true;
62 else if (p+6 <= end && strncmp(p,"&apos;",6)==0)
64 res.append('\'');
65 p+=6;
66 escapedChar=true;
68 break;
69 case 'g':
70 if (strncmp(p,"&gt;",4)==0)
72 res.append('>');
73 p+=4;
74 escapedChar=true;
76 break;
77 case 'l':
78 if (strncmp(p,"&lt;",4)==0)
80 res.append('<');
81 p+=4;
82 escapedChar=true;
84 break;
85 case 'q':
86 if (p+6 <= end && strncmp(p,"&quot;",6)==0)
88 res.append('"');
89 p+=6;
90 escapedChar=true;
92 break;
93 default:
94 break;
96 if (escapedChar) continue;
99 while (p != next)
101 res.append(*p);
102 ++p;
104 p = next;
108 using com::sun::star::uno::Reference;
109 using com::sun::star::xml::sax::XAttributeList;
110 using com::sun::star::xml::sax::XDocumentHandler;
112 DocumentHandler::DocumentHandler(Reference < XDocumentHandler > &xHandler) :
113 mxHandler(xHandler)
117 void DocumentHandler::startDocument()
119 mxHandler->startDocument();
122 void DocumentHandler::endDocument()
124 mxHandler->endDocument();
127 void DocumentHandler::startElement(const char *psName, 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(), strlen(i()->getStr().cstr()), 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])=
147 "draw:name", "svg:font-family", "style:condition", "style:num-prefix", "style:num-suffix",
148 "table:formula", "text:bullet-char", "text:label", "xlink:href"
150 static size_t const(listEncodedLength[9])= {9,15,15,16,16,13,16,10,10};
151 if (keyLength==listEncodedLength[j] && strncmp(i.key(), listEncoded[j], keyLength)==0)
153 librevenge::RVNGString decodedValue("");
154 unescapeXML(i()->getStr().cstr(), (unsigned long) strlen(i()->getStr().cstr()), decodedValue);
155 sValue=OUString(decodedValue.cstr(), strlen(decodedValue.cstr()), RTL_TEXTENCODING_UTF8);
156 break;
159 pAttrList->AddAttribute(sName, sValue);
163 OUString sElementName(psName, strlen(psName), RTL_TEXTENCODING_UTF8);
164 mxHandler->startElement(sElementName, xAttrList);
167 void DocumentHandler::endElement(const char *psName)
169 OUString sElementName(psName, strlen(psName), RTL_TEXTENCODING_UTF8);
170 mxHandler->endElement(sElementName);
173 void DocumentHandler::characters(const librevenge::RVNGString &sCharacters)
175 OUString sCharU16(sCharacters.cstr(), strlen(sCharacters.cstr()), RTL_TEXTENCODING_UTF8);
176 mxHandler->characters(sCharU16);
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */