Update ooo320-m1
[ooovba.git] / writerfilter / source / resourcemodel / util.cxx
blobb70d226d97f2305674ea47746b1bbc8eaf053a8e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: util.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fstream>
33 #include <string>
34 #include <com/sun/star/awt/Point.hpp>
35 #include <com/sun/star/awt/Rectangle.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/drawing/BitmapMode.hpp>
38 #include <com/sun/star/drawing/FillStyle.hpp>
39 #include <com/sun/star/drawing/HomogenMatrix3.hpp>
40 #include <com/sun/star/text/TextContentAnchorType.hpp>
41 #include <resourcemodel/WW8ResourceModel.hxx>
42 #include <resourcemodel/TagLogger.hxx>
44 namespace writerfilter
46 using namespace com::sun::star;
47 using namespace std;
48 using text::TextContentAnchorType;
50 static string & logger_file()
52 static string _logger_file = string(getenv("TEMP")?getenv("TEMP"):"/tmp") + "/writerfilter.ooxml.tmp";
53 return _logger_file;
56 static ofstream & logger_stream()
58 static ofstream _logger_stream(logger_file().c_str());
59 return _logger_stream;
63 void logger(string prefix, string message)
65 logger_stream() << prefix << ":" << message << endl;
66 logger_stream().flush();
69 string propertysetToString(uno::Reference<beans::XPropertySet> const & xPropSet)
71 string sResult;
73 static int nAttribNames = 9;
74 static string sPropertyAttribNames[9] =
76 "MAYBEVOID",
77 "BOUND",
78 "CONSTRAINED",
79 "TRANSIENT",
80 "READONLY",
81 "MAYBEAMBIGUOUS",
82 "MAYBEDEFAULT",
83 "REMOVEABLE",
84 "OPTIONAL"
87 static const ::rtl::OUString sMetaFile(RTL_CONSTASCII_USTRINGPARAM("MetaFile"));
89 uno::Reference<beans::XPropertySetInfo> xPropSetInfo
90 (xPropSet->getPropertySetInfo());
92 if (xPropSetInfo.is())
94 uno::Sequence<beans::Property> aProps(xPropSetInfo->getProperties());
96 sResult +="<propertyset>";
98 for (sal_Int32 n = 0; n < aProps.getLength(); n++)
100 ::rtl::OUString sPropName(aProps[n].Name);
102 if (xPropSetInfo->hasPropertyByName(sPropName))
104 bool bPropertyFound = true;
105 uno::Any aAny;
106 try
108 if (sPropName == sMetaFile)
109 bPropertyFound = false;
110 else
111 xPropSet->getPropertyValue(sPropName) >>= aAny;
113 catch (beans::UnknownPropertyException)
115 bPropertyFound = false;
118 if (bPropertyFound)
120 sResult += "<property name=\"";
121 sResult += OUStringToOString
122 (sPropName, RTL_TEXTENCODING_ASCII_US).getStr();
123 sResult +="\" type=\"";
125 ::rtl::OUString sPropType(aProps[n].Type.getTypeName());
126 sResult += OUStringToOString
127 (sPropType, RTL_TEXTENCODING_ASCII_US).getStr();
129 sResult += "\" attribs=\"";
131 sal_uInt16 nMask = 1;
132 bool bFirstAttrib = true;
133 sal_uInt16 nAttribs = aProps[n].Attributes;
134 for (int i = 0; i < nAttribNames; i++)
136 if ((nAttribs & nMask) != 0)
138 if (bFirstAttrib)
139 bFirstAttrib = false;
140 else
141 sResult += "|";
143 sResult += sPropertyAttribNames[i];
146 nMask <<= 1;
149 sResult += "\">";
151 char buffer[256];
152 if (sPropType ==
153 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
154 ("byte")))
156 sal_Int8 nValue = 0;
157 aAny >>= nValue;
159 snprintf(buffer, sizeof(buffer), "%d", nValue);
160 sResult += buffer;
162 if (sPropType ==
163 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
164 ("short")))
166 sal_Int16 nValue = 0;
167 aAny >>= nValue;
169 snprintf(buffer, sizeof(buffer), "%d", nValue);
170 sResult += buffer;
172 else if (sPropType ==
173 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
174 ("long")))
176 sal_Int32 nValue = 0;
177 aAny >>= nValue;
179 snprintf(buffer, sizeof(buffer), "%" SAL_PRIdINT32, nValue);
180 sResult += buffer;
182 else if (sPropType ==
183 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
184 ("float")))
186 float nValue = 0.0;
187 aAny >>= nValue;
189 snprintf(buffer, sizeof(buffer), "%f", nValue);
190 sResult += buffer;
192 else if (sPropType ==
193 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
194 ("double")))
196 double nValue = 0.0;
197 aAny >>= nValue;
199 snprintf(buffer, sizeof(buffer), "%lf", nValue);
200 sResult += buffer;
202 else if (sPropType ==
203 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
204 ("boolean")))
206 sal_Bool nValue = sal_False;
207 aAny >>= nValue;
209 if (nValue)
210 sResult += "true";
211 else
212 sResult += "false";
214 else if (sPropType ==
215 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
216 ("string")))
218 ::rtl::OUString sValue;
219 aAny >>= sValue;
221 sResult += OUStringToOString
222 (sValue, RTL_TEXTENCODING_ASCII_US).getStr();
224 else if (sPropType ==
225 ::rtl::OUString
226 (RTL_CONSTASCII_USTRINGPARAM
227 ("com.sun.star.text.TextContentAnchorType")))
229 text::TextContentAnchorType nValue;
230 aAny >>= nValue;
232 switch (nValue)
234 case text::TextContentAnchorType_AT_PARAGRAPH:
235 sResult += "AT_PARAGRAPH";
236 break;
237 case text::TextContentAnchorType_AS_CHARACTER:
238 sResult += "AS_CHARACTER";
239 break;
240 case text::TextContentAnchorType_AT_PAGE:
241 sResult += "AT_PAGE";
242 break;
243 case text::TextContentAnchorType_AT_FRAME:
244 sResult += "AT_FRAME";
245 break;
246 case text::TextContentAnchorType_AT_CHARACTER:
247 sResult += "AT_CHARACTER";
248 break;
249 case text::TextContentAnchorType_MAKE_FIXED_SIZE:
250 sResult += "MAKE_FIXED_SIZE";
251 break;
252 default:
253 break;
256 else if (sPropType ==
257 ::rtl::OUString
258 (RTL_CONSTASCII_USTRINGPARAM
259 ("com.sun.star.awt.Point")))
261 awt::Point aPoint;
262 aAny >>= aPoint;
264 snprintf(buffer, sizeof(buffer), "(%" SAL_PRIdINT32 ", %" SAL_PRIdINT32 ")", aPoint.X,
265 aPoint.Y);
267 sResult += buffer;
269 else if (sPropType ==
270 ::rtl::OUString
271 (RTL_CONSTASCII_USTRINGPARAM
272 ("com.sun.star.awt.Rectangle")))
274 awt::Rectangle aRect;
275 aAny >>= aRect;
277 snprintf(buffer, sizeof(buffer), "(%" SAL_PRIdINT32 ", %" SAL_PRIdINT32 ", %" SAL_PRIdINT32 ", %" SAL_PRIdINT32 ")",
278 aRect.X, aRect.Y, aRect.Width, aRect.Height);
279 sResult += buffer;
281 else if (sPropType ==
282 ::rtl::OUString
283 (RTL_CONSTASCII_USTRINGPARAM
284 ("com.sun.star.drawing.FillStyle")))
286 drawing::FillStyle nValue;
287 aAny >>= nValue;
289 switch (nValue)
291 case drawing::FillStyle_NONE:
292 sResult += "NONE";
293 break;
294 case drawing::FillStyle_SOLID:
295 sResult += "SOLID";
296 break;
297 case drawing::FillStyle_GRADIENT:
298 sResult += "GRADIENT";
299 break;
300 case drawing::FillStyle_HATCH:
301 sResult += "HATCH";
302 break;
303 case drawing::FillStyle_BITMAP:
304 sResult += "BITMAP";
305 break;
306 case drawing::FillStyle_MAKE_FIXED_SIZE:
307 sResult += "MAKE_FIXED_SIZE";
308 break;
311 else if (sPropType ==
312 ::rtl::OUString
313 (RTL_CONSTASCII_USTRINGPARAM
314 ("com.sun.star.drawing.BitmapMode")))
316 drawing::BitmapMode nValue;
317 aAny >>= nValue;
319 switch (nValue)
321 case drawing::BitmapMode_REPEAT:
322 sResult += "REPEAT";
323 break;
324 case drawing::BitmapMode_STRETCH:
325 sResult += "STRETCH";
326 break;
327 case drawing::BitmapMode_NO_REPEAT:
328 sResult += "NO_REPEAT";
329 break;
330 case drawing::BitmapMode_MAKE_FIXED_SIZE:
331 sResult += "MAKE_FIXED_SIZE";
332 break;
335 else if (sPropType ==
336 ::rtl::OUString
337 (RTL_CONSTASCII_USTRINGPARAM
338 ("com.sun.star.drawing.HomogenMatrix3")))
340 drawing::HomogenMatrix3 aMatrix;
341 aAny >>= aMatrix;
343 snprintf(buffer, sizeof(buffer),
344 "((%f %f %f)(%f %f %f)(%f %f %f))",
345 aMatrix.Line1.Column1,
346 aMatrix.Line1.Column2,
347 aMatrix.Line1.Column3,
348 aMatrix.Line2.Column1,
349 aMatrix.Line2.Column2,
350 aMatrix.Line2.Column3,
351 aMatrix.Line3.Column1,
352 aMatrix.Line3.Column2,
353 aMatrix.Line3.Column3);
354 sResult += buffer;
357 sResult += "</property>";
360 else
362 sResult += "<unknown-property>";
363 sResult += OUStringToOString
364 (sPropName, RTL_TEXTENCODING_ASCII_US).getStr();
365 sResult += "</unknown-property>";
368 sResult += "</propertyset>";
371 return sResult;
373 string xmlify(const string & str)
375 string result = "";
376 char sBuffer[16];
378 for (string::const_iterator aIt = str.begin(); aIt != str.end(); ++aIt)
380 char c = *aIt;
382 if (isprint(c) && c != '\"')
384 if (c == '<')
385 result += "&lt;";
386 else if (c == '>')
387 result += "&gt;";
388 else if (c == '&')
389 result += "&amp;";
390 else
391 result += c;
393 else
395 snprintf(sBuffer, sizeof(sBuffer), "\\%03d", c);
396 result += sBuffer;
400 return result;