LibreOffice Module xmloff (master)  1
XMLIndexBibliographyConfigurationContext.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 
22 #include <xmloff/xmlictxt.hxx>
23 #include <xmloff/xmlimp.hxx>
24 #include <xmloff/txtimp.hxx>
25 #include <xmloff/namespacemap.hxx>
26 #include <xmloff/xmlnamespace.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <sal/log.hxx>
30 #include <sax/tools/converter.hxx>
31 #include <rtl/ustring.hxx>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/frame/XModel.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <comphelper/sequence.hxx>
36 
37 using namespace ::com::sun::star::text;
38 using namespace ::com::sun::star::uno;
39 using namespace ::xmloff::token;
40 
41 using ::com::sun::star::xml::sax::XAttributeList;
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;
46 
47 
48 constexpr OUStringLiteral gsFieldMaster_Bibliography(u"com.sun.star.text.FieldMaster.Bibliography");
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 OUStringLiteral gsSortKey(u"SortKey");
55 constexpr OUStringLiteral gsIsSortAscending(u"IsSortAscending");
56 constexpr OUStringLiteral gsSortAlgorithm(u"SortAlgorithm");
57 constexpr OUStringLiteral gsLocale(u"Locale");
58 
60  SvXMLImport& rImport) :
62  maLanguageTagODF(),
63  bNumberedEntries(false),
64  bSortByPosition(true)
65 {
66 }
67 
69 {
70 }
71 
73  sal_Int32 nElement,
74  const OUString& sValue)
75 {
76  switch (nElement)
77  {
79  sPrefix = sValue;
80  break;
82  sSuffix = sValue;
83  break;
85  {
86  bool bTmp(false);
87  if (::sax::Converter::convertBool(bTmp, sValue))
88  {
89  bNumberedEntries = bTmp;
90  }
91  break;
92  }
94  {
95  bool bTmp(false);
96  if (::sax::Converter::convertBool(bTmp, sValue))
97  {
98  bSortByPosition = bTmp;
99  }
100  break;
101  }
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;
116  break;
117  }
118 }
119 
120 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexBibliographyConfigurationContext::createFastChildContext(
121  sal_Int32 nElement,
122  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
123 {
124  // process children here and use default context!
125  if ( nElement == XML_ELEMENT(TEXT, XML_SORT_KEY) )
126  {
127  std::string_view sKey;
128  bool bSort(true);
129 
130  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
131  {
132  switch (aIter.getToken())
133  {
134  case XML_ELEMENT(TEXT, XML_KEY):
135  sKey = aIter.toView();
136  break;
138  {
139  bool bTmp(false);
140  if (::sax::Converter::convertBool(bTmp, aIter.toView()))
141  bSort = bTmp;
142  break;
143  }
144  default:
145  XMLOFF_WARN_UNKNOWN("xmloff", aIter);
146  break;
147  }
148  }
149 
150  // valid data?
151  sal_uInt16 nKey;
152  if (SvXMLUnitConverter::convertEnum(nKey, sKey,
154  {
155  Sequence<PropertyValue> aKey(2);
156 
157  PropertyValue aNameValue;
158  aNameValue.Name = gsSortKey;
159  aNameValue.Value <<= static_cast<sal_Int16>(nKey);
160  aKey[0] = aNameValue;
161 
162  PropertyValue aSortValue;
163  aSortValue.Name = gsIsSortAscending;
164  aSortValue.Value <<= bSort;
165  aKey[1] = aSortValue;
166 
167  aSortKeys.push_back(aKey);
168  }
169  }
170 
171  return nullptr;
172 }
173 
175 {
176  // (code almost the same as export...)
177 
178  // insert and block mode is handled in insertStyleFamily
179 
180  // first: get field master
181  // (we'll create one, and get the only master for this type)
182  Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
183  if( !xFactory.is() )
184  return;
185 
186  Sequence<OUString> aServices = xFactory->getAvailableServiceNames();
187  // here we should use a method which compares in reverse order if available
189  return;
190 
191  Reference<XInterface> xIfc =
192  xFactory->createInstance(gsFieldMaster_Bibliography);
193  if( !xIfc.is() )
194  return;
195 
196  Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
197  Any aAny;
198 
199  xPropSet->setPropertyValue(gsBracketAfter, Any(sSuffix));
200  xPropSet->setPropertyValue(gsBracketBefore, Any(sPrefix));
201  xPropSet->setPropertyValue(gsIsNumberEntries, Any(bNumberedEntries));
202  xPropSet->setPropertyValue(gsIsSortByPosition, Any(bSortByPosition));
203 
204  if( !maLanguageTagODF.isEmpty() )
205  {
206  aAny <<= maLanguageTagODF.getLanguageTag().getLocale( false);
207  xPropSet->setPropertyValue(gsLocale, aAny);
208  }
209 
210  if( !sAlgorithm.isEmpty() )
211  {
212  xPropSet->setPropertyValue(gsSortAlgorithm, Any(sAlgorithm));
213  }
214 
215  Sequence<Sequence<PropertyValue> > aKeysSeq = comphelper::containerToSequence(aSortKeys);
216  xPropSet->setPropertyValue(gsSortKeys, Any(aKeysSeq));
217  // else: can't get FieldMaster -> ignore
218  // else: can't even get Factory -> ignore
219 }
220 
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
constexpr OUStringLiteral gsIsNumberEntries(u"IsNumberEntries")
::std::vector< css::uno::Sequence< css::beans::PropertyValue > > aSortKeys
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
LanguageTag getLanguageTag() const
Best call this only once per instance, it recreates a LanguageTag instance on every call...
constexpr OUStringLiteral gsBracketBefore(u"BracketBefore")
constexpr OUStringLiteral gsSortAlgorithm(u"SortAlgorithm")
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
XmlStyleFamily
Definition: families.hxx:47
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:113
bool isEmpty() const
constexpr OUStringLiteral gsSortKey(u"SortKey")
constexpr OUStringLiteral gsBracketAfter(u"BracketAfter")
constexpr OUStringLiteral gsLocale(u"Locale")
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
OUString maRfcLanguageTag
static bool convertBool(bool &rBool, std::u16string_view rString)
const SvXMLEnumMapEntry< sal_uInt16 > aBibliographyDataFieldMap[]
constexpr OUStringLiteral gsSortKeys(u"SortKeys")
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Handling of tokens in XML:
constexpr OUStringLiteral gsFieldMaster_Bibliography(u"com.sun.star.text.FieldMaster.Bibliography")
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
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
constexpr OUStringLiteral gsIsSortByPosition(u"IsSortByPosition")
constexpr OUStringLiteral gsIsSortAscending(u"IsSortAscending")
Reference< XSingleServiceFactory > xFactory
TEXT