diff options
Diffstat (limited to 'xmloff/source/script')
-rw-r--r-- | xmloff/source/script/XMLEventExport.cxx | 320 | ||||
-rw-r--r-- | xmloff/source/script/XMLEventImportHelper.cxx | 156 | ||||
-rw-r--r-- | xmloff/source/script/XMLEventsImportContext.cxx | 275 | ||||
-rw-r--r-- | xmloff/source/script/XMLScriptContextFactory.cxx | 89 | ||||
-rw-r--r-- | xmloff/source/script/XMLScriptExportHandler.cxx | 75 | ||||
-rw-r--r-- | xmloff/source/script/XMLStarBasicContextFactory.cxx | 114 | ||||
-rw-r--r-- | xmloff/source/script/XMLStarBasicExportHandler.cxx | 95 | ||||
-rw-r--r-- | xmloff/source/script/xmlbasicscript.cxx | 320 | ||||
-rw-r--r-- | xmloff/source/script/xmlbasicscript.hxx | 115 | ||||
-rw-r--r-- | xmloff/source/script/xmlscripti.cxx | 131 |
10 files changed, 1690 insertions, 0 deletions
diff --git a/xmloff/source/script/XMLEventExport.cxx b/xmloff/source/script/XMLEventExport.cxx new file mode 100644 index 000000000..d9b2686e2 --- /dev/null +++ b/xmloff/source/script/XMLEventExport.cxx @@ -0,0 +1,320 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <xmloff/XMLEventExport.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> + +#include <com/sun/star/document/XEventsSupplier.hpp> + +#include <com/sun/star/container/XNameReplace.hpp> +#include <sal/log.hxx> +#include <osl/diagnose.h> +#include <xmloff/xmlexp.hxx> +#include <xmloff/xmltoken.hxx> +#include <xmloff/xmlnmspe.hxx> +#include <xmloff/nmspmap.hxx> + + +using namespace ::com::sun::star::uno; + +using ::com::sun::star::beans::PropertyValue; +using ::com::sun::star::document::XEventsSupplier; +using ::com::sun::star::container::XNameReplace; +using ::com::sun::star::container::XNameAccess; +using ::xmloff::token::XML_EVENT_LISTENERS; + +static const OUStringLiteral gsEventType("EventType"); + +XMLEventExport::XMLEventExport(SvXMLExport& rExp) : + rExport(rExp), + bExtNamespace(false) +{ +} + +XMLEventExport::~XMLEventExport() +{ + // delete all handlers + aHandlerMap.clear(); +} + +void XMLEventExport::AddHandler( const OUString& rName, + std::unique_ptr<XMLEventExportHandler> pHandler ) +{ + assert(pHandler); + aHandlerMap[rName] = std::move(pHandler); +} + +void XMLEventExport::AddTranslationTable( + const XMLEventNameTranslation* pTransTable ) +{ + if (nullptr != pTransTable) + { + // put translation table into map + for(const XMLEventNameTranslation* pTrans = pTransTable; + pTrans->sAPIName != nullptr; + pTrans++) + { + aNameTranslationMap[OUString::createFromAscii(pTrans->sAPIName)] = + XMLEventName(pTrans->nPrefix, pTrans->sXMLName); + } + } + // else? ignore! +} + +void XMLEventExport::Export( Reference<XEventsSupplier> const & rSupplier, + bool bWhitespace) +{ + if (rSupplier.is()) + { + Export(rSupplier->getEvents(), bWhitespace); + } + // else: no supplier, no export -> ignore! +} + +void XMLEventExport::Export( Reference<XNameReplace> const & rReplace, + bool bWhitespace) +{ + Reference<XNameAccess> xAccess(rReplace); + Export(xAccess, bWhitespace); +} + +void XMLEventExport::Export( Reference<XNameAccess> const & rAccess, + bool bWhitespace) +{ + // early out if we don't actually get any events + if (!rAccess.is()) + { + return; + } + + // have we already processed an element? + bool bStarted = false; + + // iterate over all event types + const Sequence<OUString> aNames = rAccess->getElementNames(); + for(const auto& rName : aNames) + { + // translate name + NameMap::iterator aIter = aNameTranslationMap.find(rName); + if (aIter != aNameTranslationMap.end()) + { + const XMLEventName& rXmlName = aIter->second; + + // get PropertyValues for this event + Any aAny = rAccess->getByName( rName ); + Sequence<PropertyValue> aValues; + aAny >>= aValues; + + // now export the current event + ExportEvent( aValues, rXmlName, bWhitespace, bStarted ); + } + else + { + // don't proceed further + SAL_WARN("xmloff", "Unknown event name:" << rName ); + } + } + + // close <script:events> element (if it was opened before) + if (bStarted) + { + EndElement(bWhitespace); + } +} + +void XMLEventExport::ExportExt( Reference<XNameAccess> const & rAccess ) +{ + // set bExtNamespace flag to use XML_NAMESPACE_OFFICE_EXT namespace + // for events element (not for child elements) + bExtNamespace = true; + Export(rAccess); + bExtNamespace = false; // reset for future Export calls +} + +/// export a singular event and write <office:events> container +void XMLEventExport::ExportSingleEvent( + Sequence<PropertyValue>& rEventValues, + const OUString& rApiEventName, + bool bUseWhitespace ) +{ + // translate the name + NameMap::iterator aIter = aNameTranslationMap.find(rApiEventName); + if (aIter != aNameTranslationMap.end()) + { + const XMLEventName& rXmlName = aIter->second; + + // export the event ... + bool bStarted = false; + ExportEvent( rEventValues, rXmlName, bUseWhitespace, bStarted ); + + // ... and close the container element (if necessary) + if (bStarted) + { + EndElement(bUseWhitespace); + } + } + else + { + // don't proceed further + SAL_WARN("xmloff", "Unknown event name:" << rApiEventName ); + } +} + + +/// export a single event +void XMLEventExport::ExportEvent( + Sequence<PropertyValue>& rEventValues, + const XMLEventName& rXmlEventName, + bool bUseWhitespace, + bool& rExported ) +{ + // search for EventType value and then delegate to EventHandler + const PropertyValue* pValue = std::find_if(rEventValues.begin(), rEventValues.end(), + [](const PropertyValue& rValue) { return gsEventType == rValue.Name; }); + + if (pValue != rEventValues.end()) + { + // found! Now find handler and delegate + OUString sType; + pValue->Value >>= sType; + + if (aHandlerMap.count(sType)) + { + if (! rExported) + { + // OK, we have't yet exported the enclosing + // element. So we do that now. + rExported = true; + StartElement(bUseWhitespace); + } + + OUString aEventQName( + rExport.GetNamespaceMap().GetQNameByKey( + rXmlEventName.m_nPrefix, rXmlEventName.m_aName ) ); + + // delegate to proper ExportEventHandler + aHandlerMap[sType]->Export(rExport, aEventQName, + rEventValues, bUseWhitespace); + } + else + { + if ( sType != "None" ) + { + OSL_FAIL("unknown event type returned by API"); + // unknown type -> error (ignore) + } + // else: we ignore None fields + } + } +} + + +void XMLEventExport::StartElement(bool bWhitespace) +{ + if (bWhitespace) + { + rExport.IgnorableWhitespace(); + } + sal_uInt16 nNamespace = bExtNamespace ? XML_NAMESPACE_OFFICE_EXT + : XML_NAMESPACE_OFFICE; + rExport.StartElement( nNamespace, XML_EVENT_LISTENERS, + bWhitespace); +} + +void XMLEventExport::EndElement(bool bWhitespace) +{ + sal_uInt16 nNamespace = bExtNamespace ? XML_NAMESPACE_OFFICE_EXT + : XML_NAMESPACE_OFFICE; + rExport.EndElement(nNamespace, XML_EVENT_LISTENERS, bWhitespace); + if (bWhitespace) + { + rExport.IgnorableWhitespace(); + } +} + + +// implement aStandardEventTable (defined in xmlevent.hxx) +const XMLEventNameTranslation aStandardEventTable[] = +{ + { "OnSelect", XML_NAMESPACE_DOM, "select" }, // "on-select" + { "OnInsertStart", XML_NAMESPACE_OFFICE, "insert-start" }, // "on-insert-start" + { "OnInsertDone", XML_NAMESPACE_OFFICE, "insert-done" }, // "on-insert-done" + { "OnMailMerge", XML_NAMESPACE_OFFICE, "mail-merge" }, // "on-mail-merge" + { "OnAlphaCharInput", XML_NAMESPACE_OFFICE, "alpha-char-input" }, // "on-alpha-char-input" + { "OnNonAlphaCharInput", XML_NAMESPACE_OFFICE, "non-alpha-char-input" }, // "on-non-alpha-char-input" + { "OnResize", XML_NAMESPACE_DOM, "resize" }, // "on-resize" + { "OnMove", XML_NAMESPACE_OFFICE, "move" }, // "on-move" + { "OnPageCountChange", XML_NAMESPACE_OFFICE, "page-count-change" }, // "on-page-count-change" + { "OnMouseOver", XML_NAMESPACE_DOM, "mouseover" }, // "on-mouse-over" + { "OnClick", XML_NAMESPACE_DOM, "click" }, // "on-click" + { "OnMouseOut", XML_NAMESPACE_DOM, "mouseout" }, // "on-mouse-out" + { "OnLoadError", XML_NAMESPACE_OFFICE, "load-error" }, // "on-load-error" + { "OnLoadCancel", XML_NAMESPACE_OFFICE, "load-cancel" }, // "on-load-cancel" + { "OnLoadDone", XML_NAMESPACE_OFFICE, "load-done" }, // "on-load-done" + { "OnLoad", XML_NAMESPACE_DOM, "load" }, // "on-load" + { "OnUnload", XML_NAMESPACE_DOM, "unload" }, // "on-unload" + { "OnStartApp", XML_NAMESPACE_OFFICE, "start-app" }, // "on-start-app" + { "OnCloseApp", XML_NAMESPACE_OFFICE, "close-app" }, // "on-close-app" + { "OnNew", XML_NAMESPACE_OFFICE, "new" }, // "on-new" + { "OnSave", XML_NAMESPACE_OFFICE, "save" }, // "on-save" + { "OnSaveAs", XML_NAMESPACE_OFFICE, "save-as" }, // "on-save-as" + { "OnFocus", XML_NAMESPACE_DOM, "DOMFocusIn" }, // "on-focus" + { "OnUnfocus", XML_NAMESPACE_DOM, "DOMFocusOut" }, // "on-unfocus" + { "OnPrint", XML_NAMESPACE_OFFICE, "print" }, // "on-print" + { "OnError", XML_NAMESPACE_DOM, "error" }, // "on-error" + { "OnLoadFinished", XML_NAMESPACE_OFFICE, "load-finished" }, // "on-load-finished" + { "OnSaveFinished", XML_NAMESPACE_OFFICE, "save-finished" }, // "on-save-finished" + { "OnModifyChanged", XML_NAMESPACE_OFFICE, "modify-changed" }, // "on-modify-changed" + { "OnPrepareUnload", XML_NAMESPACE_OFFICE, "prepare-unload" }, // "on-prepare-unload" + { "OnNewMail", XML_NAMESPACE_OFFICE, "new-mail" }, // "on-new-mail" + { "OnToggleFullscreen", XML_NAMESPACE_OFFICE, "toggle-fullscreen" }, // "on-toggle-fullscreen" + { "OnSaveDone", XML_NAMESPACE_OFFICE, "save-done" }, // "on-save-done" + { "OnSaveAsDone", XML_NAMESPACE_OFFICE, "save-as-done" }, // "on-save-as-done" + { "OnCopyTo", XML_NAMESPACE_OFFICE, "copy-to" }, + { "OnCopyToDone", XML_NAMESPACE_OFFICE, "copy-to-done" }, + { "OnViewCreated", XML_NAMESPACE_OFFICE, "view-created" }, + { "OnPrepareViewClosing", XML_NAMESPACE_OFFICE, "prepare-view-closing" }, + { "OnViewClosed", XML_NAMESPACE_OFFICE, "view-close" }, + { "OnVisAreaChanged", XML_NAMESPACE_OFFICE, "visarea-changed" }, // "on-visarea-changed" + { "OnCreate", XML_NAMESPACE_OFFICE, "create" }, + { "OnSaveAsFailed", XML_NAMESPACE_OFFICE, "save-as-failed" }, + { "OnSaveFailed", XML_NAMESPACE_OFFICE, "save-failed" }, + { "OnCopyToFailed", XML_NAMESPACE_OFFICE, "copy-to-failed" }, + { "OnTitleChanged", XML_NAMESPACE_OFFICE, "title-changed" }, + { "OnModeChanged", XML_NAMESPACE_OFFICE, "mode-changed" }, + { "OnSaveTo", XML_NAMESPACE_OFFICE, "save-to" }, + { "OnSaveToDone", XML_NAMESPACE_OFFICE, "save-to-done" }, + { "OnSaveToFailed", XML_NAMESPACE_OFFICE, "save-to-failed" }, + { "OnSubComponentOpened", XML_NAMESPACE_OFFICE, "subcomponent-opened" }, + { "OnSubComponentClosed", XML_NAMESPACE_OFFICE, "subcomponent-closed" }, + { "OnStorageChanged", XML_NAMESPACE_OFFICE, "storage-changed" }, + { "OnMailMergeFinished", XML_NAMESPACE_OFFICE, "mail-merge-finished" }, + { "OnFieldMerge", XML_NAMESPACE_OFFICE, "field-merge" }, + { "OnFieldMergeFinished", XML_NAMESPACE_OFFICE, "field-merge-finished" }, + { "OnLayoutFinished", XML_NAMESPACE_OFFICE, "layout-finished" }, + { "OnDoubleClick", XML_NAMESPACE_OFFICE, "dblclick" }, + { "OnRightClick", XML_NAMESPACE_OFFICE, "contextmenu" }, + { "OnChange", XML_NAMESPACE_OFFICE, "content-changed" }, + { "OnCalculate", XML_NAMESPACE_OFFICE, "calculated" }, + + { nullptr, 0, nullptr } +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/script/XMLEventImportHelper.cxx b/xmloff/source/script/XMLEventImportHelper.cxx new file mode 100644 index 000000000..f657198dc --- /dev/null +++ b/xmloff/source/script/XMLEventImportHelper.cxx @@ -0,0 +1,156 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <XMLEventImportHelper.hxx> +#include <tools/debug.hxx> +#include <xmloff/xmlimp.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> +#include <xmloff/xmlerror.hxx> + +using ::com::sun::star::xml::sax::XAttributeList; +using ::com::sun::star::uno::Reference; +using ::com::sun::star::uno::Sequence; + +XMLEventImportHelper::XMLEventImportHelper() : + aFactoryMap(), + pEventNameMap(new NameMap), + aEventNameMapVector() +{ +} + +XMLEventImportHelper::~XMLEventImportHelper() +{ + // delete factories + aFactoryMap.clear(); + + // delete name map + pEventNameMap.reset(); +} + +void XMLEventImportHelper::RegisterFactory( + const OUString& rLanguage, + std::unique_ptr<XMLEventContextFactory> pFactory ) +{ + assert(pFactory); + aFactoryMap[rLanguage] = std::move(pFactory); +} + +void XMLEventImportHelper::AddTranslationTable( + const XMLEventNameTranslation* pTransTable ) +{ + if (nullptr != pTransTable) + { + // put translation table into map + for(const XMLEventNameTranslation* pTrans = pTransTable; + pTrans->sAPIName != nullptr; + pTrans++) + { + XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName ); + + // check for conflicting entries + DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(), + "conflicting event translations"); + + // assign new translation + (*pEventNameMap)[aName] = + OUString::createFromAscii(pTrans->sAPIName); + } + } + // else? ignore! +} + +void XMLEventImportHelper::PushTranslationTable() +{ + // save old map and install new one + aEventNameMapVector.push_back(std::move(pEventNameMap)); + pEventNameMap.reset( new NameMap ); +} + +void XMLEventImportHelper::PopTranslationTable() +{ + DBG_ASSERT(!aEventNameMapVector.empty(), + "no translation tables left to pop"); + if ( !aEventNameMapVector.empty() ) + { + // delete current and install old map + pEventNameMap = std::move(aEventNameMapVector.back()); + aEventNameMapVector.pop_back(); + } +} + + +SvXMLImportContext* XMLEventImportHelper::CreateContext( + SvXMLImport& rImport, + const Reference<XAttributeList> & xAttrList, + XMLEventsImportContext* rEvents, + const OUString& rXmlEventName, + const OUString& rLanguage) +{ + rImport.NotifyMacroEventRead(); + + SvXMLImportContext* pContext = nullptr; + + // translate event name from xml to api + OUString sMacroName; + sal_uInt16 nMacroPrefix = + rImport.GetNamespaceMap().GetKeyByAttrValueQName(rXmlEventName, + &sMacroName ); + XMLEventName aEventName( nMacroPrefix, sMacroName ); + NameMap::iterator aNameIter = pEventNameMap->find(aEventName); + if (aNameIter != pEventNameMap->end()) + { + OUString aScriptLanguage; + sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap(). + GetKeyByAttrValueQName(rLanguage, &aScriptLanguage); + if( XML_NAMESPACE_OOO != nScriptPrefix ) + aScriptLanguage = rLanguage ; + + // check for factory + FactoryMap::iterator aFactoryIterator = + aFactoryMap.find(aScriptLanguage); + if (aFactoryIterator != aFactoryMap.end()) + { + // delegate to factory + pContext = aFactoryIterator->second->CreateContext( + rImport, xAttrList, + rEvents, aNameIter->second); + } + } + + // default context (if no context was created above) + if( nullptr == pContext ) + { + pContext = new SvXMLImportContext(rImport); + + Sequence<OUString> aMsgParams(2); + + aMsgParams[0] = rXmlEventName; + aMsgParams[1] = rLanguage; + + rImport.SetError(XMLERROR_FLAG_ERROR | XMLERROR_ILLEGAL_EVENT, + aMsgParams); + + } + + return pContext; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/script/XMLEventsImportContext.cxx b/xmloff/source/script/XMLEventsImportContext.cxx new file mode 100644 index 000000000..a5a8837ec --- /dev/null +++ b/xmloff/source/script/XMLEventsImportContext.cxx @@ -0,0 +1,275 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <xmloff/XMLEventsImportContext.hxx> + +#include <XMLEventImportHelper.hxx> + +#include <com/sun/star/document/XEventsSupplier.hpp> +#include <comphelper/attributelist.hxx> +#include <xmloff/xmlimp.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> +#include <xmloff/xmltoken.hxx> +#include <xmloff/xmlerror.hxx> + +using namespace ::com::sun::star::uno; +using namespace ::xmloff::token; + +using ::com::sun::star::xml::sax::XAttributeList; +using ::com::sun::star::beans::PropertyValue; +using ::com::sun::star::container::XNameReplace; +using ::com::sun::star::document::XEventsSupplier; +using ::com::sun::star::lang::IllegalArgumentException; + + +XMLEventsImportContext::XMLEventsImportContext( + SvXMLImport& rImport, + sal_uInt16 nPrfx, + const OUString& rLocalName) : + SvXMLImportContext(rImport, nPrfx, rLocalName) +{ +} + +XMLEventsImportContext::XMLEventsImportContext(SvXMLImport& rImport) : + SvXMLImportContext(rImport) +{ +} + + +XMLEventsImportContext::XMLEventsImportContext( + SvXMLImport& rImport, + sal_uInt16 nPrfx, + const OUString& rLocalName, + const Reference<XEventsSupplier> & xEventsSupplier) : + SvXMLImportContext(rImport, nPrfx, rLocalName), + xEvents(xEventsSupplier->getEvents()) +{ +} + +XMLEventsImportContext::XMLEventsImportContext( + SvXMLImport& rImport, + const Reference<XEventsSupplier> & xEventsSupplier) : + SvXMLImportContext(rImport), + xEvents(xEventsSupplier->getEvents()) +{ +} + + +XMLEventsImportContext::XMLEventsImportContext( + SvXMLImport& rImport, + sal_uInt16 nPrfx, + const OUString& rLocalName, + const Reference<XNameReplace> & xNameReplace) : + SvXMLImportContext(rImport, nPrfx, rLocalName), + xEvents(xNameReplace) +{ +} + +XMLEventsImportContext::~XMLEventsImportContext() +{ +// // if, for whatever reason, the object gets destroyed prematurely, +// // we need to delete the collected events +} + + +void XMLEventsImportContext::StartElement( + const Reference<XAttributeList> &) +{ + // nothing to be done +} + +void XMLEventsImportContext::EndElement() +{ + // nothing to be done +} + +SvXMLImportContextRef XMLEventsImportContext::CreateChildContext( + sal_uInt16 /*p_nPrefix*/, + const OUString& /*rLocalName*/, + const Reference<XAttributeList> & xAttrList ) +{ + // a) search for script:language and script:event-name attribute + // b) delegate to factory. The factory will: + // 1) translate XML event name into API event name + // 2) get proper event context factory from import + // 3) instantiate context + + // a) search for script:language and script:event-name attribute + OUString sLanguage; + OUString sEventName; + sal_Int16 nCount = xAttrList->getLength(); + for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++) + { + OUString sLocalName; + sal_uInt16 nPrefix = GetImport().GetNamespaceMap(). + GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName ); + + if (XML_NAMESPACE_SCRIPT == nPrefix) + { + if (IsXMLToken(sLocalName, XML_EVENT_NAME)) + { + sEventName = xAttrList->getValueByIndex(nAttr); + } + else if (IsXMLToken(sLocalName, XML_LANGUAGE)) + { + sLanguage = xAttrList->getValueByIndex(nAttr); + } + // else: ignore -> let child context handle this + } + // else: ignore -> let child context handle this + } + + // b) delegate to factory + return GetImport().GetEventImport().CreateContext( + GetImport(), xAttrList, this, sEventName, sLanguage); +} + +void XMLEventsImportContext::startFastElement(sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >&) +{ + // nothing to be done +} + +void XMLEventsImportContext::endFastElement(sal_Int32 /*nElement*/) +{ + // nothing to be done +} + +css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEventsImportContext::createFastChildContext( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) +{ + // a) search for script:language and script:event-name attribute + // b) delegate to factory. The factory will: + // 1) translate XML event name into API event name + // 2) get proper event context factory from import + // 3) instantiate context + + // a) search for script:language and script:event-name attribute + OUString sLanguage; + OUString sEventName; + for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList )) + { + OUString sValue = aIter.toString(); + + if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_EVENT_NAME)) + { + sEventName = sValue; + } + else if (aIter.getToken() == XML_ELEMENT(SCRIPT, XML_LANGUAGE)) + { + sLanguage = sValue; + // else: ignore -> let child context handle this + } + // else: ignore -> let child context handle this + } + + rtl::Reference < comphelper::AttributeList > rAttrList = new comphelper::AttributeList; + const Sequence< css::xml::FastAttribute > fastAttribs = xAttrList->getFastAttributes(); + for (const auto& rAttr : fastAttribs) + { + const OUString& rAttrValue = rAttr.Value; + sal_Int32 nToken = rAttr.Token; + const OUString& rAttrNamespacePrefix = SvXMLImport::getNamespacePrefixFromToken( nToken, nullptr ); + OUString sAttrName = SvXMLImport::getNameFromToken( nToken ); + if ( !rAttrNamespacePrefix.isEmpty() ) + sAttrName = rAttrNamespacePrefix + ":" + sAttrName; + rAttrList->AddAttribute( sAttrName, "CDATA", rAttrValue ); + } + + // b) delegate to factory + return GetImport().GetEventImport().CreateContext( + GetImport(), rAttrList.get(), this, sEventName, sLanguage); +} + +void XMLEventsImportContext::SetEvents( + const Reference<XEventsSupplier> & xEventsSupplier) +{ + if (xEventsSupplier.is()) + { + SetEvents(xEventsSupplier->getEvents()); + } +} + +void XMLEventsImportContext::SetEvents( + const Reference<XNameReplace> & xNameRepl) +{ + if (xNameRepl.is()) + { + xEvents = xNameRepl; + + // now iterate over vector and a) insert b) delete all elements + for(const auto& rEvent : aCollectEvents) + { + AddEventValues(rEvent.first, rEvent.second); + } + aCollectEvents.clear(); + } +} + +void XMLEventsImportContext::GetEventSequence( + const OUString& rName, + Sequence<PropertyValue> & rSequence ) +{ + // search through the vector + // (This shouldn't take a lot of time, since this method should only get + // called if only one (or few) events are being expected) + + auto aIter = std::find_if(aCollectEvents.begin(), aCollectEvents.end(), + [&rName](EventNameValuesPair& rEvent) { return rEvent.first == rName; }); + + // if we're not at the end, set the sequence + if (aIter != aCollectEvents.end()) + { + rSequence = aIter->second; + } +} + +void XMLEventsImportContext::AddEventValues( + const OUString& rEventName, + const Sequence<PropertyValue> & rValues ) +{ + // if we already have the events, set them; else just collect + if (xEvents.is()) + { + // set event (if name is known) + if (xEvents->hasByName(rEventName)) + { + try + { + xEvents->replaceByName(rEventName, Any(rValues)); + } catch ( const IllegalArgumentException & rException ) + { + Sequence<OUString> aMsgParams { rEventName }; + + GetImport().SetError(XMLERROR_FLAG_ERROR | + XMLERROR_ILLEGAL_EVENT, + aMsgParams, rException.Message, nullptr); + } + } + } + else + { + EventNameValuesPair aPair(rEventName, rValues); + aCollectEvents.push_back(aPair); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/script/XMLScriptContextFactory.cxx b/xmloff/source/script/XMLScriptContextFactory.cxx new file mode 100644 index 000000000..da44c70d7 --- /dev/null +++ b/xmloff/source/script/XMLScriptContextFactory.cxx @@ -0,0 +1,89 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <XMLScriptContextFactory.hxx> +#include <xmloff/XMLEventsImportContext.hxx> +#include <xmloff/xmlimp.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> +#include <xmloff/xmltoken.hxx> + + +using namespace ::xmloff::token; + +using ::com::sun::star::xml::sax::XAttributeList; +using ::com::sun::star::beans::PropertyValue; +using ::com::sun::star::uno::Reference; +using ::com::sun::star::uno::Sequence; + +static const OUStringLiteral gsEventType("EventType"); +static const OUStringLiteral gsScript("Script"); +static const OUStringLiteral gsURL("Script"); + +XMLScriptContextFactory::XMLScriptContextFactory() +{ +} + +XMLScriptContextFactory::~XMLScriptContextFactory() +{ +} + +SvXMLImportContext * XMLScriptContextFactory::CreateContext +(SvXMLImport & rImport, + const Reference<XAttributeList> & xAttrList, + XMLEventsImportContext * rEvents, + const OUString & rApiEventName) +{ + OUString sURLVal; + + sal_Int16 nCount = xAttrList->getLength(); + for (sal_Int16 nAttr = 0; nAttr < nCount; nAttr++) + { + OUString sLocalName; + sal_uInt16 nPrefix = rImport.GetNamespaceMap(). + GetKeyByAttrName(xAttrList->getNameByIndex(nAttr), &sLocalName); + + if (XML_NAMESPACE_XLINK == nPrefix) + { + if (IsXMLToken(sLocalName, XML_HREF)) + sURLVal = xAttrList->getValueByIndex(nAttr); + // else: ignore + } + // else ignore + } + + Sequence<PropertyValue> aValues(2); + + // EventType + aValues[0].Name = gsEventType; + aValues[0].Value <<= OUString(gsScript); + + // URL + aValues[1].Name = gsURL; + aValues[1].Value <<= sURLVal; + + // add values for event now + rEvents->AddEventValues(rApiEventName, aValues); + + // return dummy context + return new SvXMLImportContext(rImport); +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/script/XMLScriptExportHandler.cxx b/xmloff/source/script/XMLScriptExportHandler.cxx new file mode 100644 index 000000000..aaed4ded0 --- /dev/null +++ b/xmloff/source/script/XMLScriptExportHandler.cxx @@ -0,0 +1,75 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <XMLScriptExportHandler.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <xmloff/xmlexp.hxx> +#include <xmloff/xmltoken.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> + + +using namespace ::com::sun::star::uno; +using namespace ::xmloff::token; + +using ::com::sun::star::beans::PropertyValue; + +static const OUStringLiteral gsURL("Script"); + +XMLScriptExportHandler::XMLScriptExportHandler() +{ +} + +XMLScriptExportHandler::~XMLScriptExportHandler() +{ +} + +void XMLScriptExportHandler::Export( + SvXMLExport& rExport, + const OUString& rEventQName, + const Sequence<PropertyValue> & rValues, + bool bUseWhitespace) +{ + + rExport.AddAttribute(XML_NAMESPACE_SCRIPT, XML_LANGUAGE, + rExport.GetNamespaceMap().GetQNameByKey( + XML_NAMESPACE_OOO, GetXMLToken(XML_SCRIPT) ) ); + rExport.AddAttribute(XML_NAMESPACE_SCRIPT, XML_EVENT_NAME, rEventQName); + + for(const auto& rValue : rValues) + { + if (gsURL == rValue.Name) + { + OUString sTmp; + rValue.Value >>= sTmp; + rExport.AddAttribute(XML_NAMESPACE_XLINK, XML_HREF, sTmp); + + // #i110911# xlink:type="simple" is required + rExport.AddAttribute(XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE); + } + // else: disregard + } + + SvXMLElementExport aEventElemt(rExport, XML_NAMESPACE_SCRIPT, + XML_EVENT_LISTENER, + bUseWhitespace, false); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/script/XMLStarBasicContextFactory.cxx b/xmloff/source/script/XMLStarBasicContextFactory.cxx new file mode 100644 index 000000000..7f7653978 --- /dev/null +++ b/xmloff/source/script/XMLStarBasicContextFactory.cxx @@ -0,0 +1,114 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <XMLStarBasicContextFactory.hxx> +#include <xmloff/XMLEventsImportContext.hxx> +#include <xmloff/xmlimp.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> +#include <xmloff/xmltoken.hxx> + + +using namespace ::xmloff::token; + +using ::com::sun::star::xml::sax::XAttributeList; +using ::com::sun::star::beans::PropertyValue; +using ::com::sun::star::uno::Reference; +using ::com::sun::star::uno::Sequence; + + +static const OUStringLiteral gsEventType("EventType"); +static const OUStringLiteral gsLibrary("Library"); +static const OUStringLiteral gsMacroName("MacroName"); +static const OUStringLiteral gsStarBasic("StarBasic"); + +XMLStarBasicContextFactory::XMLStarBasicContextFactory() +{ +} + +XMLStarBasicContextFactory::~XMLStarBasicContextFactory() +{ +} + +SvXMLImportContext* XMLStarBasicContextFactory::CreateContext( + SvXMLImport& rImport, + const Reference<XAttributeList> & xAttrList, + XMLEventsImportContext* rEvents, + const OUString& rApiEventName) +{ + OUString sLibraryVal; + OUString sMacroNameVal; + + sal_Int16 nCount = xAttrList->getLength(); + for(sal_Int16 nAttr = 0; nAttr < nCount; nAttr++) + { + OUString sLocalName; + sal_uInt16 nPrefix = rImport.GetNamespaceMap(). + GetKeyByAttrName( xAttrList->getNameByIndex(nAttr), &sLocalName ); + + if (XML_NAMESPACE_SCRIPT == nPrefix) + { + if (IsXMLToken(sLocalName, XML_MACRO_NAME)) + { + sMacroNameVal = xAttrList->getValueByIndex(nAttr); + } + // else: ignore + } + // else: ignore + } + + const OUString& rApp = GetXMLToken( XML_APPLICATION ); + const OUString& rDoc = GetXMLToken( XML_DOCUMENT ); + if( sMacroNameVal.getLength() > rApp.getLength()+1 && + sMacroNameVal.copy(0,rApp.getLength()).equalsIgnoreAsciiCase( rApp ) && + ':' == sMacroNameVal[rApp.getLength()] ) + { + sLibraryVal = "StarOffice"; + sMacroNameVal = sMacroNameVal.copy( rApp.getLength()+1 ); + } + else if( sMacroNameVal.getLength() > rDoc.getLength()+1 && + sMacroNameVal.copy(0,rDoc.getLength()).equalsIgnoreAsciiCase( rDoc ) && + ':' == sMacroNameVal[rDoc.getLength()] ) + { + sLibraryVal = rDoc; + sMacroNameVal = sMacroNameVal.copy( rDoc.getLength()+1 ); + } + + Sequence<PropertyValue> aValues(3); + + // EventType + aValues[0].Name = gsEventType; + aValues[0].Value <<= OUString(gsStarBasic); + + // library name + aValues[1].Name = gsLibrary; + aValues[1].Value <<= sLibraryVal; + + // macro name + aValues[2].Name = gsMacroName; + aValues[2].Value <<= sMacroNameVal; + + // add values for event now + rEvents->AddEventValues(rApiEventName, aValues); + + // return dummy context + return new SvXMLImportContext(rImport); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/script/XMLStarBasicExportHandler.cxx b/xmloff/source/script/XMLStarBasicExportHandler.cxx new file mode 100644 index 000000000..ed6d3a136 --- /dev/null +++ b/xmloff/source/script/XMLStarBasicExportHandler.cxx @@ -0,0 +1,95 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <XMLStarBasicExportHandler.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <xmloff/xmlexp.hxx> +#include <xmloff/xmltoken.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> + + +using namespace ::com::sun::star::uno; +using namespace ::xmloff::token; + +using ::com::sun::star::beans::PropertyValue; + + +static const OUStringLiteral gsStarBasic("StarBasic"); +static const OUStringLiteral gsLibrary("Library"); +static const OUStringLiteral gsMacroName("MacroName"); +static const OUStringLiteral gsStarOffice("StarOffice"); +static const OUStringLiteral gsApplication("application"); + +XMLStarBasicExportHandler::XMLStarBasicExportHandler() +{ +} + +XMLStarBasicExportHandler::~XMLStarBasicExportHandler() +{ +} + +void XMLStarBasicExportHandler::Export( + SvXMLExport& rExport, + const OUString& rEventQName, + const Sequence<PropertyValue> & rValues, + bool bUseWhitespace) +{ + rExport.AddAttribute(XML_NAMESPACE_SCRIPT, XML_LANGUAGE, + rExport.GetNamespaceMap().GetQNameByKey( + XML_NAMESPACE_OOO, gsStarBasic ) ); + rExport.AddAttribute(XML_NAMESPACE_SCRIPT, XML_EVENT_NAME, rEventQName); + + OUString sLocation, sName; + for(const auto& rValue : rValues) + { + if (gsLibrary == rValue.Name) + { + OUString sTmp; + rValue.Value >>= sTmp; + sLocation = GetXMLToken( + (sTmp.equalsIgnoreAsciiCase(gsApplication) || + sTmp.equalsIgnoreAsciiCase(gsStarOffice) ) ? XML_APPLICATION + : XML_DOCUMENT ); + } + else if (gsMacroName == rValue.Name) + { + rValue.Value >>= sName; + } + // else: disregard + } + + if( !sLocation.isEmpty() ) + { + OUString sTmp = sLocation + ":" + sName; + rExport.AddAttribute(XML_NAMESPACE_SCRIPT, XML_MACRO_NAME, + sTmp); + } + else + { + rExport.AddAttribute(XML_NAMESPACE_SCRIPT, XML_MACRO_NAME, sName ); + } + + SvXMLElementExport aEventElemt(rExport, XML_NAMESPACE_SCRIPT, + XML_EVENT_LISTENER, + bUseWhitespace, false); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/script/xmlbasicscript.cxx b/xmloff/source/script/xmlbasicscript.cxx new file mode 100644 index 000000000..efc655eca --- /dev/null +++ b/xmloff/source/script/xmlbasicscript.cxx @@ -0,0 +1,320 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include "xmlbasicscript.hxx" +#include <sal/log.hxx> +#include <xmlscript/xmlns.h> +#include <xmloff/xmlnmspe.hxx> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/lang/XMultiComponentFactory.hpp> +#include <com/sun/star/document/XEmbeddedScripts.hpp> +#include <com/sun/star/xml/sax/SAXException.hpp> +#include <cppuhelper/supportsservice.hxx> +#include <tools/diagnose_ex.h> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::xml::sax; +using namespace ::xmloff::token; + +namespace xmloff +{ +// BasicElementBase + +BasicElementBase::BasicElementBase(SvXMLImport& rImport) + : SvXMLImportContext(rImport) +{ +} + +bool BasicElementBase::getBoolAttr(bool* pRet, sal_Int32 nToken, + const Reference<XFastAttributeList>& xAttributes) +{ + OUString aValue = xAttributes->getOptionalValue(nToken); + if (!aValue.isEmpty()) + { + if (aValue == "true") + { + *pRet = true; + return true; + } + else if (aValue == "false") + { + *pRet = false; + return true; + } + else + { + throw xml::sax::SAXException(SvXMLImport::getNameFromToken(nToken) + + ": no boolean value (true|false)!", + Reference<XInterface>(), Any()); + } + } + return false; +} + +void BasicElementBase::startFastElement(sal_Int32 /*nElement*/, + const Reference<XFastAttributeList>& /*xAttributes*/) +{ +} + +// BasicLibrariesElement + +BasicLibrariesElement::BasicLibrariesElement(SvXMLImport& rImport, + const css::uno::Reference<css::frame::XModel>& rxModel) + : BasicElementBase(rImport) +{ + // try the XEmbeddedScripts interface + Reference<document::XEmbeddedScripts> xDocumentScripts(rxModel, UNO_QUERY_THROW); + m_xLibContainer.set(xDocumentScripts->getBasicLibraries().get()); + + if (!m_xLibContainer.is()) + { + // try the "BasicLibraries" property (old-style, for compatibility) + Reference<beans::XPropertySet> xPSet(rxModel, UNO_QUERY); + if (xPSet.is()) + xPSet->getPropertyValue("BasicLibraries") >>= m_xLibContainer; + } + + SAL_WARN_IF(!m_xLibContainer.is(), "xmlscript.xmlflat", + "BasicImport::startRootElement: nowhere to import to!"); + + if (!m_xLibContainer.is()) + { + throw xml::sax::SAXException("nowhere to import to", Reference<XInterface>(), Any()); + } +} + +// XElement + +Reference<XFastContextHandler> +BasicLibrariesElement::createFastChildContext(sal_Int32 nElement, + const Reference<XFastAttributeList>& xAttributes) +{ + if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO)) + { + throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any()); + } + else if ((nElement & TOKEN_MASK) == XML_LIBRARY_LINKED) + { + OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME); + + OUString aStorageURL = xAttributes->getValue(XML_ELEMENT(XLINK, XML_HREF)); + + bool bReadOnly = false; + getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_READONLY, xAttributes); + + if (m_xLibContainer.is()) + { + try + { + Reference<container::XNameAccess> xLib( + m_xLibContainer->createLibraryLink(aName, aStorageURL, bReadOnly)); + if (xLib.is()) + return new BasicElementBase(GetImport()); + } + catch (const container::ElementExistException&) + { + TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", + "BasicLibrariesElement::startChildElement"); + } + catch (const lang::IllegalArgumentException&) + { + TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", + "BasicLibrariesElement::startChildElement"); + } + } + } + else if ((nElement & TOKEN_MASK) == XML_LIBRARY_EMBEDDED) + { + // TODO: create password protected libraries + + OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME); + + bool bReadOnly = false; + getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_READONLY, xAttributes); + + if (m_xLibContainer.is()) + { + try + { + Reference<container::XNameContainer> xLib; + if (m_xLibContainer->hasByName(aName)) + { + // Standard library + m_xLibContainer->getByName(aName) >>= xLib; + } + else + { + xLib.set(m_xLibContainer->createLibrary(aName)); + } + + if (xLib.is()) + return new BasicEmbeddedLibraryElement(GetImport(), m_xLibContainer, aName, + bReadOnly); + } + catch (const lang::IllegalArgumentException&) + { + TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", + "BasicLibrariesElement::startChildElement"); + } + } + } + else + { + throw xml::sax::SAXException("expected library-linked or library-embedded element!", + Reference<XInterface>(), Any()); + } + + return nullptr; +} + +// BasicEmbeddedLibraryElement + +BasicEmbeddedLibraryElement::BasicEmbeddedLibraryElement( + SvXMLImport& rImport, const Reference<script::XLibraryContainer2>& rxLibContainer, + const OUString& rLibName, bool bReadOnly) + : BasicElementBase(rImport) + , m_xLibContainer(rxLibContainer) + , m_aLibName(rLibName) + , m_bReadOnly(bReadOnly) +{ + try + { + if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName)) + m_xLibContainer->getByName(m_aLibName) >>= m_xLib; + } + catch (const lang::WrappedTargetException&) + { + TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicEmbeddedLibraryElement::CTOR:"); + } +} + +Reference<XFastContextHandler> BasicEmbeddedLibraryElement::createFastChildContext( + sal_Int32 nElement, const Reference<XFastAttributeList>& xAttributes) +{ + if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO)) + { + throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any()); + } + else if ((nElement & TOKEN_MASK) == XML_MODULE) + { + OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME); + + if (m_xLib.is() && !aName.isEmpty()) + return new BasicModuleElement(GetImport(), m_xLib, aName); + } + else + { + throw xml::sax::SAXException("expected module element!", Reference<XInterface>(), Any()); + } + + return nullptr; +} + +void BasicEmbeddedLibraryElement::endFastElement(sal_Int32) +{ + if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName) && m_bReadOnly) + m_xLibContainer->setLibraryReadOnly(m_aLibName, m_bReadOnly); +} + +// BasicModuleElement + +BasicModuleElement::BasicModuleElement(SvXMLImport& rImport, + const Reference<container::XNameContainer>& rxLib, + const OUString& rName) + : BasicElementBase(rImport) + , m_xLib(rxLib) + , m_aName(rName) +{ +} + +Reference<XFastContextHandler> +BasicModuleElement::createFastChildContext(sal_Int32 nElement, + const Reference<XFastAttributeList>& xAttributes) +{ + // TODO: <byte-code> + + if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO)) + { + throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any()); + } + else if ((nElement & TOKEN_MASK) == XML_SOURCE_CODE) + { + // TODO: password protected libraries + + if (xAttributes.is()) + { + if (m_xLib.is() && !m_aName.isEmpty()) + return new BasicSourceCodeElement(GetImport(), m_xLib, m_aName); + } + } + else + { + throw xml::sax::SAXException("expected source-code element!", Reference<XInterface>(), + Any()); + } + + return nullptr; +} + +// BasicSourceCodeElement + +BasicSourceCodeElement::BasicSourceCodeElement(SvXMLImport& rImport, + const Reference<container::XNameContainer>& rxLib, + const OUString& rName) + : BasicElementBase(rImport) + , m_xLib(rxLib) + , m_aName(rName) +{ +} + +// XElement + +void BasicSourceCodeElement::characters(const OUString& rChars) { m_aBuffer.append(rChars); } + +void BasicSourceCodeElement::endFastElement(sal_Int32) +{ + try + { + if (m_xLib.is() && !m_aName.isEmpty()) + { + Any aElement; + aElement <<= m_aBuffer.makeStringAndClear(); + m_xLib->insertByName(m_aName, aElement); + } + } + catch (const container::ElementExistException&) + { + TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement"); + } + catch (const lang::IllegalArgumentException&) + { + TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement"); + } + catch (const lang::WrappedTargetException&) + { + TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement"); + } +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/script/xmlbasicscript.hxx b/xmloff/source/script/xmlbasicscript.hxx new file mode 100644 index 000000000..b8a03a5b7 --- /dev/null +++ b/xmloff/source/script/xmlbasicscript.hxx @@ -0,0 +1,115 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/script/XLibraryContainer2.hpp> +#include <cppuhelper/implbase.hxx> +#include <osl/mutex.hxx> +#include <rtl/ustrbuf.hxx> +#include <rtl/ref.hxx> +#include <xmloff/xmlimp.hxx> + +namespace xmloff +{ +class BasicElementBase : public SvXMLImportContext +{ +protected: + static bool + getBoolAttr(bool* pRet, sal_Int32 nToken, + const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttributes); + +public: + BasicElementBase(SvXMLImport& rImport); + + virtual void SAL_CALL startFastElement( + sal_Int32 nElement, + const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttribs) override; +}; + +class BasicLibrariesElement : public BasicElementBase +{ +private: + css::uno::Reference<css::script::XLibraryContainer2> m_xLibContainer; + +public: + BasicLibrariesElement(SvXMLImport& rImport, + const css::uno::Reference<css::frame::XModel>& rxModel); + + virtual css::uno::Reference<XFastContextHandler> SAL_CALL createFastChildContext( + sal_Int32 Element, + const css::uno::Reference<css::xml::sax::XFastAttributeList>& Attribs) override; +}; + +class BasicEmbeddedLibraryElement : public BasicElementBase +{ +private: + css::uno::Reference<css::script::XLibraryContainer2> m_xLibContainer; + css::uno::Reference<css::container::XNameContainer> m_xLib; + OUString const m_aLibName; + bool const m_bReadOnly; + +public: + BasicEmbeddedLibraryElement( + SvXMLImport& rImport, + const css::uno::Reference<css::script::XLibraryContainer2>& rxLibContainer, + const OUString& rLibName, bool bReadOnly); + + virtual css::uno::Reference<XFastContextHandler> SAL_CALL createFastChildContext( + sal_Int32 Element, + const css::uno::Reference<css::xml::sax::XFastAttributeList>& Attribs) override; + virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; +}; + +class BasicModuleElement : public BasicElementBase +{ +private: + css::uno::Reference<css::container::XNameContainer> m_xLib; + OUString const m_aName; + +public: + BasicModuleElement(SvXMLImport& rImport, + const css::uno::Reference<css::container::XNameContainer>& rxLib, + const OUString& rName); + + virtual css::uno::Reference<XFastContextHandler> SAL_CALL createFastChildContext( + sal_Int32 Element, + const css::uno::Reference<css::xml::sax::XFastAttributeList>& Attribs) override; +}; + +class BasicSourceCodeElement : public BasicElementBase +{ +private: + css::uno::Reference<css::container::XNameContainer> m_xLib; + OUString const m_aName; + OUStringBuffer m_aBuffer; + +public: + BasicSourceCodeElement(SvXMLImport& rImport, + const css::uno::Reference<css::container::XNameContainer>& rxLib, + const OUString& rName); + + virtual void SAL_CALL characters(const OUString& rChars) override; + virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; +}; + +} // namespace xmloff + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file diff --git a/xmloff/source/script/xmlscripti.cxx b/xmloff/source/script/xmlscripti.cxx new file mode 100644 index 000000000..a8bf2ab36 --- /dev/null +++ b/xmloff/source/script/xmlscripti.cxx @@ -0,0 +1,131 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <xmloff/xmlscripti.hxx> +#include <xmloff/xmlnmspe.hxx> +#include <xmloff/xmltoken.hxx> +#include <xmloff/xmlimp.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/XMLEventsImportContext.hxx> +#include "xmlbasicscript.hxx" + +#include <com/sun/star/document/XEventsSupplier.hpp> +#include <com/sun/star/document/XEmbeddedScripts.hpp> + +using namespace com::sun::star; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::frame; +using namespace com::sun::star::document; +using namespace com::sun::star::xml::sax; +using namespace ::xmloff::token; + +// XMLScriptChildContext: context for <office:script> element + +namespace { + +class XMLScriptChildContext : public SvXMLImportContext +{ +private: + css::uno::Reference< css::frame::XModel > m_xModel; + css::uno::Reference< css::document::XEmbeddedScripts > m_xDocumentScripts; + OUString m_aLanguage; + +public: + XMLScriptChildContext( SvXMLImport& rImport, + const css::uno::Reference< css::frame::XModel>& rxModel, + const OUString& rLanguage ); + + virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( + sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; + + virtual void SAL_CALL startFastElement( sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& ) override {} +}; + +} + +XMLScriptChildContext::XMLScriptChildContext( SvXMLImport& rImport, + const Reference< frame::XModel >& rxModel, const OUString& rLanguage ) + :SvXMLImportContext( rImport ) + ,m_xModel( rxModel ) + ,m_xDocumentScripts( rxModel, UNO_QUERY ) + ,m_aLanguage( rLanguage ) +{ +} + +css::uno::Reference< css::xml::sax::XFastContextHandler > XMLScriptChildContext::createFastChildContext( + sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) +{ + if ( m_xDocumentScripts.is() ) + { // document supports embedding scripts/macros + OUString aBasic( GetImport().GetNamespaceMap().GetPrefixByKey( XML_NAMESPACE_OOO ) + ":Basic" ); + + if ( m_aLanguage == aBasic && nElement == XML_ELEMENT(OOO, XML_LIBRARIES) ) + { + return new xmloff::BasicLibrariesElement( GetImport(), m_xModel ); + } + } + + return nullptr; +} + +// XMLScriptContext: context for <office:scripts> element + +XMLScriptContext::XMLScriptContext( SvXMLImport& rImport, + const Reference<XModel>& rDocModel ) + :SvXMLImportContext( rImport ) + ,m_xModel( rDocModel ) +{ +} + +XMLScriptContext::~XMLScriptContext() +{ +} + +css::uno::Reference< css::xml::sax::XFastContextHandler > XMLScriptContext::createFastChildContext( + sal_Int32 nElement, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) +{ + if ( nElement == XML_ELEMENT(OFFICE, XML_SCRIPT) ) + { + if ( m_xModel.is() ) + { + OUString aLanguage = xAttrList->getValue( XML_ELEMENT(SCRIPT, XML_LANGUAGE) ); + + uno::Sequence< beans::PropertyValue > aMedDescr = m_xModel->getArgs(); + sal_Int32 nNewLen = aMedDescr.getLength() + 1; + aMedDescr.realloc( nNewLen ); + aMedDescr[nNewLen-1].Name = "BreakMacroSignature"; + aMedDescr[nNewLen-1].Value <<= true; + m_xModel->attachResource( m_xModel->getURL(), aMedDescr ); + + return new XMLScriptChildContext( GetImport(), m_xModel, aLanguage ); + } + } + else if ( nElement == XML_ELEMENT(OFFICE, XML_EVENT_LISTENERS) ) + { + Reference< XEventsSupplier> xSupplier( GetImport().GetModel(), UNO_QUERY ); + return new XMLEventsImportContext( GetImport(), xSupplier ); + } + + return nullptr; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |