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