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