LibreOffice Module xmloff (master)  1
XMLIndexTemplateContext.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 
26 #include <xmloff/xmlictxt.hxx>
27 #include <xmloff/xmlimp.hxx>
28 #include <xmloff/txtimp.hxx>
29 #include <xmloff/namespacemap.hxx>
30 #include <xmloff/xmlnamespace.hxx>
31 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/xmluconv.hxx>
33 #include <xmloff/xmlement.hxx>
34 #include <tools/debug.hxx>
35 #include <rtl/ustring.hxx>
36 #include <sal/log.hxx>
37 #include <com/sun/star/container/XIndexReplace.hpp>
38 #include <com/sun/star/container/XNameContainer.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 
41 #include <algorithm>
42 
43 using namespace ::std;
44 using namespace ::xmloff::token;
45 
46 using ::com::sun::star::beans::XPropertySet;
47 using ::com::sun::star::beans::PropertyValues;
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::uno::Any;
51 using ::com::sun::star::xml::sax::XAttributeList;
52 using ::com::sun::star::container::XIndexReplace;
53 
55  SvXMLImport& rImport,
56  Reference<XPropertySet> & rPropSet,
57  const SvXMLEnumMapEntry<sal_uInt16>* pLevelNameMap,
58  enum XMLTokenEnum eLevelAttrName,
59  const char** pLevelStylePropMap,
60  const bool* pAllowedTokenTypes,
61  bool bT )
62 : SvXMLImportContext(rImport)
63 , pOutlineLevelNameMap(pLevelNameMap)
64 , eOutlineLevelAttrName(eLevelAttrName)
65 , pOutlineLevelStylePropMap(pLevelStylePropMap)
66 , pAllowedTokenTypesMap(pAllowedTokenTypes)
67 , nOutlineLevel(1) // all indices have level 1 (0 is for header)
68 , bStyleNameOK(false)
69 , bOutlineLevelOK(false)
70 , bTOC( bT )
71 , rPropertySet(rPropSet)
72 {
73  DBG_ASSERT( ((XML_TOKEN_INVALID != eLevelAttrName) && (nullptr != pLevelNameMap))
74  || ((XML_TOKEN_INVALID == eLevelAttrName) && (nullptr == pLevelNameMap)),
75  "need both, attribute name and value map, or neither" );
76  SAL_WARN_IF( nullptr == pOutlineLevelStylePropMap, "xmloff", "need property name map" );
77  SAL_WARN_IF( nullptr == pAllowedTokenTypes, "xmloff", "need allowed tokens map" );
78 
79  // no map for outline-level? then use 1
80  if (nullptr == pLevelNameMap)
81  {
82  nOutlineLevel = 1;
83  bOutlineLevelOK = true;
84  }
85 }
86 
88 {
89 }
90 
91 
93  const PropertyValues& aValues)
94 {
95  aValueVector.push_back(aValues);
96 }
97 
98 
100  sal_Int32 /*nElement*/,
101  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
102 {
103  // process two attributes: style-name, outline-level
104  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
105  {
106  if(aIter.getToken() == XML_ELEMENT(TEXT, XML_STYLE_NAME))
107  {
108  // style name
109  sStyleName = aIter.toString();
110  bStyleNameOK = true;
111  }
112  else if (aIter.getToken() == XML_ELEMENT(TEXT, eOutlineLevelAttrName))
113  {
114  // we have an attr name! Then see if we have the attr, too.
115  // outline level
116  sal_uInt16 nTmp;
117  if (SvXMLUnitConverter::convertEnum(nTmp, aIter.toView(), pOutlineLevelNameMap))
118  {
119  nOutlineLevel = nTmp;
120  bOutlineLevelOK = true;
121  }
122  // else: illegal value -> ignore
123  }
124  // else: attribute not in text namespace -> ignore
125  }
126 }
127 
129 {
130  if (!bOutlineLevelOK)
131  return;
132 
133  const sal_Int32 nCount = aValueVector.size();
134  Sequence<PropertyValues> aValueSequence(nCount);
135  std::copy(aValueVector.begin(), aValueVector.end(), aValueSequence.getArray());
136 
137  // get LevelFormat IndexReplace ...
138  Any aAny = rPropertySet->getPropertyValue("LevelFormat");
139  Reference<XIndexReplace> xIndexReplace;
140  aAny >>= xIndexReplace;
141 
142  // ... and insert
143  xIndexReplace->replaceByIndex(nOutlineLevel, Any(aValueSequence));
144 
145  if (!bStyleNameOK)
146  return;
147 
148  const char* pStyleProperty =
150 
151  DBG_ASSERT(nullptr != pStyleProperty, "need property name");
152  if (nullptr == pStyleProperty)
153  return;
154 
155  OUString sDisplayStyleName =
158  sStyleName );
159  // #i50288#: Check if style exists
160  const Reference < css::container::XNameContainer > & rStyles =
161  GetImport().GetTextImport()->GetParaStyles();
162  if( rStyles.is() &&
163  rStyles->hasByName( sDisplayStyleName ) )
164  {
165  rPropertySet->setPropertyValue(
166  OUString::createFromAscii(pStyleProperty), css::uno::Any(sDisplayStyleName));
167  }
168 }
169 
170 namespace {
173 {
174  XML_TOK_INDEX_TYPE_ENTRY_TEXT = 0,
175  XML_TOK_INDEX_TYPE_TAB_STOP,
176  XML_TOK_INDEX_TYPE_TEXT,
177  XML_TOK_INDEX_TYPE_PAGE_NUMBER,
178  XML_TOK_INDEX_TYPE_CHAPTER,
179  XML_TOK_INDEX_TYPE_LINK_START,
180  XML_TOK_INDEX_TYPE_LINK_END,
181  XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
182 };
183 
184 }
185 
187 {
188  { XML_INDEX_ENTRY_TEXT, XML_TOK_INDEX_TYPE_ENTRY_TEXT },
189  { XML_INDEX_ENTRY_TAB_STOP, XML_TOK_INDEX_TYPE_TAB_STOP },
190  { XML_INDEX_ENTRY_SPAN, XML_TOK_INDEX_TYPE_TEXT },
191  { XML_INDEX_ENTRY_PAGE_NUMBER, XML_TOK_INDEX_TYPE_PAGE_NUMBER },
192  { XML_INDEX_ENTRY_CHAPTER, XML_TOK_INDEX_TYPE_CHAPTER },
193  { XML_INDEX_ENTRY_LINK_START, XML_TOK_INDEX_TYPE_LINK_START },
194  { XML_INDEX_ENTRY_LINK_END, XML_TOK_INDEX_TYPE_LINK_END },
195  { XML_INDEX_ENTRY_BIBLIOGRAPHY, XML_TOK_INDEX_TYPE_BIBLIOGRAPHY },
197 };
198 
199 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexTemplateContext::createFastChildContext(
200  sal_Int32 nElement,
201  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
202 {
203  SvXMLImportContext* pContext = nullptr;
204 
206  {
209  aTemplateTokenTypeMap))
210  {
211  // can this index accept this kind of token?
212  if (pAllowedTokenTypesMap[nToken])
213  {
214  switch (nToken)
215  {
216  case XML_TOK_INDEX_TYPE_ENTRY_TEXT:
217  pContext = new XMLIndexSimpleEntryContext(
218  GetImport(), "TokenEntryText", *this);
219  break;
220 
221  case XML_TOK_INDEX_TYPE_PAGE_NUMBER:
222  pContext = new XMLIndexSimpleEntryContext(
223  GetImport(), "TokenPageNumber", *this);
224  break;
225 
226  case XML_TOK_INDEX_TYPE_LINK_START:
227  pContext = new XMLIndexSimpleEntryContext(
228  GetImport(), "TokenHyperlinkStart", *this);
229  break;
230 
231  case XML_TOK_INDEX_TYPE_LINK_END:
232  pContext = new XMLIndexSimpleEntryContext(
233  GetImport(), "TokenHyperlinkEnd", *this);
234  break;
235 
236  case XML_TOK_INDEX_TYPE_TEXT:
237  pContext = new XMLIndexSpanEntryContext(
238  GetImport(), *this);
239  break;
240 
241  case XML_TOK_INDEX_TYPE_TAB_STOP:
242  pContext = new XMLIndexTabStopEntryContext(
243  GetImport(), *this);
244  break;
245 
246  case XML_TOK_INDEX_TYPE_BIBLIOGRAPHY:
247  pContext = new XMLIndexBibliographyEntryContext(
248  GetImport(), *this);
249  break;
250 
251  case XML_TOK_INDEX_TYPE_CHAPTER:
252  pContext = new XMLIndexChapterInfoEntryContext(
253  GetImport(), *this, bTOC );
254  break;
255 
256  default:
257  // ignore!
258  break;
259  }
260  }
261  }
262  }
263 
264  // ignore unknown
265  return pContext;
266 }
267 
268 
269 // maps for the XMLIndexTemplateContext constructor
270 
271 
272 // table of content and user defined index:
273 
275 {
276  { XML_1, 1 },
277  { XML_2, 2 },
278  { XML_3, 3 },
279  { XML_4, 4 },
280  { XML_5, 5 },
281  { XML_6, 6 },
282  { XML_7, 7 },
283  { XML_8, 8 },
284  { XML_9, 9 },
285  { XML_10, 10 },
286  { XML_TOKEN_INVALID, 0 }
287 };
288 
290  { nullptr, "ParaStyleLevel1", "ParaStyleLevel2", "ParaStyleLevel3",
291  "ParaStyleLevel4", "ParaStyleLevel5", "ParaStyleLevel6",
292  "ParaStyleLevel7", "ParaStyleLevel8", "ParaStyleLevel9",
293  "ParaStyleLevel10", nullptr };
294 
295 const bool aAllowedTokenTypesTOC[] =
296 {
297  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
298  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
299  true, // XML_TOK_INDEX_TYPE_TEXT,
300  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
301  true, // XML_TOK_INDEX_TYPE_CHAPTER,
302  true, // XML_TOK_INDEX_TYPE_LINK_START,
303  true, // XML_TOK_INDEX_TYPE_LINK_END,
304  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
305 };
306 
308 {
309  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
310  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
311  true, // XML_TOK_INDEX_TYPE_TEXT,
312  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
313  true, // XML_TOK_INDEX_TYPE_CHAPTER,
314  true, // XML_TOK_INDEX_TYPE_LINK_START,
315  true, // XML_TOK_INDEX_TYPE_LINK_END,
316  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
317 };
318 
319 
320 // alphabetical index
321 
323 {
324  { XML_SEPARATOR, 1 },
325  { XML_1, 2 },
326  { XML_2, 3 },
327  { XML_3, 4 },
328  { XML_TOKEN_INVALID, 0 }
329 };
330 
332  { nullptr, "ParaStyleSeparator", "ParaStyleLevel1", "ParaStyleLevel2",
333  "ParaStyleLevel3", nullptr };
334 
336 {
337  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
338  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
339  true, // XML_TOK_INDEX_TYPE_TEXT,
340  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
341  true, // XML_TOK_INDEX_TYPE_CHAPTER,
342  false, // XML_TOK_INDEX_TYPE_LINK_START,
343  false, // XML_TOK_INDEX_TYPE_LINK_END,
344  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
345 };
346 
347 
348 // bibliography index:
349 
351 {
352  { XML_ARTICLE, 1 },
353  { XML_BOOK, 2 },
354  { XML_BOOKLET, 3 },
355  { XML_CONFERENCE, 4 },
356  { XML_CUSTOM1, 5 },
357  { XML_CUSTOM2, 6 },
358  { XML_CUSTOM3, 7 },
359  { XML_CUSTOM4, 8 },
360  { XML_CUSTOM5, 9 },
361  { XML_EMAIL, 10 },
362  { XML_INBOOK, 11 },
363  { XML_INCOLLECTION, 12 },
364  { XML_INPROCEEDINGS, 13 },
365  { XML_JOURNAL, 14 },
366  { XML_MANUAL, 15 },
367  { XML_MASTERSTHESIS, 16 },
368  { XML_MISC, 17 },
369  { XML_PHDTHESIS, 18 },
370  { XML_PROCEEDINGS, 19 },
371  { XML_TECHREPORT, 20 },
372  { XML_UNPUBLISHED, 21 },
373  { XML_WWW, 22 },
374  { XML_TOKEN_INVALID, 0 }
375 };
376 
377 // TODO: replace with real property names, when available
379 {
380  nullptr, "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
381  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
382  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
383  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
384  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
385  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
386  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
387  "ParaStyleLevel1", nullptr };
388 
390 {
391  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
392  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
393  true, // XML_TOK_INDEX_TYPE_TEXT,
394  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
395  false, // XML_TOK_INDEX_TYPE_CHAPTER,
396  false, // XML_TOK_INDEX_TYPE_LINK_START,
397  false, // XML_TOK_INDEX_TYPE_LINK_END,
398  true // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
399 };
400 
401 
402 // table, illustration and object index
403 
404 // no name map
406 
408  { nullptr, "ParaStyleLevel1", nullptr };
409 
411 {
412  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
413  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
414  true, // XML_TOK_INDEX_TYPE_TEXT,
415  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
416  true, // XML_TOK_INDEX_TYPE_CHAPTER,
417  true, // XML_TOK_INDEX_TYPE_LINK_START,
418  true, // XML_TOK_INDEX_TYPE_LINK_END,
419  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
420 };
421 
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::beans::XPropertySet > & rPropertySet
void addTemplateEntry(const css::beans::PropertyValues &aValues)
add template; to be called by child template entry contexts
const char * aLevelStylePropNameTableMap[]
Import bibliography index entry templates.
Import index entry templates.
const char * aLevelStylePropNameTOCMap[]
const SvXMLEnumMapEntry< sal_uInt16 > aLevelNameAlphaMap[]
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
const SvXMLEnumMapEntry< sal_uInt16 > aLevelNameBibliographyMap[]
Import index entry templates.
std::map< sal_Int32, std::shared_ptr< SetItemPropertyStorage > > PropertyValues
const bool aAllowedTokenTypesTOC[]
constexpr sal_uInt16 XML_NAMESPACE_LO_EXT
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:599
const bool aAllowedTokenTypesAlpha[]
const char * aLevelStylePropNameAlphaMap[]
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
int nCount
const bool aAllowedTokenTypesUser[]
const bool aAllowedTokenTypesBibliography[]
#define DBG_ASSERT(sCon, aError)
constexpr sal_uInt16 XML_NAMESPACE_TEXT
const SvXMLEnumMapEntry< sal_uInt16 > * pOutlineLevelNameMap
TemplateTokenType
template token types; used for aTokenTypeMap parameter
XMLIndexTemplateContext(SvXMLImport &rImport, css::uno::Reference< css::beans::XPropertySet > &rPropSet, const SvXMLEnumMapEntry< EnumT > *aLevelNameMap, enum::xmloff::token::XMLTokenEnum eLevelAttrName, const char **aLevelStylePropNameMap, const bool *aAllowedTokenTypes, bool bTOC_=false)
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
SvXMLEnumMapEntry< TemplateTokenType > const aTemplateTokenTypeMap[]
static const OUString & getNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:1911
const char * aLevelStylePropNameBibliographyMap[]
const SvXMLEnumMapEntry< sal_uInt16 > * aLevelNameTableMap
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
DefTokenId nToken
virtual ~XMLIndexTemplateContext() override
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
constexpr bool IsTokenInNamespace(sal_Int32 nToken, sal_uInt16 nNamespacePrefix)
Definition: xmlimp.hxx:104
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1404
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
static bool convertEnum(EnumT &rEnum, std::u16string_view rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
convert string to enum using given enum map, if the enum is not found in the map, this method will re...
Definition: xmluconv.hxx:145
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:49
Import chapter info index entry templates.
enum::xmloff::token::XMLTokenEnum eOutlineLevelAttrName
const SvXMLEnumMapEntry< sal_uInt16 > aSvLevelNameTOCMap[]
TEXT
Import index entry templates.
::std::vector< css::beans::PropertyValues > aValueVector
const bool aAllowedTokenTypesTable[]