/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ #include #include #include #include #include #include #include #include namespace writerfilter { class ResourceModelOutputWithDepth : public OutputWithDepth { public: ResourceModelOutputWithDepth() : OutputWithDepth("", "") {} ~ResourceModelOutputWithDepth() {outputGroup();} void output(const string & str) const { cout << str << endl; } }; ResourceModelOutputWithDepth output; Stream::Pointer_t createStreamHandler() { return Stream::Pointer_t(new WW8StreamHandler()); } void dump(OutputWithDepth & /*o*/, const char * /*name*/, writerfilter::Reference::Pointer_t /*props*/) { } void dump(OutputWithDepth & o, const char * name, sal_uInt32 n) { char sBuffer[256]; snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIuUINT32, n); string tmpStr = name; tmpStr += "="; tmpStr += sBuffer; o.addItem(tmpStr); } void dump(OutputWithDepth & /*o*/, const char * /*name*/, const rtl::OUString & /*str*/) { } void dump(OutputWithDepth & /*o*/, const char * /*name*/, writerfilter::Reference::Pointer_t /*binary*/) { } string gInfo = ""; // ------- WW8TableDataHandler --------- class TablePropsRef : public writerfilter::Reference { public: typedef boost::shared_ptr Pointer_t; TablePropsRef() {} virtual ~TablePropsRef() {} virtual void resolve(Properties & /*rHandler*/) {} virtual string getType() const { return "TableProps"; } void reset() {} void insert(Pointer_t /* pTablePropsRef */) {} }; typedef TablePropsRef::Pointer_t TablePropsRef_t; class WW8TableDataHandler : public TableDataHandler { public: virtual ~WW8TableDataHandler() {} typedef boost::shared_ptr Pointer_t; virtual void startTable(unsigned int nRows, unsigned int nDepth, TablePropsRef_t pProps); virtual void endTable(); virtual void startRow(unsigned int nCols, TablePropsRef_t pProps); virtual void endRow(); virtual void startCell(const string & start, TablePropsRef_t pProps); virtual void endCell(const string & end); }; void WW8TableDataHandler::startTable(unsigned int nRows, unsigned int nDepth, TablePropsRef_t /*pProps*/) { char sBuffer[256]; string tmpStr = ""; output.addItem(tmpStr); } void WW8TableDataHandler::endTable() { output.addItem(""); } void WW8TableDataHandler::startRow (unsigned int nCols, TablePropsRef_t /*pProps*/) { char sBuffer[256]; snprintf(sBuffer, sizeof(sBuffer), "%d", nCols); string tmpStr = ""; output.addItem(tmpStr); } void WW8TableDataHandler::endRow() { output.addItem(""); } void WW8TableDataHandler::startCell(const string & start, TablePropsRef_t /*pProps*/) { output.addItem(""); output.addItem(start); output.addItem(", "); } void WW8TableDataHandler::endCell(const string & end) { output.addItem(end); output.addItem(""); } // ----- WW8TableDataManager ------------------------------- class WW8TableManager : public TableManager { typedef TableDataHandler TableDataHandlerPointer_t; public: WW8TableManager(); virtual ~WW8TableManager() {} virtual void endParagraphGroup(); virtual bool sprm(Sprm & rSprm); }; WW8TableManager::WW8TableManager() { TableDataHandler::Pointer_t pHandler(new WW8TableDataHandler()); setHandler(pHandler); } bool WW8TableManager::sprm(Sprm & rSprm) { TableManager::sprm(rSprm); output.setDepth(getTableDepthNew()); return true; } void WW8TableManager::endParagraphGroup() { string tmpStr = ""; output.addItem(tmpStr); TableManager::endParagraphGroup(); } /* WW8StreamHandler */ WW8StreamHandler::WW8StreamHandler() : mnUTextCount(0) { output.closeGroup(); output.addItem(""); mpTableManager = new WW8TableManager; mpTableManager->startLevel(); } WW8StreamHandler::~WW8StreamHandler() { mpTableManager->endLevel(); delete mpTableManager; output.closeGroup(); output.addItem(""); } void WW8StreamHandler::startSectionGroup() { output.addItem(""); } void WW8StreamHandler::endSectionGroup() { output.addItem(""); } void WW8StreamHandler::startParagraphGroup() { output.openGroup(); output.addItem(""); mpTableManager->startParagraphGroup(); mpTableManager->handle(gInfo); } void WW8StreamHandler::endParagraphGroup() { mpTableManager->endParagraphGroup(); output.addItem(""); output.closeGroup(); } void WW8StreamHandler::startCharacterGroup() { output.addItem(""); } void WW8StreamHandler::endCharacterGroup() { output.addItem(""); } void WW8StreamHandler::startShape( ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > /*xShape*/ ) { output.addItem(""); } void WW8StreamHandler::endShape( ) { output.addItem( "" ); } void WW8StreamHandler::text(const sal_uInt8 * data, size_t len) { string tmpStr = ""; for (unsigned int n = 0; n < len; ++n) { switch (static_cast(data[n])) { case '<': tmpStr += "<"; break; case '>': tmpStr += ">"; break; case '&': tmpStr += "&"; break; default: if (isprint(data[n])) tmpStr += static_cast(data[n]); else { char sBuffer[256]; snprintf(sBuffer, sizeof(sBuffer), "\\0x%02x", data[n]); tmpStr += sBuffer; } } } tmpStr += ""; output.addItem(tmpStr); mpTableManager->text(data, len); } void WW8StreamHandler::utext(const sal_uInt8 * data, size_t len) { static char sBuffer[256]; snprintf(sBuffer, sizeof(sBuffer), "", mnUTextCount); string tmpStr(sBuffer); for (unsigned int n = 0; n < len; ++n) { sal_Unicode nChar = data[n * 2] + (data[n * 2 + 1] << 8); if (nChar < 0xff && isprint(nChar)) { switch (nChar) { case '&': tmpStr += "&"; break; case '<': tmpStr += "<"; break; case '>': tmpStr += ">"; break; default: tmpStr += static_cast(nChar); } } else { snprintf(sBuffer, sizeof(sBuffer), "\\0x%04x", nChar); tmpStr += sBuffer; } } tmpStr += ""; output.addItem(tmpStr); mpTableManager->utext(data, len); mnUTextCount++; } void WW8StreamHandler::props(writerfilter::Reference::Pointer_t ref) { WW8PropertiesHandler aHandler(mpTableManager); output.addItem("getType() + "\">"); ref->resolve(aHandler); //mpTableManager->props(ref); output.addItem(""); } void WW8StreamHandler::table(Id name, writerfilter::Reference::Pointer_t ref) { WW8TableHandler aHandler(mpTableManager); output.addItem("
"); try { ref->resolve(aHandler); } catch (const Exception &e) { output.addItem("" + e.getText() + ""); } output.addItem("
"); } void WW8StreamHandler::substream(Id name, writerfilter::Reference::Pointer_t ref) { output.addItem(""); mpTableManager->startLevel(); ref->resolve(*this); mpTableManager->endLevel(); output.addItem(""); } void WW8StreamHandler::info(const string & info_) { gInfo = info_; output.addItem("" + info_ + ""); } void WW8PropertiesHandler::attribute(Id name, Value & val) { boost::shared_ptr pStr(new ::rtl::OString()); ::rtl::OUString aStr = val.getString(); aStr.convertToString(pStr.get(), RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS); string sXMLValue = xmlify(pStr->getStr()); char sBuffer[256]; snprintf(sBuffer, sizeof(sBuffer), "0x%x", val.getInt()); output.addItem(""); writerfilter::Reference::Pointer_t pProps = val.getProperties(); if (pProps.get() != NULL) { output.addItem("getType() + "\">"); try { pProps->resolve(*this); } catch (const ExceptionOutOfBounds &) { } output.addItem(""); } writerfilter::Reference::Pointer_t pStream = val.getStream(); if (pStream.get() != NULL) { try { WW8StreamHandler aHandler; pStream->resolve(aHandler); } catch (const ExceptionOutOfBounds &) { } } writerfilter::Reference::Pointer_t pBinObj = val.getBinary(); if (pBinObj.get() != NULL) { try { WW8BinaryObjHandler aHandler; pBinObj->resolve(aHandler); } catch (const ExceptionOutOfBounds &) { } } output.addItem(""); } void WW8PropertiesHandler::sprm(Sprm & sprm_) { string tmpStr = ""; output.addItem(tmpStr); output.addItem(sprm_.toString()); writerfilter::Reference::Pointer_t pProps = sprm_.getProps(); if (pProps.get() != NULL) { output.addItem("getType() + "\">"); pProps->resolve(*this); output.addItem(""); } writerfilter::Reference::Pointer_t pBinObj = sprm_.getBinary(); if (pBinObj.get() != NULL) { output.addItem(""); WW8BinaryObjHandler aHandler; pBinObj->resolve(aHandler); output.addItem(""); } writerfilter::Reference::Pointer_t pStream = sprm_.getStream(); if (pStream.get() != NULL) { output.addItem(""); WW8StreamHandler aHandler; pStream->resolve(aHandler); output.addItem(""); } mpTableManager->sprm(sprm_); output.addItem(""); } void WW8TableHandler::entry(int /*pos*/, writerfilter::Reference::Pointer_t ref) { output.addItem(""); WW8PropertiesHandler aHandler(mpTableManager); try { ref->resolve(aHandler); } catch (const Exception &e) { output.addItem("" + e.getText() + ""); output.addItem(""); throw; } output.addItem(""); } void WW8BinaryObjHandler::data (const sal_uInt8 * buf, size_t length, writerfilter::Reference::Pointer_t /*pRef*/) { SubSequence aSeq(buf, length); aSeq.dump(output); } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */