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