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