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