[Libreoffice-commits] core.git: sax/source xmloff/Library_xo.mk xmloff/source

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Thu Feb 20 12:38:20 UTC 2020


 sax/source/tools/fastattribs.cxx        |    4 
 xmloff/Library_xo.mk                    |    2 
 xmloff/source/script/xmlbasici.cxx      |  147 --------------
 xmloff/source/script/xmlbasici.hxx      |   74 -------
 xmloff/source/script/xmlbasicscript.cxx |  323 ++++++++++++++++++++++++++++++++
 xmloff/source/script/xmlbasicscript.hxx |  116 +++++++++++
 xmloff/source/script/xmlscripti.cxx     |   21 --
 7 files changed, 451 insertions(+), 236 deletions(-)

New commits:
commit 4e97fa0f4e73acdf522643aeec486b1395e63727
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Thu Feb 20 13:09:32 2020 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Thu Feb 20 13:37:48 2020 +0100

    use fast-parser APIs for embedded script parsing
    
    essentially, this duplicates and converts the service in
        xmlscript/source/xmlflat_impexp/
    and then converts it to use SvXMLImport APIs, stripping about 3 layers
    of unnecessary indirection in the process.
    
    Also improve some exception throwing in sax/ to make the next
    programmers life easier when finding the source of exceptions.
    
    Change-Id: I1ac552ee0565349e8322a55f6f609cf9f4521cab
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/89084
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/sax/source/tools/fastattribs.cxx b/sax/source/tools/fastattribs.cxx
index 9b65718764fc..587749938c6b 100644
--- a/sax/source/tools/fastattribs.cxx
+++ b/sax/source/tools/fastattribs.cxx
@@ -144,7 +144,7 @@ sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token )
                        getFastAttributeValue(i),
                        AttributeValueLength( i ) );
 
-    throw SAXException();
+    throw SAXException("FastAttributeList::getValueToken: unknown token " + OUString::number(Token), nullptr, Any());
 }
 
 sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default )
@@ -216,7 +216,7 @@ OUString FastAttributeList::getValue( ::sal_Int32 Token )
         if (maAttributeTokens[i] == Token)
             return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
 
-    throw SAXException();
+    throw SAXException("FastAttributeList::getValue: unknown token " + OUString::number(Token), nullptr, Any());
 }
 
 OUString FastAttributeList::getValueByIndex( ::sal_Int32 nTokenIndex ) const
diff --git a/xmloff/Library_xo.mk b/xmloff/Library_xo.mk
index 9cee6c131e83..3ea643c4be8f 100644
--- a/xmloff/Library_xo.mk
+++ b/xmloff/Library_xo.mk
@@ -193,7 +193,7 @@ $(eval $(call gb_Library_add_exception_objects,xo,\
     xmloff/source/script/XMLScriptExportHandler \
     xmloff/source/script/XMLStarBasicContextFactory \
     xmloff/source/script/XMLStarBasicExportHandler \
-    xmloff/source/script/xmlbasici \
+    xmloff/source/script/xmlbasicscript \
     xmloff/source/script/xmlscripti \
     xmloff/source/style/AttributeContainerHandler \
     xmloff/source/style/DashStyle \
diff --git a/xmloff/source/script/xmlbasici.cxx b/xmloff/source/script/xmlbasici.cxx
deleted file mode 100644
index 0a69889f7218..000000000000
--- a/xmloff/source/script/xmlbasici.cxx
+++ /dev/null
@@ -1,147 +0,0 @@
-/* -*- 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 "xmlbasici.hxx"
-#include <xmloff/attrlist.hxx>
-#include <xmloff/nmspmap.hxx>
-#include <xmloff/xmlimp.hxx>
-#include <com/sun/star/document/XMLOasisBasicImporter.hpp>
-
-using namespace ::com::sun::star;
-using namespace ::com::sun::star::uno;
-
-// XMLBasicImportContext
-
-XMLBasicImportContext::XMLBasicImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
-        const Reference< frame::XModel >& rxModel )
-    :SvXMLImportContext( rImport, nPrfx, rLName )
-    ,m_xModel( rxModel )
-{
-    Reference< uno::XComponentContext > xContext = GetImport().GetComponentContext();
-    m_xHandler = document::XMLOasisBasicImporter::create( xContext );
-
-    m_xHandler->setTargetDocument( m_xModel );
-}
-
-XMLBasicImportContext::~XMLBasicImportContext()
-{
-}
-
-SvXMLImportContextRef XMLBasicImportContext::CreateChildContext(
-    sal_uInt16 nPrefix, const OUString& rLocalName,
-    const Reference< xml::sax::XAttributeList >& )
-{
-    SvXMLImportContext* pContext = nullptr;
-
-    if ( m_xHandler.is() )
-        pContext = new XMLBasicImportChildContext( GetImport(), nPrefix, rLocalName,
-                          Reference<xml::sax::XDocumentHandler>(m_xHandler, UNO_QUERY_THROW) );
-
-    return pContext;
-}
-
-void XMLBasicImportContext::StartElement(
-    const Reference< xml::sax::XAttributeList >& rxAttrList )
-{
-    if ( m_xHandler.is() )
-    {
-        m_xHandler->startDocument();
-
-        // copy namespace declarations
-        SvXMLAttributeList* pAttrList = new SvXMLAttributeList( rxAttrList );
-        Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
-        const SvXMLNamespaceMap& rNamespaceMap = GetImport().GetNamespaceMap();
-        sal_uInt16 nPos = rNamespaceMap.GetFirstKey();
-        while ( nPos != USHRT_MAX )
-        {
-            OUString aAttrName( rNamespaceMap.GetAttrNameByKey( nPos ) );
-            if ( xAttrList->getValueByName( aAttrName ).isEmpty() )
-                pAttrList->AddAttribute( aAttrName, rNamespaceMap.GetNameByKey( nPos ) );
-            nPos = rNamespaceMap.GetNextKey( nPos );
-        }
-
-        m_xHandler->startElement(
-            GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ),
-            xAttrList );
-    }
-}
-
-void XMLBasicImportContext::EndElement()
-{
-    if ( m_xHandler.is() )
-    {
-        m_xHandler->endElement(
-            GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ) );
-        m_xHandler->endDocument();
-    }
-}
-
-void XMLBasicImportContext::Characters( const OUString& rChars )
-{
-    if ( m_xHandler.is() )
-        m_xHandler->characters( rChars );
-}
-
-// XMLBasicImportChildContext
-
-XMLBasicImportChildContext::XMLBasicImportChildContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
-        const Reference< xml::sax::XDocumentHandler >& rxHandler )
-    :SvXMLImportContext( rImport, nPrfx, rLName )
-    ,m_xHandler( rxHandler )
-{
-}
-
-XMLBasicImportChildContext::~XMLBasicImportChildContext()
-{
-}
-
-SvXMLImportContextRef XMLBasicImportChildContext::CreateChildContext(
-    sal_uInt16 nPrefix, const OUString& rLocalName,
-    const Reference< xml::sax::XAttributeList >& )
-{
-    return new XMLBasicImportChildContext( GetImport(), nPrefix, rLocalName, m_xHandler );
-}
-
-void XMLBasicImportChildContext::StartElement(
-    const Reference< xml::sax::XAttributeList >& xAttrList )
-{
-    if ( m_xHandler.is() )
-    {
-        m_xHandler->startElement(
-            GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ),
-            xAttrList );
-    }
-}
-
-void XMLBasicImportChildContext::EndElement()
-{
-    if ( m_xHandler.is() )
-    {
-        m_xHandler->endElement(
-            GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ) );
-    }
-}
-
-void XMLBasicImportChildContext::Characters( const OUString& rChars )
-{
-    if ( m_xHandler.is() )
-        m_xHandler->characters( rChars );
-}
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/xmloff/source/script/xmlbasici.hxx b/xmloff/source/script/xmlbasici.hxx
deleted file mode 100644
index 9dd452e3b0c8..000000000000
--- a/xmloff/source/script/xmlbasici.hxx
+++ /dev/null
@@ -1,74 +0,0 @@
-/* -*- 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 .
- */
-
-#ifndef INCLUDED_XMLOFF_SOURCE_SCRIPT_XMLBASICI_HXX
-#define INCLUDED_XMLOFF_SOURCE_SCRIPT_XMLBASICI_HXX
-
-#include <com/sun/star/frame/XModel.hpp>
-#include <com/sun/star/document/XXMLOasisBasicImporter.hpp>
-#include <xmloff/xmlictxt.hxx>
-
-
-class XMLBasicImportContext : public SvXMLImportContext
-{
-private:
-    css::uno::Reference< css::frame::XModel >                 m_xModel;
-    css::uno::Reference< css::document::XXMLOasisBasicImporter >    m_xHandler;
-
-public:
-    XMLBasicImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
-        const css::uno::Reference< css::frame::XModel >& rxModel );
-
-    virtual ~XMLBasicImportContext() override;
-
-    virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName,
-        const css::uno::Reference< css::xml::sax::XAttributeList >& rxAttrList ) override;
-
-    virtual void StartElement( const css::uno::Reference< css::xml::sax::XAttributeList >& rxAttrList ) override;
-
-    virtual void EndElement() override;
-
-    virtual void Characters( const OUString& rChars ) override;
-};
-
-
-class XMLBasicImportChildContext : public SvXMLImportContext
-{
-private:
-    css::uno::Reference< css::xml::sax::XDocumentHandler >    m_xHandler;
-
-public:
-    XMLBasicImportChildContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
-        const css::uno::Reference< css::xml::sax::XDocumentHandler >& rxHandler );
-
-    virtual ~XMLBasicImportChildContext() override;
-
-    virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName,
-        const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
-
-    virtual void StartElement( const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
-
-    virtual void EndElement() override;
-
-    virtual void Characters( const OUString& rChars ) override;
-};
-
-#endif // INCLUDED_XMLOFF_SOURCE_SCRIPT_XMLBASICI_HXX
-
-/* 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 000000000000..2fc2e4e308bf
--- /dev/null
+++ b/xmloff/source/script/xmlbasicscript.cxx
@@ -0,0 +1,323 @@
+/* -*- 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, sal_Int32 nExpectedXMLNS)
+    : SvXMLImportContext(rImport)
+    , mnExpectedXMLNS(nExpectedXMLNS)
+{
+}
+
+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,
+                                             bool bOasis)
+    : BasicElementBase(rImport, bOasis ? XML_NAMESPACE_OOO : XML_NAMESPACE_SCRIPT_OOO)
+{
+    // 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 ((nElement & NMSP_MASK) != NAMESPACE_TOKEN(mnExpectedXMLNS))
+    {
+        throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any());
+    }
+    else if ((nElement & TOKEN_MASK) == XML_LIBRARY_LINKED)
+    {
+        OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(mnExpectedXMLNS) | XML_NAME);
+
+        OUString aStorageURL = xAttributes->getValue(XML_ELEMENT(XLINK, XML_HREF));
+
+        bool bReadOnly = false;
+        getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(mnExpectedXMLNS) | 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(), mnExpectedXMLNS);
+            }
+            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(mnExpectedXMLNS) | XML_NAME);
+
+        bool bReadOnly = false;
+        getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(mnExpectedXMLNS) | 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(), mnExpectedXMLNS,
+                                                           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, sal_Int32 nExpectedXMLNS,
+    const Reference<script::XLibraryContainer2>& rxLibContainer, const OUString& rLibName,
+    bool bReadOnly)
+    : BasicElementBase(rImport, nExpectedXMLNS)
+    , 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 ((nElement & NMSP_MASK) != NAMESPACE_TOKEN(mnExpectedXMLNS))
+    {
+        throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any());
+    }
+    else if ((nElement & TOKEN_MASK) == XML_MODULE)
+    {
+        OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(mnExpectedXMLNS) | XML_NAME);
+
+        if (m_xLib.is() && !aName.isEmpty())
+            return new BasicModuleElement(GetImport(), mnExpectedXMLNS, 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, sal_Int32 nExpectedXMLNS,
+                                       const Reference<container::XNameContainer>& rxLib,
+                                       const OUString& rName)
+    : BasicElementBase(rImport, nExpectedXMLNS)
+    , m_xLib(rxLib)
+    , m_aName(rName)
+{
+}
+
+Reference<XFastContextHandler>
+BasicModuleElement::createFastChildContext(sal_Int32 nElement,
+                                           const Reference<XFastAttributeList>& xAttributes)
+{
+    // TODO: <byte-code>
+
+    if ((nElement & NMSP_MASK) != NAMESPACE_TOKEN(mnExpectedXMLNS))
+    {
+        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(), mnExpectedXMLNS, m_xLib, m_aName);
+        }
+    }
+    else
+    {
+        throw xml::sax::SAXException("expected source-code element!", Reference<XInterface>(),
+                                     Any());
+    }
+
+    return nullptr;
+}
+
+// BasicSourceCodeElement
+
+BasicSourceCodeElement::BasicSourceCodeElement(SvXMLImport& rImport, sal_Int32 nExpectedXMLNS,
+                                               const Reference<container::XNameContainer>& rxLib,
+                                               const OUString& rName)
+    : BasicElementBase(rImport, nExpectedXMLNS)
+    , 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 000000000000..7bf5c2483422
--- /dev/null
+++ b/xmloff/source/script/xmlbasicscript.hxx
@@ -0,0 +1,116 @@
+/* -*- 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:
+    sal_Int32 mnExpectedXMLNS;
+    static bool
+    getBoolAttr(bool* pRet, sal_Int32 nToken,
+                const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttributes);
+
+public:
+    BasicElementBase(SvXMLImport& rImport, sal_Int32 nExpectedXMLNS);
+
+    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, bool bOasis);
+
+    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, sal_Int32 nExpectedXMLNS,
+        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, sal_Int32 nExpectedXMLNS,
+                       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, sal_Int32 nExpectedXMLNS,
+                           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
index 3820ab14e97e..d72eeeee1159 100644
--- a/xmloff/source/script/xmlscripti.cxx
+++ b/xmloff/source/script/xmlscripti.cxx
@@ -23,7 +23,7 @@
 #include <xmloff/xmlimp.hxx>
 #include <xmloff/nmspmap.hxx>
 #include <xmloff/XMLEventsImportContext.hxx>
-#include "xmlbasici.hxx"
+#include "xmlbasicscript.hxx"
 
 #include <com/sun/star/document/XEventsSupplier.hpp>
 #include <com/sun/star/document/XEmbeddedScripts.hpp>
@@ -52,11 +52,8 @@ public:
         const css::uno::Reference< css::frame::XModel>& rxModel,
         const OUString& rLanguage );
 
-    virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName,
-        const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
     virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
-            sal_Int32 /*nElement*/, const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ ) override
-    { return nullptr; }
+            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 {}
@@ -73,20 +70,20 @@ XMLScriptChildContext::XMLScriptChildContext( SvXMLImport& rImport,
 {
 }
 
-SvXMLImportContextRef XMLScriptChildContext::CreateChildContext(
-    sal_uInt16 nPrefix, const OUString& rLocalName,
-    const Reference< xml::sax::XAttributeList >& /*xAttrList*/ )
+css::uno::Reference< css::xml::sax::XFastContextHandler > XMLScriptChildContext::createFastChildContext(
+            sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ )
 {
-    SvXMLImportContextRef xContext;
     if ( m_xDocumentScripts.is() )
     {   // document supports embedding scripts/macros
         OUString aBasic( GetImport().GetNamespaceMap().GetPrefixByKey( XML_NAMESPACE_OOO ) + ":Basic" );
 
-        if ( m_aLanguage == aBasic && nPrefix == XML_NAMESPACE_OOO && IsXMLToken( rLocalName, XML_LIBRARIES ) )
-            xContext = new XMLBasicImportContext( GetImport(), nPrefix, rLocalName, m_xModel );
+        if ( m_aLanguage == aBasic && nElement == XML_ELEMENT(OOO, XML_LIBRARIES) )
+        {
+            return new xmloff::BasicLibrariesElement( GetImport(), m_xModel,/*bOasis*/ true );
+        }
     }
 
-    return xContext;
+    return nullptr;
 }
 
 // XMLScriptContext: context for <office:scripts> element


More information about the Libreoffice-commits mailing list