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 8 : XMLEventsImportContext::XMLEventsImportContext(
44 : SvXMLImport& rImport,
45 : sal_uInt16 nPrfx,
46 : const OUString& rLocalName) :
47 8 : SvXMLImportContext(rImport, nPrfx, rLocalName)
48 : {
49 8 : }
50 :
51 :
52 5 : XMLEventsImportContext::XMLEventsImportContext(
53 : SvXMLImport& rImport,
54 : sal_uInt16 nPrfx,
55 : const OUString& rLocalName,
56 : const Reference<XEventsSupplier> & xEventsSupplier) :
57 : SvXMLImportContext(rImport, nPrfx, rLocalName),
58 5 : xEvents(xEventsSupplier->getEvents())
59 : {
60 5 : }
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 18 : XMLEventsImportContext::~XMLEventsImportContext()
74 : {
75 : // // if, for whatever reason, the object gets destroyed prematurely,
76 : // // we need to delete the collected events
77 18 : }
78 :
79 :
80 13 : void XMLEventsImportContext::StartElement(
81 : const Reference<XAttributeList> &)
82 : {
83 : // nothing to be done
84 13 : }
85 :
86 13 : void XMLEventsImportContext::EndElement()
87 : {
88 : // nothing to be done
89 13 : }
90 :
91 35 : 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 35 : OUString sLanguage;
104 70 : OUString sEventName;
105 35 : sal_Int16 nCount = xAttrList->getLength();
106 149 : for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
107 : {
108 114 : OUString sLocalName;
109 114 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
110 228 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
111 :
112 114 : if (XML_NAMESPACE_SCRIPT == nPrefix)
113 : {
114 73 : if (IsXMLToken(sLocalName, XML_EVENT_NAME))
115 : {
116 35 : sEventName = xAttrList->getValueByIndex(nAttr);
117 : }
118 38 : else if (IsXMLToken(sLocalName, XML_LANGUAGE))
119 : {
120 35 : sLanguage = xAttrList->getValueByIndex(nAttr);
121 : }
122 : // else: ignore -> let child context handle this
123 : }
124 : // else: ignore -> let child context handle this
125 114 : }
126 :
127 : // b) delegate to factory
128 35 : return GetImport().GetEventImport().CreateContext(
129 35 : GetImport(), p_nPrefix, rLocalName, xAttrList,
130 105 : 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 35 : 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 35 : if (xEvents.is())
192 : {
193 : // set event (if name is known)
194 27 : if (xEvents->hasByName(rEventName))
195 : {
196 27 : Any aAny;
197 27 : aAny <<= rValues;
198 :
199 : try
200 : {
201 27 : 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 27 : }
212 : }
213 : }
214 : else
215 : {
216 8 : EventNameValuesPair aPair(rEventName, rValues);
217 8 : aCollectEvents.push_back(aPair);
218 : }
219 35 : }
220 :
221 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|