/src/libreoffice/xmloff/source/script/XMLEventsImportContext.cxx
Line | Count | Source |
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 | | |
20 | | #include <xmloff/XMLEventsImportContext.hxx> |
21 | | |
22 | | #include <XMLEventImportHelper.hxx> |
23 | | |
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> |
29 | | |
30 | | using namespace ::com::sun::star::uno; |
31 | | using namespace ::xmloff::token; |
32 | | |
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; |
37 | | |
38 | | |
39 | | XMLEventsImportContext::XMLEventsImportContext(SvXMLImport& rImport) : |
40 | 0 | SvXMLImportContext(rImport) |
41 | 0 | { |
42 | 0 | } |
43 | | |
44 | | |
45 | | XMLEventsImportContext::XMLEventsImportContext( |
46 | | SvXMLImport& rImport, |
47 | | const Reference<XEventsSupplier> & xEventsSupplier) : |
48 | 14 | SvXMLImportContext(rImport), |
49 | 14 | m_xEvents(xEventsSupplier->getEvents()) |
50 | 14 | { |
51 | 14 | } |
52 | | |
53 | | |
54 | | XMLEventsImportContext::XMLEventsImportContext( |
55 | | SvXMLImport& rImport, |
56 | | const Reference<XNameReplace> & xNameReplace) : |
57 | 0 | SvXMLImportContext(rImport), |
58 | 0 | m_xEvents(xNameReplace) |
59 | 0 | { |
60 | 0 | } |
61 | | |
62 | | XMLEventsImportContext::~XMLEventsImportContext() |
63 | 14 | { |
64 | | // // if, for whatever reason, the object gets destroyed prematurely, |
65 | | // // we need to delete the collected events |
66 | 14 | } |
67 | | |
68 | | |
69 | | css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEventsImportContext::createFastChildContext( |
70 | | sal_Int32 /*nElement*/, |
71 | | const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) |
72 | 62 | { |
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 |
78 | | |
79 | | // a) search for script:language and script:event-name attribute |
80 | 62 | OUString sLanguage; |
81 | 62 | OUString sEventName; |
82 | 62 | for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList )) |
83 | 169 | { |
84 | 169 | OUString sValue = aIter.toString(); |
85 | | |
86 | 169 | if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_EVENT_NAME)) |
87 | 7 | { |
88 | 7 | sEventName = sValue; |
89 | 7 | } |
90 | 162 | else if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_LANGUAGE)) |
91 | 7 | { |
92 | 7 | sLanguage = sValue; |
93 | | // else: ignore -> let child context handle this |
94 | 7 | } |
95 | | // else: ignore -> let child context handle this |
96 | 169 | } |
97 | | |
98 | | // b) delegate to factory |
99 | 62 | return GetImport().GetEventImport().CreateContext( |
100 | 62 | GetImport(), xAttrList, this, sEventName, sLanguage); |
101 | 62 | } |
102 | | |
103 | | void XMLEventsImportContext::SetEvents( |
104 | | const Reference<XEventsSupplier> & xEventsSupplier) |
105 | 0 | { |
106 | 0 | if (xEventsSupplier.is()) |
107 | 0 | { |
108 | 0 | SetEvents(xEventsSupplier->getEvents()); |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | void XMLEventsImportContext::SetEvents( |
113 | | const Reference<XNameReplace> & xNameRepl) |
114 | 0 | { |
115 | 0 | if (xNameRepl.is()) |
116 | 0 | { |
117 | 0 | m_xEvents = xNameRepl; |
118 | | |
119 | | // now iterate over vector and a) insert b) delete all elements |
120 | 0 | for(const auto& rEvent : m_aCollectEvents) |
121 | 0 | { |
122 | 0 | AddEventValues(rEvent.first, rEvent.second); |
123 | 0 | } |
124 | 0 | m_aCollectEvents.clear(); |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | | void XMLEventsImportContext::GetEventSequence( |
129 | | const OUString& rName, |
130 | | Sequence<PropertyValue> & rSequence ) |
131 | 0 | { |
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) |
135 | |
|
136 | 0 | auto aIter = std::find_if(m_aCollectEvents.begin(), m_aCollectEvents.end(), |
137 | 0 | [&rName](EventNameValuesPair& rEvent) { return rEvent.first == rName; }); |
138 | | |
139 | | // if we're not at the end, set the sequence |
140 | 0 | if (aIter != m_aCollectEvents.end()) |
141 | 0 | { |
142 | 0 | rSequence = aIter->second; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | void XMLEventsImportContext::AddEventValues( |
147 | | const OUString& rEventName, |
148 | | const Sequence<PropertyValue> & rValues ) |
149 | 7 | { |
150 | | // if we already have the events, set them; else just collect |
151 | 7 | if (m_xEvents.is()) |
152 | 7 | { |
153 | | // set event (if name is known) |
154 | 7 | if (m_xEvents->hasByName(rEventName)) |
155 | 7 | { |
156 | 7 | try |
157 | 7 | { |
158 | 7 | m_xEvents->replaceByName(rEventName, Any(rValues)); |
159 | 7 | } catch ( const IllegalArgumentException & rException ) |
160 | 7 | { |
161 | 0 | Sequence<OUString> aMsgParams { rEventName }; |
162 | |
|
163 | 0 | GetImport().SetError(XMLERROR_FLAG_ERROR | |
164 | 0 | XMLERROR_ILLEGAL_EVENT, |
165 | 0 | aMsgParams, rException.Message, nullptr); |
166 | 0 | } |
167 | 7 | } |
168 | 7 | } |
169 | 0 | else |
170 | 0 | { |
171 | 0 | m_aCollectEvents.emplace_back(rEventName, rValues); |
172 | 0 | } |
173 | 7 | } |
174 | | |
175 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |