LibreOffice Module xmloff (master) 1
XMLEventsImportContext.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
23
24#include <com/sun/star/document/XEventsSupplier.hpp>
26#include <xmloff/xmlimp.hxx>
29#include <xmloff/xmltoken.hxx>
30#include <xmloff/xmlerror.hxx>
31
32using namespace ::com::sun::star::uno;
33using namespace ::xmloff::token;
34
35using ::com::sun::star::beans::PropertyValue;
36using ::com::sun::star::container::XNameReplace;
37using ::com::sun::star::document::XEventsSupplier;
38using ::com::sun::star::lang::IllegalArgumentException;
39
40
42 SvXMLImportContext(rImport)
43{
44}
45
46
48 SvXMLImport& rImport,
49 const Reference<XEventsSupplier> & xEventsSupplier) :
50 SvXMLImportContext(rImport),
51 m_xEvents(xEventsSupplier->getEvents())
52{
53}
54
55
57 SvXMLImport& rImport,
58 const Reference<XNameReplace> & xNameReplace) :
59 SvXMLImportContext(rImport),
60 m_xEvents(xNameReplace)
61{
62}
63
65{
66// // if, for whatever reason, the object gets destroyed prematurely,
67// // we need to delete the collected events
68}
69
70
71css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEventsImportContext::createFastChildContext(
72 sal_Int32 /*nElement*/,
73 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
74{
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
80
81 // a) search for script:language and script:event-name attribute
82 OUString sLanguage;
83 OUString sEventName;
84 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
85 {
86 OUString sValue = aIter.toString();
87
88 if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_EVENT_NAME))
89 {
90 sEventName = sValue;
91 }
92 else if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_LANGUAGE))
93 {
94 sLanguage = sValue;
95 // else: ignore -> let child context handle this
96 }
97 // else: ignore -> let child context handle this
98 }
99
100 // b) delegate to factory
101 return GetImport().GetEventImport().CreateContext(
102 GetImport(), xAttrList, this, sEventName, sLanguage);
103}
104
106 const Reference<XEventsSupplier> & xEventsSupplier)
107{
108 if (xEventsSupplier.is())
109 {
110 SetEvents(xEventsSupplier->getEvents());
111 }
112}
113
115 const Reference<XNameReplace> & xNameRepl)
116{
117 if (xNameRepl.is())
118 {
119 m_xEvents = xNameRepl;
120
121 // now iterate over vector and a) insert b) delete all elements
122 for(const auto& rEvent : m_aCollectEvents)
123 {
124 AddEventValues(rEvent.first, rEvent.second);
125 }
126 m_aCollectEvents.clear();
127 }
128}
129
131 const OUString& rName,
132 Sequence<PropertyValue> & rSequence )
133{
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)
137
138 auto aIter = std::find_if(m_aCollectEvents.begin(), m_aCollectEvents.end(),
139 [&rName](EventNameValuesPair& rEvent) { return rEvent.first == rName; });
140
141 // if we're not at the end, set the sequence
142 if (aIter != m_aCollectEvents.end())
143 {
144 rSequence = aIter->second;
145 }
146}
147
149 const OUString& rEventName,
150 const Sequence<PropertyValue> & rValues )
151{
152 // if we already have the events, set them; else just collect
153 if (m_xEvents.is())
154 {
155 // set event (if name is known)
156 if (m_xEvents->hasByName(rEventName))
157 {
158 try
159 {
160 m_xEvents->replaceByName(rEventName, Any(rValues));
161 } catch ( const IllegalArgumentException & rException )
162 {
163 Sequence<OUString> aMsgParams { rEventName };
164
165 GetImport().SetError(XMLERROR_FLAG_ERROR |
167 aMsgParams, rException.Message, nullptr);
168 }
169 }
170 }
171 else
172 {
173 EventNameValuesPair aPair(rEventName, rValues);
174 m_aCollectEvents.push_back(aPair);
175 }
176}
177
178/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::pair< OUString, css::uno::Sequence< css::beans::PropertyValue > > EventNameValuesPair
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
XMLEventsImportContext(SvXMLImport &rImport)
css::uno::Reference< css::container::XNameReplace > m_xEvents
void GetEventSequence(const OUString &rName, css::uno::Sequence< css::beans::PropertyValue > &rSequence)
if the import operates in delayed mode, you can use this method to obtain the value sequence for a sp...
std::vector< EventNameValuesPair > m_aCollectEvents
void SetEvents(const css::uno::Reference< css::document::XEventsSupplier > &xEventsSupplier)
if the import operates in delayed mode, you can use this method to set all events that have been read...
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
virtual ~XMLEventsImportContext() override
void AddEventValues(const OUString &rEventName, const css::uno::Sequence< css::beans::PropertyValue > &rValues)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
#define XMLERROR_ILLEGAL_EVENT
Definition: xmlerror.hxx:56
#define XMLERROR_FLAG_ERROR
Definition: xmlerror.hxx:36
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97