/src/libreoffice/xmloff/source/text/XMLIndexBibliographyConfigurationContext.cxx
Line | Count | Source |
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 | | |
20 | | #include <comphelper/propertyvalue.hxx> |
21 | | #include <XMLIndexBibliographyConfigurationContext.hxx> |
22 | | #include "XMLIndexBibliographyEntryContext.hxx" |
23 | | #include <xmloff/xmlictxt.hxx> |
24 | | #include <xmloff/xmlimp.hxx> |
25 | | #include <xmloff/xmlnamespace.hxx> |
26 | | #include <xmloff/xmltoken.hxx> |
27 | | #include <xmloff/xmluconv.hxx> |
28 | | #include <sal/log.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::uno; |
37 | | using namespace ::xmloff::token; |
38 | | |
39 | | using ::com::sun::star::xml::sax::XFastAttributeList; |
40 | | using ::com::sun::star::beans::PropertyValue; |
41 | | using ::com::sun::star::beans::XPropertySet; |
42 | | using ::com::sun::star::lang::XMultiServiceFactory; |
43 | | |
44 | | |
45 | | constexpr OUString gsFieldMaster_Bibliography(u"com.sun.star.text.FieldMaster.Bibliography"_ustr); |
46 | | constexpr OUStringLiteral gsBracketBefore(u"BracketBefore"); |
47 | | constexpr OUStringLiteral gsBracketAfter(u"BracketAfter"); |
48 | | constexpr OUStringLiteral gsIsNumberEntries(u"IsNumberEntries"); |
49 | | constexpr OUStringLiteral gsIsSortByPosition(u"IsSortByPosition"); |
50 | | constexpr OUStringLiteral gsSortKeys(u"SortKeys"); |
51 | | constexpr OUString gsSortKey(u"SortKey"_ustr); |
52 | | constexpr OUString gsIsSortAscending(u"IsSortAscending"_ustr); |
53 | | constexpr OUStringLiteral gsSortAlgorithm(u"SortAlgorithm"); |
54 | | constexpr OUStringLiteral gsLocale(u"Locale"); |
55 | | |
56 | | XMLIndexBibliographyConfigurationContext::XMLIndexBibliographyConfigurationContext( |
57 | | SvXMLImport& rImport) : |
58 | 200 | SvXMLStyleContext(rImport, XmlStyleFamily::TEXT_BIBLIOGRAPHYCONFIG), |
59 | 200 | maLanguageTagODF(), |
60 | 200 | bNumberedEntries(false), |
61 | 200 | bSortByPosition(true) |
62 | 200 | { |
63 | 200 | } |
64 | | |
65 | | XMLIndexBibliographyConfigurationContext::~XMLIndexBibliographyConfigurationContext() |
66 | 200 | { |
67 | 200 | } |
68 | | |
69 | | void XMLIndexBibliographyConfigurationContext::SetAttribute( |
70 | | sal_Int32 nElement, |
71 | | const OUString& sValue) |
72 | 928 | { |
73 | 928 | switch (nElement) |
74 | 928 | { |
75 | 184 | case XML_ELEMENT(TEXT, XML_PREFIX): |
76 | 184 | sPrefix = sValue; |
77 | 184 | break; |
78 | 191 | case XML_ELEMENT(TEXT, XML_SUFFIX): |
79 | 191 | sSuffix = sValue; |
80 | 191 | break; |
81 | 0 | case XML_ELEMENT(TEXT, XML_NUMBERED_ENTRIES): |
82 | 0 | { |
83 | 0 | bool bTmp(false); |
84 | 0 | if (::sax::Converter::convertBool(bTmp, sValue)) |
85 | 0 | { |
86 | 0 | bNumberedEntries = bTmp; |
87 | 0 | } |
88 | 0 | break; |
89 | 0 | } |
90 | 0 | case XML_ELEMENT(TEXT, XML_SORT_BY_POSITION): |
91 | 0 | { |
92 | 0 | bool bTmp(false); |
93 | 0 | if (::sax::Converter::convertBool(bTmp, sValue)) |
94 | 0 | { |
95 | 0 | bSortByPosition = bTmp; |
96 | 0 | } |
97 | 0 | break; |
98 | 0 | } |
99 | 194 | case XML_ELEMENT(TEXT, XML_SORT_ALGORITHM): |
100 | 194 | sAlgorithm = sValue; |
101 | 194 | break; |
102 | 0 | case XML_ELEMENT(FO, XML_LANGUAGE): |
103 | 0 | maLanguageTagODF.maLanguage = sValue; |
104 | 0 | break; |
105 | 0 | case XML_ELEMENT(FO, XML_SCRIPT): |
106 | 0 | maLanguageTagODF.maScript = sValue; |
107 | 0 | break; |
108 | 0 | case XML_ELEMENT(FO, XML_COUNTRY): |
109 | 0 | maLanguageTagODF.maCountry = sValue; |
110 | 0 | break; |
111 | 0 | case XML_ELEMENT(STYLE, XML_RFC_LANGUAGE_TAG): |
112 | 0 | maLanguageTagODF.maRfcLanguageTag = sValue; |
113 | 0 | break; |
114 | 928 | } |
115 | 928 | } |
116 | | |
117 | | css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexBibliographyConfigurationContext::createFastChildContext( |
118 | | sal_Int32 nElement, |
119 | | const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) |
120 | 9 | { |
121 | | // process children here and use default context! |
122 | 9 | if ( nElement == XML_ELEMENT(TEXT, XML_SORT_KEY) ) |
123 | 0 | { |
124 | 0 | std::string_view sKey; |
125 | 0 | bool bSort(true); |
126 | |
|
127 | 0 | for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList )) |
128 | 0 | { |
129 | 0 | switch (aIter.getToken()) |
130 | 0 | { |
131 | 0 | case XML_ELEMENT(TEXT, XML_KEY): |
132 | 0 | sKey = aIter.toView(); |
133 | 0 | break; |
134 | 0 | case XML_ELEMENT(TEXT, XML_SORT_ASCENDING): |
135 | 0 | { |
136 | 0 | bool bTmp(false); |
137 | 0 | if (::sax::Converter::convertBool(bTmp, aIter.toView())) |
138 | 0 | bSort = bTmp; |
139 | 0 | break; |
140 | 0 | } |
141 | 0 | default: |
142 | 0 | XMLOFF_WARN_UNKNOWN("xmloff", aIter); |
143 | 0 | break; |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | | // valid data? |
148 | 0 | sal_uInt16 nKey; |
149 | 0 | if (SvXMLUnitConverter::convertEnum(nKey, sKey, |
150 | 0 | aBibliographyDataFieldMap)) |
151 | 0 | { |
152 | 0 | Sequence<PropertyValue> aKey |
153 | 0 | { |
154 | 0 | comphelper::makePropertyValue(gsSortKey, static_cast<sal_Int16>(nKey)), |
155 | 0 | comphelper::makePropertyValue(gsIsSortAscending, bSort) |
156 | 0 | }; |
157 | |
|
158 | 0 | aSortKeys.push_back(aKey); |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | 9 | return nullptr; |
163 | 9 | } |
164 | | |
165 | | void XMLIndexBibliographyConfigurationContext::CreateAndInsert(bool) |
166 | 188 | { |
167 | | // (code almost the same as export...) |
168 | | |
169 | | // insert and block mode is handled in insertStyleFamily |
170 | | |
171 | | // first: get field master |
172 | | // (we'll create one, and get the only master for this type) |
173 | 188 | Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY); |
174 | 188 | if( !xFactory.is() ) |
175 | 0 | return; |
176 | | |
177 | 188 | Sequence<OUString> aServices = xFactory->getAvailableServiceNames(); |
178 | | // here we should use a method which compares in reverse order if available |
179 | 188 | if (comphelper::findValue(aServices, gsFieldMaster_Bibliography) == -1) |
180 | 7 | return; |
181 | | |
182 | 181 | Reference<XInterface> xIfc = |
183 | 181 | xFactory->createInstance(gsFieldMaster_Bibliography); |
184 | 181 | if( !xIfc.is() ) |
185 | 0 | return; |
186 | | |
187 | 181 | Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY ); |
188 | 181 | Any aAny; |
189 | | |
190 | 181 | xPropSet->setPropertyValue(gsBracketAfter, Any(sSuffix)); |
191 | 181 | xPropSet->setPropertyValue(gsBracketBefore, Any(sPrefix)); |
192 | 181 | xPropSet->setPropertyValue(gsIsNumberEntries, Any(bNumberedEntries)); |
193 | 181 | xPropSet->setPropertyValue(gsIsSortByPosition, Any(bSortByPosition)); |
194 | | |
195 | 181 | if( !maLanguageTagODF.isEmpty() ) |
196 | 0 | { |
197 | 0 | aAny <<= maLanguageTagODF.getLanguageTag().getLocale( false); |
198 | 0 | xPropSet->setPropertyValue(gsLocale, aAny); |
199 | 0 | } |
200 | | |
201 | 181 | if( !sAlgorithm.isEmpty() ) |
202 | 176 | { |
203 | 176 | xPropSet->setPropertyValue(gsSortAlgorithm, Any(sAlgorithm)); |
204 | 176 | } |
205 | | |
206 | 181 | Sequence<Sequence<PropertyValue> > aKeysSeq = comphelper::containerToSequence(aSortKeys); |
207 | 181 | xPropSet->setPropertyValue(gsSortKeys, Any(aKeysSeq)); |
208 | | // else: can't get FieldMaster -> ignore |
209 | | // else: can't even get Factory -> ignore |
210 | 181 | } |
211 | | |
212 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |