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  sal_uInt16 nPrfx,
57  const OUString& rLocalName,
58  const SvXMLEnumMapEntry<sal_uInt16>* pLevelNameMap,
59  enum XMLTokenEnum eLevelAttrName,
60  const char** pLevelStylePropMap,
61  const bool* pAllowedTokenTypes,
62  bool bT )
63 : SvXMLImportContext(rImport, nPrfx, rLocalName)
64 , pOutlineLevelNameMap(pLevelNameMap)
65 , eOutlineLevelAttrName(eLevelAttrName)
66 , pOutlineLevelStylePropMap(pLevelStylePropMap)
67 , pAllowedTokenTypesMap(pAllowedTokenTypes)
68 , nOutlineLevel(1) // all indices have level 1 (0 is for header)
69 , bStyleNameOK(false)
70 , bOutlineLevelOK(false)
71 , bTOC( bT )
72 , rPropertySet(rPropSet)
73 {
74  DBG_ASSERT( ((XML_TOKEN_INVALID != eLevelAttrName) && (nullptr != pLevelNameMap))
75  || ((XML_TOKEN_INVALID == eLevelAttrName) && (nullptr == pLevelNameMap)),
76  "need both, attribute name and value map, or neither" );
77  SAL_WARN_IF( nullptr == pOutlineLevelStylePropMap, "xmloff", "need property name map" );
78  SAL_WARN_IF( nullptr == pAllowedTokenTypes, "xmloff", "need allowed tokens map" );
79 
80  // no map for outline-level? then use 1
81  if (nullptr == pLevelNameMap)
82  {
83  nOutlineLevel = 1;
84  bOutlineLevelOK = true;
85  }
86 }
87 
89 {
90 }
91 
92 
94  const PropertyValues& aValues)
95 {
96  aValueVector.push_back(aValues);
97 }
98 
99 
101  const Reference<XAttributeList> & xAttrList)
102 {
103  // process two attributes: style-name, outline-level
104  sal_Int16 nLength = xAttrList->getLength();
105  for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
106  {
107  OUString sLocalName;
108  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
109  GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
110  &sLocalName );
111  if (XML_NAMESPACE_TEXT == nPrefix)
112  {
113  if ( IsXMLToken( sLocalName, XML_STYLE_NAME ) )
114  {
115  // style name
116  sStyleName = xAttrList->getValueByIndex(nAttr);
117  bStyleNameOK = true;
118  }
120  {
121  // we have an attr name! Then see if we have the attr, too.
122  if (IsXMLToken(sLocalName, eOutlineLevelAttrName))
123  {
124  // outline level
125  sal_uInt16 nTmp;
127  nTmp, xAttrList->getValueByIndex(nAttr),
129  {
130  nOutlineLevel = nTmp;
131  bOutlineLevelOK = true;
132  }
133  // else: illegal value -> ignore
134  }
135  // else: unknown attribute -> ignore
136  }
137  // else: we don't care about outline-level -> ignore
138  }
139  // else: attribute not in text namespace -> ignore
140  }
141 }
142 
144 {
145  if (!bOutlineLevelOK)
146  return;
147 
148  const sal_Int32 nCount = aValueVector.size();
149  Sequence<PropertyValues> aValueSequence(nCount);
150  for(sal_Int32 i = 0; i<nCount; i++)
151  {
152  aValueSequence[i] = aValueVector[i];
153  }
154 
155  // get LevelFormat IndexReplace ...
156  Any aAny = rPropertySet->getPropertyValue("LevelFormat");
157  Reference<XIndexReplace> xIndexReplace;
158  aAny >>= xIndexReplace;
159 
160  // ... and insert
161  xIndexReplace->replaceByIndex(nOutlineLevel, Any(aValueSequence));
162 
163  if (!bStyleNameOK)
164  return;
165 
166  const char* pStyleProperty =
168 
169  DBG_ASSERT(nullptr != pStyleProperty, "need property name");
170  if (nullptr == pStyleProperty)
171  return;
172 
173  OUString sDisplayStyleName =
176  sStyleName );
177  // #i50288#: Check if style exists
178  const Reference < css::container::XNameContainer > & rStyles =
179  GetImport().GetTextImport()->GetParaStyles();
180  if( rStyles.is() &&
181  rStyles->hasByName( sDisplayStyleName ) )
182  {
183  rPropertySet->setPropertyValue(
184  OUString::createFromAscii(pStyleProperty), css::uno::Any(sDisplayStyleName));
185  }
186 }
187 
188 namespace {
191 {
192  XML_TOK_INDEX_TYPE_ENTRY_TEXT = 0,
193  XML_TOK_INDEX_TYPE_TAB_STOP,
194  XML_TOK_INDEX_TYPE_TEXT,
195  XML_TOK_INDEX_TYPE_PAGE_NUMBER,
196  XML_TOK_INDEX_TYPE_CHAPTER,
197  XML_TOK_INDEX_TYPE_LINK_START,
198  XML_TOK_INDEX_TYPE_LINK_END,
199  XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
200 };
201 
202 }
203 
205 {
206  { XML_INDEX_ENTRY_TEXT, XML_TOK_INDEX_TYPE_ENTRY_TEXT },
207  { XML_INDEX_ENTRY_TAB_STOP, XML_TOK_INDEX_TYPE_TAB_STOP },
208  { XML_INDEX_ENTRY_SPAN, XML_TOK_INDEX_TYPE_TEXT },
209  { XML_INDEX_ENTRY_PAGE_NUMBER, XML_TOK_INDEX_TYPE_PAGE_NUMBER },
210  { XML_INDEX_ENTRY_CHAPTER, XML_TOK_INDEX_TYPE_CHAPTER },
211  { XML_INDEX_ENTRY_LINK_START, XML_TOK_INDEX_TYPE_LINK_START },
212  { XML_INDEX_ENTRY_LINK_END, XML_TOK_INDEX_TYPE_LINK_END },
213  { XML_INDEX_ENTRY_BIBLIOGRAPHY, XML_TOK_INDEX_TYPE_BIBLIOGRAPHY },
215 };
216 
218  sal_uInt16 nPrefix,
219  const OUString& rLocalName,
220  const Reference<XAttributeList> & /*xAttrList*/ )
221 {
222  SvXMLImportContext* pContext = nullptr;
223 
224  if (XML_NAMESPACE_TEXT == nPrefix || XML_NAMESPACE_LO_EXT == nPrefix)
225  {
227  if (SvXMLUnitConverter::convertEnum(nToken, rLocalName,
228  aTemplateTokenTypeMap))
229  {
230  // can this index accept this kind of token?
231  if (pAllowedTokenTypesMap[nToken])
232  {
233  switch (nToken)
234  {
235  case XML_TOK_INDEX_TYPE_ENTRY_TEXT:
236  pContext = new XMLIndexSimpleEntryContext(
237  GetImport(), "TokenEntryText", *this,
238  nPrefix, rLocalName);
239  break;
240 
241  case XML_TOK_INDEX_TYPE_PAGE_NUMBER:
242  pContext = new XMLIndexSimpleEntryContext(
243  GetImport(), "TokenPageNumber", *this,
244  nPrefix, rLocalName);
245  break;
246 
247  case XML_TOK_INDEX_TYPE_LINK_START:
248  pContext = new XMLIndexSimpleEntryContext(
249  GetImport(), "TokenHyperlinkStart", *this,
250  nPrefix, rLocalName);
251  break;
252 
253  case XML_TOK_INDEX_TYPE_LINK_END:
254  pContext = new XMLIndexSimpleEntryContext(
255  GetImport(), "TokenHyperlinkEnd", *this,
256  nPrefix, rLocalName);
257  break;
258 
259  case XML_TOK_INDEX_TYPE_TEXT:
260  pContext = new XMLIndexSpanEntryContext(
261  GetImport(), *this, nPrefix, rLocalName);
262  break;
263 
264  case XML_TOK_INDEX_TYPE_TAB_STOP:
265  pContext = new XMLIndexTabStopEntryContext(
266  GetImport(), *this, nPrefix, rLocalName);
267  break;
268 
269  case XML_TOK_INDEX_TYPE_BIBLIOGRAPHY:
270  pContext = new XMLIndexBibliographyEntryContext(
271  GetImport(), *this, nPrefix, rLocalName);
272  break;
273 
274  case XML_TOK_INDEX_TYPE_CHAPTER:
275  pContext = new XMLIndexChapterInfoEntryContext(
276  GetImport(), *this, nPrefix, rLocalName, bTOC );
277  break;
278 
279  default:
280  // ignore!
281  break;
282  }
283  }
284  }
285  }
286 
287  // ignore unknown
288  return pContext;
289 }
290 
291 
292 // maps for the XMLIndexTemplateContext constructor
293 
294 
295 // table of content and user defined index:
296 
298 {
299  { XML_1, 1 },
300  { XML_2, 2 },
301  { XML_3, 3 },
302  { XML_4, 4 },
303  { XML_5, 5 },
304  { XML_6, 6 },
305  { XML_7, 7 },
306  { XML_8, 8 },
307  { XML_9, 9 },
308  { XML_10, 10 },
309  { XML_TOKEN_INVALID, 0 }
310 };
311 
313  { nullptr, "ParaStyleLevel1", "ParaStyleLevel2", "ParaStyleLevel3",
314  "ParaStyleLevel4", "ParaStyleLevel5", "ParaStyleLevel6",
315  "ParaStyleLevel7", "ParaStyleLevel8", "ParaStyleLevel9",
316  "ParaStyleLevel10", nullptr };
317 
318 const bool aAllowedTokenTypesTOC[] =
319 {
320  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
321  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
322  true, // XML_TOK_INDEX_TYPE_TEXT,
323  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
324  true, // XML_TOK_INDEX_TYPE_CHAPTER,
325  true, // XML_TOK_INDEX_TYPE_LINK_START,
326  true, // XML_TOK_INDEX_TYPE_LINK_END,
327  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
328 };
329 
331 {
332  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
333  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
334  true, // XML_TOK_INDEX_TYPE_TEXT,
335  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
336  true, // XML_TOK_INDEX_TYPE_CHAPTER,
337  true, // XML_TOK_INDEX_TYPE_LINK_START,
338  true, // XML_TOK_INDEX_TYPE_LINK_END,
339  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
340 };
341 
342 
343 // alphabetical index
344 
346 {
347  { XML_SEPARATOR, 1 },
348  { XML_1, 2 },
349  { XML_2, 3 },
350  { XML_3, 4 },
351  { XML_TOKEN_INVALID, 0 }
352 };
353 
355  { nullptr, "ParaStyleSeparator", "ParaStyleLevel1", "ParaStyleLevel2",
356  "ParaStyleLevel3", nullptr };
357 
359 {
360  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
361  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
362  true, // XML_TOK_INDEX_TYPE_TEXT,
363  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
364  true, // XML_TOK_INDEX_TYPE_CHAPTER,
365  false, // XML_TOK_INDEX_TYPE_LINK_START,
366  false, // XML_TOK_INDEX_TYPE_LINK_END,
367  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
368 };
369 
370 
371 // bibliography index:
372 
374 {
375  { XML_ARTICLE, 1 },
376  { XML_BOOK, 2 },
377  { XML_BOOKLET, 3 },
378  { XML_CONFERENCE, 4 },
379  { XML_CUSTOM1, 5 },
380  { XML_CUSTOM2, 6 },
381  { XML_CUSTOM3, 7 },
382  { XML_CUSTOM4, 8 },
383  { XML_CUSTOM5, 9 },
384  { XML_EMAIL, 10 },
385  { XML_INBOOK, 11 },
386  { XML_INCOLLECTION, 12 },
387  { XML_INPROCEEDINGS, 13 },
388  { XML_JOURNAL, 14 },
389  { XML_MANUAL, 15 },
390  { XML_MASTERSTHESIS, 16 },
391  { XML_MISC, 17 },
392  { XML_PHDTHESIS, 18 },
393  { XML_PROCEEDINGS, 19 },
394  { XML_TECHREPORT, 20 },
395  { XML_UNPUBLISHED, 21 },
396  { XML_WWW, 22 },
397  { XML_TOKEN_INVALID, 0 }
398 };
399 
400 // TODO: replace with real property names, when available
402 {
403  nullptr, "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
404  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
405  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
406  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
407  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
408  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
409  "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
410  "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  false, // XML_TOK_INDEX_TYPE_CHAPTER,
419  false, // XML_TOK_INDEX_TYPE_LINK_START,
420  false, // XML_TOK_INDEX_TYPE_LINK_END,
421  true // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
422 };
423 
424 
425 // table, illustration and object index
426 
427 // no name map
429 
431  { nullptr, "ParaStyleLevel1", nullptr };
432 
434 {
435  true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
436  true, // XML_TOK_INDEX_TYPE_TAB_STOP,
437  true, // XML_TOK_INDEX_TYPE_TEXT,
438  true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
439  true, // XML_TOK_INDEX_TYPE_CHAPTER,
440  true, // XML_TOK_INDEX_TYPE_LINK_START,
441  true, // XML_TOK_INDEX_TYPE_LINK_END,
442  false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
443 };
444 
445 /* 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[]
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
Import bibliography index entry templates.
Import index entry templates.
static bool convertEnum(EnumT &rEnum, const OUString &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:128
const char * aLevelStylePropNameTOCMap[]
const SvXMLEnumMapEntry< sal_uInt16 > aLevelNameAlphaMap[]
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
const SvXMLEnumMapEntry< sal_uInt16 > aLevelNameBibliographyMap[]
Import index entry templates.
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:394
std::map< sal_Int32, std::shared_ptr< SetItemPropertyStorage > > PropertyValues
const bool aAllowedTokenTypesTOC[]
constexpr sal_uInt16 XML_NAMESPACE_LO_EXT
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:592
const bool aAllowedTokenTypesAlpha[]
const char * aLevelStylePropNameAlphaMap[]
int nCount
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:49
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
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
SvXMLEnumMapEntry< TemplateTokenType > const aTemplateTokenTypeMap[]
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
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1469
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
XMLIndexTemplateContext(SvXMLImport &rImport, css::uno::Reference< css::beans::XPropertySet > &rPropSet, sal_uInt16 nPrfx, const OUString &rLocalName, const SvXMLEnumMapEntry< EnumT > *aLevelNameMap, enum::xmloff::token::XMLTokenEnum eLevelAttrName, const char **aLevelStylePropNameMap, const bool *aAllowedTokenTypes, bool bTOC_=false)
Import chapter info index entry templates.
sal_Int32 nLength
Definition: xmltoken.cxx:36
enum::xmloff::token::XMLTokenEnum eOutlineLevelAttrName
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
const SvXMLEnumMapEntry< sal_uInt16 > aSvLevelNameTOCMap[]
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
Import index entry templates.
::std::vector< css::beans::PropertyValues > aValueVector
const bool aAllowedTokenTypesTable[]