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 const OUStringLiteral gsFieldMaster_Bibliography(u"com.sun.star.text.FieldMaster.Bibliography");
49 const OUStringLiteral gsBracketBefore(u"BracketBefore");
50 const OUStringLiteral gsBracketAfter(u"BracketAfter");
51 const OUStringLiteral gsIsNumberEntries(u"IsNumberEntries");
52 const OUStringLiteral gsIsSortByPosition(u"IsSortByPosition");
53 const OUStringLiteral gsSortKeys(u"SortKeys");
54 const OUStringLiteral gsSortKey(u"SortKey");
55 const OUStringLiteral gsIsSortAscending(u"IsSortAscending");
56 const OUStringLiteral gsSortAlgorithm(u"SortAlgorithm");
57 const OUStringLiteral gsLocale(u"Locale");
58 
60  SvXMLImport& rImport) :
62  sSuffix(),
63  sPrefix(),
64  sAlgorithm(),
65  maLanguageTagODF(),
66  bNumberedEntries(false),
67  bSortByPosition(true)
68 {
69 }
70 
72 {
73 }
74 
76  sal_uInt16 nPrefix,
77  const OUString& sLocalName,
78  const OUString& sValue)
79 {
80  if( XML_NAMESPACE_TEXT == nPrefix )
81  {
82  if( IsXMLToken(sLocalName, XML_PREFIX) )
83  {
84  sPrefix = sValue;
85  }
86  else if( IsXMLToken(sLocalName, XML_SUFFIX) )
87  {
88  sSuffix = sValue;
89  }
90  else if( IsXMLToken(sLocalName, XML_NUMBERED_ENTRIES) )
91  {
92  bool bTmp(false);
93  if (::sax::Converter::convertBool(bTmp, sValue))
94  {
95  bNumberedEntries = bTmp;
96  }
97  }
98  else if( IsXMLToken(sLocalName, XML_SORT_BY_POSITION) )
99  {
100  bool bTmp(false);
101  if (::sax::Converter::convertBool(bTmp, sValue))
102  {
103  bSortByPosition = bTmp;
104  }
105  }
106  else if( IsXMLToken(sLocalName, XML_SORT_ALGORITHM) )
107  {
108  sAlgorithm = sValue;
109  }
110  }
111  else if( XML_NAMESPACE_FO == nPrefix )
112  {
113  if( IsXMLToken(sLocalName, XML_LANGUAGE) )
114  {
115  maLanguageTagODF.maLanguage = sValue;
116  }
117  else if( IsXMLToken(sLocalName, XML_SCRIPT) )
118  {
119  maLanguageTagODF.maScript = sValue;
120  }
121  else if( IsXMLToken(sLocalName, XML_COUNTRY) )
122  {
123  maLanguageTagODF.maCountry = sValue;
124  }
125  }
126  else if( XML_NAMESPACE_STYLE == nPrefix )
127  {
128  if( IsXMLToken(sLocalName, XML_RFC_LANGUAGE_TAG) )
129  {
131  }
132  }
133 }
134 
135 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexBibliographyConfigurationContext::createFastChildContext(
136  sal_Int32 nElement,
137  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
138 {
139  // process children here and use default context!
140  if ( nElement == XML_ELEMENT(TEXT, XML_SORT_KEY) )
141  {
142  OUString sKey;
143  bool bSort(true);
144 
145  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
146  {
147  switch (aIter.getToken())
148  {
149  case XML_ELEMENT(TEXT, XML_KEY):
150  sKey = aIter.toString();
151  break;
153  {
154  bool bTmp(false);
155  if (::sax::Converter::convertBool(bTmp, aIter.toString()))
156  bSort = bTmp;
157  break;
158  }
159  default:
160  SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << aIter.toString());
161  break;
162  }
163  }
164 
165  // valid data?
166  sal_uInt16 nKey;
167  if (SvXMLUnitConverter::convertEnum(nKey, sKey,
169  {
170  Sequence<PropertyValue> aKey(2);
171 
172  PropertyValue aNameValue;
173  aNameValue.Name = gsSortKey;
174  aNameValue.Value <<= static_cast<sal_Int16>(nKey);
175  aKey[0] = aNameValue;
176 
177  PropertyValue aSortValue;
178  aSortValue.Name = gsIsSortAscending;
179  aSortValue.Value <<= bSort;
180  aKey[1] = aSortValue;
181 
182  aSortKeys.push_back(aKey);
183  }
184  }
185 
186  return nullptr;
187 }
188 
190 {
191  // (code almost the same as export...)
192 
193  // insert and block mode is handled in insertStyleFamily
194 
195  // first: get field master
196  // (we'll create one, and get the only master for this type)
197  Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
198  if( !xFactory.is() )
199  return;
200 
201  Sequence<OUString> aServices = xFactory->getAvailableServiceNames();
202  // here we should use a method which compares in reverse order if available
204  return;
205 
206  Reference<XInterface> xIfc =
207  xFactory->createInstance(gsFieldMaster_Bibliography);
208  if( !xIfc.is() )
209  return;
210 
211  Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
212  Any aAny;
213 
214  xPropSet->setPropertyValue(gsBracketAfter, Any(sSuffix));
215  xPropSet->setPropertyValue(gsBracketBefore, Any(sPrefix));
216  xPropSet->setPropertyValue(gsIsNumberEntries, Any(bNumberedEntries));
217  xPropSet->setPropertyValue(gsIsSortByPosition, Any(bSortByPosition));
218 
219  if( !maLanguageTagODF.isEmpty() )
220  {
221  aAny <<= maLanguageTagODF.getLanguageTag().getLocale( false);
222  xPropSet->setPropertyValue(gsLocale, aAny);
223  }
224 
225  if( !sAlgorithm.isEmpty() )
226  {
227  xPropSet->setPropertyValue(gsSortAlgorithm, Any(sAlgorithm));
228  }
229 
230  Sequence<Sequence<PropertyValue> > aKeysSeq = comphelper::containerToSequence(aSortKeys);
231  xPropSet->setPropertyValue(gsSortKeys, Any(aKeysSeq));
232  // else: can't get FieldMaster -> ignore
233  // else: can't even get Factory -> ignore
234 }
235 
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUStringLiteral gsSortKey(u"SortKey")
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
static bool convertEnum(EnumT &rEnum, const OUString &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:128
static bool convertBool(bool &rBool, const OUString &rString)
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:62
::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...
const OUStringLiteral gsIsSortByPosition(u"IsSortByPosition")
const OUStringLiteral gsSortAlgorithm(u"SortAlgorithm")
const OUStringLiteral gsLocale(u"Locale")
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
XmlStyleFamily
Definition: families.hxx:47
OUString sSuffix
const OUStringLiteral gsBracketBefore(u"BracketBefore")
static OUString getPrefixAndNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:2025
OUString sPrefix
const OUStringLiteral gsFieldMaster_Bibliography(u"com.sun.star.text.FieldMaster.Bibliography")
constexpr sal_uInt16 XML_NAMESPACE_FO
bool isEmpty() const
constexpr sal_uInt16 XML_NAMESPACE_TEXT
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString &rLocalName, const OUString &rValue) override
OUString maRfcLanguageTag
const OUStringLiteral gsSortKeys(u"SortKeys")
const SvXMLEnumMapEntry< sal_uInt16 > aBibliographyDataFieldMap[]
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
const OUStringLiteral gsIsSortAscending(u"IsSortAscending")
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
#define SAL_WARN(area, stream)
Reference< XSingleServiceFactory > xFactory
const OUStringLiteral gsBracketAfter(u"BracketAfter")
const OUStringLiteral gsIsNumberEntries(u"IsNumberEntries")
TEXT
constexpr sal_uInt16 XML_NAMESPACE_STYLE