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