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 0 : XMLEventsImportContext::XMLEventsImportContext(
44 : SvXMLImport& rImport,
45 : sal_uInt16 nPrfx,
46 : const OUString& rLocalName) :
47 0 : SvXMLImportContext(rImport, nPrfx, rLocalName)
48 : {
49 0 : }
50 :
51 :
52 0 : XMLEventsImportContext::XMLEventsImportContext(
53 : SvXMLImport& rImport,
54 : sal_uInt16 nPrfx,
55 : const OUString& rLocalName,
56 : const Reference<XEventsSupplier> & xEventsSupplier) :
57 : SvXMLImportContext(rImport, nPrfx, rLocalName),
58 0 : xEvents(xEventsSupplier->getEvents())
59 : {
60 0 : }
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 0 : XMLEventsImportContext::~XMLEventsImportContext()
74 : {
75 : // // if, for whatever reason, the object gets destroyed prematurely,
76 : // // we need to delete the collected events
77 0 : }
78 :
79 :
80 0 : void XMLEventsImportContext::StartElement(
81 : const Reference<XAttributeList> &)
82 : {
83 : // nothing to be done
84 0 : }
85 :
86 0 : void XMLEventsImportContext::EndElement()
87 : {
88 : // nothing to be done
89 0 : }
90 :
91 0 : 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 0 : OUString sLanguage;
104 0 : OUString sEventName;
105 0 : sal_Int16 nCount = xAttrList->getLength();
106 0 : for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
107 : {
108 0 : OUString sLocalName;
109 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
110 0 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
111 :
112 0 : if (XML_NAMESPACE_SCRIPT == nPrefix)
113 : {
114 0 : if (IsXMLToken(sLocalName, XML_EVENT_NAME))
115 : {
116 0 : sEventName = xAttrList->getValueByIndex(nAttr);
117 : }
118 0 : else if (IsXMLToken(sLocalName, XML_LANGUAGE))
119 : {
120 0 : sLanguage = xAttrList->getValueByIndex(nAttr);
121 : }
122 : // else: ignore -> let child context handle this
123 : }
124 : // else: ignore -> let child context handle this
125 0 : }
126 :
127 : // b) delegate to factory
128 0 : return GetImport().GetEventImport().CreateContext(
129 0 : GetImport(), p_nPrefix, rLocalName, xAttrList,
130 0 : 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 : bool bRet = (aIter != aCollectEvents.end());
178 0 : if (bRet)
179 0 : rSequence = aIter->second;
180 :
181 0 : return bRet;
182 : }
183 :
184 0 : void XMLEventsImportContext::AddEventValues(
185 : const OUString& rEventName,
186 : const Sequence<PropertyValue> & rValues )
187 : {
188 : // if we already have the events, set them; else just collect
189 0 : if (xEvents.is())
190 : {
191 : // set event (if name is known)
192 0 : if (xEvents->hasByName(rEventName))
193 : {
194 0 : Any aAny;
195 0 : aAny <<= rValues;
196 :
197 : try
198 : {
199 0 : xEvents->replaceByName(rEventName, aAny);
200 0 : } catch ( const IllegalArgumentException & rException )
201 : {
202 0 : Sequence<OUString> aMsgParams(1);
203 :
204 0 : aMsgParams[0] = rEventName;
205 :
206 0 : GetImport().SetError(XMLERROR_FLAG_ERROR |
207 : XMLERROR_ILLEGAL_EVENT,
208 0 : aMsgParams, rException.Message, 0);
209 0 : }
210 : }
211 : }
212 : else
213 : {
214 0 : EventNameValuesPair aPair(rEventName, rValues);
215 0 : aCollectEvents.push_back(aPair);
216 : }
217 0 : }
218 :
219 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|