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>
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
)
40 const char *const end
= p
+ sz
;
43 const char *const next
= librvng_utf8_next_char(p
);
46 // oops, the string is invalid
49 if (p
+4 <= end
&& p
+1==next
&& *p
=='&')
51 // look for & , < , > , ' , "
52 bool escapedChar
=false;
56 if (p
+5 <= end
&& strncmp(p
,"&",5)==0)
62 else if (p
+6 <= end
&& strncmp(p
,"'",6)==0)
70 if (strncmp(p
,">",4)==0)
78 if (strncmp(p
,"<",4)==0)
86 if (p
+6 <= end
&& strncmp(p
,""",6)==0)
96 if (escapedChar
) continue;
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
) :
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
);
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: */