LibreOffice Module sw (master) 1
xmlmeta.cxx
Go to the documentation of this file.
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 <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
21#include <com/sun/star/frame/XModel.hpp>
22#include <osl/diagnose.h>
23#include <xmloff/xmlmetai.hxx>
25#include <xmloff/xmltkmap.hxx>
26#include <o3tl/safeint.hxx>
27#include <xmloff/xmluconv.hxx>
28#include <docstat.hxx>
29#include <doc.hxx>
31#include "xmlimp.hxx"
32#include "xmlexp.hxx"
33
34using namespace ::com::sun::star;
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star::lang;
37using namespace ::com::sun::star::text;
38using namespace ::xmloff::token;
39
40uno::Reference<document::XDocumentProperties>
42{
45 {
46 return nullptr;
47 }
48 uno::Reference<document::XDocumentPropertiesSupplier> const xDPS(
49 GetModel(), UNO_QUERY_THROW);
50 return xDPS->getDocumentProperties();
51}
52
54 const sal_Int32 /*nElement*/ )
55{
56 SvXMLImportContext *pContext = nullptr;
57
58 if (getImportFlags() & SvXMLImportFlags::META)
59 {
60 uno::Reference<document::XDocumentProperties> const xDocProps(
62 pContext = new SvXMLMetaDocumentContext(*this, xDocProps);
63 }
64
65 return pContext;
66}
67
68namespace {
69
70enum SvXMLTokenMapAttrs
71{
72 XML_TOK_META_STAT_TABLE = 1,
73 XML_TOK_META_STAT_IMAGE = 2,
74 XML_TOK_META_STAT_OLE = 4,
75 XML_TOK_META_STAT_PAGE = 8,
76 XML_TOK_META_STAT_PARA = 16,
77 XML_TOK_META_STAT_WORD = 32,
78 XML_TOK_META_STAT_CHAR = 64,
79 XML_TOK_META_STAT_NON_WHITE_SPACE_CHAR = 128,
80 XML_TOK_META_STAT_END=XML_TOK_UNKNOWN
81};
82
83struct statistic {
84 SvXMLTokenMapAttrs token;
85 const char* name;
86 sal_uInt16 SwDocStat::* target16;
87 sal_uLong SwDocStat::* target32; /* or 64, on LP64 platforms */
88};
89
90}
91
92const struct statistic s_stats [] = {
93 { XML_TOK_META_STAT_TABLE, "TableCount", &SwDocStat::nTable, nullptr },
94 { XML_TOK_META_STAT_IMAGE, "ImageCount", &SwDocStat::nGrf, nullptr },
95 { XML_TOK_META_STAT_OLE, "ObjectCount", &SwDocStat::nOLE, nullptr },
96 { XML_TOK_META_STAT_PAGE, "PageCount", nullptr, &SwDocStat::nPage },
97 { XML_TOK_META_STAT_PARA, "ParagraphCount", nullptr, &SwDocStat::nPara },
98 { XML_TOK_META_STAT_WORD, "WordCount", nullptr, &SwDocStat::nWord },
99 { XML_TOK_META_STAT_CHAR, "CharacterCount", nullptr, &SwDocStat::nChar },
100 { XML_TOK_META_STAT_NON_WHITE_SPACE_CHAR, "NonWhitespaceCharacterCount", nullptr, &SwDocStat::nCharExcludingSpaces },
101 { XML_TOK_META_STAT_END, nullptr, nullptr, nullptr }
102};
103
105 const Sequence< beans::NamedValue > & i_rStats)
106{
107 if( IsStylesOnlyMode() || IsInsertMode() )
108 return;
109
110 SvXMLImport::SetStatistics(i_rStats);
111
112 SwDoc *pDoc = getDoc();
113 SwDocStat aDocStat( pDoc->getIDocumentStatistics().GetDocStat() );
114
115 sal_uInt32 nTokens = 0;
116
117 for (const auto& rStat : i_rStats) {
118 for (struct statistic const* pStat = s_stats; pStat->name != nullptr;
119 ++pStat) {
120 if (rStat.Name.equalsAscii(pStat->name)) {
121 sal_Int32 val = 0;
122 if (rStat.Value >>= val) {
123 if (pStat->target16 != nullptr) {
124 aDocStat.*(pStat->target16)
125 = o3tl::narrowing<sal_uInt16> (val);
126 } else {
127 aDocStat.*(pStat->target32)
128 = static_cast<sal_uInt32> (val);
129 }
130 nTokens |= pStat->token;
131 } else {
132 OSL_FAIL("SwXMLImport::SetStatistics: invalid entry");
133 }
134 }
135 }
136 }
137
138 if( nTokens )
139 pDoc->getIDocumentStatistics().SetDocStat( aDocStat );
140
141 // set progress bar reference to #paragraphs. If not available,
142 // use #pages*10, or guesstimate 250 paragraphs. Additionally
143 // guesstimate PROGRESS_BAR_STEPS each for meta+settings, styles,
144 // and autostyles.
145 bool bSetFallback = true;
146 sal_Int32 nProgressReference = sal_Int32(); // silence C4701
147 const sal_Int32 nProgressReferenceWriggleRoom = 3 * PROGRESS_BAR_STEP;
148 if (nTokens & XML_TOK_META_STAT_PARA)
149 {
150 nProgressReference = static_cast<sal_Int32>(aDocStat.nPara);
151 bSetFallback = false;
152 }
153 else if (nTokens & XML_TOK_META_STAT_PAGE)
154 bSetFallback = o3tl::checked_multiply<sal_Int32>(aDocStat.nPage, 10, nProgressReference);
155 if (!bSetFallback)
156 bSetFallback = o3tl::checked_add(nProgressReference, nProgressReferenceWriggleRoom, nProgressReference);
157 if (bSetFallback)
158 nProgressReference = 250 + nProgressReferenceWriggleRoom;
159 ProgressBarHelper* pProgress = GetProgressBarHelper();
160 pProgress->SetReference(nProgressReference);
161 pProgress->SetValue( 0 );
162}
163
165{
167
168 if( !m_bBlock && IsShowProgress() )
169 {
171 pProgress->SetValue( pProgress->GetValue() + 2 );
172 }
173}
174
175/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual const SwDocStat & GetDocStat() const =0
Document - Statistics.
virtual void SetDocStat(const SwDocStat &rStat)=0
Set the document statistics.
void SetValue(sal_Int32 nValue)
void SetReference(sal_Int32 nVal)
sal_Int32 GetValue() const
ProgressBarHelper * GetProgressBarHelper()
virtual void ExportMeta_()
Definition: doc.hxx:197
IDocumentStatistics const & getIDocumentStatistics() const
Definition: doc.cxx:387
bool IsShowProgress() const
Definition: xmlexp.hxx:133
virtual void ExportMeta_() override
Definition: xmlmeta.cxx:164
bool m_bBlock
Definition: xmlexp.hxx:61
const SwDoc * getDoc() const
Definition: xmlimp.cxx:1683
SvXMLImportContext * CreateMetaContext(const sal_Int32 nElement)
Definition: xmlmeta.cxx:53
css::uno::Reference< css::document::XDocumentProperties > GetDocumentProperties() const
Definition: xmlmeta.cxx:41
virtual void SetStatistics(const css::uno::Sequence< css::beans::NamedValue > &i_rStats) override
Definition: xmlmeta.cxx:104
bool IsBlockMode() const
Definition: xmlimp.hxx:139
bool m_bOrganizerMode
Definition: xmlimp.hxx:82
bool IsInsertMode() const
Definition: xmlimp.hxx:137
bool IsStylesOnlyMode() const
Definition: xmlimp.hxx:138
const char * name
std::enable_if< std::is_signed< T >::value, bool >::type checked_add(T a, T b, T &result)
sal_uIntPtr sal_uLong
sal_uLong nWord
Definition: docstat.hxx:35
sal_uInt16 nOLE
Definition: docstat.hxx:29
sal_uInt16 nTable
Definition: docstat.hxx:27
sal_uLong nCharExcludingSpaces
Definition: docstat.hxx:38
sal_uInt16 nGrf
Definition: docstat.hxx:28
sal_uLong nPara
paragraphs for document statistic: non-empty and non-hidden ones
Definition: docstat.hxx:32
sal_uLong nPage
Definition: docstat.hxx:30
sal_uLong nChar
Definition: docstat.hxx:37
#define PROGRESS_BAR_STEP
Definition: xmlimp.hxx:49
const struct statistic s_stats[]
Definition: xmlmeta.cxx:92
#define XML_TOK_UNKNOWN