<commit_before>/* DocumentCollector: Collects sections and runs of text from a
 * file (and styles to go along with them) and writes them
 * to a Writer target document
 *
 * Copyright (C) 2002-2004 <NAME> (<EMAIL>)
 * Copyright (C) 2003-2004 Net Integration Technologies (http://www.net-itech.com)
 * Copyright (C) 2004 <NAME> (<EMAIL>)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * For further information visit http://libwpd.sourceforge.net
 *
 */

/* "This product is not manufactured, approved, or supported by
 * Corel Corporation or Corel Corporation Limited."
 */

#if defined _MSC_VER
#pragma warning( push, 1 )
#endif
#include <libwpd/libwpd.h>
#if defined _MSC_VER
#pragma warning( pop )
#endif
#include <string.h> // for strcmp

#include "DocumentCollector.hxx"
#include "DocumentElement.hxx"
#include "TextRunStyle.hxx"
#include "FontStyle.hxx"
#include "ListStyle.hxx"
#include "PageSpan.hxx"
#include "SectionStyle.hxx"
#include "TableStyle.hxx"
#include "FilterInternal.hxx"
#include "WriterProperties.hxx"

_WriterDocumentState::_WriterDocumentState() :
    mbFirstElement(true),
    mbInFakeSection(false),
    mbListElementOpenedAtCurrentLevel(false),
    mbTableCellOpened(false),
    mbHeaderRow(false),
    mbInNote(false)
{
}

DocumentCollector::DocumentCollector(WPXInputStream *pInput, DocumentHandler *pHandler) :
        mpInput(pInput),
        mpHandler(pHandler),
    mbUsed(false),
    mfSectionSpaceAfter(0.0f),
    miNumListStyles(0),
    mpCurrentContentElements(&mBodyElements),
    mpCurrentPageSpan(NULL),
    miNumPageStyles(0),
    mpCurrentListStyle(NULL),
    miCurrentListLevel(0),
    miLastListLevel(0),
    miLastListNumber(0),
    mbListContinueNumbering(false),
    mbListElementOpened(false),
    mbListElementParagraphOpened(false)
{
}

DocumentCollector::~DocumentCollector()
{
}

bool DocumentCollector::filter()
{
    // The contract for DocumentCollector is that it will only be used once after it is
    // instantiated
    if (mbUsed)
        return false;

    mbUsed = true;

    // parse & write
        // WLACH_REFACTORING: Remove these args..
     if (!parseSourceDocument(*mpInput))
        return false;
    if (!_writeTargetDocument(mpHandler))
        return false;

     // clean up the mess we made
     WRITER_DEBUG_MSG(("WriterWordPerfect: Cleaning up our mess..\n"));

    WRITER_DEBUG_MSG(("Destroying the body elements\n"));
    for (std::vector<DocumentElement *>::iterator iterBody = mBodyElements.begin(); iterBody != mBodyElements.end(); iterBody++) {
        delete((*iterBody));
        (*iterBody) = NULL;
    }

    WRITER_DEBUG_MSG(("Destroying the styles elements\n"));
    for (std::vector<DocumentElement *>::iterator iterStyles = mStylesElements.begin(); iterStyles != mStylesElements.end(); iterStyles++) {
         delete (*iterStyles);
        (*iterStyles) = NULL; // we may pass over the same element again (in the case of headers/footers spanning multiple pages)
                      // so make sure we don't do a double del
    }

    WRITER_DEBUG_MSG(("Destroying the rest of the styles elements\n"));
    for (std::map<WPXString, ParagraphStyle *, ltstr>::iterator iterTextStyle = mTextStyleHash.begin(); iterTextStyle != mTextStyleHash.end(); iterTextStyle++) {
        delete iterTextStyle->second;
    }
    for (std::map<WPXString, SpanStyle *, ltstr>::iterator iterSpanStyle = mSpanStyleHash.begin(); iterSpanStyle != mSpanStyleHash.end(); iterSpanStyle++) {
        delete iterSpanStyle->second;
    }

    for (std::map<WPXString, FontStyle *, ltstr>::iterator iterFont = mFontHash.begin(); iterFont != mFontHash.end(); iterFont++) {
        delete iterFont->second;
    }

    for (std::vector<ListStyle *>::iterator iterListStyles = mListStyles.begin(); iterListStyles != mListStyles.end(); iterListStyles++) {
        delete (*iterListStyles);
    }
    for (std::vector<SectionStyle *>::iterator iterSectionStyles = mSectionStyles.begin(); iterSectionStyles != mSectionStyles.end(); iterSectionStyles++) {
        delete (*iterSectionStyles);
    }
    for (std::vector<TableStyle *>::iterator iterTableStyles = mTableStyles.begin(); iterTableStyles != mTableStyles.end(); iterTableStyles++) {
        delete (*iterTableStyles);
    }

    for (std::vector<PageSpan *>::iterator iterPageSpans = mPageSpans.begin(); iterPageSpans != mPageSpans.end(); iterPageSpans++) {
        delete (*iterPageSpans);
    }

     return true;
}

void DocumentCollector::_writeDefaultStyles(DocumentHandler *pHandler)
{
    TagOpenElement stylesOpenElement("office:styles");
    stylesOpenElement.write(pHandler);

    TagOpenElement defaultParagraphStyleOpenElement("style:default-style");
    defaultParagraphStyleOpenElement.addAttribute("style:family", "paragraph");
    defaultParagraphStyleOpenElement.write(pHandler);

    TagOpenElement defaultParagraphStylePropertiesOpenElement("style:properties");
    defaultParagraphStylePropertiesOpenElement.addAttribute("style:family", "paragraph");
    defaultParagraphStylePropertiesOpenElement.addAttribute("style:tab-stop-distance", "0.5inch");
    defaultParagraphStylePropertiesOpenElement.write(pHandler);
    TagCloseElement defaultParagraphStylePropertiesCloseElement("style:properties");
    defaultParagraphStylePropertiesCloseElement.write(pHandler);

    TagCloseElement defaultParagraphStyleCloseElement("style:default-style");
    defaultParagraphStyleCloseElement.write(pHandler);

    TagOpenElement standardStyleOpenElement("style:style");
        standardStyleOpenElement.addAttribute("style:name", "Standard");
        standardStyleOpenElement.addAttribute("style:family", "paragraph");
        standardStyleOpenElement.addAttribute("style:class", "text");
        standardStyleOpenElement.write(pHandler);
        TagCloseElement standardStyleCloseElement("style:style");
        standardStyleCloseElement.write(pHandler);

        TagOpenElement textBodyStyleOpenElement("style:style");
        textBodyStyleOpenElement.addAttribute("style:name", "Text Body");
        textBodyStyleOpenElement.addAttribute("style:family", "paragraph");
        textBodyStyleOpenElement.addAttribute("style:parent-style-name", "Standard");
        textBodyStyleOpenElement.addAttribute("style:class", "text");
        textBodyStyleOpenElement.write(pHandler);
        TagCloseElement textBodyStyleCloseElement("style:style");
        textBodyStyleCloseElement.write(pHandler);

        TagOpenElement tableContentsStyleOpenElement("style:style");
        tableContentsStyleOpenElement.addAttribute("style:name", "Table Contents");
        tableContentsStyleOpenElement.addAttribute("style:family", "paragraph");
        tableContentsStyleOpenElement.addAttribute("style:parent-style-name", "Text Body");
        tableContentsStyleOpenElement.addAttribute("style:class", "extra");
        tableContentsStyleOpenElement.write(pHandler);
        TagCloseElement tableContentsStyleCloseElement("style:style");
        tableContentsStyleCloseElement.write(pHandler);

        TagOpenElement tableHeadingStyleOpenElement("style:style");
        tableHeadingStyleOpenElement.addAttribute("style:name", "Table Heading");
        tableHeadingStyleOpenElement.addAttribute("style:family", "paragraph");
        tableHeadingStyleOpenElement.addAttribute("style:parent-style-name", "Table Contents");
        tableHeadingStyleOpenElement.addAttribute("style:class", "extra");
        tableHeadingStyleOpenElement.write(pHandler);
        TagCloseElement tableHeadingStyleCloseElement("style:style");
        tableHeadingStyleCloseElement.write(pHandler);

    TagCloseElement stylesCloseElement("office:styles");
    stylesCloseElement.write(pHandler);

}

// writes everything up to the automatic styles declarations..
void DocumentCollector::_writeBegin()
{
}

void DocumentCollector::_writeMasterPages(DocumentHandler *pHandler)
{
        WPXPropertyList xBlankAttrList;

    pHandler->startElement("office:master-styles", xBlankAttrList);
    int pageNumber = 1;
    for (unsigned int i=0; i<mPageSpans.size(); i++)
    {
        bool bLastPage;
        (i == (mPageSpans.size() - 1)) ? bLastPage = true : bLastPage = false;
        mPageSpans[i]->writeMasterPages(pageNumber, i, bLastPage, pHandler);
        pageNumber += mPageSpans[i]->getSpan();
    }
    pHandler->endElement("office:master-styles");
}

void DocumentCollector::_writePageMasters(DocumentHandler *pHandler)
{
    for (unsigned int i=0; i<mPageSpans.size(); i++)
    {
        mPageSpans[i]->writePageMaster(i, pHandler);
    }
}

bool DocumentCollector::_writeTargetDocument(DocumentHandler *pHandler)
{
    WRITER_DEBUG_MSG(("WriterWordPerfect: Document Body: Printing out the header stuff..\n"));
    WPXPropertyList xBlankAttrList;
<commit_msg>INTEGRATION: CWS cmcfixes45 (1.3.16); FILE MERGED 2008/05/12 07:20:05 cmc 1.3.16.1: #i86324# remove unused methods<commit_after>/* DocumentCollector: Collects sections and runs of text from a
 * file (and styles to go along with them) and writes them
 * to a Writer target document
 *
 * Copyright (C) 2002-2004 <NAME> (<EMAIL>)
 * Copyright (C) 2003-2004 Net Integration Technologies (http://www.net-itech.com)
 * Copyright (C) 2004 <NAME> (<EMAIL>)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * For further information visit http://libwpd.sourceforge.net
 *
 */

/* "This product is not manufactured, approved, or supported by
 * Corel Corporation or Corel Corporation Limited."
 */

#if defined _MSC_VER
#pragma warning( push, 1 )
#endif
#include <libwpd/libwpd.h>
#if defined _MSC_VER
#pragma warning( pop )
#endif
#include <string.h> // for strcmp

#include "DocumentCollector.hxx"
#include "DocumentElement.hxx"
#include "TextRunStyle.hxx"
#include "FontStyle.hxx"
#include "ListStyle.hxx"
#include "PageSpan.hxx"
#include "SectionStyle.hxx"
#include "TableStyle.hxx"
#include "FilterInternal.hxx"
#include "WriterProperties.hxx"

_WriterDocumentState::_WriterDocumentState() :
    mbFirstElement(true),
    mbInFakeSection(false),
    mbListElementOpenedAtCurrentLevel(false),
    mbTableCellOpened(false),
    mbHeaderRow(false),
    mbInNote(false)
{
}

DocumentCollector::DocumentCollector(WPXInputStream *pInput, DocumentHandler *pHandler) :
        mpInput(pInput),
        mpHandler(pHandler),
    mbUsed(false),
    mfSectionSpaceAfter(0.0f),
    miNumListStyles(0),
    mpCurrentContentElements(&mBodyElements),
    mpCurrentPageSpan(NULL),
    miNumPageStyles(0),
    mpCurrentListStyle(NULL),
    miCurrentListLevel(0),
    miLastListLevel(0),
    miLastListNumber(0),
    mbListContinueNumbering(false),
    mbListElementOpened(false),
    mbListElementParagraphOpened(false)
{
}

DocumentCollector::~DocumentCollector()
{
}

bool DocumentCollector::filter()
{
    // The contract for DocumentCollector is that it will only be used once after it is
    // instantiated
    if (mbUsed)
        return false;

    mbUsed = true;

    // parse & write
        // WLACH_REFACTORING: Remove these args..
     if (!parseSourceDocument(*mpInput))
        return false;
    if (!_writeTargetDocument(mpHandler))
        return false;

     // clean up the mess we made
     WRITER_DEBUG_MSG(("WriterWordPerfect: Cleaning up our mess..\n"));

    WRITER_DEBUG_MSG(("Destroying the body elements\n"));
    for (std::vector<DocumentElement *>::iterator iterBody = mBodyElements.begin(); iterBody != mBodyElements.end(); iterBody++) {
        delete((*iterBody));
        (*iterBody) = NULL;
    }

    WRITER_DEBUG_MSG(("Destroying the styles elements\n"));
    for (std::vector<DocumentElement *>::iterator iterStyles = mStylesElements.begin(); iterStyles != mStylesElements.end(); iterStyles++) {
         delete (*iterStyles);
        (*iterStyles) = NULL; // we may pass over the same element again (in the case of headers/footers spanning multiple pages)
                      // so make sure we don't do a double del
    }

    WRITER_DEBUG_MSG(("Destroying the rest of the styles elements\n"));
    for (std::map<WPXString, ParagraphStyle *, ltstr>::iterator iterTextStyle = mTextStyleHash.begin(); iterTextStyle != mTextStyleHash.end(); iterTextStyle++) {
        delete iterTextStyle->second;
    }
    for (std::map<WPXString, SpanStyle *, ltstr>::iterator iterSpanStyle = mSpanStyleHash.begin(); iterSpanStyle != mSpanStyleHash.end(); iterSpanStyle++) {
        delete iterSpanStyle->second;
    }

    for (std::map<WPXString, FontStyle *, ltstr>::iterator iterFont = mFontHash.begin(); iterFont != mFontHash.end(); iterFont++) {
        delete iterFont->second;
    }

    for (std::vector<ListStyle *>::iterator iterListStyles = mListStyles.begin(); iterListStyles != mListStyles.end(); iterListStyles++) {
        delete (*iterListStyles);
    }
    for (std::vector<SectionStyle *>::iterator iterSectionStyles = mSectionStyles.begin(); iterSectionStyles != mSectionStyles.end(); iterSectionStyles++) {
        delete (*iterSectionStyles);
    }
    for (std::vector<TableStyle *>::iterator iterTableStyles = mTableStyles.begin(); iterTableStyles != mTableStyles.end(); iterTableStyles++) {
        delete (*iterTableStyles);
    }

    for (std::vector<PageSpan *>::iterator iterPageSpans = mPageSpans.begin(); iterPageSpans != mPageSpans.end(); iterPageSpans++) {
        delete (*iterPageSpans);
    }

     return true;
}

void DocumentCollector::_writeDefaultStyles(DocumentHandler *pHandler)
{
    TagOpenElement stylesOpenElement("office:styles");
    stylesOpenElement.write(pHandler);

    TagOpenElement defaultParagraphStyleOpenElement("style:default-style");
    defaultParagraphStyleOpenElement.addAttribute("style:family", "paragraph");
    defaultParagraphStyleOpenElement.write(pHandler);

    TagOpenElement defaultParagraphStylePropertiesOpenElement("style:properties");
    defaultParagraphStylePropertiesOpenElement.addAttribute("style:family", "paragraph");
    defaultParagraphStylePropertiesOpenElement.addAttribute("style:tab-stop-distance", "0.5inch");
    defaultParagraphStylePropertiesOpenElement.write(pHandler);
    TagCloseElement defaultParagraphStylePropertiesCloseElement("style:properties");
    defaultParagraphStylePropertiesCloseElement.write(pHandler);

    TagCloseElement defaultParagraphStyleCloseElement("style:default-style");
    defaultParagraphStyleCloseElement.write(pHandler);

    TagOpenElement standardStyleOpenElement("style:style");
        standardStyleOpenElement.addAttribute("style:name", "Standard");
        standardStyleOpenElement.addAttribute("style:family", "paragraph");
        standardStyleOpenElement.addAttribute("style:class", "text");
        standardStyleOpenElement.write(pHandler);
        TagCloseElement standardStyleCloseElement("style:style");
        standardStyleCloseElement.write(pHandler);

        TagOpenElement textBodyStyleOpenElement("style:style");
        textBodyStyleOpenElement.addAttribute("style:name", "Text Body");
        textBodyStyleOpenElement.addAttribute("style:family", "paragraph");
        textBodyStyleOpenElement.addAttribute("style:parent-style-name", "Standard");
        textBodyStyleOpenElement.addAttribute("style:class", "text");
        textBodyStyleOpenElement.write(pHandler);
        TagCloseElement textBodyStyleCloseElement("style:style");
        textBodyStyleCloseElement.write(pHandler);

        TagOpenElement tableContentsStyleOpenElement("style:style");
        tableContentsStyleOpenElement.addAttribute("style:name", "Table Contents");
        tableContentsStyleOpenElement.addAttribute("style:family", "paragraph");
        tableContentsStyleOpenElement.addAttribute("style:parent-style-name", "Text Body");
        tableContentsStyleOpenElement.addAttribute("style:class", "extra");
        tableContentsStyleOpenElement.write(pHandler);
        TagCloseElement tableContentsStyleCloseElement("style:style");
        tableContentsStyleCloseElement.write(pHandler);

        TagOpenElement tableHeadingStyleOpenElement("style:style");
        tableHeadingStyleOpenElement.addAttribute("style:name", "Table Heading");
        tableHeadingStyleOpenElement.addAttribute("style:family", "paragraph");
        tableHeadingStyleOpenElement.addAttribute("style:parent-style-name", "Table Contents");
        tableHeadingStyleOpenElement.addAttribute("style:class", "extra");
        tableHeadingStyleOpenElement.write(pHandler);
        TagCloseElement tableHeadingStyleCloseElement("style:style");
        tableHeadingStyleCloseElement.write(pHandler);

    TagCloseElement stylesCloseElement("office:styles");
    stylesCloseElement.write(pHandler);

}

void DocumentCollector::_writeMasterPages(DocumentHandler *pHandler)
{
        WPXPropertyList xBlankAttrList;

    pHandler->startElement("office:master-styles", xBlankAttrList);
    int pageNumber = 1;
    for (unsigned int i=0; i<mPageSpans.size(); i++)
    {
        bool bLastPage;
        (i == (mPageSpans.size() - 1)) ? bLastPage = true : bLastPage = false;
        mPageSpans[i]->writeMasterPages(pageNumber, i, bLastPage, pHandler);
        pageNumber += mPageSpans[i]->getSpan();
    }
    pHandler->endElement("office:master-styles");
}

void DocumentCollector::_writePageMasters(DocumentHandler *pHandler)
{
    for (unsigned int i=0; i<mPageSpans.size(); i++)
    {
        mPageSpans[i]->writePageMaster(i, pHandler);
    }
}

bool DocumentCollector::_writeTargetDocument(DocumentHandler *pHandler)
{
    WRITER_DEBUG_MSG(("WriterWordPerfect: Document Body: Printing out the header stuff..\n"));
    WPXPropertyList xBlankAttrList;

{'fraction_non_alphanumeric': 0.11313965179658146, 'fraction_numerical': 0.006773561941048844, 'mean_word_length': 4.687029792356305, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5389423', 'n_tokens_mistral': 5338, 'n_tokens_neox': 5234, 'n_words': 1175}