update dev300-m58
[ooovba.git] / writerfilter / source / resourcemodel / resourcemodel.cxx
blobf311d36aaee4c609de6bdef60c9a867e807be5ee
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: resourcemodel.cxx,v $
10 * $Revision: 1.7 $
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 ************************************************************************/
31 #include <stdio.h>
32 #include <resourcemodel/WW8ResourceModel.hxx>
33 #include <resourcemodel/TableManager.hxx>
34 #include <resourcemodel/QNameToString.hxx>
35 #include <resourcemodel/exceptions.hxx>
36 #include <resourcemodel/SubSequence.hxx>
37 #include <resourcemodel/util.hxx>
38 #include <resourcemodel.hxx>
40 namespace writerfilter {
42 class ResourceModelOutputWithDepth : public OutputWithDepth<string>
44 public:
45 ResourceModelOutputWithDepth()
46 : OutputWithDepth<string>("<tablegroup>", "</tablegroup>") {}
48 ~ResourceModelOutputWithDepth() {outputGroup();}
50 void output(const string & str) const { cout << str << endl; }
53 ResourceModelOutputWithDepth output;
55 Stream::Pointer_t createStreamHandler()
57 return Stream::Pointer_t(new WW8StreamHandler());
60 void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
61 writerfilter::Reference<Properties>::Pointer_t /*props*/)
65 void dump(OutputWithDepth<string> & o, const char * name, sal_uInt32 n)
67 char sBuffer[256];
68 snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIuUINT32, n);
69 string tmpStr = name;
70 tmpStr += "=";
71 tmpStr += sBuffer;
73 o.addItem(tmpStr);
76 void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
77 const rtl::OUString & /*str*/)
81 void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
82 writerfilter::Reference<BinaryObj>::Pointer_t /*binary*/)
86 string gInfo = "";
87 // ------- WW8TableDataHandler ---------
89 class TablePropsRef : public writerfilter::Reference<Properties>
91 public:
92 typedef boost::shared_ptr<TablePropsRef> Pointer_t;
94 TablePropsRef() {}
95 virtual ~TablePropsRef() {}
97 virtual void resolve(Properties & /*rHandler*/) {}
99 virtual string getType() const { return "TableProps"; }
100 void reset() {}
101 void insert(Pointer_t /* pTablePropsRef */) {}
104 typedef TablePropsRef::Pointer_t TablePropsRef_t;
106 class WW8TableDataHandler : public TableDataHandler<string,
107 TablePropsRef_t>
109 public:
110 typedef boost::shared_ptr<WW8TableDataHandler> Pointer_t;
111 virtual void startTable(unsigned int nRows, unsigned int nDepth,
112 TablePropsRef_t pProps);
113 virtual void endTable();
114 virtual void startRow(unsigned int nCols,
115 TablePropsRef_t pProps);
116 virtual void endRow();
117 virtual void startCell(const string & start, TablePropsRef_t pProps);
118 virtual void endCell(const string & end);
121 void WW8TableDataHandler::startTable(unsigned int nRows, unsigned int nDepth,
122 TablePropsRef_t /*pProps*/)
124 char sBuffer[256];
126 string tmpStr = "<tabledata.table rows=\"";
127 snprintf(sBuffer, sizeof(sBuffer), "%d", nRows);
128 tmpStr += sBuffer;
129 tmpStr += "\" depth=\"";
130 snprintf(sBuffer, sizeof(sBuffer), "%d", nDepth);
131 tmpStr += sBuffer;
132 tmpStr += "\">";
134 output.addItem(tmpStr);
137 void WW8TableDataHandler::endTable()
139 output.addItem("</tabledata.table>");
142 void WW8TableDataHandler::startRow
143 (unsigned int nCols, TablePropsRef_t /*pProps*/)
145 char sBuffer[256];
147 snprintf(sBuffer, sizeof(sBuffer), "%d", nCols);
148 string tmpStr = "<tabledata.row cells=\"";
149 tmpStr += sBuffer;
150 tmpStr += "\">";
151 output.addItem(tmpStr);
154 void WW8TableDataHandler::endRow()
156 output.addItem("</tabledata.row>");
159 void WW8TableDataHandler::startCell(const string & start,
160 TablePropsRef_t /*pProps*/)
162 output.addItem("<tabledata.cell>");
163 output.addItem(start);
164 output.addItem(", ");
167 void WW8TableDataHandler::endCell(const string & end)
169 output.addItem(end);
170 output.addItem("</tabledata.cell>");
173 // ----- WW8TableDataManager -------------------------------
175 class WW8TableManager :
176 public TableManager<string, TablePropsRef_t>
178 typedef TableDataHandler<string, TablePropsRef_t>
179 TableDataHandlerPointer_t;
181 public:
182 WW8TableManager();
183 virtual ~WW8TableManager() {}
184 virtual void endParagraphGroup();
185 virtual bool sprm(Sprm & rSprm);
188 WW8TableManager::WW8TableManager()
190 TableDataHandler<string, TablePropsRef_t>::Pointer_t pHandler(new WW8TableDataHandler());
191 setHandler(pHandler);
194 bool WW8TableManager::sprm(Sprm & rSprm)
196 TableManager<string, TablePropsRef_t>::sprm(rSprm);
197 output.setDepth(getTableDepthNew());
198 return true;
201 void WW8TableManager::endParagraphGroup()
203 string tmpStr = "<tabledepth depth=\"";
204 char sBuffer[256];
205 snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIuUINT32, getTableDepthNew());
206 tmpStr += sBuffer;
207 tmpStr += "\"/>";
208 output.addItem(tmpStr);
209 TableManager<string, TablePropsRef_t>::endParagraphGroup();
212 WW8TableManager gTableManager;
214 /* WW8StreamHandler */
216 WW8StreamHandler::WW8StreamHandler()
217 : mnUTextCount(0)
219 output.closeGroup();
220 output.addItem("<stream>");
221 gTableManager.startLevel();
224 WW8StreamHandler::~WW8StreamHandler()
226 gTableManager.endLevel();
228 output.closeGroup();
229 output.addItem("</stream>");
232 void WW8StreamHandler::startSectionGroup()
234 output.addItem("<section-group>");
237 void WW8StreamHandler::endSectionGroup()
239 output.addItem("</section-group>");
242 void WW8StreamHandler::startParagraphGroup()
244 output.openGroup();
245 output.addItem("<paragraph-group>");
247 gTableManager.startParagraphGroup();
248 gTableManager.handle(gInfo);
251 void WW8StreamHandler::endParagraphGroup()
253 gTableManager.endParagraphGroup();
255 output.addItem("</paragraph-group>");
256 output.closeGroup();
260 void WW8StreamHandler::startCharacterGroup()
262 output.addItem("<character-group>");
265 void WW8StreamHandler::endCharacterGroup()
267 output.addItem("</character-group>");
270 void WW8StreamHandler::text(const sal_uInt8 * data, size_t len)
272 string tmpStr = "<text>";
274 for (unsigned int n = 0; n < len; ++n)
276 switch (static_cast<unsigned char>(data[n]))
278 case '<':
279 tmpStr += "&lt;";
281 break;
282 case '>':
283 tmpStr += "&gt;";
285 break;
287 case '&':
288 tmpStr += "&amp;";
290 break;
291 default:
292 if (isprint(data[n]))
293 tmpStr += static_cast<char>(data[n]);
294 else
296 char sBuffer[256];
298 snprintf(sBuffer, sizeof(sBuffer), "\\0x%02x", data[n]);
300 tmpStr += sBuffer;
305 tmpStr += "</text>";
307 output.addItem(tmpStr);
309 gTableManager.text(data, len);
312 void WW8StreamHandler::utext(const sal_uInt8 * data, size_t len)
314 static char sBuffer[256];
315 snprintf(sBuffer, sizeof(sBuffer), "<utext count=\"%d\">", mnUTextCount);
316 string tmpStr(sBuffer);
318 for (unsigned int n = 0; n < len; ++n)
320 sal_Unicode nChar = data[n * 2] + (data[n * 2 + 1] << 8);
321 if (nChar < 0xff && isprint(nChar))
323 switch (nChar)
325 case '&':
326 tmpStr += "&amp;";
327 break;
328 case '<':
329 tmpStr += "&lt;";
330 break;
331 case '>':
332 tmpStr += "&gt;";
333 break;
334 default:
335 tmpStr += static_cast<char>(nChar);
338 else
340 snprintf(sBuffer, sizeof(sBuffer), "\\0x%04x", nChar);
342 tmpStr += sBuffer;
346 tmpStr += "</utext>";
348 output.addItem(tmpStr);
350 gTableManager.utext(data, len);
352 mnUTextCount++;
355 void WW8StreamHandler::props(writerfilter::Reference<Properties>::Pointer_t ref)
357 WW8PropertiesHandler aHandler;
359 output.addItem("<properties type=\"" + ref->getType() + "\">");
360 ref->resolve(aHandler);
362 //gTableManager.props(ref);
364 output.addItem("</properties>");
367 void WW8StreamHandler::table(Id name, writerfilter::Reference<Table>::Pointer_t ref)
369 WW8TableHandler aHandler;
371 output.addItem("<table id=\"" + (*QNameToString::Instance())(name)
372 + "\">");
376 ref->resolve(aHandler);
378 catch (Exception e)
380 output.addItem("<exception>" + e.getText() + "</exception>");
383 output.addItem("</table>");
386 void WW8StreamHandler::substream(Id name,
387 writerfilter::Reference<Stream>::Pointer_t ref)
389 output.addItem("<substream name=\"" + (*QNameToString::Instance())(name)
390 + "\">");
392 gTableManager.startLevel();
394 ref->resolve(*this);
396 gTableManager.endLevel();
398 output.addItem("</substream>");
401 void WW8StreamHandler::info(const string & info_)
403 gInfo = info_;
404 output.addItem("<info>" + info_ + "</info>");
407 void WW8PropertiesHandler::attribute(Id name, Value & val)
409 boost::shared_ptr<rtl::OString> pStr(new ::rtl::OString());
410 ::rtl::OUString aStr = val.getString();
411 aStr.convertToString(pStr.get(), RTL_TEXTENCODING_ASCII_US,
412 OUSTRING_TO_OSTRING_CVTFLAGS);
413 string sXMLValue = xmlify(pStr->getStr());
415 char sBuffer[256];
416 snprintf(sBuffer, sizeof(sBuffer), "0x%x", val.getInt());
418 output.addItem("<attribute name=\"" +
419 (*QNameToString::Instance())(name) +
420 "\" value=\"" +
421 sXMLValue +
422 + "\" hexvalue=\""
423 + sBuffer + "\">");
425 writerfilter::Reference<Properties>::Pointer_t pProps = val.getProperties();
427 if (pProps.get() != NULL)
429 output.addItem("<properties name=\"" +
430 (*QNameToString::Instance())(name)
431 + "\" type=\"" + pProps->getType() + "\">");
435 pProps->resolve(*this);
437 catch (ExceptionOutOfBounds e)
441 output.addItem("</properties>");
444 writerfilter::Reference<Stream>::Pointer_t pStream = val.getStream();
446 if (pStream.get() != NULL)
450 WW8StreamHandler aHandler;
452 pStream->resolve(aHandler);
454 catch (ExceptionOutOfBounds e)
459 writerfilter::Reference<BinaryObj>::Pointer_t pBinObj = val.getBinary();
461 if (pBinObj.get() != NULL)
465 WW8BinaryObjHandler aHandler;
467 pBinObj->resolve(aHandler);
469 catch (ExceptionOutOfBounds e)
474 output.addItem("</attribute>");
477 bool WW8PropertiesHandler::compare(SprmSharedPointer_t sprm1,
478 SprmSharedPointer_t sprm2)
480 return sprm1->getId() < sprm2->getId();
483 void WW8PropertiesHandler::sprm(Sprm & sprm_)
485 string tmpStr = "<sprm id=\"";
486 char buffer[256];
487 snprintf(buffer, sizeof(buffer), "0x%" SAL_PRIxUINT32, sprm_.getId());
488 tmpStr += buffer;
489 tmpStr += "\" name=\"";
490 tmpStr += sprm_.getName();
491 tmpStr += "\">";
492 output.addItem(tmpStr);
493 output.addItem(sprm_.toString());
495 writerfilter::Reference<Properties>::Pointer_t pProps = sprm_.getProps();
497 if (pProps.get() != NULL)
499 output.addItem("<properties type=\"" + pProps->getType() + "\">");
500 pProps->resolve(*this);
501 output.addItem("</properties>");
504 writerfilter::Reference<BinaryObj>::Pointer_t pBinObj = sprm_.getBinary();
506 if (pBinObj.get() != NULL)
508 output.addItem("<binary>");
509 WW8BinaryObjHandler aHandler;
510 pBinObj->resolve(aHandler);
511 output.addItem("</binary>");
514 writerfilter::Reference<Stream>::Pointer_t pStream = sprm_.getStream();
516 if (pStream.get() != NULL)
518 output.addItem("<stream>");
519 WW8StreamHandler aHandler;
520 pStream->resolve(aHandler);
521 output.addItem("</stream>");
524 gTableManager.sprm(sprm_);
526 output.addItem("</sprm>");
529 void WW8TableHandler::entry(int /*pos*/,
530 writerfilter::Reference<Properties>::Pointer_t ref)
532 output.addItem("<tableentry>");
534 WW8PropertiesHandler aHandler;
538 ref->resolve(aHandler);
540 catch (Exception e)
542 output.addItem("<exception>" + e.getText() + "</exception>");
543 output.addItem("</tableentry>");
545 throw e;
548 output.addItem("</tableentry>");
551 void WW8BinaryObjHandler::data
552 (const sal_uInt8 * buf, size_t length,
553 writerfilter::Reference<Properties>::Pointer_t /*pRef*/)
555 #if 1
556 SubSequence<sal_uInt8> aSeq(buf, length);
558 aSeq.dump(output);
559 #endif