bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / XMLIndexBibliographyConfigurationContext.cxx
blob9129b62d28cf0f8550a5c80c3c41c90fb22b8c8a
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 "XMLIndexBibliographyConfigurationContext.hxx"
21 #include "XMLIndexBibliographyEntryContext.hxx"
22 #include <xmloff/xmlictxt.hxx>
23 #include <xmloff/xmlimp.hxx>
24 #include <xmloff/txtimp.hxx>
25 #include <xmloff/nmspmap.hxx>
26 #include <xmloff/xmlnmspe.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmluconv.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/lang/XMultiServiceFactory.hpp>
34 using namespace ::com::sun::star::text;
35 using namespace ::com::sun::star::uno;
36 using namespace ::xmloff::token;
38 using ::com::sun::star::xml::sax::XAttributeList;
39 using ::com::sun::star::beans::PropertyValue;
40 using ::com::sun::star::beans::XPropertySet;
41 using ::com::sun::star::lang::XMultiServiceFactory;
43 const sal_Char sAPI_FieldMaster_Bibliography[] =
44 "com.sun.star.text.FieldMaster.Bibliography";
47 TYPEINIT1( XMLIndexBibliographyConfigurationContext, SvXMLStyleContext );
49 XMLIndexBibliographyConfigurationContext::XMLIndexBibliographyConfigurationContext(
50 SvXMLImport& rImport,
51 sal_uInt16 nPrfx,
52 const OUString& rLocalName,
53 const Reference<XAttributeList> & xAttrList) :
54 SvXMLStyleContext(rImport, nPrfx, rLocalName, xAttrList, XML_STYLE_FAMILY_TEXT_BIBLIOGRAPHYCONFIG),
55 sFieldMaster_Bibliography(
56 sAPI_FieldMaster_Bibliography),
57 sBracketBefore("BracketBefore"),
58 sBracketAfter("BracketAfter"),
59 sIsNumberEntries("IsNumberEntries"),
60 sIsSortByPosition("IsSortByPosition"),
61 sSortKeys("SortKeys"),
62 sSortKey("SortKey"),
63 sIsSortAscending("IsSortAscending"),
64 sSortAlgorithm("SortAlgorithm"),
65 sLocale("Locale"),
66 sSuffix(),
67 sPrefix(),
68 sAlgorithm(),
69 maLanguageTagODF(),
70 bNumberedEntries(false),
71 bSortByPosition(true)
75 XMLIndexBibliographyConfigurationContext::~XMLIndexBibliographyConfigurationContext()
79 void XMLIndexBibliographyConfigurationContext::StartElement(
80 const Reference<XAttributeList> & xAttrList)
82 sal_Int16 nLength = xAttrList->getLength();
83 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
85 OUString sLocalName;
86 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
87 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
88 &sLocalName );
90 ProcessAttribute(nPrefix, sLocalName,
91 xAttrList->getValueByIndex(nAttr));
92 // else: ignore
96 void XMLIndexBibliographyConfigurationContext::ProcessAttribute(
97 sal_uInt16 nPrefix,
98 const OUString& sLocalName,
99 const OUString& sValue)
101 if( XML_NAMESPACE_TEXT == nPrefix )
103 if( IsXMLToken(sLocalName, XML_PREFIX) )
105 sPrefix = sValue;
107 else if( IsXMLToken(sLocalName, XML_SUFFIX) )
109 sSuffix = sValue;
111 else if( IsXMLToken(sLocalName, XML_NUMBERED_ENTRIES) )
113 bool bTmp(false);
114 if (::sax::Converter::convertBool(bTmp, sValue))
116 bNumberedEntries = bTmp;
119 else if( IsXMLToken(sLocalName, XML_SORT_BY_POSITION) )
121 bool bTmp(false);
122 if (::sax::Converter::convertBool(bTmp, sValue))
124 bSortByPosition = bTmp;
127 else if( IsXMLToken(sLocalName, XML_SORT_ALGORITHM) )
129 sAlgorithm = sValue;
132 else if( XML_NAMESPACE_FO == nPrefix )
134 if( IsXMLToken(sLocalName, XML_LANGUAGE) )
136 maLanguageTagODF.maLanguage = sValue;
138 else if( IsXMLToken(sLocalName, XML_SCRIPT) )
140 maLanguageTagODF.maScript = sValue;
142 else if( IsXMLToken(sLocalName, XML_COUNTRY) )
144 maLanguageTagODF.maCountry = sValue;
147 else if( XML_NAMESPACE_STYLE == nPrefix )
149 if( IsXMLToken(sLocalName, XML_RFC_LANGUAGE_TAG) )
151 maLanguageTagODF.maRfcLanguageTag = sValue;
157 SvXMLImportContext *XMLIndexBibliographyConfigurationContext::CreateChildContext(
158 sal_uInt16 nPrefix,
159 const OUString& rLocalName,
160 const Reference<XAttributeList> & xAttrList )
162 OUString sKey;
163 sal_Bool bSort(sal_True);
165 // process children here and use default context!
166 if ( ( nPrefix == XML_NAMESPACE_TEXT ) &&
167 IsXMLToken( rLocalName, XML_SORT_KEY ) )
169 sal_Int16 nLength = xAttrList->getLength();
170 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
172 OUString sLocalName;
173 sal_uInt16 nPrfx = GetImport().GetNamespaceMap().
174 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
175 &sLocalName );
177 if (nPrfx == XML_NAMESPACE_TEXT)
179 if ( IsXMLToken( sLocalName, XML_KEY ) )
181 sKey = xAttrList->getValueByIndex(nAttr);
183 else if ( IsXMLToken( sLocalName, XML_SORT_ASCENDING ) )
185 bool bTmp(false);
186 if (::sax::Converter::convertBool(
187 bTmp, xAttrList->getValueByIndex(nAttr)))
189 bSort = bTmp;
195 // valid data?
196 sal_uInt16 nKey;
197 if (SvXMLUnitConverter::convertEnum(nKey, sKey,
198 aBibliographyDataFieldMap))
201 Any aAny;
202 Sequence<PropertyValue> aKey(2);
204 PropertyValue aNameValue;
205 aNameValue.Name = sSortKey;
206 aAny <<= (sal_Int16)nKey;
207 aNameValue.Value = aAny;
208 aKey[0] = aNameValue;
210 PropertyValue aSortValue;
211 aSortValue.Name = sIsSortAscending;
212 aAny.setValue(&bSort, cppu::UnoType<bool>::get());
213 aSortValue.Value = aAny;
214 aKey[1] = aSortValue;
216 aSortKeys.push_back(aKey);
220 return SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
221 xAttrList);
224 void XMLIndexBibliographyConfigurationContext::CreateAndInsert(bool)
226 // (code almost the same as export...)
228 // insert and block mode is handled in insertStyleFamily
230 // first: get field master
231 // (we'll create one, and get the only master for this type)
232 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
233 if( xFactory.is() )
235 Sequence<OUString> aServices = xFactory->getAvailableServiceNames();
236 bool bFound(false);
237 sal_Int32 i(0);
238 sal_Int32 nServiceCount(aServices.getLength());
239 while (i < nServiceCount && !bFound)
241 if (aServices[i].equals(sFieldMaster_Bibliography))
242 // here we should use a method which compares in reverse order if available
243 // #85282#
244 bFound = true;
245 else
246 i++;
248 if (bFound)
250 Reference<XInterface> xIfc =
251 xFactory->createInstance(sFieldMaster_Bibliography);
252 if( xIfc.is() )
254 Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
255 Any aAny;
257 aAny <<= sSuffix;
258 xPropSet->setPropertyValue(sBracketAfter, aAny);
260 aAny <<= sPrefix;
261 xPropSet->setPropertyValue(sBracketBefore, aAny);
263 aAny.setValue(&bNumberedEntries, cppu::UnoType<bool>::get());
264 xPropSet->setPropertyValue(sIsNumberEntries, aAny);
266 aAny.setValue(&bSortByPosition, cppu::UnoType<bool>::get());
267 xPropSet->setPropertyValue(sIsSortByPosition, aAny);
269 if( !maLanguageTagODF.isEmpty() )
271 aAny <<= maLanguageTagODF.getLanguageTag().getLocale( false);
272 xPropSet->setPropertyValue(sLocale, aAny);
275 if( !sAlgorithm.isEmpty() )
277 aAny <<= sAlgorithm;
278 xPropSet->setPropertyValue(sSortAlgorithm, aAny);
281 sal_Int32 nCount = aSortKeys.size();
282 Sequence<Sequence<PropertyValue> > aKeysSeq(nCount);
283 for(i = 0; i < nCount; i++)
285 aKeysSeq[i] = aSortKeys[i];
287 aAny <<= aKeysSeq;
288 xPropSet->setPropertyValue(sSortKeys, aAny);
290 // else: can't get FieldMaster -> ignore
293 // else: can't even get Factory -> ignore
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */