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