1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19*63bba73cSAndrew Rist  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include <xmloff/XMLEventsImportContext.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #ifndef _XMLOFF_XMLEVENTIMPORTHELPER_HXX
29cdf0e10cSrcweir #include "XMLEventImportHelper.hxx"
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #ifndef _COM_SUN_STAR_DOCUMENT_XEVENTSSUPPLIER_HPP
33cdf0e10cSrcweir #include <com/sun/star/document/XEventsSupplier.hpp>
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir #include <tools/debug.hxx>
36cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
37cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
38cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
39cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
40cdf0e10cSrcweir #include "xmloff/xmlerror.hxx"
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::com::sun::star::uno;
43cdf0e10cSrcweir using namespace ::xmloff::token;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using ::rtl::OUString;
46cdf0e10cSrcweir using ::com::sun::star::xml::sax::XAttributeList;
47cdf0e10cSrcweir using ::com::sun::star::beans::PropertyValue;
48cdf0e10cSrcweir using ::com::sun::star::container::XNameReplace;
49cdf0e10cSrcweir using ::com::sun::star::document::XEventsSupplier;
50cdf0e10cSrcweir using ::com::sun::star::lang::IllegalArgumentException;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir TYPEINIT1(XMLEventsImportContext,  SvXMLImportContext);
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
XMLEventsImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLocalName)55cdf0e10cSrcweir XMLEventsImportContext::XMLEventsImportContext(
56cdf0e10cSrcweir 	SvXMLImport& rImport,
57cdf0e10cSrcweir 	sal_uInt16 nPrfx,
58cdf0e10cSrcweir 	const OUString& rLocalName) :
59cdf0e10cSrcweir 		SvXMLImportContext(rImport, nPrfx, rLocalName)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
XMLEventsImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLocalName,const Reference<XEventsSupplier> & xEventsSupplier)64cdf0e10cSrcweir XMLEventsImportContext::XMLEventsImportContext(
65cdf0e10cSrcweir 	SvXMLImport& rImport,
66cdf0e10cSrcweir 	sal_uInt16 nPrfx,
67cdf0e10cSrcweir 	const OUString& rLocalName,
68cdf0e10cSrcweir 	const Reference<XEventsSupplier> & xEventsSupplier) :
69cdf0e10cSrcweir 		SvXMLImportContext(rImport, nPrfx, rLocalName),
70cdf0e10cSrcweir 		xEvents(xEventsSupplier->getEvents())
71cdf0e10cSrcweir {
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 
XMLEventsImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLocalName,const Reference<XNameReplace> & xNameReplace)75cdf0e10cSrcweir XMLEventsImportContext::XMLEventsImportContext(
76cdf0e10cSrcweir 	SvXMLImport& rImport,
77cdf0e10cSrcweir 	sal_uInt16 nPrfx,
78cdf0e10cSrcweir 	const OUString& rLocalName,
79cdf0e10cSrcweir 	const Reference<XNameReplace> & xNameReplace) :
80cdf0e10cSrcweir 		SvXMLImportContext(rImport, nPrfx, rLocalName),
81cdf0e10cSrcweir 		xEvents(xNameReplace)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
~XMLEventsImportContext()85cdf0e10cSrcweir XMLEventsImportContext::~XMLEventsImportContext()
86cdf0e10cSrcweir {
87cdf0e10cSrcweir // 	// if, for whatever reason, the object gets destroyed prematurely,
88cdf0e10cSrcweir // 	// we need to delete the collected events
89cdf0e10cSrcweir // 	EventsVector::iterator aEnd = aCollectEvents.end();
90cdf0e10cSrcweir // 	for(EventsVector::iterator aIter = aCollectEvents.begin();
91cdf0e10cSrcweir // 		aIter != aEnd;
92cdf0e10cSrcweir // 		aIter++)
93cdf0e10cSrcweir // 	{
94cdf0e10cSrcweir // 		EventNameValuesPair* pPair = &(*aIter);
95cdf0e10cSrcweir // 		delete pPair;
96cdf0e10cSrcweir // 	}
97cdf0e10cSrcweir // 	aCollectEvents.clear();
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
StartElement(const Reference<XAttributeList> &)101cdf0e10cSrcweir void XMLEventsImportContext::StartElement(
102cdf0e10cSrcweir 	const Reference<XAttributeList> &)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	// nothing to be done
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
EndElement()107cdf0e10cSrcweir void XMLEventsImportContext::EndElement()
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	// nothing to be done
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
CreateChildContext(sal_uInt16 p_nPrefix,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList)112cdf0e10cSrcweir SvXMLImportContext* XMLEventsImportContext::CreateChildContext(
113cdf0e10cSrcweir 	sal_uInt16 p_nPrefix,
114cdf0e10cSrcweir 	const OUString& rLocalName,
115cdf0e10cSrcweir 	const Reference<XAttributeList> & xAttrList )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir 	// a) search for script:language and script:event-name attribute
118cdf0e10cSrcweir 	// b) delegate to factory. The factory will:
119cdf0e10cSrcweir 	//    1) translate XML event name into API event name
120cdf0e10cSrcweir 	//    2) get proper event context factory from import
121cdf0e10cSrcweir 	//    3) instantiate context
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	// a) search for script:language and script:event-name attribute
124cdf0e10cSrcweir 	OUString sLanguage;
125cdf0e10cSrcweir 	OUString sEventName;
126cdf0e10cSrcweir 	sal_Int16 nCount = xAttrList->getLength();
127cdf0e10cSrcweir 	for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
128cdf0e10cSrcweir 	{
129cdf0e10cSrcweir 		OUString sLocalName;
130cdf0e10cSrcweir 		sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
131cdf0e10cSrcweir 			GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		if (XML_NAMESPACE_SCRIPT == nPrefix)
134cdf0e10cSrcweir 		{
135cdf0e10cSrcweir 			if (IsXMLToken(sLocalName, XML_EVENT_NAME))
136cdf0e10cSrcweir 			{
137cdf0e10cSrcweir 				sEventName = xAttrList->getValueByIndex(nAttr);
138cdf0e10cSrcweir 			}
139cdf0e10cSrcweir 			else if (IsXMLToken(sLocalName, XML_LANGUAGE))
140cdf0e10cSrcweir 			{
141cdf0e10cSrcweir 				sLanguage = xAttrList->getValueByIndex(nAttr);
142cdf0e10cSrcweir 			}
143cdf0e10cSrcweir 			// else: ignore -> let child context handle this
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 		// else: ignore -> let child context handle this
146cdf0e10cSrcweir 	}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	// b) delegate to factory
149cdf0e10cSrcweir 	return GetImport().GetEventImport().CreateContext(
150cdf0e10cSrcweir 		GetImport(), p_nPrefix, rLocalName, xAttrList,
151cdf0e10cSrcweir 		this, sEventName, sLanguage);
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
SetEvents(const Reference<XEventsSupplier> & xEventsSupplier)154cdf0e10cSrcweir void XMLEventsImportContext::SetEvents(
155cdf0e10cSrcweir 	const Reference<XEventsSupplier> & xEventsSupplier)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	if (xEventsSupplier.is())
158cdf0e10cSrcweir 	{
159cdf0e10cSrcweir 		SetEvents(xEventsSupplier->getEvents());
160cdf0e10cSrcweir 	}
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
SetEvents(const Reference<XNameReplace> & xNameRepl)163cdf0e10cSrcweir void XMLEventsImportContext::SetEvents(
164cdf0e10cSrcweir 	const Reference<XNameReplace> & xNameRepl)
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	if (xNameRepl.is())
167cdf0e10cSrcweir 	{
168cdf0e10cSrcweir 		xEvents = xNameRepl;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 		// now iterate over vector and a) insert b) delete all elements
171cdf0e10cSrcweir 		EventsVector::iterator aEnd = aCollectEvents.end();
172cdf0e10cSrcweir 		for(EventsVector::iterator aIter = aCollectEvents.begin();
173cdf0e10cSrcweir 			aIter != aEnd;
174cdf0e10cSrcweir 			aIter++)
175cdf0e10cSrcweir 		{
176cdf0e10cSrcweir 			AddEventValues(aIter->first, aIter->second);
177cdf0e10cSrcweir // 			EventNameValuesPair* pPair = &(*aIter);
178cdf0e10cSrcweir // 			delete pPair;
179cdf0e10cSrcweir 		}
180cdf0e10cSrcweir 		aCollectEvents.clear();
181cdf0e10cSrcweir 	}
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
GetEventSequence(const OUString & rName,Sequence<PropertyValue> & rSequence)184cdf0e10cSrcweir sal_Bool XMLEventsImportContext::GetEventSequence(
185cdf0e10cSrcweir     const OUString& rName,
186cdf0e10cSrcweir     Sequence<PropertyValue> & rSequence )
187cdf0e10cSrcweir {
188cdf0e10cSrcweir     // search through the vector
189cdf0e10cSrcweir     // (This shouldn't take a lot of time, since this method should only get
190cdf0e10cSrcweir     //  called if only one (or few) events are being expected)
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     // iterate over vector until end or rName is found;
193cdf0e10cSrcweir     EventsVector::iterator aIter = aCollectEvents.begin();
194cdf0e10cSrcweir     while( (aIter != aCollectEvents.end()) && (aIter->first != rName) )
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         aIter++;
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     // if we're not at the end, set the sequence
200cdf0e10cSrcweir     sal_Bool bRet = (aIter != aCollectEvents.end());
201cdf0e10cSrcweir     if (bRet)
202cdf0e10cSrcweir         rSequence = aIter->second;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     return bRet;
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
AddEventValues(const OUString & rEventName,const Sequence<PropertyValue> & rValues)207cdf0e10cSrcweir void XMLEventsImportContext::AddEventValues(
208cdf0e10cSrcweir 	const OUString& rEventName,
209cdf0e10cSrcweir 	const Sequence<PropertyValue> & rValues )
210cdf0e10cSrcweir {
211cdf0e10cSrcweir 	// if we already have the events, set them; else just collect
212cdf0e10cSrcweir 	if (xEvents.is())
213cdf0e10cSrcweir 	{
214cdf0e10cSrcweir 		// set event (if name is known)
215cdf0e10cSrcweir 		if (xEvents->hasByName(rEventName))
216cdf0e10cSrcweir 		{
217cdf0e10cSrcweir 			Any aAny;
218cdf0e10cSrcweir 			aAny <<= rValues;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir             try
221cdf0e10cSrcweir             {
222cdf0e10cSrcweir                 xEvents->replaceByName(rEventName, aAny);
223cdf0e10cSrcweir             } catch ( const IllegalArgumentException & rException )
224cdf0e10cSrcweir             {
225cdf0e10cSrcweir                 Sequence<OUString> aMsgParams(1);
226cdf0e10cSrcweir 
227cdf0e10cSrcweir                 aMsgParams[0] = rEventName;
228cdf0e10cSrcweir 
229cdf0e10cSrcweir                 GetImport().SetError(XMLERROR_FLAG_ERROR |
230cdf0e10cSrcweir                                      XMLERROR_ILLEGAL_EVENT,
231cdf0e10cSrcweir                                      aMsgParams, rException.Message, 0);
232cdf0e10cSrcweir             }
233cdf0e10cSrcweir 		}
234cdf0e10cSrcweir 	}
235cdf0e10cSrcweir 	else
236cdf0e10cSrcweir 	{
237cdf0e10cSrcweir // 		EventNameValuesPair* aPair = new EventNameValuesPair(rEventName,
238cdf0e10cSrcweir // 															rValues);
239cdf0e10cSrcweir // 		aCollectEvents.push_back(*aPair);
240cdf0e10cSrcweir 		EventNameValuesPair aPair(rEventName, rValues);
241cdf0e10cSrcweir 		aCollectEvents.push_back(aPair);
242cdf0e10cSrcweir 	}
243cdf0e10cSrcweir }
244