Bump version to 4.1-6
[LibreOffice.git] / writerfilter / source / resourcemodel / resourcemodel.cxx
blobbf414caca4b664a473992915a919164563bccb63
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
21 #include <resourcemodel/WW8ResourceModel.hxx>
22 #include <resourcemodel/TableManager.hxx>
23 #include <resourcemodel/QNameToString.hxx>
24 #include <resourcemodel/exceptions.hxx>
25 #include <resourcemodel/SubSequence.hxx>
26 #include <resourcemodel/util.hxx>
27 #include <resourcemodel.hxx>
29 namespace writerfilter {
31 class ResourceModelOutputWithDepth : public OutputWithDepth<string>
33 public:
34 ResourceModelOutputWithDepth()
35 : OutputWithDepth<string>("<tablegroup>", "</tablegroup>") {}
37 ~ResourceModelOutputWithDepth() {outputGroup();}
39 void output(const string & str) const { cout << str << endl; }
42 ResourceModelOutputWithDepth output;
44 void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
45 writerfilter::Reference<Properties>::Pointer_t /*props*/)
49 void dump(OutputWithDepth<string> & o, const char * name, sal_uInt32 n)
51 char sBuffer[256];
52 snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIuUINT32, n);
53 string tmpStr = name;
54 tmpStr += "=";
55 tmpStr += sBuffer;
57 o.addItem(tmpStr);
60 void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
61 const OUString & /*str*/)
65 void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
66 writerfilter::Reference<BinaryObj>::Pointer_t /*binary*/)
70 string gInfo = "";
71 // ------- WW8TableDataHandler ---------
73 class TablePropsRef : public writerfilter::Reference<Properties>
75 public:
76 typedef boost::shared_ptr<TablePropsRef> Pointer_t;
78 TablePropsRef() {}
79 virtual ~TablePropsRef() {}
81 virtual void resolve(Properties & /*rHandler*/) {}
83 virtual string getType() const { return "TableProps"; }
84 void reset() {}
85 void InsertProps(Pointer_t /* pTablePropsRef */) {}
88 typedef TablePropsRef::Pointer_t TablePropsRef_t;
90 class WW8TableDataHandler : public TableDataHandler<string,
91 TablePropsRef_t>
93 public:
94 virtual ~WW8TableDataHandler() {}
96 typedef boost::shared_ptr<WW8TableDataHandler> Pointer_t;
97 virtual void startTable(unsigned int nRows, unsigned int nDepth,
98 TablePropsRef_t pProps);
99 virtual void endTable(unsigned int nestedTableLevel);
100 virtual void startRow(unsigned int nCols,
101 TablePropsRef_t pProps);
102 virtual void endRow();
103 virtual void startCell(const string & start, TablePropsRef_t pProps);
104 virtual void endCell(const string & end);
107 void WW8TableDataHandler::startTable(unsigned int nRows, unsigned int nDepth,
108 TablePropsRef_t /*pProps*/)
110 char sBuffer[256];
112 string tmpStr = "<tabledata.table rows=\"";
113 snprintf(sBuffer, sizeof(sBuffer), "%u", nRows);
114 tmpStr += sBuffer;
115 tmpStr += "\" depth=\"";
116 snprintf(sBuffer, sizeof(sBuffer), "%u", nDepth);
117 tmpStr += sBuffer;
118 tmpStr += "\">";
120 output.addItem(tmpStr);
123 void WW8TableDataHandler::endTable(unsigned int /*nestedTableLevel*/)
125 output.addItem("</tabledata.table>");
128 void WW8TableDataHandler::startRow
129 (unsigned int nCols, TablePropsRef_t /*pProps*/)
131 char sBuffer[256];
133 snprintf(sBuffer, sizeof(sBuffer), "%u", nCols);
134 string tmpStr = "<tabledata.row cells=\"";
135 tmpStr += sBuffer;
136 tmpStr += "\">";
137 output.addItem(tmpStr);
140 void WW8TableDataHandler::endRow()
142 output.addItem("</tabledata.row>");
145 void WW8TableDataHandler::startCell(const string & start,
146 TablePropsRef_t /*pProps*/)
148 output.addItem("<tabledata.cell>");
149 output.addItem(start);
150 output.addItem(", ");
153 void WW8TableDataHandler::endCell(const string & end)
155 output.addItem(end);
156 output.addItem("</tabledata.cell>");
159 // ----- WW8TableDataManager -------------------------------
161 class WW8TableManager :
162 public TableManager<string, TablePropsRef_t>
164 typedef TableDataHandler<string, TablePropsRef_t>
165 TableDataHandlerPointer_t;
167 public:
168 WW8TableManager();
169 virtual ~WW8TableManager() {}
170 virtual void endParagraphGroup();
171 virtual bool sprm(Sprm & rSprm);
174 WW8TableManager::WW8TableManager()
176 TableDataHandler<string, TablePropsRef_t>::Pointer_t pHandler(new WW8TableDataHandler());
177 setHandler(pHandler);
180 bool WW8TableManager::sprm(Sprm & rSprm)
182 TableManager<string, TablePropsRef_t>::sprm(rSprm);
183 output.setDepth(getTableDepthNew());
184 return true;
187 void WW8TableManager::endParagraphGroup()
189 string tmpStr = "<tabledepth depth=\"";
190 char sBuffer[256];
191 snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIuUINT32, getTableDepthNew());
192 tmpStr += sBuffer;
193 tmpStr += "\"/>";
194 output.addItem(tmpStr);
195 TableManager<string, TablePropsRef_t>::endParagraphGroup();
198 /* WW8StreamHandler */
200 WW8StreamHandler::WW8StreamHandler()
201 : mnUTextCount(0)
203 output.closeGroup();
204 output.addItem("<stream>");
205 mpTableManager = new WW8TableManager;
206 mpTableManager->startLevel();
209 WW8StreamHandler::~WW8StreamHandler()
211 mpTableManager->endLevel();
212 delete mpTableManager;
214 output.closeGroup();
215 output.addItem("</stream>");
218 void WW8StreamHandler::startSectionGroup()
220 output.addItem("<section-group>");
223 void WW8StreamHandler::endSectionGroup()
225 output.addItem("</section-group>");
228 void WW8StreamHandler::startParagraphGroup()
230 output.openGroup();
231 output.addItem("<paragraph-group>");
233 mpTableManager->startParagraphGroup();
234 mpTableManager->handle(gInfo);
237 void WW8StreamHandler::endParagraphGroup()
239 mpTableManager->endParagraphGroup();
241 output.addItem("</paragraph-group>");
242 output.closeGroup();
246 void WW8StreamHandler::startCharacterGroup()
248 output.addItem("<character-group>");
251 void WW8StreamHandler::endCharacterGroup()
253 output.addItem("</character-group>");
256 void WW8StreamHandler::startShape( ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > /*xShape*/ )
258 output.addItem("<shape>");
261 void WW8StreamHandler::endShape( )
263 output.addItem( "</shape>" );
266 void WW8StreamHandler::text(const sal_uInt8 * data, size_t len)
268 string tmpStr = "<text>";
270 for (unsigned int n = 0; n < len; ++n)
272 switch (static_cast<unsigned char>(data[n]))
274 case '<':
275 tmpStr += "&lt;";
277 break;
278 case '>':
279 tmpStr += "&gt;";
281 break;
283 case '&':
284 tmpStr += "&amp;";
286 break;
287 default:
288 if (isprint(data[n]))
289 tmpStr += static_cast<char>(data[n]);
290 else
292 char sBuffer[256];
294 snprintf(sBuffer, sizeof(sBuffer), "\\0x%02x", data[n]);
296 tmpStr += sBuffer;
301 tmpStr += "</text>";
303 output.addItem(tmpStr);
305 mpTableManager->text(data, len);
308 void WW8StreamHandler::utext(const sal_uInt8 * data, size_t len)
310 static char sBuffer[256];
311 snprintf(sBuffer, sizeof(sBuffer), "<utext count=\"%d\">", mnUTextCount);
312 string tmpStr(sBuffer);
314 for (unsigned int n = 0; n < len; ++n)
316 sal_Unicode nChar = data[n * 2] + (data[n * 2 + 1] << 8);
317 if (nChar < 0xff && isprint(nChar))
319 switch (nChar)
321 case '&':
322 tmpStr += "&amp;";
323 break;
324 case '<':
325 tmpStr += "&lt;";
326 break;
327 case '>':
328 tmpStr += "&gt;";
329 break;
330 default:
331 tmpStr += static_cast<char>(nChar);
334 else
336 snprintf(sBuffer, sizeof(sBuffer), "\\0x%04x", nChar);
338 tmpStr += sBuffer;
342 tmpStr += "</utext>";
344 output.addItem(tmpStr);
346 mpTableManager->utext(data, len);
348 mnUTextCount++;
351 void WW8StreamHandler::props(writerfilter::Reference<Properties>::Pointer_t ref)
353 WW8PropertiesHandler aHandler(mpTableManager);
355 output.addItem("<properties type=\"" + ref->getType() + "\">");
356 ref->resolve(aHandler);
358 //mpTableManager->props(ref);
360 output.addItem("</properties>");
363 void WW8StreamHandler::table(Id name, writerfilter::Reference<Table>::Pointer_t ref)
365 WW8TableHandler aHandler(mpTableManager);
367 output.addItem("<table id=\"" + (*QNameToString::Instance())(name)
368 + "\">");
372 ref->resolve(aHandler);
374 catch (const Exception &e)
376 output.addItem("<exception>" + e.getText() + "</exception>");
379 output.addItem("</table>");
382 void WW8StreamHandler::substream(Id name,
383 writerfilter::Reference<Stream>::Pointer_t ref)
385 output.addItem("<substream name=\"" + (*QNameToString::Instance())(name)
386 + "\">");
388 mpTableManager->startLevel();
390 ref->resolve(*this);
392 mpTableManager->endLevel();
394 output.addItem("</substream>");
397 void WW8StreamHandler::info(const string & info_)
399 gInfo = info_;
400 output.addItem("<info>" + info_ + "</info>");
403 void WW8PropertiesHandler::attribute(Id name, Value & val)
405 boost::shared_ptr<OString> pStr(new OString());
406 OUString aStr = val.getString();
407 aStr.convertToString(pStr.get(), RTL_TEXTENCODING_ASCII_US,
408 OUSTRING_TO_OSTRING_CVTFLAGS);
409 string sXMLValue = xmlify(pStr->getStr());
411 char sBuffer[256];
412 snprintf(sBuffer, sizeof(sBuffer), "0x%x", val.getInt());
414 output.addItem("<attribute name=\"" +
415 (*QNameToString::Instance())(name) +
416 "\" value=\"" +
417 sXMLValue +
418 + "\" hexvalue=\""
419 + sBuffer + "\">");
421 writerfilter::Reference<Properties>::Pointer_t pProps = val.getProperties();
423 if (pProps.get() != NULL)
425 output.addItem("<properties name=\"" +
426 (*QNameToString::Instance())(name)
427 + "\" type=\"" + pProps->getType() + "\">");
431 pProps->resolve(*this);
433 catch (const ExceptionOutOfBounds &)
437 output.addItem("</properties>");
440 writerfilter::Reference<Stream>::Pointer_t pStream = val.getStream();
442 if (pStream.get() != NULL)
446 WW8StreamHandler aHandler;
448 pStream->resolve(aHandler);
450 catch (const ExceptionOutOfBounds &)
455 writerfilter::Reference<BinaryObj>::Pointer_t pBinObj = val.getBinary();
457 if (pBinObj.get() != NULL)
461 WW8BinaryObjHandler aHandler;
463 pBinObj->resolve(aHandler);
465 catch (const ExceptionOutOfBounds &)
470 output.addItem("</attribute>");
473 void WW8PropertiesHandler::sprm(Sprm & sprm_)
475 string tmpStr = "<sprm id=\"";
476 char buffer[256];
477 snprintf(buffer, sizeof(buffer), "0x%" SAL_PRIxUINT32, sprm_.getId());
478 tmpStr += buffer;
479 tmpStr += "\" name=\"";
480 tmpStr += sprm_.getName();
481 tmpStr += "\">";
482 output.addItem(tmpStr);
483 output.addItem(sprm_.toString());
485 writerfilter::Reference<Properties>::Pointer_t pProps = sprm_.getProps();
487 if (pProps.get() != NULL)
489 output.addItem("<properties type=\"" + pProps->getType() + "\">");
490 pProps->resolve(*this);
491 output.addItem("</properties>");
494 writerfilter::Reference<BinaryObj>::Pointer_t pBinObj = sprm_.getBinary();
496 if (pBinObj.get() != NULL)
498 output.addItem("<binary>");
499 WW8BinaryObjHandler aHandler;
500 pBinObj->resolve(aHandler);
501 output.addItem("</binary>");
504 writerfilter::Reference<Stream>::Pointer_t pStream = sprm_.getStream();
506 if (pStream.get() != NULL)
508 output.addItem("<stream>");
509 WW8StreamHandler aHandler;
510 pStream->resolve(aHandler);
511 output.addItem("</stream>");
514 mpTableManager->sprm(sprm_);
516 output.addItem("</sprm>");
519 void WW8TableHandler::entry(int /*pos*/,
520 writerfilter::Reference<Properties>::Pointer_t ref)
522 output.addItem("<tableentry>");
524 WW8PropertiesHandler aHandler(mpTableManager);
528 ref->resolve(aHandler);
530 catch (const Exception &e)
532 output.addItem("<exception>" + e.getText() + "</exception>");
533 output.addItem("</tableentry>");
535 throw;
538 output.addItem("</tableentry>");
541 void WW8BinaryObjHandler::data
542 (const sal_uInt8 * buf, size_t length,
543 writerfilter::Reference<Properties>::Pointer_t /*pRef*/)
545 SubSequence<sal_uInt8> aSeq(buf, length);
547 aSeq.dump(output);
552 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */