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/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/frame/XModel.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <comphelper/sequence.hxx>
35 
36 using namespace ::com::sun::star::text;
37 using namespace ::com::sun::star::uno;
38 using namespace ::xmloff::token;
39 
40 using ::com::sun::star::xml::sax::XAttributeList;
41 using ::com::sun::star::beans::PropertyValue;
42 using ::com::sun::star::beans::XPropertySet;
43 using ::com::sun::star::lang::XMultiServiceFactory;
44 
45 
46 static const OUStringLiteral gsFieldMaster_Bibliography("com.sun.star.text.FieldMaster.Bibliography");
47 static const OUStringLiteral gsBracketBefore("BracketBefore");
48 static const OUStringLiteral gsBracketAfter("BracketAfter");
49 static const OUStringLiteral gsIsNumberEntries("IsNumberEntries");
50 static const OUStringLiteral gsIsSortByPosition("IsSortByPosition");
51 static const OUStringLiteral gsSortKeys("SortKeys");
52 static const OUStringLiteral gsSortKey("SortKey");
53 static const OUStringLiteral gsIsSortAscending("IsSortAscending");
54 static const OUStringLiteral gsSortAlgorithm("SortAlgorithm");
55 static const OUStringLiteral gsLocale("Locale");
56 
58  SvXMLImport& rImport,
59  sal_uInt16 nPrfx,
60  const OUString& rLocalName,
61  const Reference<XAttributeList> & xAttrList) :
62  SvXMLStyleContext(rImport, nPrfx, rLocalName, xAttrList, XmlStyleFamily::TEXT_BIBLIOGRAPHYCONFIG),
63  sSuffix(),
64  sPrefix(),
65  sAlgorithm(),
66  maLanguageTagODF(),
67  bNumberedEntries(false),
68  bSortByPosition(true)
69 {
70 }
71 
73 {
74 }
75 
77  sal_uInt16 nPrefix,
78  const OUString& sLocalName,
79  const OUString& sValue)
80 {
81  if( XML_NAMESPACE_TEXT == nPrefix )
82  {
83  if( IsXMLToken(sLocalName, XML_PREFIX) )
84  {
85  sPrefix = sValue;
86  }
87  else if( IsXMLToken(sLocalName, XML_SUFFIX) )
88  {
89  sSuffix = sValue;
90  }
91  else if( IsXMLToken(sLocalName, XML_NUMBERED_ENTRIES) )
92  {
93  bool bTmp(false);
94  if (::sax::Converter::convertBool(bTmp, sValue))
95  {
96  bNumberedEntries = bTmp;
97  }
98  }
99  else if( IsXMLToken(sLocalName, XML_SORT_BY_POSITION) )
100  {
101  bool bTmp(false);
102  if (::sax::Converter::convertBool(bTmp, sValue))
103  {
104  bSortByPosition = bTmp;
105  }
106  }
107  else if( IsXMLToken(sLocalName, XML_SORT_ALGORITHM) )
108  {
109  sAlgorithm = sValue;
110  }
111  }
112  else if( XML_NAMESPACE_FO == nPrefix )
113  {
114  if( IsXMLToken(sLocalName, XML_LANGUAGE) )
115  {
116  maLanguageTagODF.maLanguage = sValue;
117  }
118  else if( IsXMLToken(sLocalName, XML_SCRIPT) )
119  {
120  maLanguageTagODF.maScript = sValue;
121  }
122  else if( IsXMLToken(sLocalName, XML_COUNTRY) )
123  {
124  maLanguageTagODF.maCountry = sValue;
125  }
126  }
127  else if( XML_NAMESPACE_STYLE == nPrefix )
128  {
129  if( IsXMLToken(sLocalName, XML_RFC_LANGUAGE_TAG) )
130  {
132  }
133  }
134 }
135 
137  sal_uInt16 nPrefix,
138  const OUString& rLocalName,
139  const Reference<XAttributeList> & xAttrList )
140 {
141  // process children here and use default context!
142  if ( ( nPrefix == XML_NAMESPACE_TEXT ) &&
143  IsXMLToken( rLocalName, XML_SORT_KEY ) )
144  {
145  OUString sKey;
146  bool bSort(true);
147 
148  sal_Int16 nLength = xAttrList->getLength();
149  for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
150  {
151  OUString sLocalName;
152  sal_uInt16 nPrfx = GetImport().GetNamespaceMap().
153  GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
154  &sLocalName );
155 
156  if (nPrfx == XML_NAMESPACE_TEXT)
157  {
158  if ( IsXMLToken( sLocalName, XML_KEY ) )
159  {
160  sKey = xAttrList->getValueByIndex(nAttr);
161  }
162  else if ( IsXMLToken( sLocalName, XML_SORT_ASCENDING ) )
163  {
164  bool bTmp(false);
166  bTmp, xAttrList->getValueByIndex(nAttr)))
167  {
168  bSort = bTmp;
169  }
170  }
171  }
172  }
173 
174  // valid data?
175  sal_uInt16 nKey;
176  if (SvXMLUnitConverter::convertEnum(nKey, sKey,
178  {
179  Sequence<PropertyValue> aKey(2);
180 
181  PropertyValue aNameValue;
182  aNameValue.Name = gsSortKey;
183  aNameValue.Value <<= static_cast<sal_Int16>(nKey);
184  aKey[0] = aNameValue;
185 
186  PropertyValue aSortValue;
187  aSortValue.Name = gsIsSortAscending;
188  aSortValue.Value <<= bSort;
189  aKey[1] = aSortValue;
190 
191  aSortKeys.push_back(aKey);
192  }
193  }
194 
195  return nullptr;
196 }
197 
199 {
200  // (code almost the same as export...)
201 
202  // insert and block mode is handled in insertStyleFamily
203 
204  // first: get field master
205  // (we'll create one, and get the only master for this type)
206  Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
207  if( xFactory.is() )
208  {
209  Sequence<OUString> aServices = xFactory->getAvailableServiceNames();
210  // here we should use a method which compares in reverse order if available
212  {
213  Reference<XInterface> xIfc =
214  xFactory->createInstance(gsFieldMaster_Bibliography);
215  if( xIfc.is() )
216  {
217  Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
218  Any aAny;
219 
220  xPropSet->setPropertyValue(gsBracketAfter, Any(sSuffix));
221  xPropSet->setPropertyValue(gsBracketBefore, Any(sPrefix));
222  xPropSet->setPropertyValue(gsIsNumberEntries, Any(bNumberedEntries));
223  xPropSet->setPropertyValue(gsIsSortByPosition, Any(bSortByPosition));
224 
225  if( !maLanguageTagODF.isEmpty() )
226  {
227  aAny <<= maLanguageTagODF.getLanguageTag().getLocale( false);
228  xPropSet->setPropertyValue(gsLocale, aAny);
229  }
230 
231  if( !sAlgorithm.isEmpty() )
232  {
233  xPropSet->setPropertyValue(gsSortAlgorithm, Any(sAlgorithm));
234  }
235 
236  Sequence<Sequence<PropertyValue> > aKeysSeq = comphelper::containerToSequence(aSortKeys);
237  xPropSet->setPropertyValue(gsSortKeys, Any(aKeysSeq));
238  }
239  // else: can't get FieldMaster -> ignore
240  }
241  }
242  // else: can't even get Factory -> ignore
243 }
244 
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const OUStringLiteral gsSortKey("SortKey")
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
constexpr sal_uInt16 XML_NAMESPACE_STYLE
Definition: xmlnmspe.hxx:30
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 const OUStringLiteral gsBracketAfter("BracketAfter")
static bool convertBool(bool &rBool, const OUString &rString)
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
static const OUStringLiteral gsIsNumberEntries("IsNumberEntries")
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:397
::std::vector< css::uno::Sequence< css::beans::PropertyValue > > aSortKeys
LanguageTag getLanguageTag() const
Best call this only once per instance, it recreates a LanguageTag instance on every call...
constexpr sal_uInt16 XML_NAMESPACE_TEXT
Definition: xmlnmspe.hxx:31
static const OUStringLiteral gsBracketBefore("BracketBefore")
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
XmlStyleFamily
Definition: families.hxx:47
OUString sSuffix
XMLIndexBibliographyConfigurationContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)
OUString sPrefix
static const OUStringLiteral gsLocale("Locale")
bool isEmpty() const
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString &rLocalName, const OUString &rValue) override
static const OUStringLiteral gsSortKeys("SortKeys")
OUString maRfcLanguageTag
static const OUStringLiteral gsIsSortAscending("IsSortAscending")
static const OUStringLiteral gsSortAlgorithm("SortAlgorithm")
constexpr sal_uInt16 XML_NAMESPACE_FO
Definition: xmlnmspe.hxx:34
const SvXMLEnumMapEntry< sal_uInt16 > aBibliographyDataFieldMap[]
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Handling of tokens in XML:
static const OUStringLiteral gsFieldMaster_Bibliography("com.sun.star.text.FieldMaster.Bibliography")
Reference< XSingleServiceFactory > xFactory
sal_Int32 nLength
Definition: xmltoken.cxx:36
static const OUStringLiteral gsIsSortByPosition("IsSortByPosition")