1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/xmlnamespace.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmlerror.hxx>
30 using namespace ::com::sun::star::uno
;
31 using namespace ::xmloff::token
;
33 using ::com::sun::star::beans::PropertyValue
;
34 using ::com::sun::star::container::XNameReplace
;
35 using ::com::sun::star::document::XEventsSupplier
;
36 using ::com::sun::star::lang::IllegalArgumentException
;
39 XMLEventsImportContext::XMLEventsImportContext(SvXMLImport
& rImport
) :
40 SvXMLImportContext(rImport
)
45 XMLEventsImportContext::XMLEventsImportContext(
47 const Reference
<XEventsSupplier
> & xEventsSupplier
) :
48 SvXMLImportContext(rImport
),
49 m_xEvents(xEventsSupplier
->getEvents())
54 XMLEventsImportContext::XMLEventsImportContext(
56 const Reference
<XNameReplace
> & xNameReplace
) :
57 SvXMLImportContext(rImport
),
58 m_xEvents(xNameReplace
)
62 XMLEventsImportContext::~XMLEventsImportContext()
64 // // if, for whatever reason, the object gets destroyed prematurely,
65 // // we need to delete the collected events
69 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLEventsImportContext::createFastChildContext(
70 sal_Int32
/*nElement*/,
71 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
73 // a) search for script:language and script:event-name attribute
74 // b) delegate to factory. The factory will:
75 // 1) translate XML event name into API event name
76 // 2) get proper event context factory from import
77 // 3) instantiate context
79 // a) search for script:language and script:event-name attribute
82 for (auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
))
84 OUString sValue
= aIter
.toString();
86 if (aIter
.getToken() == XML_ELEMENT(SCRIPT
, XML_EVENT_NAME
))
90 else if (aIter
.getToken() == XML_ELEMENT(SCRIPT
, XML_LANGUAGE
))
93 // else: ignore -> let child context handle this
95 // else: ignore -> let child context handle this
98 // b) delegate to factory
99 return GetImport().GetEventImport().CreateContext(
100 GetImport(), xAttrList
, this, sEventName
, sLanguage
);
103 void XMLEventsImportContext::SetEvents(
104 const Reference
<XEventsSupplier
> & xEventsSupplier
)
106 if (xEventsSupplier
.is())
108 SetEvents(xEventsSupplier
->getEvents());
112 void XMLEventsImportContext::SetEvents(
113 const Reference
<XNameReplace
> & xNameRepl
)
117 m_xEvents
= xNameRepl
;
119 // now iterate over vector and a) insert b) delete all elements
120 for(const auto& rEvent
: m_aCollectEvents
)
122 AddEventValues(rEvent
.first
, rEvent
.second
);
124 m_aCollectEvents
.clear();
128 void XMLEventsImportContext::GetEventSequence(
129 const OUString
& rName
,
130 Sequence
<PropertyValue
> & rSequence
)
132 // search through the vector
133 // (This shouldn't take a lot of time, since this method should only get
134 // called if only one (or few) events are being expected)
136 auto aIter
= std::find_if(m_aCollectEvents
.begin(), m_aCollectEvents
.end(),
137 [&rName
](EventNameValuesPair
& rEvent
) { return rEvent
.first
== rName
; });
139 // if we're not at the end, set the sequence
140 if (aIter
!= m_aCollectEvents
.end())
142 rSequence
= aIter
->second
;
146 void XMLEventsImportContext::AddEventValues(
147 const OUString
& rEventName
,
148 const Sequence
<PropertyValue
> & rValues
)
150 // if we already have the events, set them; else just collect
153 // set event (if name is known)
154 if (m_xEvents
->hasByName(rEventName
))
158 m_xEvents
->replaceByName(rEventName
, Any(rValues
));
159 } catch ( const IllegalArgumentException
& rException
)
161 Sequence
<OUString
> aMsgParams
{ rEventName
};
163 GetImport().SetError(XMLERROR_FLAG_ERROR
|
164 XMLERROR_ILLEGAL_EVENT
,
165 aMsgParams
, rException
.Message
, nullptr);
171 EventNameValuesPair
aPair(rEventName
, rValues
);
172 m_aCollectEvents
.push_back(aPair
);
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */