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 
42 using namespace ::std;
43 using namespace ::xmloff::token;
44 
45 using ::com::sun::star::beans::XPropertySet;
46 using ::com::sun::star::beans::PropertyValues;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Sequence;
49 using ::com::sun::star::uno::Any;
50 using ::com::sun::star::xml::sax::XAttributeList;
51 using ::com::sun::star::container::XIndexReplace;
52 
54  SvXMLImport& rImport,
55  Reference<XPropertySet> & rPropSet,
56  const SvXMLEnumMapEntry<sal_uInt16>* pLevelNameMap,
57  enum XMLTokenEnum eLevelAttrName,
58  const char** pLevelStylePropMap,
59  const bool* pAllowedTokenTypes,
60  bool bT )
61 : SvXMLImportContext(rImport)
62 , pOutlineLevelNameMap(pLevelNameMap)
63 , eOutlineLevelAttrName(eLevelAttrName)
64 , pOutlineLevelStylePropMap(pLevelStylePropMap)
65 , pAllowedTokenTypesMap(pAllowedTokenTypes)
66 , nOutlineLevel(1) // all indices have level 1 (0 is for header)
67 , bStyleNameOK(false)
68 , bOutlineLevelOK(false)
69 , bTOC( bT )
70 , rPropertySet(rPropSet)
71 {
72  DBG_ASSERT( ((XML_TOKEN_INVALID != eLevelAttrName) && (nullptr != pLevelNameMap))
73  || ((XML_TOKEN_INVALID == eLevelAttrName) && (nullptr == pLevelNameMap)),
74  "need both, attribute name and value map, or neither" );
75  SAL_WARN_IF( nullptr == pOutlineLevelStylePropMap, "xmloff", "need property name map" );
76  SAL_WARN_IF( nullptr == pAllowedTokenTypes, "xmloff", "need allowed tokens map" );
77 
78  // no map for outline-level? then use 1
79  if (nullptr == pLevelNameMap)
80  {
81  nOutlineLevel = 1;
82  bOutlineLevelOK = true;
83  }
84 }
85 
87 {
88 }
89 
90 
92  const PropertyValues& aValues)
93 {
94  aValueVector.push_back(aValues);
95 }
96 
97 
99  sal_Int32 /*nElement*/,
100  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
101 {
102  // process two attributes: style-name, outline-level
103  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
104  {
105  if(aIter.getToken() == XML_ELEMENT(TEXT, XML_STYLE_NAME))
106  {
107  // style name
108  sStyleName = aIter.toString();
109  bStyleNameOK = true;
110  }
111  else if (aIter.getToken() == XML_ELEMENT(TEXT, eOutlineLevelAttrName))
112  {
113  // we have an attr name! Then see if we have the attr, too.
114  // outline level
115  sal_uInt16 nTmp;
116  if (SvXMLUnitConverter::convertEnum(nTmp, aIter.toView(), pOutlineLevelNameMap))
117  {
118  nOutlineLevel = nTmp;
119  bOutlineLevelOK = true;
120  }
121  // else: illegal value -> ignore
122  }
123  // else: attribute not in text namespace -> ignore
124  }
125 }
126 
128 {
129  if (!bOutlineLevelOK)
130  return;
131 
132  const sal_Int32 nCount = aValueVector.size();
133  Sequence<PropertyValues> aValueSequence(nCount);
134  for(sal_Int32 i = 0; i<nCount; i++)
135  {
136  aValueSequence[i] = aValueVector[i];
137  }
138 
139  // get LevelFormat IndexReplace ...
140  Any aAny = rPropertySet->getPropertyValue("LevelFormat");
141  Reference<XIndexReplace> xIndexReplace;
142  aAny >>= xIndexReplace;
143 
144  // ... and insert
145  xIndexReplace->replaceByIndex(nOutlineLevel, Any(aValueSequence));
146 
147  if (!bStyleNameOK)
148  return;
149 
150  const char* pStyleProperty =
152 
153  DBG_ASSERT(nullptr != pStyleProperty, "need property name");
154  if (nullptr == pStyleProperty)
155  return;
156 
157  OUString sDisplayStyleName =
160  sStyleName );
161  // #i50288#: Check if style exists
162  const Reference < css::container::XNameContainer > & rStyles =
163  GetImport().GetTextImport()->GetParaStyles();
164  if( rStyles.is() &&
165  rStyles->hasByName( sDisplayStyleName ) )
166  {
167  rPropertySet->setPropertyValue(
168  OUString::createFromAscii(pStyleProperty), css::uno::Any(sDisplayStyleName));
169  }
170 }
171 
172 namespace {
175 {
176  XML_TOK_INDEX_TYPE_ENTRY_TEXT = 0,
177  XML_TOK_INDEX_TYPE_TAB_STOP,
178  XML_TOK_INDEX_TYPE_TEXT,
179  XML_TOK_INDEX_TYPE_PAGE_NUMBER,
180  XML_TOK_INDEX_TYPE_CHAPTER,
181  XML_TOK_INDEX_TYPE_LINK_START,
182  XML_TOK_INDEX_TYPE_LINK_END,
183  XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
184 };
185 
186 }
187 
189 {
190  { XML_INDEX_ENTRY_TEXT, XML_TOK_INDEX_TYPE_ENTRY_TEXT },
191  { XML_INDEX_ENTRY_TAB_STOP, XML_TOK_INDEX_TYPE_TAB_STOP },
192  { XML_INDEX_ENTRY_SPAN, XML_TOK_INDEX_TYPE_TEXT },
193  { XML_INDEX_ENTRY_PAGE_NUMBER, XML_TOK_INDEX_TYPE_PAGE_NUMBER },
194  { XML_INDEX_ENTRY_CHAPTER, XML_TOK_INDEX_TYPE_CHAPTER },
195  { XML_INDEX_ENTRY_LINK_START, XML_TOK_INDEX_TYPE_LINK_START },
196  { XML_INDEX_ENTRY_LINK_END, XML_TOK_INDEX_TYPE_LINK_END },
197  { XML_INDEX_ENTRY_BIBLIOGRAPHY, XML_TOK_INDEX_TYPE_BIBLIOGRAPHY },
199 };
200 
201 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexTemplateContext::createFastChildContext(
202  sal_Int32 nElement,
203  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
204 {
205  SvXMLImportContext* pContext = nullptr;
206 
208  {
211  aTemplateTokenTypeMap))
212  {
213  // can this index accept this kind of token?
214  if (pAllowedTokenTypesMap[nToken])
215  {
216  switch (nToken)
217  {
218  case XML_TOK_INDEX_TYPE_ENTRY_TEXT:
219  pContext = new XMLIndexSimpleEntryContext(
220  GetImport(), "TokenEntryText", *this);
221  break;
222 
223  case XML_TOK_INDEX_TYPE_PAGE_NUMBER:
224  pContext = new XMLIndexSimpleEntryContext(
225  GetImport(), "TokenPageNumber", *this);
226  break;
227 
228  case XML_TOK_INDEX_TYPE_LINK_START:
229  pContext = new XMLIndexSimpleEntryContext(
230  GetImport(), "TokenHyperlinkStart", *this);
231  break;
232 
233  case XML_TOK_INDEX_TYPE_LINK_END:
234  pContext = new XMLIndexSimpleEntryContext(
235  GetImport(), "TokenHyperlinkEnd", *this);
236  break;
237 
238  case XML_TOK_INDEX_TYPE_TEXT:
239  pContext = new XMLIndexSpanEntryContext(
240  GetImport(), *this);
241  break;
242 
243  case XML_TOK_INDEX_TYPE_TAB_STOP:
244  pContext = new XMLIndexTabStopEntryContext(
245  GetImport(), *this);
246  break;
247 
248  case XML_TOK_INDEX_TYPE_BIBLIOGRAPHY:
249  pContext = new XMLIndexBibliographyEntryContext(
250  GetImport(), *this);
251  break;
252 
253  case XML_TOK_INDEX_TYPE_CHAPTER:
254  pContext = new XMLIndexChapterInfoEntryContext(
255  GetImport(), *this, bTOC );
256  break;
257 
258  default:
259  // ignore!
260  break;
261  }
262  }
263  }
264  }
265 
266  // ignore unknown
267  return pContext;
268 }
269 
270 
271 // maps for the XMLIndexTemplateContext constructor
272 
273 
274 // table of content and user defined index:
275 
277 {
278  { XML_1, 1 },
279  { XML_2, 2 },
280  { XML_3, 3 },
281  { XML_4, 4 },
282  { XML_5, 5 },
283  { XML_6, 6 },
284  { XML_7, 7 },
285  { XML_8, 8 },
286  { XML_9, 9 },
287  { XML_10, 10 },
288  { XML_TOKEN_INVALID, 0 }
289 };
290 
292  { nullptr, "ParaStyleLevel1", "ParaStyleLevel2", "ParaStyleLevel3",
293  "ParaStyleLevel4", "ParaStyleLevel5", "ParaStyleLevel6",
294  "ParaStyleLevel7", "ParaStyleLevel8", "ParaStyleLevel9",
295  "ParaStyleLevel10", nullptr };
296 
297 const bool aAllowedTokenTypesTOC[] =
298 {
299  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
300  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
301  true, // XML_TOK_INDEX_TYPE_TEXT,
302  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
303  true, // XML_TOK_INDEX_TYPE_CHAPTER,
304  true, // XML_TOK_INDEX_TYPE_LINK_START,
305  true, // XML_TOK_INDEX_TYPE_LINK_END,
306  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
307 };
308 
310 {
311  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
312  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
313  true, // XML_TOK_INDEX_TYPE_TEXT,
314  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
315  true, // XML_TOK_INDEX_TYPE_CHAPTER,
316  true, // XML_TOK_INDEX_TYPE_LINK_START,
317  true, // XML_TOK_INDEX_TYPE_LINK_END,
318  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
319 };
320 
321 
322 // alphabetical index
323 
325 {
326  { XML_SEPARATOR, 1 },
327  { XML_1, 2 },
328  { XML_2, 3 },
329  { XML_3, 4 },
330  { XML_TOKEN_INVALID, 0 }
331 };
332 
334  { nullptr, "ParaStyleSeparator", "ParaStyleLevel1", "ParaStyleLevel2",
335  "ParaStyleLevel3", nullptr };
336 
338 {
339  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
340  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
341  true, // XML_TOK_INDEX_TYPE_TEXT,
342  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
343  true, // XML_TOK_INDEX_TYPE_CHAPTER,
344  false, // XML_TOK_INDEX_TYPE_LINK_START,
345  false, // XML_TOK_INDEX_TYPE_LINK_END,
346  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
347 };
348 
349 
350 // bibliography index:
351 
353 {
354  { XML_ARTICLE, 1 },
355  { XML_BOOK, 2 },
356  { XML_BOOKLET, 3 },
357  { XML_CONFERENCE, 4 },
358  { XML_CUSTOM1, 5 },
359  { XML_CUSTOM2, 6 },
360  { XML_CUSTOM3, 7 },
361  { XML_CUSTOM4, 8 },
362  { XML_CUSTOM5, 9 },
363  { XML_EMAIL, 10 },
364  { XML_INBOOK, 11 },
365  { XML_INCOLLECTION, 12 },
366  { XML_INPROCEEDINGS, 13 },
367  { XML_JOURNAL, 14 },
368  { XML_MANUAL, 15 },
369  { XML_MASTERSTHESIS, 16 },
370  { XML_MISC, 17 },
371  { XML_PHDTHESIS, 18 },
372  { XML_PROCEEDINGS, 19 },
373  { XML_TECHREPORT, 20 },
374  { XML_UNPUBLISHED, 21 },
375  { XML_WWW, 22 },
376  { XML_TOKEN_INVALID, 0 }
377 };
378 
379 // TODO: replace with real property names, when available
381 {
382  nullptr, "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
383  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
384  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
385  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
386  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
387  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
388  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
389  "ParaStyleLevel1", nullptr };
390 
392 {
393  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
394  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
395  true, // XML_TOK_INDEX_TYPE_TEXT,
396  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
397  false, // XML_TOK_INDEX_TYPE_CHAPTER,
398  false, // XML_TOK_INDEX_TYPE_LINK_START,
399  false, // XML_TOK_INDEX_TYPE_LINK_END,
400  true // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
401 };
402 
403 
404 // table, illustration and object index
405 
406 // no name map
408 
410  { nullptr, "ParaStyleLevel1", nullptr };
411 
413 {
414  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
415  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
416  true, // XML_TOK_INDEX_TYPE_TEXT,
417  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
418  true, // XML_TOK_INDEX_TYPE_CHAPTER,
419  true, // XML_TOK_INDEX_TYPE_LINK_START,
420  true, // XML_TOK_INDEX_TYPE_LINK_END,
421  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
422 };
423 
424 /* 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:600
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)
int i
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:1924
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:1410
#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[]