Line data Source code
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 "XMLIndexBibliographyConfigurationContext.hxx"
21 : #include "XMLIndexBibliographyEntryContext.hxx"
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/lang/XMultiServiceFactory.hpp>
33 :
34 : using namespace ::com::sun::star::text;
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::xmloff::token;
37 :
38 : using ::rtl::OUString;
39 : using ::com::sun::star::xml::sax::XAttributeList;
40 : using ::com::sun::star::beans::PropertyValue;
41 : using ::com::sun::star::beans::XPropertySet;
42 : using ::com::sun::star::lang::XMultiServiceFactory;
43 :
44 : const sal_Char sAPI_FieldMaster_Bibliography[] =
45 : "com.sun.star.text.FieldMaster.Bibliography";
46 :
47 :
48 0 : TYPEINIT1( XMLIndexBibliographyConfigurationContext, SvXMLStyleContext );
49 :
50 0 : XMLIndexBibliographyConfigurationContext::XMLIndexBibliographyConfigurationContext(
51 : SvXMLImport& rImport,
52 : sal_uInt16 nPrfx,
53 : const OUString& rLocalName,
54 : const Reference<XAttributeList> & xAttrList) :
55 : SvXMLStyleContext(rImport, nPrfx, rLocalName, xAttrList, XML_STYLE_FAMILY_TEXT_BIBLIOGRAPHYCONFIG),
56 : sFieldMaster_Bibliography(
57 : RTL_CONSTASCII_USTRINGPARAM(sAPI_FieldMaster_Bibliography)),
58 : sBracketBefore(RTL_CONSTASCII_USTRINGPARAM("BracketBefore")),
59 : sBracketAfter(RTL_CONSTASCII_USTRINGPARAM("BracketAfter")),
60 : sIsNumberEntries(RTL_CONSTASCII_USTRINGPARAM("IsNumberEntries")),
61 : sIsSortByPosition(RTL_CONSTASCII_USTRINGPARAM("IsSortByPosition")),
62 : sSortKeys(RTL_CONSTASCII_USTRINGPARAM("SortKeys")),
63 : sSortKey(RTL_CONSTASCII_USTRINGPARAM("SortKey")),
64 : sIsSortAscending(RTL_CONSTASCII_USTRINGPARAM("IsSortAscending")),
65 : sSortAlgorithm(RTL_CONSTASCII_USTRINGPARAM("SortAlgorithm")),
66 : sLocale(RTL_CONSTASCII_USTRINGPARAM("Locale")),
67 : sSuffix(),
68 : sPrefix(),
69 : sAlgorithm(),
70 : aLocale(),
71 : bNumberedEntries(sal_False),
72 0 : bSortByPosition(sal_True)
73 : {
74 0 : }
75 :
76 0 : XMLIndexBibliographyConfigurationContext::~XMLIndexBibliographyConfigurationContext()
77 : {
78 0 : }
79 :
80 0 : void XMLIndexBibliographyConfigurationContext::StartElement(
81 : const Reference<XAttributeList> & xAttrList)
82 : {
83 0 : sal_Int16 nLength = xAttrList->getLength();
84 0 : for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
85 : {
86 0 : OUString sLocalName;
87 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
88 0 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
89 0 : &sLocalName );
90 :
91 : ProcessAttribute(nPrefix, sLocalName,
92 0 : xAttrList->getValueByIndex(nAttr));
93 : // else: ignore
94 0 : }
95 0 : }
96 :
97 0 : void XMLIndexBibliographyConfigurationContext::ProcessAttribute(
98 : sal_uInt16 nPrefix,
99 : OUString sLocalName,
100 : OUString sValue)
101 : {
102 0 : if( XML_NAMESPACE_TEXT == nPrefix )
103 : {
104 0 : if( IsXMLToken(sLocalName, XML_PREFIX) )
105 : {
106 0 : sPrefix = sValue;
107 : }
108 0 : else if( IsXMLToken(sLocalName, XML_SUFFIX) )
109 : {
110 0 : sSuffix = sValue;
111 : }
112 0 : else if( IsXMLToken(sLocalName, XML_NUMBERED_ENTRIES) )
113 : {
114 0 : bool bTmp(false);
115 0 : if (::sax::Converter::convertBool(bTmp, sValue))
116 : {
117 0 : bNumberedEntries = bTmp;
118 : }
119 : }
120 0 : else if( IsXMLToken(sLocalName, XML_SORT_BY_POSITION) )
121 : {
122 0 : bool bTmp(false);
123 0 : if (::sax::Converter::convertBool(bTmp, sValue))
124 : {
125 0 : bSortByPosition = bTmp;
126 : }
127 : }
128 0 : else if( IsXMLToken(sLocalName, XML_SORT_ALGORITHM) )
129 : {
130 0 : sAlgorithm = sValue;
131 : }
132 : }
133 0 : else if( XML_NAMESPACE_FO == nPrefix )
134 : {
135 0 : if( IsXMLToken(sLocalName, XML_LANGUAGE) )
136 : {
137 0 : aLocale.Language = sValue;
138 : }
139 0 : else if( IsXMLToken(sLocalName, XML_COUNTRY) )
140 : {
141 0 : aLocale.Country = sValue;
142 : }
143 : }
144 0 : }
145 :
146 :
147 0 : SvXMLImportContext *XMLIndexBibliographyConfigurationContext::CreateChildContext(
148 : sal_uInt16 nPrefix,
149 : const OUString& rLocalName,
150 : const Reference<XAttributeList> & xAttrList )
151 : {
152 0 : OUString sKey;
153 0 : sal_Bool bSort(sal_True);
154 :
155 : // process children here and use default context!
156 0 : if ( ( nPrefix == XML_NAMESPACE_TEXT ) &&
157 0 : IsXMLToken( rLocalName, XML_SORT_KEY ) )
158 : {
159 0 : sal_Int16 nLength = xAttrList->getLength();
160 0 : for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
161 : {
162 0 : OUString sLocalName;
163 0 : sal_uInt16 nPrfx = GetImport().GetNamespaceMap().
164 0 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
165 0 : &sLocalName );
166 :
167 0 : if (nPrfx == XML_NAMESPACE_TEXT)
168 : {
169 0 : if ( IsXMLToken( sLocalName, XML_KEY ) )
170 : {
171 0 : sKey = xAttrList->getValueByIndex(nAttr);
172 : }
173 0 : else if ( IsXMLToken( sLocalName, XML_SORT_ASCENDING ) )
174 : {
175 0 : bool bTmp(false);
176 0 : if (::sax::Converter::convertBool(
177 0 : bTmp, xAttrList->getValueByIndex(nAttr)))
178 : {
179 0 : bSort = bTmp;
180 : }
181 : }
182 : }
183 0 : }
184 :
185 : // valid data?
186 : sal_uInt16 nKey;
187 0 : if (SvXMLUnitConverter::convertEnum(nKey, sKey,
188 0 : aBibliographyDataFieldMap))
189 : {
190 :
191 0 : Any aAny;
192 0 : Sequence<PropertyValue> aKey(2);
193 :
194 0 : PropertyValue aNameValue;
195 0 : aNameValue.Name = sSortKey;
196 0 : aAny <<= (sal_Int16)nKey;
197 0 : aNameValue.Value = aAny;
198 0 : aKey[0] = aNameValue;
199 :
200 0 : PropertyValue aSortValue;
201 0 : aSortValue.Name = sIsSortAscending;
202 0 : aAny.setValue(&bSort, ::getBooleanCppuType());
203 0 : aSortValue.Value = aAny;
204 0 : aKey[1] = aSortValue;
205 :
206 0 : aSortKeys.push_back(aKey);
207 : }
208 : }
209 :
210 : return SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
211 0 : xAttrList);
212 : }
213 :
214 0 : void XMLIndexBibliographyConfigurationContext::CreateAndInsert(sal_Bool)
215 : {
216 : // (code almost the same as export...)
217 :
218 : // insert and block mode is handled in insertStyleFamily
219 :
220 : // first: get field master
221 : // (we'll create one, and get the only master for this type)
222 0 : Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
223 0 : if( xFactory.is() )
224 : {
225 0 : Sequence<rtl::OUString> aServices = xFactory->getAvailableServiceNames();
226 0 : sal_Bool bFound(sal_False);
227 0 : sal_Int32 i(0);
228 0 : sal_Int32 nServiceCount(aServices.getLength());
229 0 : while (i < nServiceCount && !bFound)
230 : {
231 0 : if (aServices[i].equals(sFieldMaster_Bibliography))
232 : // here we should use a method which compares in reverse order if available
233 : // #85282#
234 0 : bFound = sal_True;
235 : else
236 0 : i++;
237 : }
238 0 : if (bFound)
239 : {
240 : Reference<XInterface> xIfc =
241 0 : xFactory->createInstance(sFieldMaster_Bibliography);
242 0 : if( xIfc.is() )
243 : {
244 0 : Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
245 0 : Any aAny;
246 :
247 0 : aAny <<= sSuffix;
248 0 : xPropSet->setPropertyValue(sBracketAfter, aAny);
249 :
250 0 : aAny <<= sPrefix;
251 0 : xPropSet->setPropertyValue(sBracketBefore, aAny);
252 :
253 0 : aAny.setValue(&bNumberedEntries, ::getBooleanCppuType());
254 0 : xPropSet->setPropertyValue(sIsNumberEntries, aAny);
255 :
256 0 : aAny.setValue(&bSortByPosition, ::getBooleanCppuType());
257 0 : xPropSet->setPropertyValue(sIsSortByPosition, aAny);
258 :
259 0 : if( !aLocale.Language.isEmpty() && !aLocale.Country.isEmpty() )
260 : {
261 0 : aAny <<= aLocale;
262 0 : xPropSet->setPropertyValue(sLocale, aAny);
263 : }
264 :
265 0 : if( !sAlgorithm.isEmpty() )
266 : {
267 0 : aAny <<= sAlgorithm;
268 0 : xPropSet->setPropertyValue(sSortAlgorithm, aAny);
269 : }
270 :
271 0 : sal_Int32 nCount = aSortKeys.size();
272 0 : Sequence<Sequence<PropertyValue> > aKeysSeq(nCount);
273 0 : for(i = 0; i < nCount; i++)
274 : {
275 0 : aKeysSeq[i] = aSortKeys[i];
276 : }
277 0 : aAny <<= aKeysSeq;
278 0 : xPropSet->setPropertyValue(sSortKeys, aAny);
279 0 : }
280 : // else: can't get FieldMaster -> ignore
281 0 : }
282 0 : }
283 : // else: can't even get Factory -> ignore
284 0 : }
285 :
286 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|