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