bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / script / XMLEventsImportContext.cxx
blob916e8b5dc0bb07861320324ff7136bc1aa7e104e
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 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 if (aIter != aCollectEvents.end())
179 rSequence = aIter->second;
180 return true;
183 return false;
186 void XMLEventsImportContext::AddEventValues(
187 const OUString& rEventName,
188 const Sequence<PropertyValue> & rValues )
190 // if we already have the events, set them; else just collect
191 if (xEvents.is())
193 // set event (if name is known)
194 if (xEvents->hasByName(rEventName))
196 Any aAny;
197 aAny <<= rValues;
201 xEvents->replaceByName(rEventName, aAny);
202 } catch ( const IllegalArgumentException & rException )
204 Sequence<OUString> aMsgParams(1);
206 aMsgParams[0] = rEventName;
208 GetImport().SetError(XMLERROR_FLAG_ERROR |
209 XMLERROR_ILLEGAL_EVENT,
210 aMsgParams, rException.Message, 0);
214 else
216 EventNameValuesPair aPair(rEventName, rValues);
217 aCollectEvents.push_back(aPair);
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */