Line data Source code
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/nmspmap.hxx>
27 : #include <xmloff/xmlnmspe.hxx>
28 : #include <xmloff/xmltoken.hxx>
29 : #include <xmloff/xmlerror.hxx>
30 :
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::xmloff::token;
33 :
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;
39 :
40 0 : TYPEINIT1(XMLEventsImportContext, SvXMLImportContext);
41 :
42 :
43 16 : XMLEventsImportContext::XMLEventsImportContext(
44 : SvXMLImport& rImport,
45 : sal_uInt16 nPrfx,
46 : const OUString& rLocalName) :
47 16 : SvXMLImportContext(rImport, nPrfx, rLocalName)
48 : {
49 16 : }
50 :
51 :
52 6 : XMLEventsImportContext::XMLEventsImportContext(
53 : SvXMLImport& rImport,
54 : sal_uInt16 nPrfx,
55 : const OUString& rLocalName,
56 : const Reference<XEventsSupplier> & xEventsSupplier) :
57 : SvXMLImportContext(rImport, nPrfx, rLocalName),
58 6 : xEvents(xEventsSupplier->getEvents())
59 : {
60 6 : }
61 :
62 :
63 0 : XMLEventsImportContext::XMLEventsImportContext(
64 : SvXMLImport& rImport,
65 : sal_uInt16 nPrfx,
66 : const OUString& rLocalName,
67 : const Reference<XNameReplace> & xNameReplace) :
68 : SvXMLImportContext(rImport, nPrfx, rLocalName),
69 0 : xEvents(xNameReplace)
70 : {
71 0 : }
72 :
73 28 : XMLEventsImportContext::~XMLEventsImportContext()
74 : {
75 : // // if, for whatever reason, the object gets destroyed prematurely,
76 : // // we need to delete the collected events
77 28 : }
78 :
79 :
80 22 : void XMLEventsImportContext::StartElement(
81 : const Reference<XAttributeList> &)
82 : {
83 : // nothing to be done
84 22 : }
85 :
86 22 : void XMLEventsImportContext::EndElement()
87 : {
88 : // nothing to be done
89 22 : }
90 :
91 22 : SvXMLImportContext* XMLEventsImportContext::CreateChildContext(
92 : sal_uInt16 p_nPrefix,
93 : const OUString& rLocalName,
94 : const Reference<XAttributeList> & xAttrList )
95 : {
96 : // a) search for script:language and script:event-name attribute
97 : // b) delegate to factory. The factory will:
98 : // 1) translate XML event name into API event name
99 : // 2) get proper event context factory from import
100 : // 3) instantiate context
101 :
102 : // a) search for script:language and script:event-name attribute
103 22 : OUString sLanguage;
104 44 : OUString sEventName;
105 22 : sal_Int16 nCount = xAttrList->getLength();
106 104 : for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
107 : {
108 82 : OUString sLocalName;
109 82 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
110 164 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
111 :
112 82 : if (XML_NAMESPACE_SCRIPT == nPrefix)
113 : {
114 50 : if (IsXMLToken(sLocalName, XML_EVENT_NAME))
115 : {
116 22 : sEventName = xAttrList->getValueByIndex(nAttr);
117 : }
118 28 : else if (IsXMLToken(sLocalName, XML_LANGUAGE))
119 : {
120 22 : sLanguage = xAttrList->getValueByIndex(nAttr);
121 : }
122 : // else: ignore -> let child context handle this
123 : }
124 : // else: ignore -> let child context handle this
125 82 : }
126 :
127 : // b) delegate to factory
128 22 : return GetImport().GetEventImport().CreateContext(
129 22 : GetImport(), p_nPrefix, rLocalName, xAttrList,
130 66 : this, sEventName, sLanguage);
131 : }
132 :
133 0 : void XMLEventsImportContext::SetEvents(
134 : const Reference<XEventsSupplier> & xEventsSupplier)
135 : {
136 0 : if (xEventsSupplier.is())
137 : {
138 0 : SetEvents(xEventsSupplier->getEvents());
139 : }
140 0 : }
141 :
142 0 : void XMLEventsImportContext::SetEvents(
143 : const Reference<XNameReplace> & xNameRepl)
144 : {
145 0 : if (xNameRepl.is())
146 : {
147 0 : xEvents = xNameRepl;
148 :
149 : // now iterate over vector and a) insert b) delete all elements
150 0 : EventsVector::iterator aEnd = aCollectEvents.end();
151 0 : for(EventsVector::iterator aIter = aCollectEvents.begin();
152 : aIter != aEnd;
153 : ++aIter)
154 : {
155 0 : AddEventValues(aIter->first, aIter->second);
156 : }
157 0 : aCollectEvents.clear();
158 : }
159 0 : }
160 :
161 0 : bool XMLEventsImportContext::GetEventSequence(
162 : const OUString& rName,
163 : Sequence<PropertyValue> & rSequence )
164 : {
165 : // search through the vector
166 : // (This shouldn't take a lot of time, since this method should only get
167 : // called if only one (or few) events are being expected)
168 :
169 : // iterate over vector until end or rName is found;
170 0 : EventsVector::iterator aIter = aCollectEvents.begin();
171 0 : while( (aIter != aCollectEvents.end()) && (aIter->first != rName) )
172 : {
173 0 : ++aIter;
174 : }
175 :
176 : // if we're not at the end, set the sequence
177 0 : if (aIter != aCollectEvents.end())
178 : {
179 0 : rSequence = aIter->second;
180 0 : return true;
181 : }
182 :
183 0 : return false;
184 : }
185 :
186 22 : void XMLEventsImportContext::AddEventValues(
187 : const OUString& rEventName,
188 : const Sequence<PropertyValue> & rValues )
189 : {
190 : // if we already have the events, set them; else just collect
191 22 : if (xEvents.is())
192 : {
193 : // set event (if name is known)
194 6 : if (xEvents->hasByName(rEventName))
195 : {
196 6 : Any aAny;
197 6 : aAny <<= rValues;
198 :
199 : try
200 : {
201 6 : xEvents->replaceByName(rEventName, aAny);
202 0 : } catch ( const IllegalArgumentException & rException )
203 : {
204 0 : Sequence<OUString> aMsgParams(1);
205 :
206 0 : aMsgParams[0] = rEventName;
207 :
208 0 : GetImport().SetError(XMLERROR_FLAG_ERROR |
209 : XMLERROR_ILLEGAL_EVENT,
210 0 : aMsgParams, rException.Message, 0);
211 6 : }
212 : }
213 : }
214 : else
215 : {
216 16 : EventNameValuesPair aPair(rEventName, rValues);
217 16 : aCollectEvents.push_back(aPair);
218 : }
219 22 : }
220 :
221 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|