tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / xmloff / source / text / XMLIndexBibliographyConfigurationContext.cxx
blob1be742637df09aeae2cda0c8c1dc9fff71d0d86b
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/xmlnamespace.hxx>
26 #include <xmloff/xmltoken.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <sal/log.hxx>
29 #include <sax/tools/converter.hxx>
30 #include <rtl/ustring.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/frame/XModel.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <comphelper/sequence.hxx>
36 using namespace ::com::sun::star::uno;
37 using namespace ::xmloff::token;
39 using ::com::sun::star::xml::sax::XFastAttributeList;
40 using ::com::sun::star::beans::PropertyValue;
41 using ::com::sun::star::beans::XPropertySet;
42 using ::com::sun::star::lang::XMultiServiceFactory;
45 constexpr OUString gsFieldMaster_Bibliography(u"com.sun.star.text.FieldMaster.Bibliography"_ustr);
46 constexpr OUStringLiteral gsBracketBefore(u"BracketBefore");
47 constexpr OUStringLiteral gsBracketAfter(u"BracketAfter");
48 constexpr OUStringLiteral gsIsNumberEntries(u"IsNumberEntries");
49 constexpr OUStringLiteral gsIsSortByPosition(u"IsSortByPosition");
50 constexpr OUStringLiteral gsSortKeys(u"SortKeys");
51 constexpr OUString gsSortKey(u"SortKey"_ustr);
52 constexpr OUString gsIsSortAscending(u"IsSortAscending"_ustr);
53 constexpr OUStringLiteral gsSortAlgorithm(u"SortAlgorithm");
54 constexpr OUStringLiteral gsLocale(u"Locale");
56 XMLIndexBibliographyConfigurationContext::XMLIndexBibliographyConfigurationContext(
57 SvXMLImport& rImport) :
58 SvXMLStyleContext(rImport, XmlStyleFamily::TEXT_BIBLIOGRAPHYCONFIG),
59 maLanguageTagODF(),
60 bNumberedEntries(false),
61 bSortByPosition(true)
65 XMLIndexBibliographyConfigurationContext::~XMLIndexBibliographyConfigurationContext()
69 void XMLIndexBibliographyConfigurationContext::SetAttribute(
70 sal_Int32 nElement,
71 const OUString& sValue)
73 switch (nElement)
75 case XML_ELEMENT(TEXT, XML_PREFIX):
76 sPrefix = sValue;
77 break;
78 case XML_ELEMENT(TEXT, XML_SUFFIX):
79 sSuffix = sValue;
80 break;
81 case XML_ELEMENT(TEXT, XML_NUMBERED_ENTRIES):
83 bool bTmp(false);
84 if (::sax::Converter::convertBool(bTmp, sValue))
86 bNumberedEntries = bTmp;
88 break;
90 case XML_ELEMENT(TEXT, XML_SORT_BY_POSITION):
92 bool bTmp(false);
93 if (::sax::Converter::convertBool(bTmp, sValue))
95 bSortByPosition = bTmp;
97 break;
99 case XML_ELEMENT(TEXT, XML_SORT_ALGORITHM):
100 sAlgorithm = sValue;
101 break;
102 case XML_ELEMENT(FO, XML_LANGUAGE):
103 maLanguageTagODF.maLanguage = sValue;
104 break;
105 case XML_ELEMENT(FO, XML_SCRIPT):
106 maLanguageTagODF.maScript = sValue;
107 break;
108 case XML_ELEMENT(FO, XML_COUNTRY):
109 maLanguageTagODF.maCountry = sValue;
110 break;
111 case XML_ELEMENT(STYLE, XML_RFC_LANGUAGE_TAG):
112 maLanguageTagODF.maRfcLanguageTag = sValue;
113 break;
117 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexBibliographyConfigurationContext::createFastChildContext(
118 sal_Int32 nElement,
119 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
121 // process children here and use default context!
122 if ( nElement == XML_ELEMENT(TEXT, XML_SORT_KEY) )
124 std::string_view sKey;
125 bool bSort(true);
127 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
129 switch (aIter.getToken())
131 case XML_ELEMENT(TEXT, XML_KEY):
132 sKey = aIter.toView();
133 break;
134 case XML_ELEMENT(TEXT, XML_SORT_ASCENDING):
136 bool bTmp(false);
137 if (::sax::Converter::convertBool(bTmp, aIter.toView()))
138 bSort = bTmp;
139 break;
141 default:
142 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
143 break;
147 // valid data?
148 sal_uInt16 nKey;
149 if (SvXMLUnitConverter::convertEnum(nKey, sKey,
150 aBibliographyDataFieldMap))
152 Sequence<PropertyValue> aKey
154 comphelper::makePropertyValue(gsSortKey, static_cast<sal_Int16>(nKey)),
155 comphelper::makePropertyValue(gsIsSortAscending, bSort)
158 aSortKeys.push_back(aKey);
162 return nullptr;
165 void XMLIndexBibliographyConfigurationContext::CreateAndInsert(bool)
167 // (code almost the same as export...)
169 // insert and block mode is handled in insertStyleFamily
171 // first: get field master
172 // (we'll create one, and get the only master for this type)
173 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
174 if( !xFactory.is() )
175 return;
177 Sequence<OUString> aServices = xFactory->getAvailableServiceNames();
178 // here we should use a method which compares in reverse order if available
179 if (comphelper::findValue(aServices, gsFieldMaster_Bibliography) == -1)
180 return;
182 Reference<XInterface> xIfc =
183 xFactory->createInstance(gsFieldMaster_Bibliography);
184 if( !xIfc.is() )
185 return;
187 Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
188 Any aAny;
190 xPropSet->setPropertyValue(gsBracketAfter, Any(sSuffix));
191 xPropSet->setPropertyValue(gsBracketBefore, Any(sPrefix));
192 xPropSet->setPropertyValue(gsIsNumberEntries, Any(bNumberedEntries));
193 xPropSet->setPropertyValue(gsIsSortByPosition, Any(bSortByPosition));
195 if( !maLanguageTagODF.isEmpty() )
197 aAny <<= maLanguageTagODF.getLanguageTag().getLocale( false);
198 xPropSet->setPropertyValue(gsLocale, aAny);
201 if( !sAlgorithm.isEmpty() )
203 xPropSet->setPropertyValue(gsSortAlgorithm, Any(sAlgorithm));
206 Sequence<Sequence<PropertyValue> > aKeysSeq = comphelper::containerToSequence(aSortKeys);
207 xPropSet->setPropertyValue(gsSortKeys, Any(aKeysSeq));
208 // else: can't get FieldMaster -> ignore
209 // else: can't even get Factory -> ignore
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */