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