update credits
[LibreOffice.git] / xmloff / source / script / XMLEventsImportContext.cxx
blob1f3329de71055a00cdd8fe2d11645070def5885a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <xmloff/XMLEventsImportContext.hxx>
22 #include "XMLEventImportHelper.hxx"
24 #include <com/sun/star/document/XEventsSupplier.hpp>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/nmspmap.hxx>
27 #include "xmloff/xmlnmspe.hxx"
28 #include <xmloff/xmltoken.hxx>
29 #include "xmloff/xmlerror.hxx"
31 using namespace ::com::sun::star::uno;
32 using namespace ::xmloff::token;
34 using ::com::sun::star::xml::sax::XAttributeList;
35 using ::com::sun::star::beans::PropertyValue;
36 using ::com::sun::star::container::XNameReplace;
37 using ::com::sun::star::document::XEventsSupplier;
38 using ::com::sun::star::lang::IllegalArgumentException;
40 TYPEINIT1(XMLEventsImportContext, SvXMLImportContext);
43 XMLEventsImportContext::XMLEventsImportContext(
44 SvXMLImport& rImport,
45 sal_uInt16 nPrfx,
46 const OUString& rLocalName) :
47 SvXMLImportContext(rImport, nPrfx, rLocalName)
52 XMLEventsImportContext::XMLEventsImportContext(
53 SvXMLImport& rImport,
54 sal_uInt16 nPrfx,
55 const OUString& rLocalName,
56 const Reference<XEventsSupplier> & xEventsSupplier) :
57 SvXMLImportContext(rImport, nPrfx, rLocalName),
58 xEvents(xEventsSupplier->getEvents())
63 XMLEventsImportContext::XMLEventsImportContext(
64 SvXMLImport& rImport,
65 sal_uInt16 nPrfx,
66 const OUString& rLocalName,
67 const Reference<XNameReplace> & xNameReplace) :
68 SvXMLImportContext(rImport, nPrfx, rLocalName),
69 xEvents(xNameReplace)
73 XMLEventsImportContext::~XMLEventsImportContext()
75 // // if, for whatever reason, the object gets destroyed prematurely,
76 // // we need to delete the collected events
80 void XMLEventsImportContext::StartElement(
81 const Reference<XAttributeList> &)
83 // nothing to be done
86 void XMLEventsImportContext::EndElement()
88 // nothing to be done
91 SvXMLImportContext* XMLEventsImportContext::CreateChildContext(
92 sal_uInt16 p_nPrefix,
93 const OUString& rLocalName,
94 const Reference<XAttributeList> & xAttrList )
96 // a) search for script:language and script:event-name attribute
97 // b) delegate to factory. The factory will:
98 // 1) translate XML event name into API event name
99 // 2) get proper event context factory from import
100 // 3) instantiate context
102 // a) search for script:language and script:event-name attribute
103 OUString sLanguage;
104 OUString sEventName;
105 sal_Int16 nCount = xAttrList->getLength();
106 for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
108 OUString sLocalName;
109 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
110 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
112 if (XML_NAMESPACE_SCRIPT == nPrefix)
114 if (IsXMLToken(sLocalName, XML_EVENT_NAME))
116 sEventName = xAttrList->getValueByIndex(nAttr);
118 else if (IsXMLToken(sLocalName, XML_LANGUAGE))
120 sLanguage = xAttrList->getValueByIndex(nAttr);
122 // else: ignore -> let child context handle this
124 // else: ignore -> let child context handle this
127 // b) delegate to factory
128 return GetImport().GetEventImport().CreateContext(
129 GetImport(), p_nPrefix, rLocalName, xAttrList,
130 this, sEventName, sLanguage);
133 void XMLEventsImportContext::SetEvents(
134 const Reference<XEventsSupplier> & xEventsSupplier)
136 if (xEventsSupplier.is())
138 SetEvents(xEventsSupplier->getEvents());
142 void XMLEventsImportContext::SetEvents(
143 const Reference<XNameReplace> & xNameRepl)
145 if (xNameRepl.is())
147 xEvents = xNameRepl;
149 // now iterate over vector and a) insert b) delete all elements
150 EventsVector::iterator aEnd = aCollectEvents.end();
151 for(EventsVector::iterator aIter = aCollectEvents.begin();
152 aIter != aEnd;
153 ++aIter)
155 AddEventValues(aIter->first, aIter->second);
157 aCollectEvents.clear();
161 sal_Bool XMLEventsImportContext::GetEventSequence(
162 const OUString& rName,
163 Sequence<PropertyValue> & rSequence )
165 // search through the vector
166 // (This shouldn't take a lot of time, since this method should only get
167 // called if only one (or few) events are being expected)
169 // iterate over vector until end or rName is found;
170 EventsVector::iterator aIter = aCollectEvents.begin();
171 while( (aIter != aCollectEvents.end()) && (aIter->first != rName) )
173 ++aIter;
176 // if we're not at the end, set the sequence
177 sal_Bool bRet = (aIter != aCollectEvents.end());
178 if (bRet)
179 rSequence = aIter->second;
181 return bRet;
184 void XMLEventsImportContext::AddEventValues(
185 const OUString& rEventName,
186 const Sequence<PropertyValue> & rValues )
188 // if we already have the events, set them; else just collect
189 if (xEvents.is())
191 // set event (if name is known)
192 if (xEvents->hasByName(rEventName))
194 Any aAny;
195 aAny <<= rValues;
199 xEvents->replaceByName(rEventName, aAny);
200 } catch ( const IllegalArgumentException & rException )
202 Sequence<OUString> aMsgParams(1);
204 aMsgParams[0] = rEventName;
206 GetImport().SetError(XMLERROR_FLAG_ERROR |
207 XMLERROR_ILLEGAL_EVENT,
208 aMsgParams, rException.Message, 0);
212 else
214 EventNameValuesPair aPair(rEventName, rValues);
215 aCollectEvents.push_back(aPair);
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */