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