merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / script / XMLEventExport.cxx
blobcf4b66225c4aed98ea3b859219893b51735fd43b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLEventExport.cxx,v $
10 * $Revision: 1.21.86.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <xmloff/XMLEventExport.hxx>
35 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #endif
39 #ifndef _COM_SUN_STAR_DOCUMENT_XEVENTSSUPPLIER_HPP
40 #include <com/sun/star/document/XEventsSupplier.hpp>
41 #endif
43 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEREPLACE_HPP
44 #include <com/sun/star/container/XNameReplace.hpp>
45 #endif
46 #include <tools/debug.hxx>
47 #include <xmloff/xmlexp.hxx>
48 #include <xmloff/xmltoken.hxx>
49 #include "xmlnmspe.hxx"
50 #include <xmloff/nmspmap.hxx>
53 using namespace ::com::sun::star::uno;
55 using std::map;
56 using ::rtl::OUString;
57 using ::com::sun::star::beans::PropertyValue;
58 using ::com::sun::star::document::XEventsSupplier;
59 using ::com::sun::star::container::XNameReplace;
60 using ::com::sun::star::container::XNameAccess;
61 using ::xmloff::token::GetXMLToken;
62 using ::xmloff::token::XML_EVENT_LISTENERS;
65 XMLEventExport::XMLEventExport(SvXMLExport& rExp,
66 const XMLEventNameTranslation* pTranslationTable) :
67 sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
68 rExport(rExp)
70 AddTranslationTable(pTranslationTable);
73 XMLEventExport::~XMLEventExport()
75 // delete all handlers
76 HandlerMap::iterator aEnd = aHandlerMap.end();
77 for( HandlerMap::iterator aIter =
78 aHandlerMap.begin();
79 aIter != aEnd;
80 aIter++ )
82 delete aIter->second;
84 aHandlerMap.clear();
87 void XMLEventExport::AddHandler( const OUString& rName,
88 XMLEventExportHandler* pHandler )
90 DBG_ASSERT(pHandler != NULL, "Need EventExportHandler");
91 if (pHandler != NULL)
93 aHandlerMap[rName] = pHandler;
97 void XMLEventExport::AddTranslationTable(
98 const XMLEventNameTranslation* pTransTable )
100 if (NULL != pTransTable)
102 // put translation table into map
103 for(const XMLEventNameTranslation* pTrans = pTransTable;
104 pTrans->sAPIName != NULL;
105 pTrans++)
107 aNameTranslationMap[OUString::createFromAscii(pTrans->sAPIName)] =
108 XMLEventName(pTrans->nPrefix, pTrans->sXMLName);
111 // else? ignore!
114 void XMLEventExport::Export( Reference<XEventsSupplier> & rSupplier,
115 sal_Bool bWhitespace)
117 if (rSupplier.is())
119 Reference<XNameAccess> xAccess(rSupplier->getEvents(), UNO_QUERY);
120 Export(xAccess, bWhitespace);
122 // else: no supplier, no export -> ignore!
125 void XMLEventExport::Export( Reference<XNameReplace> & rReplace,
126 sal_Bool bWhitespace)
128 Reference<XNameAccess> xAccess(rReplace, UNO_QUERY);
129 Export(xAccess, bWhitespace);
132 void XMLEventExport::Export( Reference<XNameAccess> & rAccess,
133 sal_Bool bWhitespace)
135 // early out if we don't actually get any events
136 if (!rAccess.is())
138 return;
141 // have we already processed an element?
142 sal_Bool bStarted = sal_False;
144 // iterate over all event types
145 Sequence<OUString> aNames = rAccess->getElementNames();
146 sal_Int32 nCount = aNames.getLength();
147 for(sal_Int32 i = 0; i < nCount; i++)
149 // translate name
150 NameMap::iterator aIter = aNameTranslationMap.find(aNames[i]);
151 if (aIter != aNameTranslationMap.end())
153 const XMLEventName& rXmlName = aIter->second;
155 // get PropertyValues for this event
156 Any aAny = rAccess->getByName( aNames[i] );
157 Sequence<PropertyValue> aValues;
158 aAny >>= aValues;
160 // now export the current event
161 ExportEvent( aValues, rXmlName, bWhitespace, bStarted );
163 #ifdef DBG_UTIL
164 else
166 // don't proceed further
167 ::rtl::OString aStr("Unknown event name:" );
168 aStr += ::rtl::OUStringToOString( aNames[i], RTL_TEXTENCODING_UTF8 );
169 DBG_ERROR( aStr.getStr() );
171 #endif
174 // close <script:events> element (if it was opened before)
175 if (bStarted)
177 EndElement(bWhitespace);
181 /// export a singular event and wirte <office:events> container
182 void XMLEventExport::ExportSingleEvent(
183 Sequence<PropertyValue>& rEventValues,
184 const OUString& rApiEventName,
185 sal_Bool bUseWhitespace )
187 // translate the name
188 NameMap::iterator aIter = aNameTranslationMap.find(rApiEventName);
189 if (aIter != aNameTranslationMap.end())
191 const XMLEventName& rXmlName = aIter->second;
193 // export the event ...
194 sal_Bool bStarted = sal_False;
195 ExportEvent( rEventValues, rXmlName, bUseWhitespace, bStarted );
197 // ... and close the container element (if necessary)
198 if (bStarted)
200 EndElement(bUseWhitespace);
203 #ifdef DBG_UTIL
204 else
206 // don't proceed further
207 ::rtl::OString aStr("Unknown event name:" );
208 aStr += ::rtl::OUStringToOString( rApiEventName, RTL_TEXTENCODING_UTF8 );
209 DBG_ERROR( aStr.getStr() );
211 #endif
215 /// export a single event
216 void XMLEventExport::ExportEvent(
217 Sequence<PropertyValue>& rEventValues,
218 const XMLEventName& rXmlEventName,
219 sal_Bool bUseWhitespace,
220 sal_Bool& rExported )
222 // search for EventType value and then delegate to EventHandler
223 sal_Int32 nValues = rEventValues.getLength();
224 const PropertyValue* pValues = rEventValues.getConstArray();
226 for(sal_Int32 nVal = 0; nVal < nValues; nVal++)
228 if (sEventType.equals(pValues[nVal].Name))
230 // found! Now find handler and delegate
231 OUString sType;
232 pValues[nVal].Value >>= sType;
234 if (aHandlerMap.count(sType))
236 if (! rExported)
238 // OK, we have't yet exported the enclosing
239 // element. So we do that now.
240 rExported = sal_True;
241 StartElement(bUseWhitespace);
244 OUString aEventQName(
245 rExport.GetNamespaceMap().GetQNameByKey(
246 rXmlEventName.m_nPrefix, rXmlEventName.m_aName ) );
248 // delegate to proper ExportEventHandler
249 aHandlerMap[sType]->Export(rExport, aEventQName,
250 rEventValues, bUseWhitespace);
252 else
254 if (! sType.equalsAsciiL("None", sizeof("None")-1))
256 DBG_ERROR("unknown event type returned by API");
257 // unknown type -> error (ignore)
259 // else: we ignore None fields
262 // early out: we don't need to look for another type
263 break;
265 // else: we only care for EventType -> ignore
270 void XMLEventExport::StartElement(sal_Bool bWhitespace)
272 if (bWhitespace)
274 rExport.IgnorableWhitespace();
276 rExport.StartElement( XML_NAMESPACE_OFFICE, XML_EVENT_LISTENERS,
277 bWhitespace);
280 void XMLEventExport::EndElement(sal_Bool bWhitespace)
282 rExport.EndElement(XML_NAMESPACE_OFFICE, XML_EVENT_LISTENERS, bWhitespace);
283 if (bWhitespace)
285 rExport.IgnorableWhitespace();
290 // implement aStandardEventTable (defined in xmlevent.hxx)
291 const XMLEventNameTranslation aStandardEventTable[] =
293 { "OnSelect", XML_NAMESPACE_DOM, "select" }, // "on-select"
294 { "OnInsertStart", XML_NAMESPACE_OFFICE, "insert-start" }, // "on-insert-start"
295 { "OnInsertDone", XML_NAMESPACE_OFFICE, "insert-done" }, // "on-insert-done"
296 { "OnMailMerge", XML_NAMESPACE_OFFICE, "mail-merge" }, // "on-mail-merge"
297 { "OnAlphaCharInput", XML_NAMESPACE_OFFICE, "alpha-char-input" }, // "on-alpha-char-input"
298 { "OnNonAlphaCharInput", XML_NAMESPACE_OFFICE, "non-alpha-char-input" }, // "on-non-alpha-char-input"
299 { "OnResize", XML_NAMESPACE_DOM, "resize" }, // "on-resize"
300 { "OnMove", XML_NAMESPACE_OFFICE, "move" }, // "on-move"
301 { "OnPageCountChange", XML_NAMESPACE_OFFICE, "page-count-change" }, // "on-page-count-change"
302 { "OnMouseOver", XML_NAMESPACE_DOM, "mouseover" }, // "on-mouse-over"
303 { "OnClick", XML_NAMESPACE_DOM, "click" }, // "on-click"
304 { "OnMouseOut", XML_NAMESPACE_DOM, "mouseout" }, // "on-mouse-out"
305 { "OnLoadError", XML_NAMESPACE_OFFICE, "load-error" }, // "on-load-error"
306 { "OnLoadCancel", XML_NAMESPACE_OFFICE, "load-cancel" }, // "on-load-cancel"
307 { "OnLoadDone", XML_NAMESPACE_OFFICE, "load-done" }, // "on-load-done"
308 { "OnLoad", XML_NAMESPACE_DOM, "load" }, // "on-load"
309 { "OnUnload", XML_NAMESPACE_DOM, "unload" }, // "on-unload"
310 { "OnStartApp", XML_NAMESPACE_OFFICE, "start-app" }, // "on-start-app"
311 { "OnCloseApp", XML_NAMESPACE_OFFICE, "close-app" }, // "on-close-app"
312 { "OnNew", XML_NAMESPACE_OFFICE, "new" }, // "on-new"
313 { "OnSave", XML_NAMESPACE_OFFICE, "save" }, // "on-save"
314 { "OnSaveAs", XML_NAMESPACE_OFFICE, "save-as" }, // "on-save-as"
315 { "OnFocus", XML_NAMESPACE_DOM, "DOMFocusIn" }, // "on-focus"
316 { "OnUnfocus", XML_NAMESPACE_DOM, "DOMFocusOut" }, // "on-unfocus"
317 { "OnPrint", XML_NAMESPACE_OFFICE, "print" }, // "on-print"
318 { "OnError", XML_NAMESPACE_DOM, "error" }, // "on-error"
319 { "OnLoadFinished", XML_NAMESPACE_OFFICE, "load-finished" }, // "on-load-finished"
320 { "OnSaveFinished", XML_NAMESPACE_OFFICE, "save-finished" }, // "on-save-finished"
321 { "OnModifyChanged", XML_NAMESPACE_OFFICE, "modify-changed" }, // "on-modify-changed"
322 { "OnPrepareUnload", XML_NAMESPACE_OFFICE, "prepare-unload" }, // "on-prepare-unload"
323 { "OnNewMail", XML_NAMESPACE_OFFICE, "new-mail" }, // "on-new-mail"
324 { "OnToggleFullscreen", XML_NAMESPACE_OFFICE, "toggle-fullscreen" }, // "on-toggle-fullscreen"
325 { "OnSaveDone", XML_NAMESPACE_OFFICE, "save-done" }, // "on-save-done"
326 { "OnSaveAsDone", XML_NAMESPACE_OFFICE, "save-as-done" }, // "on-save-as-done"
327 { "OnCopyTo", XML_NAMESPACE_OFFICE, "copy-to" },
328 { "OnCopyToDone", XML_NAMESPACE_OFFICE, "copy-to-done" },
329 { "OnViewCreated", XML_NAMESPACE_OFFICE, "view-created" },
330 { "OnPrepareViewClosing", XML_NAMESPACE_OFFICE, "prepare-view-closing" },
331 { "OnViewClosed", XML_NAMESPACE_OFFICE, "view-close" },
332 { "OnVisAreaChanged", XML_NAMESPACE_OFFICE, "visarea-changed" }, // "on-visarea-changed"
333 { "OnCreate", XML_NAMESPACE_OFFICE, "create" },
334 { "OnSaveAsFailed", XML_NAMESPACE_OFFICE, "save-as-failed" },
335 { "OnSaveFailed", XML_NAMESPACE_OFFICE, "save-failed" },
336 { "OnCopyToFailed", XML_NAMESPACE_OFFICE, "copy-to-failed" },
337 { "OnTitleChanged", XML_NAMESPACE_OFFICE, "title-changed" },
338 { "OnModeChanged", XML_NAMESPACE_OFFICE, "mode-changed" },
339 { "OnSaveTo", XML_NAMESPACE_OFFICE, "save-to" },
340 { "OnSaveToDone", XML_NAMESPACE_OFFICE, "save-to-done" },
341 { "OnSaveToFailed", XML_NAMESPACE_OFFICE, "save-to-failed" },
342 { "OnSubComponentOpened", XML_NAMESPACE_OFFICE, "subcomponent-opened" },
343 { "OnSubComponentClosed", XML_NAMESPACE_OFFICE, "subcomponent-closed" },
345 { NULL, 0, 0 }