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 
22 #include <XMLEventImportHelper.hxx>
23 
24 #include <com/sun/star/document/XEventsSupplier.hpp>
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>
31 
32 using namespace ::com::sun::star::uno;
33 using namespace ::xmloff::token;
34 
35 using ::com::sun::star::xml::sax::XAttributeList;
36 using ::com::sun::star::beans::PropertyValue;
37 using ::com::sun::star::container::XNameReplace;
38 using ::com::sun::star::document::XEventsSupplier;
39 using ::com::sun::star::lang::IllegalArgumentException;
40 
41 
43  SvXMLImportContext(rImport)
44 {
45 }
46 
47 
49  SvXMLImport& rImport,
50  const Reference<XEventsSupplier> & xEventsSupplier) :
51  SvXMLImportContext(rImport),
52  xEvents(xEventsSupplier->getEvents())
53 {
54 }
55 
56 
58  SvXMLImport& rImport,
59  const Reference<XNameReplace> & xNameReplace) :
60  SvXMLImportContext(rImport),
61  xEvents(xNameReplace)
62 {
63 }
64 
66 {
67 // // if, for whatever reason, the object gets destroyed prematurely,
68 // // we need to delete the collected events
69 }
70 
71 
72 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEventsImportContext::createFastChildContext(
73  sal_Int32 /*nElement*/,
74  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
75 {
76  // a) search for script:language and script:event-name attribute
77  // b) delegate to factory. The factory will:
78  // 1) translate XML event name into API event name
79  // 2) get proper event context factory from import
80  // 3) instantiate context
81 
82  // a) search for script:language and script:event-name attribute
83  OUString sLanguage;
84  OUString sEventName;
85  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
86  {
87  OUString sValue = aIter.toString();
88 
89  if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_EVENT_NAME))
90  {
91  sEventName = sValue;
92  }
93  else if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_LANGUAGE))
94  {
95  sLanguage = sValue;
96  // else: ignore -> let child context handle this
97  }
98  // else: ignore -> let child context handle this
99  }
100 
101  // b) delegate to factory
103  GetImport(), xAttrList, this, sEventName, sLanguage);
104 }
105 
107  const Reference<XEventsSupplier> & xEventsSupplier)
108 {
109  if (xEventsSupplier.is())
110  {
111  SetEvents(xEventsSupplier->getEvents());
112  }
113 }
114 
116  const Reference<XNameReplace> & xNameRepl)
117 {
118  if (xNameRepl.is())
119  {
120  xEvents = xNameRepl;
121 
122  // now iterate over vector and a) insert b) delete all elements
123  for(const auto& rEvent : aCollectEvents)
124  {
125  AddEventValues(rEvent.first, rEvent.second);
126  }
127  aCollectEvents.clear();
128  }
129 }
130 
132  const OUString& rName,
133  Sequence<PropertyValue> & rSequence )
134 {
135  // search through the vector
136  // (This shouldn't take a lot of time, since this method should only get
137  // called if only one (or few) events are being expected)
138 
139  auto aIter = std::find_if(aCollectEvents.begin(), aCollectEvents.end(),
140  [&rName](EventNameValuesPair& rEvent) { return rEvent.first == rName; });
141 
142  // if we're not at the end, set the sequence
143  if (aIter != aCollectEvents.end())
144  {
145  rSequence = aIter->second;
146  }
147 }
148 
150  const OUString& rEventName,
151  const Sequence<PropertyValue> & rValues )
152 {
153  // if we already have the events, set them; else just collect
154  if (xEvents.is())
155  {
156  // set event (if name is known)
157  if (xEvents->hasByName(rEventName))
158  {
159  try
160  {
161  xEvents->replaceByName(rEventName, Any(rValues));
162  } catch ( const IllegalArgumentException & rException )
163  {
164  Sequence<OUString> aMsgParams { rEventName };
165 
168  aMsgParams, rException.Message, nullptr);
169  }
170  }
171  }
172  else
173  {
174  EventNameValuesPair aPair(rEventName, rValues);
175  aCollectEvents.push_back(aPair);
176  }
177 }
178 
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
css::uno::Reference< css::container::XNameReplace > xEvents
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...
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
void AddEventValues(const OUString &rEventName, const css::uno::Sequence< css::beans::PropertyValue > &rValues)
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...
#define XMLERROR_ILLEGAL_EVENT
Definition: xmlerror.hxx:56
XMLEventImportHelper & GetEventImport()
Definition: xmlimp.cxx:1500
virtual ~XMLEventsImportContext() override
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
::std::pair< OUString, css::uno::Sequence< css::beans::PropertyValue > > EventNameValuesPair
Handling of tokens in XML:
#define XMLERROR_FLAG_ERROR
Definition: xmlerror.hxx:36
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
void SetError(sal_Int32 nId, const css::uno::Sequence< OUString > &rMsgParams, const OUString &rExceptionMessage, const css::uno::Reference< css::xml::sax::XLocator > &rLocator)
Record an error condition that occurred during import.
SvXMLImportContext * CreateContext(SvXMLImport &rImport, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, XMLEventsImportContext *rEvents, const OUString &rXmlEventName, const OUString &rLanguage)
create an appropriate import context for a particular event
std::vector< EventNameValuesPair > aCollectEvents
XMLEventsImportContext(SvXMLImport &rImport)