LCOV - code coverage report
Current view: top level - libreoffice/writerfilter/source/resourcemodel - resourcemodel.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 6 253 2.4 %
Date: 2012-12-27 Functions: 4 50 8.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      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>
      28             : 
      29             : namespace writerfilter {
      30             : 
      31             : class ResourceModelOutputWithDepth : public OutputWithDepth<string>
      32             : {
      33             : public:
      34           5 :     ResourceModelOutputWithDepth()
      35           5 :     : OutputWithDepth<string>("<tablegroup>", "</tablegroup>") {}
      36             : 
      37           5 :     ~ResourceModelOutputWithDepth() {outputGroup();}
      38             : 
      39           0 :     void output(const string & str) const { cout << str << endl; }
      40             : };
      41             : 
      42           5 : ResourceModelOutputWithDepth output;
      43             : 
      44           0 : void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
      45             :           writerfilter::Reference<Properties>::Pointer_t /*props*/)
      46             : {
      47           0 : }
      48             : 
      49           0 : void dump(OutputWithDepth<string> & o, const char * name, sal_uInt32 n)
      50             : {
      51             :     char sBuffer[256];
      52           0 :     snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIuUINT32, n);
      53           0 :     string tmpStr = name;
      54           0 :     tmpStr += "=";
      55           0 :     tmpStr += sBuffer;
      56             : 
      57           0 :     o.addItem(tmpStr);
      58           0 : }
      59             : 
      60           0 : void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
      61             :           const OUString & /*str*/)
      62             : {
      63           0 : }
      64             : 
      65           0 : void dump(OutputWithDepth<string> & /*o*/, const char * /*name*/,
      66             :           writerfilter::Reference<BinaryObj>::Pointer_t /*binary*/)
      67             : {
      68           0 : }
      69             : 
      70           5 : string gInfo = "";
      71             : // ------- WW8TableDataHandler ---------
      72             : 
      73             : class TablePropsRef : public writerfilter::Reference<Properties>
      74             : {
      75             : public:
      76             :     typedef boost::shared_ptr<TablePropsRef> Pointer_t;
      77             : 
      78             :     TablePropsRef() {}
      79           0 :     virtual ~TablePropsRef() {}
      80             : 
      81           0 :     virtual void resolve(Properties & /*rHandler*/) {}
      82             : 
      83           0 :     virtual string getType() const { return "TableProps"; }
      84             :     void reset() {}
      85           0 :     void InsertProps(Pointer_t /* pTablePropsRef */) {}
      86             : };
      87             : 
      88             : typedef TablePropsRef::Pointer_t TablePropsRef_t;
      89             : 
      90           0 : class WW8TableDataHandler : public TableDataHandler<string,
      91             :                             TablePropsRef_t>
      92             : {
      93             : public:
      94           0 :     virtual ~WW8TableDataHandler() {}
      95             : 
      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);
     105             : };
     106             : 
     107           0 : void WW8TableDataHandler::startTable(unsigned int nRows, unsigned int nDepth,
     108             :                                      TablePropsRef_t /*pProps*/)
     109             : {
     110             :     char sBuffer[256];
     111             : 
     112           0 :     string tmpStr = "<tabledata.table rows=\"";
     113           0 :     snprintf(sBuffer, sizeof(sBuffer), "%d", nRows);
     114           0 :     tmpStr += sBuffer;
     115           0 :     tmpStr += "\" depth=\"";
     116           0 :     snprintf(sBuffer, sizeof(sBuffer), "%d", nDepth);
     117           0 :     tmpStr += sBuffer;
     118           0 :     tmpStr += "\">";
     119             : 
     120           0 :     output.addItem(tmpStr);
     121           0 : }
     122             : 
     123           0 : void WW8TableDataHandler::endTable(unsigned int /*nestedTableLevel*/)
     124             : {
     125           0 :     output.addItem("</tabledata.table>");
     126           0 : }
     127             : 
     128           0 : void WW8TableDataHandler::startRow
     129             : (unsigned int nCols, TablePropsRef_t /*pProps*/)
     130             : {
     131             :     char sBuffer[256];
     132             : 
     133           0 :     snprintf(sBuffer, sizeof(sBuffer), "%d", nCols);
     134           0 :     string tmpStr = "<tabledata.row cells=\"";
     135           0 :     tmpStr += sBuffer;
     136           0 :     tmpStr += "\">";
     137           0 :     output.addItem(tmpStr);
     138           0 : }
     139             : 
     140           0 : void WW8TableDataHandler::endRow()
     141             : {
     142           0 :     output.addItem("</tabledata.row>");
     143           0 : }
     144             : 
     145           0 : void WW8TableDataHandler::startCell(const string & start,
     146             :                                     TablePropsRef_t /*pProps*/)
     147             : {
     148           0 :     output.addItem("<tabledata.cell>");
     149           0 :     output.addItem(start);
     150           0 :     output.addItem(", ");
     151           0 : }
     152             : 
     153           0 : void WW8TableDataHandler::endCell(const string & end)
     154             : {
     155           0 :     output.addItem(end);
     156           0 :     output.addItem("</tabledata.cell>");
     157           0 : }
     158             : 
     159             : // ----- WW8TableDataManager -------------------------------
     160             : 
     161             : class WW8TableManager :
     162             :     public TableManager<string, TablePropsRef_t>
     163             : {
     164             :     typedef TableDataHandler<string, TablePropsRef_t>
     165             :     TableDataHandlerPointer_t;
     166             : 
     167             : public:
     168             :     WW8TableManager();
     169           0 :     virtual ~WW8TableManager() {}
     170             :     virtual void endParagraphGroup();
     171             :     virtual bool sprm(Sprm & rSprm);
     172             : };
     173             : 
     174           0 : WW8TableManager::WW8TableManager()
     175             : {
     176           0 :     TableDataHandler<string, TablePropsRef_t>::Pointer_t pHandler(new WW8TableDataHandler());
     177           0 :     setHandler(pHandler);
     178           0 : }
     179             : 
     180           0 : bool WW8TableManager::sprm(Sprm & rSprm)
     181             : {
     182           0 :     TableManager<string, TablePropsRef_t>::sprm(rSprm);
     183           0 :     output.setDepth(getTableDepthNew());
     184           0 :     return true;
     185             : }
     186             : 
     187           0 : void WW8TableManager::endParagraphGroup()
     188             : {
     189           0 :     string tmpStr = "<tabledepth depth=\"";
     190             :     char sBuffer[256];
     191           0 :     snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIuUINT32, getTableDepthNew());
     192           0 :     tmpStr += sBuffer;
     193           0 :     tmpStr += "\"/>";
     194           0 :     output.addItem(tmpStr);
     195           0 :     TableManager<string, TablePropsRef_t>::endParagraphGroup();
     196           0 : }
     197             : 
     198             : /* WW8StreamHandler */
     199             : 
     200           0 : WW8StreamHandler::WW8StreamHandler()
     201           0 : : mnUTextCount(0)
     202             : {
     203           0 :     output.closeGroup();
     204           0 :     output.addItem("<stream>");
     205           0 :     mpTableManager = new WW8TableManager;
     206           0 :     mpTableManager->startLevel();
     207           0 : }
     208             : 
     209           0 : WW8StreamHandler::~WW8StreamHandler()
     210             : {
     211           0 :     mpTableManager->endLevel();
     212           0 :     delete mpTableManager;
     213             : 
     214           0 :     output.closeGroup();
     215           0 :     output.addItem("</stream>");
     216           0 : }
     217             : 
     218           0 : void WW8StreamHandler::startSectionGroup()
     219             : {
     220           0 :     output.addItem("<section-group>");
     221           0 : }
     222             : 
     223           0 : void WW8StreamHandler::endSectionGroup()
     224             : {
     225           0 :     output.addItem("</section-group>");
     226           0 : }
     227             : 
     228           0 : void WW8StreamHandler::startParagraphGroup()
     229             : {
     230           0 :     output.openGroup();
     231           0 :     output.addItem("<paragraph-group>");
     232             : 
     233           0 :     mpTableManager->startParagraphGroup();
     234           0 :     mpTableManager->handle(gInfo);
     235           0 : }
     236             : 
     237           0 : void WW8StreamHandler::endParagraphGroup()
     238             : {
     239           0 :     mpTableManager->endParagraphGroup();
     240             : 
     241           0 :     output.addItem("</paragraph-group>");
     242           0 :     output.closeGroup();
     243             : 
     244           0 : }
     245             : 
     246           0 : void WW8StreamHandler::startCharacterGroup()
     247             : {
     248           0 :     output.addItem("<character-group>");
     249           0 : }
     250             : 
     251           0 : void WW8StreamHandler::endCharacterGroup()
     252             : {
     253           0 :     output.addItem("</character-group>");
     254           0 : }
     255             : 
     256           0 : void WW8StreamHandler::startShape( ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > /*xShape*/ )
     257             : {
     258           0 :     output.addItem("<shape>");
     259           0 : }
     260             : 
     261           0 : void WW8StreamHandler::endShape( )
     262             : {
     263           0 :     output.addItem( "</shape>" );
     264           0 : }
     265             : 
     266           0 : void WW8StreamHandler::text(const sal_uInt8 * data, size_t len)
     267             : {
     268           0 :     string tmpStr = "<text>";
     269             : 
     270           0 :     for (unsigned int n = 0; n < len; ++n)
     271             :     {
     272           0 :         switch (static_cast<unsigned char>(data[n]))
     273             :         {
     274             :         case '<':
     275           0 :             tmpStr += "&lt;";
     276             : 
     277           0 :             break;
     278             :         case '>':
     279           0 :             tmpStr += "&gt;";
     280             : 
     281           0 :             break;
     282             : 
     283             :         case '&':
     284           0 :             tmpStr += "&amp;";
     285             : 
     286           0 :             break;
     287             :         default:
     288           0 :             if (isprint(data[n]))
     289           0 :                 tmpStr += static_cast<char>(data[n]);
     290             :             else
     291             :             {
     292             :                 char sBuffer[256];
     293             : 
     294           0 :                 snprintf(sBuffer, sizeof(sBuffer), "\\0x%02x", data[n]);
     295             : 
     296           0 :                 tmpStr += sBuffer;
     297             :             }
     298             :         }
     299             :     }
     300             : 
     301           0 :     tmpStr += "</text>";
     302             : 
     303           0 :     output.addItem(tmpStr);
     304             : 
     305           0 :     mpTableManager->text(data, len);
     306           0 : }
     307             : 
     308           0 : void WW8StreamHandler::utext(const sal_uInt8 * data, size_t len)
     309             : {
     310             :     static char sBuffer[256];
     311           0 :     snprintf(sBuffer, sizeof(sBuffer), "<utext count=\"%d\">", mnUTextCount);
     312           0 :     string tmpStr(sBuffer);
     313             : 
     314           0 :     for (unsigned int n = 0; n < len; ++n)
     315             :     {
     316           0 :         sal_Unicode nChar = data[n * 2] + (data[n * 2 + 1] << 8);
     317           0 :         if (nChar < 0xff && isprint(nChar))
     318             :         {
     319           0 :             switch (nChar)
     320             :             {
     321             :             case '&':
     322           0 :                 tmpStr += "&amp;";
     323           0 :                 break;
     324             :             case '<':
     325           0 :                 tmpStr += "&lt;";
     326           0 :                 break;
     327             :             case '>':
     328           0 :                 tmpStr += "&gt;";
     329           0 :                 break;
     330             :             default:
     331           0 :                 tmpStr += static_cast<char>(nChar);
     332             :             }
     333             :         }
     334             :         else
     335             :         {
     336           0 :             snprintf(sBuffer, sizeof(sBuffer), "\\0x%04x", nChar);
     337             : 
     338           0 :             tmpStr += sBuffer;
     339             :         }
     340             :     }
     341             : 
     342           0 :     tmpStr += "</utext>";
     343             : 
     344           0 :     output.addItem(tmpStr);
     345             : 
     346           0 :     mpTableManager->utext(data, len);
     347             : 
     348           0 :     mnUTextCount++;
     349           0 : }
     350             : 
     351           0 : void WW8StreamHandler::props(writerfilter::Reference<Properties>::Pointer_t ref)
     352             : {
     353           0 :     WW8PropertiesHandler aHandler(mpTableManager);
     354             : 
     355           0 :     output.addItem("<properties type=\"" + ref->getType() + "\">");
     356           0 :     ref->resolve(aHandler);
     357             : 
     358             :     //mpTableManager->props(ref);
     359             : 
     360           0 :     output.addItem("</properties>");
     361           0 : }
     362             : 
     363           0 : void WW8StreamHandler::table(Id name, writerfilter::Reference<Table>::Pointer_t ref)
     364             : {
     365           0 :     WW8TableHandler aHandler(mpTableManager);
     366             : 
     367           0 :     output.addItem("<table id=\"" + (*QNameToString::Instance())(name)
     368           0 :                    + "\">");
     369             : 
     370             :     try
     371             :     {
     372           0 :         ref->resolve(aHandler);
     373             :     }
     374           0 :     catch (const Exception &e)
     375             :     {
     376           0 :         output.addItem("<exception>" + e.getText() + "</exception>");
     377             :     }
     378             : 
     379           0 :     output.addItem("</table>");
     380           0 : }
     381             : 
     382           0 : void WW8StreamHandler::substream(Id name,
     383             :                                  writerfilter::Reference<Stream>::Pointer_t ref)
     384             : {
     385           0 :     output.addItem("<substream name=\"" + (*QNameToString::Instance())(name)
     386           0 :                    + "\">");
     387             : 
     388           0 :     mpTableManager->startLevel();
     389             : 
     390           0 :     ref->resolve(*this);
     391             : 
     392           0 :     mpTableManager->endLevel();
     393             : 
     394           0 :     output.addItem("</substream>");
     395           0 : }
     396             : 
     397           0 : void WW8StreamHandler::info(const string & info_)
     398             : {
     399           0 :     gInfo = info_;
     400           0 :     output.addItem("<info>" + info_ + "</info>");
     401           0 : }
     402             : 
     403           0 : void WW8PropertiesHandler::attribute(Id name, Value & val)
     404             : {
     405           0 :     boost::shared_ptr<OString> pStr(new OString());
     406           0 :     OUString aStr = val.getString();
     407             :     aStr.convertToString(pStr.get(), RTL_TEXTENCODING_ASCII_US,
     408           0 :                          OUSTRING_TO_OSTRING_CVTFLAGS);
     409           0 :     string sXMLValue = xmlify(pStr->getStr());
     410             : 
     411             :     char sBuffer[256];
     412           0 :     snprintf(sBuffer, sizeof(sBuffer), "0x%x", val.getInt());
     413             : 
     414             :     output.addItem("<attribute name=\"" +
     415           0 :                    (*QNameToString::Instance())(name) +
     416             :                    "\" value=\"" +
     417             :                    sXMLValue +
     418             :                    + "\" hexvalue=\""
     419           0 :                    + sBuffer + "\">");
     420             : 
     421           0 :     writerfilter::Reference<Properties>::Pointer_t pProps = val.getProperties();
     422             : 
     423           0 :     if (pProps.get() != NULL)
     424             :     {
     425             :         output.addItem("<properties name=\"" +
     426           0 :                        (*QNameToString::Instance())(name)
     427           0 :                        + "\" type=\"" + pProps->getType() + "\">");
     428             : 
     429             :         try
     430             :         {
     431           0 :             pProps->resolve(*this);
     432             :         }
     433           0 :         catch (const ExceptionOutOfBounds &)
     434             :         {
     435             :         }
     436             : 
     437           0 :         output.addItem("</properties>");
     438             :     }
     439             : 
     440           0 :     writerfilter::Reference<Stream>::Pointer_t pStream = val.getStream();
     441             : 
     442           0 :     if (pStream.get() != NULL)
     443             :     {
     444             :         try
     445             :         {
     446           0 :             WW8StreamHandler aHandler;
     447             : 
     448           0 :             pStream->resolve(aHandler);
     449             :         }
     450           0 :         catch (const ExceptionOutOfBounds &)
     451             :         {
     452             :         }
     453             :     }
     454             : 
     455           0 :     writerfilter::Reference<BinaryObj>::Pointer_t pBinObj = val.getBinary();
     456             : 
     457           0 :     if (pBinObj.get() != NULL)
     458             :     {
     459             :         try
     460             :         {
     461           0 :             WW8BinaryObjHandler aHandler;
     462             : 
     463           0 :             pBinObj->resolve(aHandler);
     464             :         }
     465           0 :         catch (const ExceptionOutOfBounds &)
     466             :         {
     467             :         }
     468             :     }
     469             : 
     470           0 :     output.addItem("</attribute>");
     471           0 : }
     472             : 
     473           0 : void WW8PropertiesHandler::sprm(Sprm & sprm_)
     474             : {
     475           0 :     string tmpStr = "<sprm id=\"";
     476             :     char buffer[256];
     477           0 :     snprintf(buffer, sizeof(buffer), "0x%" SAL_PRIxUINT32, sprm_.getId());
     478           0 :     tmpStr += buffer;
     479           0 :     tmpStr += "\" name=\"";
     480           0 :     tmpStr += sprm_.getName();
     481           0 :     tmpStr += "\">";
     482           0 :     output.addItem(tmpStr);
     483           0 :     output.addItem(sprm_.toString());
     484             : 
     485           0 :     writerfilter::Reference<Properties>::Pointer_t pProps = sprm_.getProps();
     486             : 
     487           0 :     if (pProps.get() != NULL)
     488             :     {
     489           0 :         output.addItem("<properties type=\"" + pProps->getType() + "\">");
     490           0 :         pProps->resolve(*this);
     491           0 :         output.addItem("</properties>");
     492             :     }
     493             : 
     494           0 :     writerfilter::Reference<BinaryObj>::Pointer_t pBinObj = sprm_.getBinary();
     495             : 
     496           0 :     if (pBinObj.get() != NULL)
     497             :     {
     498           0 :         output.addItem("<binary>");
     499           0 :         WW8BinaryObjHandler aHandler;
     500           0 :         pBinObj->resolve(aHandler);
     501           0 :         output.addItem("</binary>");
     502             :     }
     503             : 
     504           0 :     writerfilter::Reference<Stream>::Pointer_t pStream = sprm_.getStream();
     505             : 
     506           0 :     if (pStream.get() != NULL)
     507             :     {
     508           0 :         output.addItem("<stream>");
     509           0 :         WW8StreamHandler aHandler;
     510           0 :         pStream->resolve(aHandler);
     511           0 :         output.addItem("</stream>");
     512             :     }
     513             : 
     514           0 :     mpTableManager->sprm(sprm_);
     515             : 
     516           0 :     output.addItem("</sprm>");
     517           0 : }
     518             : 
     519           0 : void WW8TableHandler::entry(int /*pos*/,
     520             :                             writerfilter::Reference<Properties>::Pointer_t ref)
     521             : {
     522           0 :     output.addItem("<tableentry>");
     523             : 
     524           0 :     WW8PropertiesHandler aHandler(mpTableManager);
     525             : 
     526             :     try
     527             :     {
     528           0 :         ref->resolve(aHandler);
     529             :     }
     530           0 :     catch (const Exception &e)
     531             :     {
     532           0 :         output.addItem("<exception>" + e.getText() + "</exception>");
     533           0 :         output.addItem("</tableentry>");
     534             : 
     535           0 :         throw;
     536             :     }
     537             : 
     538           0 :     output.addItem("</tableentry>");
     539           0 : }
     540             : 
     541           0 : void WW8BinaryObjHandler::data
     542             : (const sal_uInt8 * buf, size_t length,
     543             :  writerfilter::Reference<Properties>::Pointer_t /*pRef*/)
     544             : {
     545           0 :     SubSequence<sal_uInt8> aSeq(buf, length);
     546             : 
     547           0 :     aSeq.dump(output);
     548           0 : }
     549             : 
     550          15 : }
     551             : 
     552             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10