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 <comphelper/attributelist.hxx>
26 #include <xmloff/xmlimp.hxx>
27 #include <xmloff/namespacemap.hxx>
28 #include <xmloff/xmlnamespace.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/xmlerror.hxx>
32 using namespace ::com::sun::star::uno
;
33 using namespace ::xmloff::token
;
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(SvXMLImport
& rImport
) :
42 SvXMLImportContext(rImport
)
47 XMLEventsImportContext::XMLEventsImportContext(
49 const Reference
<XEventsSupplier
> & xEventsSupplier
) :
50 SvXMLImportContext(rImport
),
51 m_xEvents(xEventsSupplier
->getEvents())
56 XMLEventsImportContext::XMLEventsImportContext(
58 const Reference
<XNameReplace
> & xNameReplace
) :
59 SvXMLImportContext(rImport
),
60 m_xEvents(xNameReplace
)
64 XMLEventsImportContext::~XMLEventsImportContext()
66 // // if, for whatever reason, the object gets destroyed prematurely,
67 // // we need to delete the collected events
71 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLEventsImportContext::createFastChildContext(
72 sal_Int32
/*nElement*/,
73 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
75 // a) search for script:language and script:event-name attribute
76 // b) delegate to factory. The factory will:
77 // 1) translate XML event name into API event name
78 // 2) get proper event context factory from import
79 // 3) instantiate context
81 // a) search for script:language and script:event-name attribute
84 for (auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
))
86 OUString sValue
= aIter
.toString();
88 if (aIter
.getToken() == XML_ELEMENT(SCRIPT
, XML_EVENT_NAME
))
92 else if (aIter
.getToken() == XML_ELEMENT(SCRIPT
, XML_LANGUAGE
))
95 // else: ignore -> let child context handle this
97 // else: ignore -> let child context handle this
100 // b) delegate to factory
101 return GetImport().GetEventImport().CreateContext(
102 GetImport(), xAttrList
, this, sEventName
, sLanguage
);
105 void XMLEventsImportContext::SetEvents(
106 const Reference
<XEventsSupplier
> & xEventsSupplier
)
108 if (xEventsSupplier
.is())
110 SetEvents(xEventsSupplier
->getEvents());
114 void XMLEventsImportContext::SetEvents(
115 const Reference
<XNameReplace
> & xNameRepl
)
119 m_xEvents
= xNameRepl
;
121 // now iterate over vector and a) insert b) delete all elements
122 for(const auto& rEvent
: m_aCollectEvents
)
124 AddEventValues(rEvent
.first
, rEvent
.second
);
126 m_aCollectEvents
.clear();
130 void XMLEventsImportContext::GetEventSequence(
131 const OUString
& rName
,
132 Sequence
<PropertyValue
> & rSequence
)
134 // search through the vector
135 // (This shouldn't take a lot of time, since this method should only get
136 // called if only one (or few) events are being expected)
138 auto aIter
= std::find_if(m_aCollectEvents
.begin(), m_aCollectEvents
.end(),
139 [&rName
](EventNameValuesPair
& rEvent
) { return rEvent
.first
== rName
; });
141 // if we're not at the end, set the sequence
142 if (aIter
!= m_aCollectEvents
.end())
144 rSequence
= aIter
->second
;
148 void XMLEventsImportContext::AddEventValues(
149 const OUString
& rEventName
,
150 const Sequence
<PropertyValue
> & rValues
)
152 // if we already have the events, set them; else just collect
155 // set event (if name is known)
156 if (m_xEvents
->hasByName(rEventName
))
160 m_xEvents
->replaceByName(rEventName
, Any(rValues
));
161 } catch ( const IllegalArgumentException
& rException
)
163 Sequence
<OUString
> aMsgParams
{ rEventName
};
165 GetImport().SetError(XMLERROR_FLAG_ERROR
|
166 XMLERROR_ILLEGAL_EVENT
,
167 aMsgParams
, rException
.Message
, nullptr);
173 EventNameValuesPair
aPair(rEventName
, rValues
);
174 m_aCollectEvents
.push_back(aPair
);
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */