1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "XMLIndexTemplateContext.hxx"
21 #include "XMLIndexSimpleEntryContext.hxx"
22 #include "XMLIndexSpanEntryContext.hxx"
23 #include "XMLIndexTabStopEntryContext.hxx"
24 #include "XMLIndexBibliographyEntryContext.hxx"
25 #include "XMLIndexChapterInfoEntryContext.hxx"
26 #include <xmloff/xmlictxt.hxx>
27 #include <xmloff/xmlimp.hxx>
28 #include <xmloff/txtimp.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <xmloff/xmltoken.hxx>
31 #include <xmloff/xmluconv.hxx>
32 #include <xmloff/xmlement.hxx>
33 #include <tools/debug.hxx>
34 #include <rtl/ustring.hxx>
35 #include <sal/log.hxx>
36 #include <com/sun/star/container/XIndexReplace.hpp>
37 #include <com/sun/star/container/XNameContainer.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
42 using namespace ::xmloff::token
;
44 using ::com::sun::star::beans::XPropertySet
;
45 using ::com::sun::star::beans::PropertyValues
;
46 using ::com::sun::star::uno::Reference
;
47 using ::com::sun::star::uno::Sequence
;
48 using ::com::sun::star::uno::Any
;
49 using ::com::sun::star::container::XIndexReplace
;
51 XMLIndexTemplateContext::XMLIndexTemplateContext(
53 Reference
<XPropertySet
> & rPropSet
,
54 const SvXMLEnumMapEntry
<sal_uInt16
>* pLevelNameMap
,
55 enum XMLTokenEnum eLevelAttrName
,
56 std::span
<const OUString
> pLevelStylePropMap
,
57 const bool* pAllowedTokenTypes
,
59 : SvXMLImportContext(rImport
)
60 , pOutlineLevelNameMap(pLevelNameMap
)
61 , eOutlineLevelAttrName(eLevelAttrName
)
62 , pOutlineLevelStylePropMap(pLevelStylePropMap
)
63 , pAllowedTokenTypesMap(pAllowedTokenTypes
)
64 , nOutlineLevel(1) // all indices have level 1 (0 is for header)
66 , bOutlineLevelOK(false)
68 , rPropertySet(rPropSet
)
70 DBG_ASSERT( ((XML_TOKEN_INVALID
!= eLevelAttrName
) && (nullptr != pLevelNameMap
))
71 || ((XML_TOKEN_INVALID
== eLevelAttrName
) && (nullptr == pLevelNameMap
)),
72 "need both, attribute name and value map, or neither" );
73 SAL_WARN_IF( nullptr == pAllowedTokenTypes
, "xmloff", "need allowed tokens map" );
75 // no map for outline-level? then use 1
76 if (nullptr == pLevelNameMap
)
79 bOutlineLevelOK
= true;
83 XMLIndexTemplateContext::~XMLIndexTemplateContext()
88 void XMLIndexTemplateContext::addTemplateEntry(
89 const PropertyValues
& aValues
)
91 aValueVector
.push_back(aValues
);
95 void XMLIndexTemplateContext::startFastElement(
96 sal_Int32
/*nElement*/,
97 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
99 // process two attributes: style-name, outline-level
100 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
102 if(aIter
.getToken() == XML_ELEMENT(TEXT
, XML_STYLE_NAME
))
105 sStyleName
= aIter
.toString();
108 else if (aIter
.getToken() == XML_ELEMENT(TEXT
, eOutlineLevelAttrName
))
110 // we have an attr name! Then see if we have the attr, too.
113 if (SvXMLUnitConverter::convertEnum(nTmp
, aIter
.toView(), pOutlineLevelNameMap
))
115 nOutlineLevel
= nTmp
;
116 bOutlineLevelOK
= true;
118 // else: illegal value -> ignore
120 // else: attribute not in text namespace -> ignore
124 void XMLIndexTemplateContext::endFastElement(sal_Int32
)
126 if (!bOutlineLevelOK
)
129 const sal_Int32 nCount
= aValueVector
.size();
130 Sequence
<PropertyValues
> aValueSequence(nCount
);
131 std::copy(aValueVector
.begin(), aValueVector
.end(), aValueSequence
.getArray());
133 // get LevelFormat IndexReplace ...
134 Any aAny
= rPropertySet
->getPropertyValue(u
"LevelFormat"_ustr
);
135 Reference
<XIndexReplace
> xIndexReplace
;
136 aAny
>>= xIndexReplace
;
139 xIndexReplace
->replaceByIndex(nOutlineLevel
, Any(aValueSequence
));
144 const OUString pStyleProperty
=
145 pOutlineLevelStylePropMap
[nOutlineLevel
];
147 DBG_ASSERT(!pStyleProperty
.isEmpty(), "need property name");
148 if (pStyleProperty
.isEmpty())
151 OUString sDisplayStyleName
=
152 GetImport().GetStyleDisplayName(
153 XmlStyleFamily::TEXT_PARAGRAPH
,
155 // #i50288#: Check if style exists
156 const Reference
< css::container::XNameContainer
> & rStyles
=
157 GetImport().GetTextImport()->GetParaStyles();
159 rStyles
->hasByName( sDisplayStyleName
) )
161 rPropertySet
->setPropertyValue(
162 pStyleProperty
, css::uno::Any(sDisplayStyleName
));
167 /// template token types; used for aTokenTypeMap parameter
168 enum TemplateTokenType
170 XML_TOK_INDEX_TYPE_ENTRY_TEXT
= 0,
171 XML_TOK_INDEX_TYPE_TAB_STOP
,
172 XML_TOK_INDEX_TYPE_TEXT
,
173 XML_TOK_INDEX_TYPE_PAGE_NUMBER
,
174 XML_TOK_INDEX_TYPE_CHAPTER
,
175 XML_TOK_INDEX_TYPE_LINK_START
,
176 XML_TOK_INDEX_TYPE_LINK_END
,
177 XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
182 SvXMLEnumMapEntry
<TemplateTokenType
> const aTemplateTokenTypeMap
[] =
184 { XML_INDEX_ENTRY_TEXT
, XML_TOK_INDEX_TYPE_ENTRY_TEXT
},
185 { XML_INDEX_ENTRY_TAB_STOP
, XML_TOK_INDEX_TYPE_TAB_STOP
},
186 { XML_INDEX_ENTRY_SPAN
, XML_TOK_INDEX_TYPE_TEXT
},
187 { XML_INDEX_ENTRY_PAGE_NUMBER
, XML_TOK_INDEX_TYPE_PAGE_NUMBER
},
188 { XML_INDEX_ENTRY_CHAPTER
, XML_TOK_INDEX_TYPE_CHAPTER
},
189 { XML_INDEX_ENTRY_LINK_START
, XML_TOK_INDEX_TYPE_LINK_START
},
190 { XML_INDEX_ENTRY_LINK_END
, XML_TOK_INDEX_TYPE_LINK_END
},
191 { XML_INDEX_ENTRY_BIBLIOGRAPHY
, XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
},
192 { XML_TOKEN_INVALID
, TemplateTokenType(0) }
195 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLIndexTemplateContext::createFastChildContext(
197 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
199 SvXMLImportContext
* pContext
= nullptr;
201 if (IsTokenInNamespace(nElement
, XML_NAMESPACE_TEXT
) || IsTokenInNamespace(nElement
, XML_NAMESPACE_LO_EXT
))
203 TemplateTokenType nToken
;
204 if (SvXMLUnitConverter::convertEnum(nToken
, SvXMLImport::getNameFromToken(nElement
),
205 aTemplateTokenTypeMap
))
207 // can this index accept this kind of token?
208 if (pAllowedTokenTypesMap
[nToken
])
212 case XML_TOK_INDEX_TYPE_ENTRY_TEXT
:
213 pContext
= new XMLIndexSimpleEntryContext(
214 GetImport(), u
"TokenEntryText"_ustr
, *this);
217 case XML_TOK_INDEX_TYPE_PAGE_NUMBER
:
218 pContext
= new XMLIndexSimpleEntryContext(
219 GetImport(), u
"TokenPageNumber"_ustr
, *this);
222 case XML_TOK_INDEX_TYPE_LINK_START
:
223 pContext
= new XMLIndexSimpleEntryContext(
224 GetImport(), u
"TokenHyperlinkStart"_ustr
, *this);
227 case XML_TOK_INDEX_TYPE_LINK_END
:
228 pContext
= new XMLIndexSimpleEntryContext(
229 GetImport(), u
"TokenHyperlinkEnd"_ustr
, *this);
232 case XML_TOK_INDEX_TYPE_TEXT
:
233 pContext
= new XMLIndexSpanEntryContext(
237 case XML_TOK_INDEX_TYPE_TAB_STOP
:
238 pContext
= new XMLIndexTabStopEntryContext(
242 case XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
:
243 pContext
= new XMLIndexBibliographyEntryContext(
247 case XML_TOK_INDEX_TYPE_CHAPTER
:
248 pContext
= new XMLIndexChapterInfoEntryContext(
249 GetImport(), *this, bTOC
);
265 // maps for the XMLIndexTemplateContext constructor
268 // table of content and user defined index:
270 const SvXMLEnumMapEntry
<sal_uInt16
> aSvLevelNameTOCMap
[] =
282 { XML_TOKEN_INVALID
, 0 }
285 constexpr OUString aLevelStylePropNameTOCMapArray
[] =
286 { u
""_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel2"_ustr
, u
"ParaStyleLevel3"_ustr
,
287 u
"ParaStyleLevel4"_ustr
, u
"ParaStyleLevel5"_ustr
, u
"ParaStyleLevel6"_ustr
,
288 u
"ParaStyleLevel7"_ustr
, u
"ParaStyleLevel8"_ustr
, u
"ParaStyleLevel9"_ustr
,
289 u
"ParaStyleLevel10"_ustr
, u
""_ustr
};
290 std::span
<const OUString
> const aLevelStylePropNameTOCMap
= aLevelStylePropNameTOCMapArray
;
292 const bool aAllowedTokenTypesTOC
[] =
294 true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
295 true, // XML_TOK_INDEX_TYPE_TAB_STOP,
296 true, // XML_TOK_INDEX_TYPE_TEXT,
297 true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
298 true, // XML_TOK_INDEX_TYPE_CHAPTER,
299 true, // XML_TOK_INDEX_TYPE_LINK_START,
300 true, // XML_TOK_INDEX_TYPE_LINK_END,
301 false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
304 const bool aAllowedTokenTypesUser
[] =
306 true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
307 true, // XML_TOK_INDEX_TYPE_TAB_STOP,
308 true, // XML_TOK_INDEX_TYPE_TEXT,
309 true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
310 true, // XML_TOK_INDEX_TYPE_CHAPTER,
311 true, // XML_TOK_INDEX_TYPE_LINK_START,
312 true, // XML_TOK_INDEX_TYPE_LINK_END,
313 false // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
317 // alphabetical index
319 const SvXMLEnumMapEntry
<sal_uInt16
> aLevelNameAlphaMap
[] =
321 { XML_SEPARATOR
, 1 },
325 { XML_TOKEN_INVALID
, 0 }
328 constexpr OUString aLevelStylePropNameAlphaMapArray
[] =
329 { u
""_ustr
, u
"ParaStyleSeparator"_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel2"_ustr
,
330 u
"ParaStyleLevel3"_ustr
, u
""_ustr
};
331 std::span
<const OUString
> const aLevelStylePropNameAlphaMap
= aLevelStylePropNameAlphaMapArray
;
333 const bool aAllowedTokenTypesAlpha
[] =
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
346 // bibliography index:
348 const SvXMLEnumMapEntry
<sal_uInt16
> aLevelNameBibliographyMap
[] =
353 { XML_CONFERENCE
, 4 },
361 { XML_INCOLLECTION
, 12 },
362 { XML_INPROCEEDINGS
, 13 },
365 { XML_MASTERSTHESIS
, 16 },
367 { XML_PHDTHESIS
, 18 },
368 { XML_PROCEEDINGS
, 19 },
369 { XML_TECHREPORT
, 20 },
370 { XML_UNPUBLISHED
, 21 },
372 { XML_TOKEN_INVALID
, 0 }
375 // TODO: replace with real property names, when available
376 constexpr OUString aLevelStylePropNameBibliographyMapArray
[] =
378 u
""_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
,
379 u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
,
380 u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
,
381 u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
,
382 u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
,
383 u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
,
384 u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
, u
"ParaStyleLevel1"_ustr
,
385 u
"ParaStyleLevel1"_ustr
, u
""_ustr
};
386 std::span
<const OUString
> const aLevelStylePropNameBibliographyMap
= aLevelStylePropNameBibliographyMapArray
;
388 const bool aAllowedTokenTypesBibliography
[] =
390 true, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
391 true, // XML_TOK_INDEX_TYPE_TAB_STOP,
392 true, // XML_TOK_INDEX_TYPE_TEXT,
393 true, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
394 false, // XML_TOK_INDEX_TYPE_CHAPTER,
395 false, // XML_TOK_INDEX_TYPE_LINK_START,
396 false, // XML_TOK_INDEX_TYPE_LINK_END,
397 true // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
401 // table, illustration and object index
404 const SvXMLEnumMapEntry
<sal_uInt16
>* aLevelNameTableMap
= nullptr;
406 constexpr OUString aLevelStylePropNameTableMapArray
[] =
407 { u
""_ustr
, u
"ParaStyleLevel1"_ustr
, u
""_ustr
};
408 std::span
<const OUString
> const aLevelStylePropNameTableMap
= aLevelStylePropNameTableMapArray
;
410 const bool aAllowedTokenTypesTable
[] =
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
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */