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/nmspmap.hxx>
28 #include <xmloff/xmlnmspe.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  SvXMLImport& rImport,
44  sal_uInt16 nPrfx,
45  const OUString& rLocalName) :
46  SvXMLImportContext(rImport, nPrfx, rLocalName)
47 {
48 }
49 
51  SvXMLImportContext(rImport)
52 {
53 }
54 
55 
57  SvXMLImport& rImport,
58  sal_uInt16 nPrfx,
59  const OUString& rLocalName,
60  const Reference<XEventsSupplier> & xEventsSupplier) :
61  SvXMLImportContext(rImport, nPrfx, rLocalName),
62  xEvents(xEventsSupplier->getEvents())
63 {
64 }
65 
67  SvXMLImport& rImport,
68  const Reference<XEventsSupplier> & xEventsSupplier) :
69  SvXMLImportContext(rImport),
70  xEvents(xEventsSupplier->getEvents())
71 {
72 }
73 
74 
76  SvXMLImport& rImport,
77  sal_uInt16 nPrfx,
78  const OUString& rLocalName,
79  const Reference<XNameReplace> & xNameReplace) :
80  SvXMLImportContext(rImport, nPrfx, rLocalName),
81  xEvents(xNameReplace)
82 {
83 }
84 
86 {
87 // // if, for whatever reason, the object gets destroyed prematurely,
88 // // we need to delete the collected events
89 }
90 
91 
93  const Reference<XAttributeList> &)
94 {
95  // nothing to be done
96 }
97 
99 {
100  // nothing to be done
101 }
102 
104  sal_uInt16 /*p_nPrefix*/,
105  const OUString& /*rLocalName*/,
106  const Reference<XAttributeList> & xAttrList )
107 {
108  // a) search for script:language and script:event-name attribute
109  // b) delegate to factory. The factory will:
110  // 1) translate XML event name into API event name
111  // 2) get proper event context factory from import
112  // 3) instantiate context
113 
114  // a) search for script:language and script:event-name attribute
115  OUString sLanguage;
116  OUString sEventName;
117  sal_Int16 nCount = xAttrList->getLength();
118  for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
119  {
120  OUString sLocalName;
121  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
122  GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
123 
124  if (XML_NAMESPACE_SCRIPT == nPrefix)
125  {
126  if (IsXMLToken(sLocalName, XML_EVENT_NAME))
127  {
128  sEventName = xAttrList->getValueByIndex(nAttr);
129  }
130  else if (IsXMLToken(sLocalName, XML_LANGUAGE))
131  {
132  sLanguage = xAttrList->getValueByIndex(nAttr);
133  }
134  // else: ignore -> let child context handle this
135  }
136  // else: ignore -> let child context handle this
137  }
138 
139  // b) delegate to factory
141  GetImport(), xAttrList, this, sEventName, sLanguage);
142 }
143 
144 void XMLEventsImportContext::startFastElement(sal_Int32 /*nElement*/,
145  const css::uno::Reference< css::xml::sax::XFastAttributeList >&)
146 {
147  // nothing to be done
148 }
149 
150 void XMLEventsImportContext::endFastElement(sal_Int32 /*nElement*/)
151 {
152  // nothing to be done
153 }
154 
155 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEventsImportContext::createFastChildContext(
156  sal_Int32 /*nElement*/,
157  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
158 {
159  // a) search for script:language and script:event-name attribute
160  // b) delegate to factory. The factory will:
161  // 1) translate XML event name into API event name
162  // 2) get proper event context factory from import
163  // 3) instantiate context
164 
165  // a) search for script:language and script:event-name attribute
166  OUString sLanguage;
167  OUString sEventName;
168  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
169  {
170  OUString sValue = aIter.toString();
171 
172  if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_EVENT_NAME))
173  {
174  sEventName = sValue;
175  }
176  else if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_EVENT_NAME))
177  {
178  sLanguage = sValue;
179  // else: ignore -> let child context handle this
180  }
181  // else: ignore -> let child context handle this
182  }
183 
185  const Sequence< css::xml::FastAttribute > fastAttribs = xAttrList->getFastAttributes();
186  for (const auto& rAttr : fastAttribs)
187  {
188  const OUString& rAttrValue = rAttr.Value;
189  sal_Int32 nToken = rAttr.Token;
190  const OUString& rAttrNamespacePrefix = SvXMLImport::getNamespacePrefixFromToken( nToken, nullptr );
191  OUString sAttrName = SvXMLImport::getNameFromToken( nToken );
192  if ( !rAttrNamespacePrefix.isEmpty() )
193  sAttrName = rAttrNamespacePrefix + ":" + sAttrName;
194  rAttrList->AddAttribute( sAttrName, "CDATA", rAttrValue );
195  }
196 
197  // b) delegate to factory
199  GetImport(), rAttrList.get(), this, sEventName, sLanguage);
200 }
201 
203  const Reference<XEventsSupplier> & xEventsSupplier)
204 {
205  if (xEventsSupplier.is())
206  {
207  SetEvents(xEventsSupplier->getEvents());
208  }
209 }
210 
212  const Reference<XNameReplace> & xNameRepl)
213 {
214  if (xNameRepl.is())
215  {
216  xEvents = xNameRepl;
217 
218  // now iterate over vector and a) insert b) delete all elements
219  for(const auto& rEvent : aCollectEvents)
220  {
221  AddEventValues(rEvent.first, rEvent.second);
222  }
223  aCollectEvents.clear();
224  }
225 }
226 
228  const OUString& rName,
229  Sequence<PropertyValue> & rSequence )
230 {
231  // search through the vector
232  // (This shouldn't take a lot of time, since this method should only get
233  // called if only one (or few) events are being expected)
234 
235  auto aIter = std::find_if(aCollectEvents.begin(), aCollectEvents.end(),
236  [&rName](EventNameValuesPair& rEvent) { return rEvent.first == rName; });
237 
238  // if we're not at the end, set the sequence
239  if (aIter != aCollectEvents.end())
240  {
241  rSequence = aIter->second;
242  }
243 }
244 
246  const OUString& rEventName,
247  const Sequence<PropertyValue> & rValues )
248 {
249  // if we already have the events, set them; else just collect
250  if (xEvents.is())
251  {
252  // set event (if name is known)
253  if (xEvents->hasByName(rEventName))
254  {
255  try
256  {
257  xEvents->replaceByName(rEventName, Any(rValues));
258  } catch ( const IllegalArgumentException & rException )
259  {
260  Sequence<OUString> aMsgParams { rEventName };
261 
264  aMsgParams, rException.Message, nullptr);
265  }
266  }
267  }
268  else
269  {
270  EventNameValuesPair aPair(rEventName, rValues);
271  aCollectEvents.push_back(aPair);
272  }
273 }
274 
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
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...
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:397
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
SvXMLImportContext * CreateContext(SvXMLImport &rImport, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, XMLEventsImportContext *rEvents, const OUString &rXmlEventName, const OUString &rLanguage)
create an appropriate import context for a particular event
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
void AddEventValues(const OUString &rEventName, const css::uno::Sequence< css::beans::PropertyValue > &rValues)
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
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...
int nCount
#define XMLERROR_ILLEGAL_EVENT
Definition: xmlerror.hxx:56
XMLEventImportHelper & GetEventImport()
Definition: xmlimp.cxx:1585
virtual ~XMLEventsImportContext() override
static OUString getNamespacePrefixFromToken(sal_Int32 nToken, const SvXMLNamespaceMap *pMap)
Definition: xmlimp.cxx:2030
XMLEventsImportContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLocalName)
static const OUString & getNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:2015
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
DefTokenId nToken
::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:94
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.
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &) override
constexpr sal_uInt16 XML_NAMESPACE_SCRIPT
Definition: xmlnmspe.hxx:45