cURL: follow redirects
[LibreOffice.git] / xmloff / source / script / XMLEventsImportContext.cxx
blob07ffa2f048b023aab87f304e92f4a0201ddc1f49
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <xmloff/XMLEventsImportContext.hxx>
22 #include "XMLEventImportHelper.hxx"
24 #include <com/sun/star/document/XEventsSupplier.hpp>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/nmspmap.hxx>
27 #include <xmloff/xmlnmspe.hxx>
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/xmlerror.hxx>
31 using namespace ::com::sun::star::uno;
32 using namespace ::xmloff::token;
34 using ::com::sun::star::xml::sax::XAttributeList;
35 using ::com::sun::star::beans::PropertyValue;
36 using ::com::sun::star::container::XNameReplace;
37 using ::com::sun::star::document::XEventsSupplier;
38 using ::com::sun::star::lang::IllegalArgumentException;
41 XMLEventsImportContext::XMLEventsImportContext(
42 SvXMLImport& rImport,
43 sal_uInt16 nPrfx,
44 const OUString& rLocalName) :
45 SvXMLImportContext(rImport, nPrfx, rLocalName)
50 XMLEventsImportContext::XMLEventsImportContext(
51 SvXMLImport& rImport,
52 sal_uInt16 nPrfx,
53 const OUString& rLocalName,
54 const Reference<XEventsSupplier> & xEventsSupplier) :
55 SvXMLImportContext(rImport, nPrfx, rLocalName),
56 xEvents(xEventsSupplier->getEvents())
61 XMLEventsImportContext::XMLEventsImportContext(
62 SvXMLImport& rImport,
63 sal_uInt16 nPrfx,
64 const OUString& rLocalName,
65 const Reference<XNameReplace> & xNameReplace) :
66 SvXMLImportContext(rImport, nPrfx, rLocalName),
67 xEvents(xNameReplace)
71 XMLEventsImportContext::~XMLEventsImportContext()
73 // // if, for whatever reason, the object gets destroyed prematurely,
74 // // we need to delete the collected events
78 void XMLEventsImportContext::StartElement(
79 const Reference<XAttributeList> &)
81 // nothing to be done
84 void XMLEventsImportContext::EndElement()
86 // nothing to be done
89 SvXMLImportContext* XMLEventsImportContext::CreateChildContext(
90 sal_uInt16 p_nPrefix,
91 const OUString& rLocalName,
92 const Reference<XAttributeList> & xAttrList )
94 // a) search for script:language and script:event-name attribute
95 // b) delegate to factory. The factory will:
96 // 1) translate XML event name into API event name
97 // 2) get proper event context factory from import
98 // 3) instantiate context
100 // a) search for script:language and script:event-name attribute
101 OUString sLanguage;
102 OUString sEventName;
103 sal_Int16 nCount = xAttrList->getLength();
104 for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
106 OUString sLocalName;
107 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
108 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
110 if (XML_NAMESPACE_SCRIPT == nPrefix)
112 if (IsXMLToken(sLocalName, XML_EVENT_NAME))
114 sEventName = xAttrList->getValueByIndex(nAttr);
116 else if (IsXMLToken(sLocalName, XML_LANGUAGE))
118 sLanguage = xAttrList->getValueByIndex(nAttr);
120 // else: ignore -> let child context handle this
122 // else: ignore -> let child context handle this
125 // b) delegate to factory
126 return GetImport().GetEventImport().CreateContext(
127 GetImport(), p_nPrefix, rLocalName, xAttrList,
128 this, sEventName, sLanguage);
131 void XMLEventsImportContext::SetEvents(
132 const Reference<XEventsSupplier> & xEventsSupplier)
134 if (xEventsSupplier.is())
136 SetEvents(xEventsSupplier->getEvents());
140 void XMLEventsImportContext::SetEvents(
141 const Reference<XNameReplace> & xNameRepl)
143 if (xNameRepl.is())
145 xEvents = xNameRepl;
147 // now iterate over vector and a) insert b) delete all elements
148 EventsVector::iterator aEnd = aCollectEvents.end();
149 for(EventsVector::iterator aIter = aCollectEvents.begin();
150 aIter != aEnd;
151 ++aIter)
153 AddEventValues(aIter->first, aIter->second);
155 aCollectEvents.clear();
159 void XMLEventsImportContext::GetEventSequence(
160 const OUString& rName,
161 Sequence<PropertyValue> & rSequence )
163 // search through the vector
164 // (This shouldn't take a lot of time, since this method should only get
165 // called if only one (or few) events are being expected)
167 // iterate over vector until end or rName is found;
168 EventsVector::iterator aIter = aCollectEvents.begin();
169 while( (aIter != aCollectEvents.end()) && (aIter->first != rName) )
171 ++aIter;
174 // if we're not at the end, set the sequence
175 if (aIter != aCollectEvents.end())
177 rSequence = aIter->second;
181 void XMLEventsImportContext::AddEventValues(
182 const OUString& rEventName,
183 const Sequence<PropertyValue> & rValues )
185 // if we already have the events, set them; else just collect
186 if (xEvents.is())
188 // set event (if name is known)
189 if (xEvents->hasByName(rEventName))
193 xEvents->replaceByName(rEventName, Any(rValues));
194 } catch ( const IllegalArgumentException & rException )
196 Sequence<OUString> aMsgParams { rEventName };
198 GetImport().SetError(XMLERROR_FLAG_ERROR |
199 XMLERROR_ILLEGAL_EVENT,
200 aMsgParams, rException.Message, nullptr);
204 else
206 EventNameValuesPair aPair(rEventName, rValues);
207 aCollectEvents.push_back(aPair);
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */