1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <fstream>
26 #include <string>
27 #include <com/sun/star/awt/Point.hpp>
28 #include <com/sun/star/awt/Rectangle.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/drawing/BitmapMode.hpp>
31 #include <com/sun/star/drawing/FillStyle.hpp>
32 #include <com/sun/star/drawing/HomogenMatrix3.hpp>
33 #include <com/sun/star/text/TextContentAnchorType.hpp>
34 #include <resourcemodel/WW8ResourceModel.hxx>
35 #include <resourcemodel/TagLogger.hxx>
36 #include <resourcemodel/util.hxx>
37 
38 namespace writerfilter
39 {
40 using namespace com::sun::star;
41 using namespace std;
42 using text::TextContentAnchorType;
43 
logger_file()44 static string & logger_file()
45 {
46 	static string _logger_file = string(getenv("TEMP")?getenv("TEMP"):"/tmp") + "/writerfilter.ooxml.tmp";
47 	return _logger_file;
48 }
49 
logger_stream()50 static ofstream & logger_stream()
51 {
52 	static ofstream _logger_stream(logger_file().c_str());
53 	return _logger_stream;
54 }
55 
56 
logger(string prefix,string message)57 void logger(string prefix, string message)
58 {
59     logger_stream() << prefix <<  ":" << message << endl;
60     logger_stream().flush();
61 }
62 
xmlify(const string & str)63     string xmlify(const string & str)
64     {
65         string result = "";
66         char sBuffer[16];
67 
68         for (string::const_iterator aIt = str.begin(); aIt != str.end(); ++aIt)
69         {
70             char c = *aIt;
71 
72             if (isprint(c) && c != '\"')
73             {
74                 if (c == '<')
75                     result += "&lt;";
76                 else if (c == '>')
77                     result += "&gt;";
78                 else if (c == '&')
79                     result += "&amp;";
80                 else
81                     result += c;
82             }
83             else
84             {
85                 snprintf(sBuffer, sizeof(sBuffer), "\\%03d", c);
86                 result += sBuffer;
87             }
88         }
89 
90         return result;
91     }
92 
93 #ifdef DEBUG
propertysetToString(uno::Reference<beans::XPropertySet> const & xPropSet)94 string propertysetToString(uno::Reference<beans::XPropertySet> const & xPropSet)
95 {
96     string sResult;
97 
98     static int nAttribNames = 9;
99     static string sPropertyAttribNames[9] =
100         {
101             "MAYBEVOID",
102             "BOUND",
103             "CONSTRAINED",
104             "TRANSIENT",
105             "READONLY",
106             "MAYBEAMBIGUOUS",
107             "MAYBEDEFAULT",
108             "REMOVEABLE",
109             "OPTIONAL"
110         };
111 
112     static const ::rtl::OUString sMetaFile(RTL_CONSTASCII_USTRINGPARAM("MetaFile"));
113 
114     uno::Reference<beans::XPropertySetInfo> xPropSetInfo
115         (xPropSet->getPropertySetInfo());
116 
117     if (xPropSetInfo.is())
118     {
119         uno::Sequence<beans::Property> aProps(xPropSetInfo->getProperties());
120 
121         sResult +="<propertyset>";
122 
123         for (sal_Int32 n = 0; n < aProps.getLength(); n++)
124         {
125             ::rtl::OUString sPropName(aProps[n].Name);
126 
127             if (xPropSetInfo->hasPropertyByName(sPropName))
128             {
129                 bool bPropertyFound = true;
130                 uno::Any aAny;
131                 try
132                 {
133                     if (sPropName == sMetaFile)
134                         bPropertyFound = false;
135                     else
136                         xPropSet->getPropertyValue(sPropName) >>= aAny;
137                 }
138                 catch (beans::UnknownPropertyException)
139                 {
140                     bPropertyFound = false;
141                 }
142 
143                 if (bPropertyFound)
144                 {
145                     sResult += "<property name=\"";
146                     sResult += OUStringToOString
147                         (sPropName, RTL_TEXTENCODING_ASCII_US).getStr();
148                     sResult +="\" type=\"";
149 
150                     ::rtl::OUString sPropType(aProps[n].Type.getTypeName());
151                     sResult += OUStringToOString
152                         (sPropType, RTL_TEXTENCODING_ASCII_US).getStr();
153 
154                     sResult += "\" attribs=\"";
155 
156                     sal_uInt16 nMask = 1;
157                     bool bFirstAttrib = true;
158                     sal_uInt16 nAttribs = aProps[n].Attributes;
159                     for (int i = 0; i < nAttribNames; i++)
160                     {
161                         if ((nAttribs & nMask) != 0)
162                         {
163                             if (bFirstAttrib)
164                                 bFirstAttrib = false;
165                             else
166                                 sResult += "|";
167 
168                             sResult += sPropertyAttribNames[i];
169                         }
170 
171                         nMask <<= 1;
172                     }
173 
174                     sResult += "\">";
175 
176                     char buffer[256];
177                     if (sPropType ==
178                         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
179                                         ("byte")))
180                     {
181                         sal_Int8 nValue = 0;
182                         aAny >>= nValue;
183 
184                         snprintf(buffer, sizeof(buffer), "%d", nValue);
185                         sResult += buffer;
186                     }
187                     if (sPropType ==
188                         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
189                                         ("short")))
190                     {
191                         sal_Int16 nValue = 0;
192                         aAny >>= nValue;
193 
194                         snprintf(buffer, sizeof(buffer), "%d", nValue);
195                         sResult += buffer;
196                     }
197                     else if (sPropType ==
198                              ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
199                                              ("long")))
200                     {
201                         sal_Int32 nValue = 0;
202                         aAny >>= nValue;
203 
204                         snprintf(buffer, sizeof(buffer), "%" SAL_PRIdINT32, nValue);
205                         sResult += buffer;
206                     }
207                     else if (sPropType ==
208                              ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
209                                              ("float")))
210                     {
211                         float nValue = 0.0;
212                         aAny >>= nValue;
213 
214                         snprintf(buffer, sizeof(buffer), "%f", nValue);
215                         sResult += buffer;
216                     }
217                     else if (sPropType ==
218                              ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
219                                              ("double")))
220                     {
221                         double nValue = 0.0;
222                         aAny >>= nValue;
223 
224                         snprintf(buffer, sizeof(buffer), "%lf", nValue);
225                         sResult += buffer;
226                     }
227                     else if (sPropType ==
228                              ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
229                                              ("boolean")))
230                     {
231                         sal_Bool nValue = sal_False;
232                         aAny >>= nValue;
233 
234                         if (nValue)
235                             sResult += "true";
236                         else
237                             sResult += "false";
238                     }
239                     else if (sPropType ==
240                              ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
241                                              ("string")))
242                     {
243                         ::rtl::OUString sValue;
244                         aAny >>= sValue;
245 
246                         sResult += OUStringToOString
247                             (sValue, RTL_TEXTENCODING_ASCII_US).getStr();
248                     }
249                     else if (sPropType ==
250                              ::rtl::OUString
251                              (RTL_CONSTASCII_USTRINGPARAM
252                               ("com.sun.star.text.TextContentAnchorType")))
253                     {
254                         text::TextContentAnchorType nValue;
255                         aAny >>= nValue;
256 
257                         switch (nValue)
258                         {
259                         case text::TextContentAnchorType_AT_PARAGRAPH:
260                             sResult += "AT_PARAGRAPH";
261                             break;
262                         case text::TextContentAnchorType_AS_CHARACTER:
263                             sResult += "AS_CHARACTER";
264                             break;
265                         case text::TextContentAnchorType_AT_PAGE:
266                             sResult += "AT_PAGE";
267                             break;
268                         case text::TextContentAnchorType_AT_FRAME:
269                             sResult += "AT_FRAME";
270                             break;
271                         case text::TextContentAnchorType_AT_CHARACTER:
272                             sResult += "AT_CHARACTER";
273                             break;
274                         case text::TextContentAnchorType_MAKE_FIXED_SIZE:
275                             sResult += "MAKE_FIXED_SIZE";
276                             break;
277                         default:
278                             break;
279                         }
280                     }
281                     else if (sPropType ==
282                              ::rtl::OUString
283                              (RTL_CONSTASCII_USTRINGPARAM
284                               ("com.sun.star.awt.Point")))
285                     {
286                         awt::Point aPoint;
287                         aAny >>= aPoint;
288 
289                         snprintf(buffer, sizeof(buffer), "(%" SAL_PRIdINT32 ", %" SAL_PRIdINT32 ")", aPoint.X,
290                                  aPoint.Y);
291 
292                         sResult += buffer;
293                     }
294                     else if (sPropType ==
295                              ::rtl::OUString
296                              (RTL_CONSTASCII_USTRINGPARAM
297                               ("com.sun.star.awt.Rectangle")))
298                     {
299                         awt::Rectangle aRect;
300                         aAny >>= aRect;
301 
302                         snprintf(buffer, sizeof(buffer), "(%" SAL_PRIdINT32 ", %" SAL_PRIdINT32 ", %" SAL_PRIdINT32 ", %" SAL_PRIdINT32 ")",
303                                  aRect.X, aRect.Y, aRect.Width, aRect.Height);
304                         sResult += buffer;
305                     }
306                     else if (sPropType ==
307                              ::rtl::OUString
308                              (RTL_CONSTASCII_USTRINGPARAM
309                               ("com.sun.star.drawing.FillStyle")))
310                     {
311                         drawing::FillStyle nValue;
312                         aAny >>= nValue;
313 
314                         switch (nValue)
315                         {
316                         case drawing::FillStyle_NONE:
317                             sResult += "NONE";
318                             break;
319                         case drawing::FillStyle_SOLID:
320                             sResult += "SOLID";
321                             break;
322                         case drawing::FillStyle_GRADIENT:
323                             sResult += "GRADIENT";
324                             break;
325                         case drawing::FillStyle_HATCH:
326                             sResult += "HATCH";
327                             break;
328                         case drawing::FillStyle_BITMAP:
329                             sResult += "BITMAP";
330                             break;
331                         case drawing::FillStyle_MAKE_FIXED_SIZE:
332                             sResult += "MAKE_FIXED_SIZE";
333                             break;
334                         }
335                     }
336                     else if (sPropType ==
337                              ::rtl::OUString
338                              (RTL_CONSTASCII_USTRINGPARAM
339                               ("com.sun.star.drawing.BitmapMode")))
340                     {
341                         drawing::BitmapMode nValue;
342                         aAny >>= nValue;
343 
344                         switch (nValue)
345                         {
346                         case drawing::BitmapMode_REPEAT:
347                             sResult += "REPEAT";
348                             break;
349                         case drawing::BitmapMode_STRETCH:
350                             sResult += "STRETCH";
351                             break;
352                         case drawing::BitmapMode_NO_REPEAT:
353                             sResult += "NO_REPEAT";
354                             break;
355                         case drawing::BitmapMode_MAKE_FIXED_SIZE:
356                             sResult += "MAKE_FIXED_SIZE";
357                             break;
358                         }
359                     }
360                     else if (sPropType ==
361                              ::rtl::OUString
362                              (RTL_CONSTASCII_USTRINGPARAM
363                               ("com.sun.star.drawing.HomogenMatrix3")))
364                     {
365                         drawing::HomogenMatrix3 aMatrix;
366                         aAny >>= aMatrix;
367 
368                         snprintf(buffer, sizeof(buffer),
369                                  "((%f %f %f)(%f %f %f)(%f %f %f))",
370                                  aMatrix.Line1.Column1,
371                                  aMatrix.Line1.Column2,
372                                  aMatrix.Line1.Column3,
373                                  aMatrix.Line2.Column1,
374                                  aMatrix.Line2.Column2,
375                                  aMatrix.Line2.Column3,
376                                  aMatrix.Line3.Column1,
377                                  aMatrix.Line3.Column2,
378                                  aMatrix.Line3.Column3);
379                         sResult += buffer;
380                     }
381 
382                     sResult += "</property>";
383                 }
384             }
385 			else
386 			{
387 				sResult += "<unknown-property>";
388 				sResult += OUStringToOString
389 				(sPropName, RTL_TEXTENCODING_ASCII_US).getStr();
390 				sResult += "</unknown-property>";
391 			}
392         }
393         sResult += "</propertyset>";
394     }
395 
396     return sResult;
397 }
398 
toString(uno::Reference<text::XTextRange> textRange)399 string toString(uno::Reference< text::XTextRange > textRange)
400 {
401     string result;
402 
403     if (textRange.get())
404     {
405         rtl::OUString aOUStr = textRange->getString();
406         rtl::OString aOStr(aOUStr.getStr(), aOUStr.getLength(),  RTL_TEXTENCODING_ASCII_US );
407 
408         result = aOStr.getStr();
409     }
410     else
411     {
412         result="(nil)";
413     }
414 
415     return result;
416 }
417 
toString(const string & rString)418 string toString(const string & rString)
419 {
420     return rString;
421 }
422 #endif
423 }
424