Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <xmloff/XMLEventsImportContext.hxx>
30 : :
31 : : #include "XMLEventImportHelper.hxx"
32 : :
33 : : #include <com/sun/star/document/XEventsSupplier.hpp>
34 : : #include <xmloff/xmlimp.hxx>
35 : : #include <xmloff/nmspmap.hxx>
36 : : #include "xmloff/xmlnmspe.hxx"
37 : : #include <xmloff/xmltoken.hxx>
38 : : #include "xmloff/xmlerror.hxx"
39 : :
40 : : using namespace ::com::sun::star::uno;
41 : : using namespace ::xmloff::token;
42 : :
43 : : using ::rtl::OUString;
44 : : using ::com::sun::star::xml::sax::XAttributeList;
45 : : using ::com::sun::star::beans::PropertyValue;
46 : : using ::com::sun::star::container::XNameReplace;
47 : : using ::com::sun::star::document::XEventsSupplier;
48 : : using ::com::sun::star::lang::IllegalArgumentException;
49 : :
50 [ # # ][ # # ]: 0 : TYPEINIT1(XMLEventsImportContext, SvXMLImportContext);
51 : :
52 : :
53 : 14 : XMLEventsImportContext::XMLEventsImportContext(
54 : : SvXMLImport& rImport,
55 : : sal_uInt16 nPrfx,
56 : : const OUString& rLocalName) :
57 [ + - ]: 14 : SvXMLImportContext(rImport, nPrfx, rLocalName)
58 : : {
59 : 14 : }
60 : :
61 : :
62 : 0 : XMLEventsImportContext::XMLEventsImportContext(
63 : : SvXMLImport& rImport,
64 : : sal_uInt16 nPrfx,
65 : : const OUString& rLocalName,
66 : : const Reference<XEventsSupplier> & xEventsSupplier) :
67 : : SvXMLImportContext(rImport, nPrfx, rLocalName),
68 [ # # ][ # # ]: 0 : xEvents(xEventsSupplier->getEvents())
[ # # ]
69 : : {
70 : 0 : }
71 : :
72 : :
73 : 0 : XMLEventsImportContext::XMLEventsImportContext(
74 : : SvXMLImport& rImport,
75 : : sal_uInt16 nPrfx,
76 : : const OUString& rLocalName,
77 : : const Reference<XNameReplace> & xNameReplace) :
78 : : SvXMLImportContext(rImport, nPrfx, rLocalName),
79 [ # # ]: 0 : xEvents(xNameReplace)
80 : : {
81 : 0 : }
82 : :
83 : 14 : XMLEventsImportContext::~XMLEventsImportContext()
84 : : {
85 : : // // if, for whatever reason, the object gets destroyed prematurely,
86 : : // // we need to delete the collected events
87 [ - + ]: 14 : }
88 : :
89 : :
90 : 14 : void XMLEventsImportContext::StartElement(
91 : : const Reference<XAttributeList> &)
92 : : {
93 : : // nothing to be done
94 : 14 : }
95 : :
96 : 14 : void XMLEventsImportContext::EndElement()
97 : : {
98 : : // nothing to be done
99 : 14 : }
100 : :
101 : 14 : SvXMLImportContext* XMLEventsImportContext::CreateChildContext(
102 : : sal_uInt16 p_nPrefix,
103 : : const OUString& rLocalName,
104 : : const Reference<XAttributeList> & xAttrList )
105 : : {
106 : : // a) search for script:language and script:event-name attribute
107 : : // b) delegate to factory. The factory will:
108 : : // 1) translate XML event name into API event name
109 : : // 2) get proper event context factory from import
110 : : // 3) instantiate context
111 : :
112 : : // a) search for script:language and script:event-name attribute
113 : 14 : OUString sLanguage;
114 : 14 : OUString sEventName;
115 [ + - ][ + - ]: 14 : sal_Int16 nCount = xAttrList->getLength();
116 [ + + ]: 56 : for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
117 : : {
118 : 42 : OUString sLocalName;
119 : 42 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
120 [ + - + - ]: 84 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName );
[ + - ]
121 : :
122 [ + + ]: 42 : if (XML_NAMESPACE_SCRIPT == nPrefix)
123 : : {
124 [ + - ][ + + ]: 34 : if (IsXMLToken(sLocalName, XML_EVENT_NAME))
125 : : {
126 [ + - ][ + - ]: 14 : sEventName = xAttrList->getValueByIndex(nAttr);
127 : : }
128 [ + - ][ + + ]: 20 : else if (IsXMLToken(sLocalName, XML_LANGUAGE))
129 : : {
130 [ + - ][ + - ]: 14 : sLanguage = xAttrList->getValueByIndex(nAttr);
131 : : }
132 : : // else: ignore -> let child context handle this
133 : : }
134 : : // else: ignore -> let child context handle this
135 : 42 : }
136 : :
137 : : // b) delegate to factory
138 [ + - ]: 14 : return GetImport().GetEventImport().CreateContext(
139 : 14 : GetImport(), p_nPrefix, rLocalName, xAttrList,
140 [ + - ]: 14 : this, sEventName, sLanguage);
141 : : }
142 : :
143 : 0 : void XMLEventsImportContext::SetEvents(
144 : : const Reference<XEventsSupplier> & xEventsSupplier)
145 : : {
146 [ # # ]: 0 : if (xEventsSupplier.is())
147 : : {
148 [ # # ]: 0 : SetEvents(xEventsSupplier->getEvents());
149 : : }
150 : 0 : }
151 : :
152 : 0 : void XMLEventsImportContext::SetEvents(
153 : : const Reference<XNameReplace> & xNameRepl)
154 : : {
155 [ # # ]: 0 : if (xNameRepl.is())
156 : : {
157 [ # # ]: 0 : xEvents = xNameRepl;
158 : :
159 : : // now iterate over vector and a) insert b) delete all elements
160 : 0 : EventsVector::iterator aEnd = aCollectEvents.end();
161 [ # # ][ # # ]: 0 : for(EventsVector::iterator aIter = aCollectEvents.begin();
162 : : aIter != aEnd;
163 : : ++aIter)
164 : : {
165 [ # # ]: 0 : AddEventValues(aIter->first, aIter->second);
166 : : }
167 : 0 : aCollectEvents.clear();
168 : : }
169 : 0 : }
170 : :
171 : 0 : sal_Bool XMLEventsImportContext::GetEventSequence(
172 : : const OUString& rName,
173 : : Sequence<PropertyValue> & rSequence )
174 : : {
175 : : // search through the vector
176 : : // (This shouldn't take a lot of time, since this method should only get
177 : : // called if only one (or few) events are being expected)
178 : :
179 : : // iterate over vector until end or rName is found;
180 : 0 : EventsVector::iterator aIter = aCollectEvents.begin();
181 [ # # ][ # # ]: 0 : while( (aIter != aCollectEvents.end()) && (aIter->first != rName) )
[ # # ][ # # ]
[ # # # # ]
182 : : {
183 : 0 : ++aIter;
184 : : }
185 : :
186 : : // if we're not at the end, set the sequence
187 [ # # ]: 0 : sal_Bool bRet = (aIter != aCollectEvents.end());
188 [ # # ]: 0 : if (bRet)
189 [ # # ]: 0 : rSequence = aIter->second;
190 : :
191 : 0 : return bRet;
192 : : }
193 : :
194 : 14 : void XMLEventsImportContext::AddEventValues(
195 : : const OUString& rEventName,
196 : : const Sequence<PropertyValue> & rValues )
197 : : {
198 : : // if we already have the events, set them; else just collect
199 [ - + ]: 14 : if (xEvents.is())
200 : : {
201 : : // set event (if name is known)
202 [ # # ]: 0 : if (xEvents->hasByName(rEventName))
203 : : {
204 : 0 : Any aAny;
205 [ # # ]: 0 : aAny <<= rValues;
206 : :
207 : : try
208 : : {
209 [ # # ][ # # ]: 0 : xEvents->replaceByName(rEventName, aAny);
210 [ # # # # ]: 0 : } catch ( const IllegalArgumentException & rException )
211 : : {
212 [ # # ]: 0 : Sequence<OUString> aMsgParams(1);
213 : :
214 [ # # ]: 0 : aMsgParams[0] = rEventName;
215 : :
216 : 0 : GetImport().SetError(XMLERROR_FLAG_ERROR |
217 : : XMLERROR_ILLEGAL_EVENT,
218 [ # # # # : 0 : aMsgParams, rException.Message, 0);
# # ]
219 : 0 : }
220 : : }
221 : : }
222 : : else
223 : : {
224 [ + - ]: 14 : EventNameValuesPair aPair(rEventName, rValues);
225 [ + - ][ + - ]: 14 : aCollectEvents.push_back(aPair);
226 : : }
227 : 14 : }
228 : :
229 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|