1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 
27 
28 #include "XMLIndexBibliographyConfigurationContext.hxx"
29 #include "XMLIndexBibliographyEntryContext.hxx"
30 #include <xmloff/xmlictxt.hxx>
31 #include <xmloff/xmlimp.hxx>
32 #include <xmloff/txtimp.hxx>
33 #include <xmloff/nmspmap.hxx>
34 #include "xmloff/xmlnmspe.hxx"
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmluconv.hxx>
37 #include <tools/debug.hxx>
38 #include <rtl/ustring.hxx>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 
42 using namespace ::com::sun::star::text;
43 using namespace ::com::sun::star::uno;
44 using namespace ::xmloff::token;
45 
46 using ::rtl::OUString;
47 using ::com::sun::star::xml::sax::XAttributeList;
48 using ::com::sun::star::beans::PropertyValue;
49 using ::com::sun::star::beans::XPropertySet;
50 using ::com::sun::star::lang::XMultiServiceFactory;
51 
52 const sal_Char sAPI_FieldMaster_Bibliography[] =
53 								"com.sun.star.text.FieldMaster.Bibliography";
54 
55 
56 TYPEINIT1( XMLIndexBibliographyConfigurationContext, SvXMLStyleContext );
57 
XMLIndexBibliographyConfigurationContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList)58 XMLIndexBibliographyConfigurationContext::XMLIndexBibliographyConfigurationContext(
59 	SvXMLImport& rImport,
60 	sal_uInt16 nPrfx,
61 	const OUString& rLocalName,
62 	const Reference<XAttributeList> & xAttrList) :
63 		SvXMLStyleContext(rImport, nPrfx, rLocalName, xAttrList, XML_STYLE_FAMILY_TEXT_BIBLIOGRAPHYCONFIG),
64 		sFieldMaster_Bibliography(
65 			RTL_CONSTASCII_USTRINGPARAM(sAPI_FieldMaster_Bibliography)),
66 		sBracketBefore(RTL_CONSTASCII_USTRINGPARAM("BracketBefore")),
67 		sBracketAfter(RTL_CONSTASCII_USTRINGPARAM("BracketAfter")),
68 		sIsNumberEntries(RTL_CONSTASCII_USTRINGPARAM("IsNumberEntries")),
69 		sIsSortByPosition(RTL_CONSTASCII_USTRINGPARAM("IsSortByPosition")),
70 		sSortKeys(RTL_CONSTASCII_USTRINGPARAM("SortKeys")),
71 		sSortKey(RTL_CONSTASCII_USTRINGPARAM("SortKey")),
72 		sIsSortAscending(RTL_CONSTASCII_USTRINGPARAM("IsSortAscending")),
73         sSortAlgorithm(RTL_CONSTASCII_USTRINGPARAM("SortAlgorithm")),
74         sLocale(RTL_CONSTASCII_USTRINGPARAM("Locale")),
75 		sSuffix(),
76 		sPrefix(),
77         sAlgorithm(),
78         aLocale(),
79 		bNumberedEntries(sal_False),
80 		bSortByPosition(sal_True)
81 {
82 }
83 
~XMLIndexBibliographyConfigurationContext()84 XMLIndexBibliographyConfigurationContext::~XMLIndexBibliographyConfigurationContext()
85 {
86 }
87 
StartElement(const Reference<XAttributeList> & xAttrList)88 void XMLIndexBibliographyConfigurationContext::StartElement(
89 	const Reference<XAttributeList> & xAttrList)
90 {
91 	sal_Int16 nLength = xAttrList->getLength();
92 	for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
93 	{
94 		OUString sLocalName;
95 		sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
96 			GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
97 							  &sLocalName );
98 
99         ProcessAttribute(nPrefix, sLocalName,
100                          xAttrList->getValueByIndex(nAttr));
101 		// else: ignore
102 	}
103 }
104 
ProcessAttribute(sal_uInt16 nPrefix,OUString sLocalName,OUString sValue)105 void XMLIndexBibliographyConfigurationContext::ProcessAttribute(
106     sal_uInt16 nPrefix,
107 	OUString sLocalName,
108 	OUString sValue)
109 {
110     if( XML_NAMESPACE_TEXT == nPrefix )
111     {
112         if( IsXMLToken(sLocalName, XML_PREFIX) )
113         {
114             sPrefix = sValue;
115         }
116         else if( IsXMLToken(sLocalName, XML_SUFFIX) )
117         {
118             sSuffix = sValue;
119         }
120         else if( IsXMLToken(sLocalName, XML_NUMBERED_ENTRIES) )
121         {
122             sal_Bool bTmp;
123             if( SvXMLUnitConverter::convertBool(bTmp, sValue) )
124             {
125                 bNumberedEntries = bTmp;
126             }
127         }
128         else if( IsXMLToken(sLocalName, XML_SORT_BY_POSITION) )
129         {
130             sal_Bool bTmp;
131             if (SvXMLUnitConverter::convertBool(bTmp, sValue))
132             {
133                 bSortByPosition = bTmp;
134             }
135         }
136         else if( IsXMLToken(sLocalName, XML_SORT_ALGORITHM) )
137         {
138             sAlgorithm = sValue;
139         }
140 	}
141     else if( XML_NAMESPACE_FO == nPrefix )
142     {
143         if( IsXMLToken(sLocalName, XML_LANGUAGE) )
144         {
145             aLocale.Language = sValue;
146         }
147         else if( IsXMLToken(sLocalName, XML_COUNTRY) )
148         {
149             aLocale.Country = sValue;
150         }
151     }
152 }
153 
154 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList)155 SvXMLImportContext *XMLIndexBibliographyConfigurationContext::CreateChildContext(
156 	sal_uInt16 nPrefix,
157 	const OUString& rLocalName,
158 	const Reference<XAttributeList> & xAttrList )
159 {
160 	OUString sKey;
161 	sal_Bool bSort(sal_True);
162 
163 	// process children here and use default context!
164 	if ( ( nPrefix == XML_NAMESPACE_TEXT ) &&
165          IsXMLToken( rLocalName, XML_SORT_KEY ) )
166 	{
167 		sal_Int16 nLength = xAttrList->getLength();
168 		for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
169 		{
170 			OUString sLocalName;
171 			sal_uInt16 nPrfx = GetImport().GetNamespaceMap().
172 				GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
173 								  &sLocalName );
174 
175 			if (nPrfx == XML_NAMESPACE_TEXT)
176 			{
177 				if ( IsXMLToken( sLocalName, XML_KEY ) )
178 				{
179 					sKey = xAttrList->getValueByIndex(nAttr);
180 				}
181 				else if ( IsXMLToken( sLocalName, XML_SORT_ASCENDING ) )
182 				{
183 					sal_Bool bTmp;
184 					if (SvXMLUnitConverter::convertBool(
185 						bTmp, xAttrList->getValueByIndex(nAttr)))
186 					{
187 						bSort = bTmp;
188 					}
189 				}
190 			}
191 		}
192 
193 		// valid data?
194 		sal_uInt16 nKey;
195 		if (SvXMLUnitConverter::convertEnum(nKey, sKey,
196 											aBibliographyDataFieldMap))
197 		{
198 
199 			Any aAny;
200 			Sequence<PropertyValue> aKey(2);
201 
202 			PropertyValue aNameValue;
203 			aNameValue.Name = sSortKey;
204 			aAny <<= (sal_Int16)nKey;
205 			aNameValue.Value = aAny;
206 			aKey[0] = aNameValue;
207 
208 			PropertyValue aSortValue;
209 			aSortValue.Name = sIsSortAscending;
210 			aAny.setValue(&bSort, ::getBooleanCppuType());
211 			aSortValue.Value = aAny;
212 			aKey[1] = aSortValue;
213 
214 			aSortKeys.push_back(aKey);
215 		}
216 	}
217 
218 	return SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
219 												  xAttrList);
220 }
221 
CreateAndInsert(sal_Bool)222 void XMLIndexBibliographyConfigurationContext::CreateAndInsert(sal_Bool)
223 {
224 	// (code almost the same as export...)
225 
226 	// insert and block mode is handled in insertStyleFamily
227 
228 	// first: get field master
229 	// (we'll create one, and get the only master for this type)
230 	Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
231 	if( xFactory.is() )
232 	{
233 		Sequence<rtl::OUString> aServices = xFactory->getAvailableServiceNames();
234 		sal_Bool bFound(sal_False);
235 		sal_Int32 i(0);
236 		sal_Int32 nServiceCount(aServices.getLength());
237 		while (i < nServiceCount && !bFound)
238 		{
239 			if (aServices[i].equals(sFieldMaster_Bibliography))
240 			// here we should use a methode which compares in reverse order if available
241 			// #85282#
242 				bFound = sal_True;
243 			else
244 				i++;
245 		}
246 		if (bFound)
247 		{
248 			Reference<XInterface> xIfc =
249 				xFactory->createInstance(sFieldMaster_Bibliography);
250 			if( xIfc.is() )
251 			{
252 				Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
253 				Any aAny;
254 
255                 aAny <<= sSuffix;
256                 xPropSet->setPropertyValue(sBracketAfter, aAny);
257 
258                 aAny <<= sPrefix;
259                 xPropSet->setPropertyValue(sBracketBefore, aAny);
260 
261 				aAny.setValue(&bNumberedEntries, ::getBooleanCppuType());
262 				xPropSet->setPropertyValue(sIsNumberEntries, aAny);
263 
264 				aAny.setValue(&bSortByPosition, ::getBooleanCppuType());
265 				xPropSet->setPropertyValue(sIsSortByPosition, aAny);
266 
267                 if( (aLocale.Language.getLength() > 0) &&
268                     (aLocale.Country.getLength() > 0)     )
269                 {
270                     aAny <<= aLocale;
271                     xPropSet->setPropertyValue(sLocale, aAny);
272                 }
273 
274                 if( sAlgorithm.getLength() > 0 )
275                 {
276                     aAny <<= sAlgorithm;
277                     xPropSet->setPropertyValue(sSortAlgorithm, aAny);
278                 }
279 
280 				sal_Int32 nCount = aSortKeys.size();
281 				Sequence<Sequence<PropertyValue> > aKeysSeq(nCount);
282 				for(i = 0; i < nCount; i++)
283 				{
284 					aKeysSeq[i] = aSortKeys[i];
285 				}
286 				aAny <<= aKeysSeq;
287 				xPropSet->setPropertyValue(sSortKeys, aAny);
288 			}
289 			// else: can't get FieldMaster -> ignore
290 		}
291 	}
292 	// else: can't even get Factory -> ignore
293 }
294