Branch libreoffice-6-3
[LibreOffice.git] / xmloff / source / script / XMLEventsImportContext.cxx
blobaea5eb9e62ac58e9527e9070fc720bc967846266
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;
41 XMLEventsImportContext::XMLEventsImportContext(
42 SvXMLImport& rImport,
43 sal_uInt16 nPrfx,
44 const OUString& rLocalName) :
45 SvXMLImportContext(rImport, nPrfx, rLocalName)
50 XMLEventsImportContext::XMLEventsImportContext(
51 SvXMLImport& rImport,
52 sal_uInt16 nPrfx,
53 const OUString& rLocalName,
54 const Reference<XEventsSupplier> & xEventsSupplier) :
55 SvXMLImportContext(rImport, nPrfx, rLocalName),
56 xEvents(xEventsSupplier->getEvents())
61 XMLEventsImportContext::XMLEventsImportContext(
62 SvXMLImport& rImport,
63 sal_uInt16 nPrfx,
64 const OUString& rLocalName,
65 const Reference<XNameReplace> & xNameReplace) :
66 SvXMLImportContext(rImport, nPrfx, rLocalName),
67 xEvents(xNameReplace)
71 XMLEventsImportContext::~XMLEventsImportContext()
73 // // if, for whatever reason, the object gets destroyed prematurely,
74 // // we need to delete the collected events
78 void XMLEventsImportContext::StartElement(
79 const Reference<XAttributeList> &)
81 // nothing to be done
84 void XMLEventsImportContext::EndElement()
86 // nothing to be done
89 SvXMLImportContextRef XMLEventsImportContext::CreateChildContext(
90 sal_uInt16 p_nPrefix,
91 const OUString& rLocalName,
92 const Reference<XAttributeList> & xAttrList )
94 // a) search for script:language and script:event-name attribute
95 // b) delegate to factory. The factory will:
96 // 1) translate XML event name into API event name
97 // 2) get proper event context factory from import
98 // 3) instantiate context
100 // a) search for script:language and script:event-name attribute
101 OUString sLanguage;
102 OUString sEventName;
103 sal_Int16 nCount = xAttrList->getLength();
104 for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
106 OUString sLocalName;
107 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
108 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
110 if (XML_NAMESPACE_SCRIPT == nPrefix)
112 if (IsXMLToken(sLocalName, XML_EVENT_NAME))
114 sEventName = xAttrList->getValueByIndex(nAttr);
116 else if (IsXMLToken(sLocalName, XML_LANGUAGE))
118 sLanguage = xAttrList->getValueByIndex(nAttr);
120 // else: ignore -> let child context handle this
122 // else: ignore -> let child context handle this
125 // b) delegate to factory
126 return GetImport().GetEventImport().CreateContext(
127 GetImport(), p_nPrefix, rLocalName, xAttrList,
128 this, sEventName, sLanguage);
131 void XMLEventsImportContext::SetEvents(
132 const Reference<XEventsSupplier> & xEventsSupplier)
134 if (xEventsSupplier.is())
136 SetEvents(xEventsSupplier->getEvents());
140 void XMLEventsImportContext::SetEvents(
141 const Reference<XNameReplace> & xNameRepl)
143 if (xNameRepl.is())
145 xEvents = xNameRepl;
147 // now iterate over vector and a) insert b) delete all elements
148 for(const auto& rEvent : aCollectEvents)
150 AddEventValues(rEvent.first, rEvent.second);
152 aCollectEvents.clear();
156 void XMLEventsImportContext::GetEventSequence(
157 const OUString& rName,
158 Sequence<PropertyValue> & rSequence )
160 // search through the vector
161 // (This shouldn't take a lot of time, since this method should only get
162 // called if only one (or few) events are being expected)
164 auto aIter = std::find_if(aCollectEvents.begin(), aCollectEvents.end(),
165 [&rName](EventNameValuesPair& rEvent) { return rEvent.first == rName; });
167 // if we're not at the end, set the sequence
168 if (aIter != aCollectEvents.end())
170 rSequence = aIter->second;
174 void XMLEventsImportContext::AddEventValues(
175 const OUString& rEventName,
176 const Sequence<PropertyValue> & rValues )
178 // if we already have the events, set them; else just collect
179 if (xEvents.is())
181 // set event (if name is known)
182 if (xEvents->hasByName(rEventName))
186 xEvents->replaceByName(rEventName, Any(rValues));
187 } catch ( const IllegalArgumentException & rException )
189 Sequence<OUString> aMsgParams { rEventName };
191 GetImport().SetError(XMLERROR_FLAG_ERROR |
192 XMLERROR_ILLEGAL_EVENT,
193 aMsgParams, rException.Message, nullptr);
197 else
199 EventNameValuesPair aPair(rEventName, rValues);
200 aCollectEvents.push_back(aPair);
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */