summaryrefslogtreecommitdiff
path: root/sw/source/filter/xml/xmlmeta.cxx
blob: 50c8f8b1c9c743010e26416a1b1ced3b6aa88494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- 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 <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <osl/diagnose.h>
#include <xmloff/xmlmetai.hxx>
#include <xmloff/ProgressBarHelper.hxx>
#include <o3tl/safeint.hxx>
#include <xmloff/xmluconv.hxx>
#include <docstat.hxx>
#include <doc.hxx>
#include <IDocumentStatistics.hxx>
#include "xmlimp.hxx"
#include "xmlexp.hxx"

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::text;
using namespace ::xmloff::token;

uno::Reference<document::XDocumentProperties>
SwXMLImport::GetDocumentProperties() const
{
    if (m_bOrganizerMode || IsStylesOnlyMode() ||
        IsBlockMode() || IsInsertMode())
    {
        return nullptr;
    }
    uno::Reference<document::XDocumentPropertiesSupplier> const xDPS(
        GetModel(), UNO_QUERY_THROW);
    return xDPS->getDocumentProperties();
}

SvXMLImportContext *SwXMLImport::CreateMetaContext(
                                       const sal_Int32 /*nElement*/ )
{
    SvXMLImportContext *pContext = nullptr;

    if (getImportFlags() & SvXMLImportFlags::META)
    {
        uno::Reference<document::XDocumentProperties> const xDocProps(
                GetDocumentProperties());
        pContext = new SvXMLMetaDocumentContext(*this, xDocProps);
    }

    return pContext;
}

namespace {

enum SvXMLTokenMapAttrs
{
    XML_TOK_META_STAT_TABLE = 1,
    XML_TOK_META_STAT_IMAGE = 2,
    XML_TOK_META_STAT_OLE = 4,
    XML_TOK_META_STAT_PAGE = 8,
    XML_TOK_META_STAT_PARA = 16,
    XML_TOK_META_STAT_WORD = 32,
    XML_TOK_META_STAT_CHAR = 64,
    XML_TOK_META_STAT_NON_WHITE_SPACE_CHAR = 128,
    XML_TOK_META_STAT_END=XML_TOK_UNKNOWN
};

struct statistic {
    SvXMLTokenMapAttrs token;
    const char* name;
    sal_uInt16 SwDocStat::* target16;
    sal_uLong  SwDocStat::* target32; /* or 64, on LP64 platforms */
};

}

const struct statistic s_stats [] = {
    { XML_TOK_META_STAT_TABLE, "TableCount",     &SwDocStat::nTable, nullptr  },
    { XML_TOK_META_STAT_IMAGE, "ImageCount",     &SwDocStat::nGrf, nullptr  },
    { XML_TOK_META_STAT_OLE,   "ObjectCount",    &SwDocStat::nOLE, nullptr  },
    { XML_TOK_META_STAT_PAGE,  "PageCount",      nullptr, &SwDocStat::nPage },
    { XML_TOK_META_STAT_PARA,  "ParagraphCount", nullptr, &SwDocStat::nPara },
    { XML_TOK_META_STAT_WORD,  "WordCount",      nullptr, &SwDocStat::nWord },
    { XML_TOK_META_STAT_CHAR,  "CharacterCount", nullptr, &SwDocStat::nChar },
    { XML_TOK_META_STAT_NON_WHITE_SPACE_CHAR,  "NonWhitespaceCharacterCount", nullptr, &SwDocStat::nCharExcludingSpaces },
    { XML_TOK_META_STAT_END,   nullptr,                nullptr, nullptr                 }
};

void SwXMLImport::SetStatistics(
        const Sequence< beans::NamedValue > & i_rStats)
{
    if( IsStylesOnlyMode() || IsInsertMode() )
        return;

    SvXMLImport::SetStatistics(i_rStats);

    SwDoc *pDoc = SwImport::GetDocFromXMLImport( *this );
    SwDocStat aDocStat( pDoc->getIDocumentStatistics().GetDocStat() );

    sal_uInt32 nTokens = 0;

    for (const auto& rStat : i_rStats) {
        for (struct statistic const* pStat = s_stats; pStat->name != nullptr;
                ++pStat) {
            if (rStat.Name.equalsAscii(pStat->name)) {
                sal_Int32 val = 0;
                if (rStat.Value >>= val) {
                    if (pStat->target16 != nullptr) {
                        aDocStat.*(pStat->target16)
                            = static_cast<sal_uInt16> (val);
                    } else {
                        aDocStat.*(pStat->target32)
                            = static_cast<sal_uInt32> (val);
                    }
                    nTokens |= pStat->token;
                } else {
                    OSL_FAIL("SwXMLImport::SetStatistics: invalid entry");
                }
            }
        }
    }

    if( nTokens )
        pDoc->getIDocumentStatistics().SetDocStat( aDocStat );

    // set progress bar reference to #paragraphs. If not available,
    // use #pages*10, or guesstimate 250 paragraphs. Additionally
    // guesstimate PROGRESS_BAR_STEPS each for meta+settings, styles,
    // and autostyles.
    bool bSetFallback = true;
    sal_Int32 nProgressReference = sal_Int32(); // silence C4701
    const sal_Int32 nProgressReferenceWriggleRoom = 3 * PROGRESS_BAR_STEP;
    if (nTokens & XML_TOK_META_STAT_PARA)
    {
        nProgressReference = static_cast<sal_Int32>(aDocStat.nPara);
        bSetFallback = false;
    }
    else if (nTokens & XML_TOK_META_STAT_PAGE)
        bSetFallback = o3tl::checked_multiply<sal_Int32>(aDocStat.nPage, 10, nProgressReference);
    if (!bSetFallback)
        bSetFallback = o3tl::checked_add(nProgressReference, nProgressReferenceWriggleRoom, nProgressReference);
    if (bSetFallback)
        nProgressReference = 250 + nProgressReferenceWriggleRoom;
    ProgressBarHelper* pProgress = GetProgressBarHelper();
    pProgress->SetReference(nProgressReference);
    pProgress->SetValue( 0 );
}

void SwXMLExport::ExportMeta_()
{
    SvXMLExport::ExportMeta_();

    if( !m_bBlock && IsShowProgress() )
    {
        ProgressBarHelper *pProgress = GetProgressBarHelper();
        pProgress->SetValue( pProgress->GetValue() + 2 );
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */