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