Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / text / XMLIndexBibliographyConfigurationContext.cxx
blobe7ba5e6baee7959178200dcfde296d54ea1eb24d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <comphelper/propertyvalue.hxx>
21 #include <XMLIndexBibliographyConfigurationContext.hxx>
22 #include "XMLIndexBibliographyEntryContext.hxx"
23 #include <xmloff/xmlictxt.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <xmloff/txtimp.hxx>
26 #include <xmloff/namespacemap.hxx>
27 #include <xmloff/xmlnamespace.hxx>
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/xmluconv.hxx>
30 #include <sal/log.hxx>
31 #include <sax/tools/converter.hxx>
32 #include <rtl/ustring.hxx>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/frame/XModel.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <comphelper/sequence.hxx>
38 using namespace ::com::sun::star::text;
39 using namespace ::com::sun::star::uno;
40 using namespace ::xmloff::token;
42 using ::com::sun::star::xml::sax::XFastAttributeList;
43 using ::com::sun::star::beans::PropertyValue;
44 using ::com::sun::star::beans::XPropertySet;
45 using ::com::sun::star::lang::XMultiServiceFactory;
48 constexpr OUString gsFieldMaster_Bibliography(u"com.sun.star.text.FieldMaster.Bibliography"_ustr);
49 constexpr OUStringLiteral gsBracketBefore(u"BracketBefore");
50 constexpr OUStringLiteral gsBracketAfter(u"BracketAfter");
51 constexpr OUStringLiteral gsIsNumberEntries(u"IsNumberEntries");
52 constexpr OUStringLiteral gsIsSortByPosition(u"IsSortByPosition");
53 constexpr OUStringLiteral gsSortKeys(u"SortKeys");
54 constexpr OUString gsSortKey(u"SortKey"_ustr);
55 constexpr OUString gsIsSortAscending(u"IsSortAscending"_ustr);
56 constexpr OUStringLiteral gsSortAlgorithm(u"SortAlgorithm");
57 constexpr OUStringLiteral gsLocale(u"Locale");
59 XMLIndexBibliographyConfigurationContext::XMLIndexBibliographyConfigurationContext(
60 SvXMLImport& rImport) :
61 SvXMLStyleContext(rImport, XmlStyleFamily::TEXT_BIBLIOGRAPHYCONFIG),
62 maLanguageTagODF(),
63 bNumberedEntries(false),
64 bSortByPosition(true)
68 XMLIndexBibliographyConfigurationContext::~XMLIndexBibliographyConfigurationContext()
72 void XMLIndexBibliographyConfigurationContext::SetAttribute(
73 sal_Int32 nElement,
74 const OUString& sValue)
76 switch (nElement)
78 case XML_ELEMENT(TEXT, XML_PREFIX):
79 sPrefix = sValue;
80 break;
81 case XML_ELEMENT(TEXT, XML_SUFFIX):
82 sSuffix = sValue;
83 break;
84 case XML_ELEMENT(TEXT, XML_NUMBERED_ENTRIES):
86 bool bTmp(false);
87 if (::sax::Converter::convertBool(bTmp, sValue))
89 bNumberedEntries = bTmp;
91 break;
93 case XML_ELEMENT(TEXT, XML_SORT_BY_POSITION):
95 bool bTmp(false);
96 if (::sax::Converter::convertBool(bTmp, sValue))
98 bSortByPosition = bTmp;
100 break;
102 case XML_ELEMENT(TEXT, XML_SORT_ALGORITHM):
103 sAlgorithm = sValue;
104 break;
105 case XML_ELEMENT(FO, XML_LANGUAGE):
106 maLanguageTagODF.maLanguage = sValue;
107 break;
108 case XML_ELEMENT(FO, XML_SCRIPT):
109 maLanguageTagODF.maScript = sValue;
110 break;
111 case XML_ELEMENT(FO, XML_COUNTRY):
112 maLanguageTagODF.maCountry = sValue;
113 break;
114 case XML_ELEMENT(STYLE, XML_RFC_LANGUAGE_TAG):
115 maLanguageTagODF.maRfcLanguageTag = sValue;
116 break;
120 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexBibliographyConfigurationContext::createFastChildContext(
121 sal_Int32 nElement,
122 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
124 // process children here and use default context!
125 if ( nElement == XML_ELEMENT(TEXT, XML_SORT_KEY) )
127 std::string_view sKey;
128 bool bSort(true);
130 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
132 switch (aIter.getToken())
134 case XML_ELEMENT(TEXT, XML_KEY):
135 sKey = aIter.toView();
136 break;
137 case XML_ELEMENT(TEXT, XML_SORT_ASCENDING):
139 bool bTmp(false);
140 if (::sax::Converter::convertBool(bTmp, aIter.toView()))
141 bSort = bTmp;
142 break;
144 default:
145 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
146 break;
150 // valid data?
151 sal_uInt16 nKey;
152 if (SvXMLUnitConverter::convertEnum(nKey, sKey,
153 aBibliographyDataFieldMap))
155 Sequence<PropertyValue> aKey
157 comphelper::makePropertyValue(gsSortKey, static_cast<sal_Int16>(nKey)),
158 comphelper::makePropertyValue(gsIsSortAscending, bSort)
161 aSortKeys.push_back(aKey);
165 return nullptr;
168 void XMLIndexBibliographyConfigurationContext::CreateAndInsert(bool)
170 // (code almost the same as export...)
172 // insert and block mode is handled in insertStyleFamily
174 // first: get field master
175 // (we'll create one, and get the only master for this type)
176 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
177 if( !xFactory.is() )
178 return;
180 Sequence<OUString> aServices = xFactory->getAvailableServiceNames();
181 // here we should use a method which compares in reverse order if available
182 if (comphelper::findValue(aServices, gsFieldMaster_Bibliography) == -1)
183 return;
185 Reference<XInterface> xIfc =
186 xFactory->createInstance(gsFieldMaster_Bibliography);
187 if( !xIfc.is() )
188 return;
190 Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
191 Any aAny;
193 xPropSet->setPropertyValue(gsBracketAfter, Any(sSuffix));
194 xPropSet->setPropertyValue(gsBracketBefore, Any(sPrefix));
195 xPropSet->setPropertyValue(gsIsNumberEntries, Any(bNumberedEntries));
196 xPropSet->setPropertyValue(gsIsSortByPosition, Any(bSortByPosition));
198 if( !maLanguageTagODF.isEmpty() )
200 aAny <<= maLanguageTagODF.getLanguageTag().getLocale( false);
201 xPropSet->setPropertyValue(gsLocale, aAny);
204 if( !sAlgorithm.isEmpty() )
206 xPropSet->setPropertyValue(gsSortAlgorithm, Any(sAlgorithm));
209 Sequence<Sequence<PropertyValue> > aKeysSeq = comphelper::containerToSequence(aSortKeys);
210 xPropSet->setPropertyValue(gsSortKeys, Any(aKeysSeq));
211 // else: can't get FieldMaster -> ignore
212 // else: can't even get Factory -> ignore
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */