1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/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>
37 using namespace ::com::sun::star::text
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::xmloff::token
;
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
;
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");
59 XMLIndexBibliographyConfigurationContext::XMLIndexBibliographyConfigurationContext(
60 SvXMLImport
& rImport
) :
61 SvXMLStyleContext(rImport
, XmlStyleFamily::TEXT_BIBLIOGRAPHYCONFIG
),
66 bNumberedEntries(false),
71 XMLIndexBibliographyConfigurationContext::~XMLIndexBibliographyConfigurationContext()
75 void XMLIndexBibliographyConfigurationContext::SetAttribute(
77 const OUString
& sLocalName
,
78 const OUString
& sValue
)
80 if( XML_NAMESPACE_TEXT
== nPrefix
)
82 if( IsXMLToken(sLocalName
, XML_PREFIX
) )
86 else if( IsXMLToken(sLocalName
, XML_SUFFIX
) )
90 else if( IsXMLToken(sLocalName
, XML_NUMBERED_ENTRIES
) )
93 if (::sax::Converter::convertBool(bTmp
, sValue
))
95 bNumberedEntries
= bTmp
;
98 else if( IsXMLToken(sLocalName
, XML_SORT_BY_POSITION
) )
101 if (::sax::Converter::convertBool(bTmp
, sValue
))
103 bSortByPosition
= bTmp
;
106 else if( IsXMLToken(sLocalName
, XML_SORT_ALGORITHM
) )
111 else if( XML_NAMESPACE_FO
== nPrefix
)
113 if( IsXMLToken(sLocalName
, XML_LANGUAGE
) )
115 maLanguageTagODF
.maLanguage
= sValue
;
117 else if( IsXMLToken(sLocalName
, XML_SCRIPT
) )
119 maLanguageTagODF
.maScript
= sValue
;
121 else if( IsXMLToken(sLocalName
, XML_COUNTRY
) )
123 maLanguageTagODF
.maCountry
= sValue
;
126 else if( XML_NAMESPACE_STYLE
== nPrefix
)
128 if( IsXMLToken(sLocalName
, XML_RFC_LANGUAGE_TAG
) )
130 maLanguageTagODF
.maRfcLanguageTag
= sValue
;
135 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLIndexBibliographyConfigurationContext::createFastChildContext(
137 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
139 // process children here and use default context!
140 if ( nElement
== XML_ELEMENT(TEXT
, XML_SORT_KEY
) )
145 for (auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
))
147 switch (aIter
.getToken())
149 case XML_ELEMENT(TEXT
, XML_KEY
):
150 sKey
= aIter
.toString();
152 case XML_ELEMENT(TEXT
, XML_SORT_ASCENDING
):
155 if (::sax::Converter::convertBool(bTmp
, aIter
.toString()))
160 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
167 if (SvXMLUnitConverter::convertEnum(nKey
, sKey
,
168 aBibliographyDataFieldMap
))
170 Sequence
<PropertyValue
> aKey(2);
172 PropertyValue aNameValue
;
173 aNameValue
.Name
= gsSortKey
;
174 aNameValue
.Value
<<= static_cast<sal_Int16
>(nKey
);
175 aKey
[0] = aNameValue
;
177 PropertyValue aSortValue
;
178 aSortValue
.Name
= gsIsSortAscending
;
179 aSortValue
.Value
<<= bSort
;
180 aKey
[1] = aSortValue
;
182 aSortKeys
.push_back(aKey
);
189 void XMLIndexBibliographyConfigurationContext::CreateAndInsert(bool)
191 // (code almost the same as export...)
193 // insert and block mode is handled in insertStyleFamily
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
);
201 Sequence
<OUString
> aServices
= xFactory
->getAvailableServiceNames();
202 // here we should use a method which compares in reverse order if available
203 if (comphelper::findValue(aServices
, gsFieldMaster_Bibliography
) == -1)
206 Reference
<XInterface
> xIfc
=
207 xFactory
->createInstance(gsFieldMaster_Bibliography
);
211 Reference
<XPropertySet
> xPropSet( xIfc
, UNO_QUERY
);
214 xPropSet
->setPropertyValue(gsBracketAfter
, Any(sSuffix
));
215 xPropSet
->setPropertyValue(gsBracketBefore
, Any(sPrefix
));
216 xPropSet
->setPropertyValue(gsIsNumberEntries
, Any(bNumberedEntries
));
217 xPropSet
->setPropertyValue(gsIsSortByPosition
, Any(bSortByPosition
));
219 if( !maLanguageTagODF
.isEmpty() )
221 aAny
<<= maLanguageTagODF
.getLanguageTag().getLocale( false);
222 xPropSet
->setPropertyValue(gsLocale
, aAny
);
225 if( !sAlgorithm
.isEmpty() )
227 xPropSet
->setPropertyValue(gsSortAlgorithm
, Any(sAlgorithm
));
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
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */