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 
24 #include <WW8ResourceModelImpl.hxx>
25 #include <resources.hxx>
26 #include <WW8OutputWithDepth.hxx>
27 #include <resourcemodel/TableManager.hxx>
28 #include <rtl/string.hxx>
29 #include <resourcemodel/QNameToString.hxx>
30 
31 namespace writerfilter {
32 
33 namespace doctok
34 {
35 using namespace ::std;
36 
37 
38 // ------- WW8TableDataHandler ---------
39 
40 typedef WW8PropertySet::Pointer_t TablePropsPointer_t;
41 
42 class WW8TableDataHandler : public TableDataHandler<string,
43                             TablePropsPointer_t>
44 {
45 public:
46     typedef boost::shared_ptr<WW8TableDataHandler> Pointer_t;
47     virtual void startTable(
48         unsigned int nRows,
49         unsigned int nDepth,
50         TablePropsPointer_t pProps );
51     virtual void endTable(
52         const unsigned int nDepth );
53     virtual void startRow(unsigned int nCols,
54                           TablePropsPointer_t pProps);
55     virtual void endRow();
56     virtual void startCell(const string & start, TablePropsPointer_t pProps);
57     virtual void endCell(const string & end);
58 };
59 
startTable(unsigned int nRows,unsigned int nDepth,TablePropsPointer_t)60 void WW8TableDataHandler::startTable(unsigned int nRows, unsigned int nDepth,
61                                      TablePropsPointer_t /*pProps*/)
62 {
63     char sBuffer[256];
64 
65     string tmpStr = "<tabledata.table rows=\"";
66     snprintf(sBuffer, sizeof(sBuffer), "%d", nRows);
67     tmpStr += sBuffer;
68     tmpStr += "\" depth=\"";
69     snprintf(sBuffer, sizeof(sBuffer), "%d", nDepth);
70     tmpStr += sBuffer;
71     tmpStr += "\">";
72 
73     output.addItem(tmpStr);
74 }
75 
endTable(const unsigned int)76 void WW8TableDataHandler::endTable(
77     const unsigned int /*nTableDepth*/ )
78 {
79     output.addItem("</tabledata.table>");
80 }
81 
startRow(unsigned int nCols,TablePropsPointer_t)82 void WW8TableDataHandler::startRow
83 (unsigned int nCols, TablePropsPointer_t /*pProps*/)
84 {
85     char sBuffer[256];
86 
87     snprintf(sBuffer, sizeof(sBuffer), "%d", nCols);
88     string tmpStr = "<tabledata.row cells=\"";
89     tmpStr += sBuffer;
90     tmpStr += "\">";
91     output.addItem(tmpStr);
92 }
93 
endRow()94 void WW8TableDataHandler::endRow()
95 {
96     output.addItem("</tabledata.row>");
97 }
98 
startCell(const string & start,TablePropsPointer_t)99 void WW8TableDataHandler::startCell(const string & start,
100                                     TablePropsPointer_t /*pProps*/)
101 {
102     output.addItem("<tabledata.cell>");
103     output.addItem(start);
104     output.addItem(", ");
105 }
106 
endCell(const string & end)107 void WW8TableDataHandler::endCell(const string & end)
108 {
109     output.addItem(end);
110     output.addItem("</tabledata.cell>");
111 }
112 
113 // ----- WW8TableDataManager -------------------------------
114 
115 class WW8TableManager :
116     public TableManager<string, TablePropsPointer_t>
117 {
118     typedef TableDataHandler<string, TablePropsPointer_t>
119     TableDataHandlerPointer_t;
120 
121 public:
122     WW8TableManager();
~WW8TableManager()123     virtual ~WW8TableManager() {}
124     virtual void endParagraphGroup();
125     virtual bool sprm(Sprm & rSprm);
126 };
127 
WW8TableManager()128 WW8TableManager::WW8TableManager()
129 {
130     TableDataHandler<string, TablePropsPointer_t>::Pointer_t pHandler(new WW8TableDataHandler());
131     setHandler(pHandler);
132 }
133 
sprm(Sprm & rSprm)134 bool WW8TableManager::sprm(Sprm & rSprm)
135 {
136     TableManager<string, TablePropsPointer_t>::sprm(rSprm);
137     output.setDepth(getTableDepthNew());
138     return true;
139 }
140 
endParagraphGroup()141 void WW8TableManager::endParagraphGroup()
142 {
143     string tmpStr = "<tabledepth depth=\"";
144     char sBuffer[256];
145     snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIdINT32, getTableDepthNew());
146     tmpStr += sBuffer;
147     tmpStr += "\"/>";
148     output.addItem(tmpStr);
149     TableManager<string, TablePropsPointer_t>::endParagraphGroup();
150 }
151 
152 WW8TableManager gTableManager;
153 
154 
155 //-------- WW8TableReference -----------------------------------
156 
resolve(Table &)157 void WW8TableReference::resolve(Table & /*rHandler*/)
158 {
159     output.addItem("<table/>");
160 }
161 
getType() const162 string WW8TableReference::getType() const
163 {
164     return "WW8TableReference";
165 }
166 
resolve(Properties & rHandler)167 void WW8PropertiesReference::resolve(Properties & rHandler)
168 {
169     if( bool(mpPropSet))
170     {
171         if (mpPropSet->isPap())
172         {
173             WW8IntValue aValue(mpPropSet->get_istd());
174 
175             rHandler.attribute(NS_rtf::LN_ISTD, aValue);
176         }
177 
178         WW8PropertySetIterator::Pointer_t pIt = mpPropSet->begin();
179         WW8PropertySetIterator::Pointer_t pItEnd = mpPropSet->end();
180 
181         try
182         {
183             while (! pIt->equal(*pItEnd))
184             {
185                 WW8Sprm aSprm(pIt->get());
186                 rHandler.sprm(aSprm);
187 
188                 ++(*pIt);
189             }
190         }
191         catch (ExceptionOutOfBounds e)
192         {
193             (void) e;
194         }
195     }
196 }
197 
getType() const198 string WW8PropertiesReference::getType() const
199 {
200     return "WW8PropertiesReference";
201 }
202 
WW8BinaryObjReference(WW8StructBase & rParent,sal_uInt32 nOffset,sal_uInt32 nCount)203 WW8BinaryObjReference::WW8BinaryObjReference
204 (WW8StructBase & rParent, sal_uInt32 nOffset, sal_uInt32 nCount)
205 : WW8StructBase(rParent, nOffset, nCount)
206 {
207 }
208 
WW8BinaryObjReference(WW8StructBase * pParent,sal_uInt32 nOffset,sal_uInt32 nCount)209 WW8BinaryObjReference::WW8BinaryObjReference
210 (WW8StructBase * pParent, sal_uInt32 nOffset, sal_uInt32 nCount)
211 : WW8StructBase(pParent, nOffset, nCount)
212 {
213 }
214 
WW8BinaryObjReference(WW8StructBase * pParent)215 WW8BinaryObjReference::WW8BinaryObjReference
216 (WW8StructBase * pParent)
217 : WW8StructBase(pParent, 0x0, pParent->getCount())
218 {
219 }
220 
WW8BinaryObjReference(WW8Stream & rStream,sal_uInt32 nOffset,sal_uInt32 nCount)221 WW8BinaryObjReference::WW8BinaryObjReference
222 (WW8Stream & rStream, sal_uInt32 nOffset, sal_uInt32 nCount)
223 : WW8StructBase(rStream, nOffset, nCount)
224 {
225 }
226 
227 writerfilter::Reference<BinaryObj>::Pointer_t
getBinary()228 WW8BinaryObjReference::getBinary()
229 {
230     return writerfilter::Reference<BinaryObj>::Pointer_t
231         (new WW8BinaryObjReference(*this));
232 }
233 
resolve(BinaryObj & rHandler)234 void WW8BinaryObjReference::resolve(BinaryObj & rHandler)
235 {
236     writerfilter::Reference<Properties>::Pointer_t pRef =
237         writerfilter::Reference<Properties>::Pointer_t();
238 
239     if (getCount() > 0)
240         rHandler.data(get(0), getCount(), pRef);
241 }
242 
getType() const243 string WW8BinaryObjReference::getType() const
244 {
245     return "WW8BinaryObjReference";
246 }
247 
getId() const248 sal_uInt32 WW8Sprm::getId() const
249 {
250     sal_uInt32 nResult = 0;
251 
252     if (mpProperty.get() != NULL)
253         nResult = mpProperty->getId();
254     else if (mpBinary.get() != NULL)
255         nResult = NS_rtf::LN_blob;
256 
257     return nResult;
258 }
259 
toString() const260 string WW8Sprm::toString() const
261 {
262     string sResult = "";
263 
264     if (mpProperty.get() != NULL)
265         sResult = mpProperty->toString();
266 
267     return sResult;
268 }
269 
getValue()270 Value::Pointer_t WW8Sprm::getValue()
271 {
272     Value::Pointer_t pResult;
273 
274     if (mpProperty.get() != NULL)
275         pResult = Value::Pointer_t(createValue(mpProperty->getParam()));
276 
277     return pResult;
278 }
279 
getBinary()280 writerfilter::Reference<BinaryObj>::Pointer_t WW8Sprm::getBinary()
281 {
282     writerfilter::Reference<BinaryObj>::Pointer_t pResult;
283 
284     if (mpBinary.get() != NULL)
285         pResult = writerfilter::Reference<BinaryObj>::Pointer_t
286             (mpBinary->clone());
287     else if (mpProperty.get() != NULL)
288         pResult = createSprmBinary
289             (dynamic_cast<WW8PropertyImpl &>(*(mpProperty.get())));
290 
291     return pResult;
292 }
293 
getStream()294 writerfilter::Reference<Stream>::Pointer_t WW8Sprm::getStream()
295 {
296     return writerfilter::Reference<Stream>::Pointer_t();
297 }
298 
getProps()299 writerfilter::Reference<Properties>::Pointer_t WW8Sprm::getProps()
300 {
301     writerfilter::Reference<Properties>::Pointer_t pResult;
302 
303     if (mpProperty.get() != NULL)
304     {
305         pResult = createSprmProps
306             (dynamic_cast<WW8PropertyImpl &>(*(mpProperty.get())));
307     }
308 
309     return pResult;
310 }
311 
getKind()312 Sprm::Kind WW8Sprm::getKind()
313 {
314     return SprmKind(getId());
315 }
316 
getName() const317 string WW8Sprm::getName() const
318 {
319     return (*SprmIdToString::Instance())(getId());
320 }
321 
getInt() const322 sal_Int32 WW8Value::getInt() const
323 {
324     return 0;
325 }
326 
getAny() const327 uno::Any WW8Value::getAny() const
328 {
329     return uno::Any();
330 }
331 
getString() const332 ::rtl::OUString WW8Value::getString() const
333 {
334     return ::rtl::OUString();
335 }
336 
toString() const337 string WW8Value::toString() const
338 {
339     return string();
340 }
341 
getProperties()342 writerfilter::Reference<Properties>::Pointer_t WW8Value::getProperties()
343 {
344     return writerfilter::Reference<Properties>::Pointer_t();
345 }
346 
getStream()347 writerfilter::Reference<Stream>::Pointer_t WW8Value::getStream()
348 {
349     return writerfilter::Reference<Stream>::Pointer_t();
350 }
351 
getBinary()352 writerfilter::Reference<BinaryObj>::Pointer_t WW8Value::getBinary()
353 {
354     return writerfilter::Reference<BinaryObj>::Pointer_t();
355 }
356 
getInt() const357 sal_Int32 WW8IntValue::getInt() const
358 {
359     return mValue;
360 }
361 
getString() const362 ::rtl::OUString WW8IntValue::getString() const
363 {
364     return ::rtl::OUString::valueOf(static_cast<sal_Int32>(mValue));
365 }
366 
getAny() const367 uno::Any WW8IntValue::getAny() const
368 {
369     uno::Any aResult;
370 
371     aResult <<= static_cast<sal_uInt32>(mValue);
372 
373     return aResult;
374 }
375 
toString() const376 string WW8IntValue::toString() const
377 {
378     char sBuffer[255];
379 
380     snprintf(sBuffer, sizeof(sBuffer), "%x", mValue);
381 
382     return string(sBuffer);
383 }
384 
createValue(int value)385 WW8Value::Pointer_t createValue(int value)
386 {
387     return WW8Value::Pointer_t(new WW8IntValue(value));
388 }
389 
createValue(WW8Value::Pointer_t value)390 WW8Value::Pointer_t createValue(WW8Value::Pointer_t value)
391 {
392     return value;
393 }
394 
getInt() const395 sal_Int32 WW8StringValue::getInt() const
396 {
397     return 0;
398 }
399 
getString() const400 ::rtl::OUString WW8StringValue::getString() const
401 {
402     return mString;
403 }
404 
getAny() const405 uno::Any WW8StringValue::getAny() const
406 {
407     uno::Any aResult;
408 
409     aResult <<= mString;
410 
411     return aResult;
412 }
413 
toString() const414 string WW8StringValue::toString() const
415 {
416     string result;
417 
418     sal_uInt32 nCount = mString.getLength();
419     for (sal_uInt32 n = 0; n < nCount; ++n)
420     {
421         if (mString[n] <= 0xff && isprint(mString[n]))
422         {
423             sal_Unicode nC = mString[n];
424 
425             if (nC < 256)
426                 result += sal::static_int_cast<char>(nC);
427             else
428                 result += ".";
429         }
430         else
431         {
432             char sBuffer[64];
433 
434             snprintf(sBuffer, sizeof(sBuffer), "\\u%04x", mString[n]);
435             result += sBuffer;
436         }
437     }
438 
439     return result;
440 }
441 
createValue(const rtl::OUString & rStr)442 WW8Value::Pointer_t createValue(const rtl::OUString & rStr)
443 {
444     return WW8Value::Pointer_t(new WW8StringValue(rStr));
445 }
446 
447 writerfilter::Reference<Properties>::Pointer_t
getProperties()448 WW8PropertiesValue::getProperties()
449 {
450     return mRef;
451 }
452 
toString() const453 string WW8PropertiesValue::toString() const
454 {
455     return "properties";
456 }
457 
getStream()458 writerfilter::Reference<Stream>::Pointer_t WW8StreamValue::getStream()
459 {
460     return mRef;
461 }
462 
toString() const463 string WW8StreamValue::toString() const
464 {
465     return "stream";
466 }
467 
getBinary()468 writerfilter::Reference<BinaryObj>::Pointer_t WW8BinaryObjValue::getBinary()
469 {
470     return mRef;
471 }
472 
toString() const473 string WW8BinaryObjValue::toString() const
474 {
475     return "binaryObj";
476 }
477 
createValue(writerfilter::Reference<Properties>::Pointer_t rRef)478 WW8Value::Pointer_t createValue
479 (writerfilter::Reference<Properties>::Pointer_t rRef)
480 {
481     return WW8Value::Pointer_t(new WW8PropertiesValue(rRef));
482 }
483 
createValue(writerfilter::Reference<Stream>::Pointer_t rRef)484 WW8Value::Pointer_t createValue(writerfilter::Reference<Stream>::Pointer_t rRef)
485 {
486     return WW8Value::Pointer_t(new WW8StreamValue(rRef));
487 }
488 
createValue(writerfilter::Reference<BinaryObj>::Pointer_t rRef)489 WW8Value::Pointer_t createValue
490 (writerfilter::Reference<BinaryObj>::Pointer_t rRef)
491 {
492     return WW8Value::Pointer_t(new WW8BinaryObjValue(rRef));
493 }
494 
495 
496 }
497 
498 }
499 
500