xref: /AOO41X/main/xmloff/source/script/XMLEventsImportContext.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 #include <xmloff/XMLEventsImportContext.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #ifndef _XMLOFF_XMLEVENTIMPORTHELPER_HXX
33*cdf0e10cSrcweir #include "XMLEventImportHelper.hxx"
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_DOCUMENT_XEVENTSSUPPLIER_HPP
37*cdf0e10cSrcweir #include <com/sun/star/document/XEventsSupplier.hpp>
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir #include <tools/debug.hxx>
40*cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
41*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
42*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
43*cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
44*cdf0e10cSrcweir #include "xmloff/xmlerror.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
47*cdf0e10cSrcweir using namespace ::xmloff::token;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir using ::rtl::OUString;
50*cdf0e10cSrcweir using ::com::sun::star::xml::sax::XAttributeList;
51*cdf0e10cSrcweir using ::com::sun::star::beans::PropertyValue;
52*cdf0e10cSrcweir using ::com::sun::star::container::XNameReplace;
53*cdf0e10cSrcweir using ::com::sun::star::document::XEventsSupplier;
54*cdf0e10cSrcweir using ::com::sun::star::lang::IllegalArgumentException;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir TYPEINIT1(XMLEventsImportContext,  SvXMLImportContext);
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir XMLEventsImportContext::XMLEventsImportContext(
60*cdf0e10cSrcweir 	SvXMLImport& rImport,
61*cdf0e10cSrcweir 	sal_uInt16 nPrfx,
62*cdf0e10cSrcweir 	const OUString& rLocalName) :
63*cdf0e10cSrcweir 		SvXMLImportContext(rImport, nPrfx, rLocalName)
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir XMLEventsImportContext::XMLEventsImportContext(
69*cdf0e10cSrcweir 	SvXMLImport& rImport,
70*cdf0e10cSrcweir 	sal_uInt16 nPrfx,
71*cdf0e10cSrcweir 	const OUString& rLocalName,
72*cdf0e10cSrcweir 	const Reference<XEventsSupplier> & xEventsSupplier) :
73*cdf0e10cSrcweir 		SvXMLImportContext(rImport, nPrfx, rLocalName),
74*cdf0e10cSrcweir 		xEvents(xEventsSupplier->getEvents())
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir XMLEventsImportContext::XMLEventsImportContext(
80*cdf0e10cSrcweir 	SvXMLImport& rImport,
81*cdf0e10cSrcweir 	sal_uInt16 nPrfx,
82*cdf0e10cSrcweir 	const OUString& rLocalName,
83*cdf0e10cSrcweir 	const Reference<XNameReplace> & xNameReplace) :
84*cdf0e10cSrcweir 		SvXMLImportContext(rImport, nPrfx, rLocalName),
85*cdf0e10cSrcweir 		xEvents(xNameReplace)
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir XMLEventsImportContext::~XMLEventsImportContext()
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir // 	// if, for whatever reason, the object gets destroyed prematurely,
92*cdf0e10cSrcweir // 	// we need to delete the collected events
93*cdf0e10cSrcweir // 	EventsVector::iterator aEnd = aCollectEvents.end();
94*cdf0e10cSrcweir // 	for(EventsVector::iterator aIter = aCollectEvents.begin();
95*cdf0e10cSrcweir // 		aIter != aEnd;
96*cdf0e10cSrcweir // 		aIter++)
97*cdf0e10cSrcweir // 	{
98*cdf0e10cSrcweir // 		EventNameValuesPair* pPair = &(*aIter);
99*cdf0e10cSrcweir // 		delete pPair;
100*cdf0e10cSrcweir // 	}
101*cdf0e10cSrcweir // 	aCollectEvents.clear();
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir void XMLEventsImportContext::StartElement(
106*cdf0e10cSrcweir 	const Reference<XAttributeList> &)
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	// nothing to be done
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir void XMLEventsImportContext::EndElement()
112*cdf0e10cSrcweir {
113*cdf0e10cSrcweir 	// nothing to be done
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir SvXMLImportContext* XMLEventsImportContext::CreateChildContext(
117*cdf0e10cSrcweir 	sal_uInt16 p_nPrefix,
118*cdf0e10cSrcweir 	const OUString& rLocalName,
119*cdf0e10cSrcweir 	const Reference<XAttributeList> & xAttrList )
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir 	// a) search for script:language and script:event-name attribute
122*cdf0e10cSrcweir 	// b) delegate to factory. The factory will:
123*cdf0e10cSrcweir 	//    1) translate XML event name into API event name
124*cdf0e10cSrcweir 	//    2) get proper event context factory from import
125*cdf0e10cSrcweir 	//    3) instantiate context
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	// a) search for script:language and script:event-name attribute
128*cdf0e10cSrcweir 	OUString sLanguage;
129*cdf0e10cSrcweir 	OUString sEventName;
130*cdf0e10cSrcweir 	sal_Int16 nCount = xAttrList->getLength();
131*cdf0e10cSrcweir 	for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
132*cdf0e10cSrcweir 	{
133*cdf0e10cSrcweir 		OUString sLocalName;
134*cdf0e10cSrcweir 		sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
135*cdf0e10cSrcweir 			GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		if (XML_NAMESPACE_SCRIPT == nPrefix)
138*cdf0e10cSrcweir 		{
139*cdf0e10cSrcweir 			if (IsXMLToken(sLocalName, XML_EVENT_NAME))
140*cdf0e10cSrcweir 			{
141*cdf0e10cSrcweir 				sEventName = xAttrList->getValueByIndex(nAttr);
142*cdf0e10cSrcweir 			}
143*cdf0e10cSrcweir 			else if (IsXMLToken(sLocalName, XML_LANGUAGE))
144*cdf0e10cSrcweir 			{
145*cdf0e10cSrcweir 				sLanguage = xAttrList->getValueByIndex(nAttr);
146*cdf0e10cSrcweir 			}
147*cdf0e10cSrcweir 			// else: ignore -> let child context handle this
148*cdf0e10cSrcweir 		}
149*cdf0e10cSrcweir 		// else: ignore -> let child context handle this
150*cdf0e10cSrcweir 	}
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	// b) delegate to factory
153*cdf0e10cSrcweir 	return GetImport().GetEventImport().CreateContext(
154*cdf0e10cSrcweir 		GetImport(), p_nPrefix, rLocalName, xAttrList,
155*cdf0e10cSrcweir 		this, sEventName, sLanguage);
156*cdf0e10cSrcweir }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir void XMLEventsImportContext::SetEvents(
159*cdf0e10cSrcweir 	const Reference<XEventsSupplier> & xEventsSupplier)
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	if (xEventsSupplier.is())
162*cdf0e10cSrcweir 	{
163*cdf0e10cSrcweir 		SetEvents(xEventsSupplier->getEvents());
164*cdf0e10cSrcweir 	}
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir void XMLEventsImportContext::SetEvents(
168*cdf0e10cSrcweir 	const Reference<XNameReplace> & xNameRepl)
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir 	if (xNameRepl.is())
171*cdf0e10cSrcweir 	{
172*cdf0e10cSrcweir 		xEvents = xNameRepl;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 		// now iterate over vector and a) insert b) delete all elements
175*cdf0e10cSrcweir 		EventsVector::iterator aEnd = aCollectEvents.end();
176*cdf0e10cSrcweir 		for(EventsVector::iterator aIter = aCollectEvents.begin();
177*cdf0e10cSrcweir 			aIter != aEnd;
178*cdf0e10cSrcweir 			aIter++)
179*cdf0e10cSrcweir 		{
180*cdf0e10cSrcweir 			AddEventValues(aIter->first, aIter->second);
181*cdf0e10cSrcweir // 			EventNameValuesPair* pPair = &(*aIter);
182*cdf0e10cSrcweir // 			delete pPair;
183*cdf0e10cSrcweir 		}
184*cdf0e10cSrcweir 		aCollectEvents.clear();
185*cdf0e10cSrcweir 	}
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir sal_Bool XMLEventsImportContext::GetEventSequence(
189*cdf0e10cSrcweir     const OUString& rName,
190*cdf0e10cSrcweir     Sequence<PropertyValue> & rSequence )
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir     // search through the vector
193*cdf0e10cSrcweir     // (This shouldn't take a lot of time, since this method should only get
194*cdf0e10cSrcweir     //  called if only one (or few) events are being expected)
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     // iterate over vector until end or rName is found;
197*cdf0e10cSrcweir     EventsVector::iterator aIter = aCollectEvents.begin();
198*cdf0e10cSrcweir     while( (aIter != aCollectEvents.end()) && (aIter->first != rName) )
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir         aIter++;
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     // if we're not at the end, set the sequence
204*cdf0e10cSrcweir     sal_Bool bRet = (aIter != aCollectEvents.end());
205*cdf0e10cSrcweir     if (bRet)
206*cdf0e10cSrcweir         rSequence = aIter->second;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     return bRet;
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir void XMLEventsImportContext::AddEventValues(
212*cdf0e10cSrcweir 	const OUString& rEventName,
213*cdf0e10cSrcweir 	const Sequence<PropertyValue> & rValues )
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir 	// if we already have the events, set them; else just collect
216*cdf0e10cSrcweir 	if (xEvents.is())
217*cdf0e10cSrcweir 	{
218*cdf0e10cSrcweir 		// set event (if name is known)
219*cdf0e10cSrcweir 		if (xEvents->hasByName(rEventName))
220*cdf0e10cSrcweir 		{
221*cdf0e10cSrcweir 			Any aAny;
222*cdf0e10cSrcweir 			aAny <<= rValues;
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir             try
225*cdf0e10cSrcweir             {
226*cdf0e10cSrcweir                 xEvents->replaceByName(rEventName, aAny);
227*cdf0e10cSrcweir             } catch ( const IllegalArgumentException & rException )
228*cdf0e10cSrcweir             {
229*cdf0e10cSrcweir                 Sequence<OUString> aMsgParams(1);
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir                 aMsgParams[0] = rEventName;
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir                 GetImport().SetError(XMLERROR_FLAG_ERROR |
234*cdf0e10cSrcweir                                      XMLERROR_ILLEGAL_EVENT,
235*cdf0e10cSrcweir                                      aMsgParams, rException.Message, 0);
236*cdf0e10cSrcweir             }
237*cdf0e10cSrcweir 		}
238*cdf0e10cSrcweir 	}
239*cdf0e10cSrcweir 	else
240*cdf0e10cSrcweir 	{
241*cdf0e10cSrcweir // 		EventNameValuesPair* aPair = new EventNameValuesPair(rEventName,
242*cdf0e10cSrcweir // 															rValues);
243*cdf0e10cSrcweir // 		aCollectEvents.push_back(*aPair);
244*cdf0e10cSrcweir 		EventNameValuesPair aPair(rEventName, rValues);
245*cdf0e10cSrcweir 		aCollectEvents.push_back(aPair);
246*cdf0e10cSrcweir 	}
247*cdf0e10cSrcweir }
248