diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /xmlscript/source/xmllib_imexp | |
parent | Initial commit. (diff) | |
download | libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.tar.xz libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xmlscript/source/xmllib_imexp')
-rw-r--r-- | xmlscript/source/xmllib_imexp/imp_share.hxx | 218 | ||||
-rw-r--r-- | xmlscript/source/xmllib_imexp/xmllib_export.cxx | 136 | ||||
-rw-r--r-- | xmlscript/source/xmllib_imexp/xmllib_import.cxx | 257 |
3 files changed, 611 insertions, 0 deletions
diff --git a/xmlscript/source/xmllib_imexp/imp_share.hxx b/xmlscript/source/xmllib_imexp/imp_share.hxx new file mode 100644 index 000000000..5178a36a4 --- /dev/null +++ b/xmlscript/source/xmllib_imexp/imp_share.hxx @@ -0,0 +1,218 @@ +/* -*- 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 <xmlscript/xmllib_imexp.hxx> + +#include <cppuhelper/implbase.hxx> + +#include <com/sun/star/xml/input/XRoot.hpp> +#include <com/sun/star/xml/sax/SAXException.hpp> +#include <rtl/ref.hxx> + +#include <vector> + +namespace xmlscript +{ +inline sal_Int32 toInt32( OUString const & rStr ) +{ + sal_Int32 nVal; + if (rStr.getLength() > 2 && rStr[ 0 ] == '0' && rStr[ 1 ] == 'x') + { + nVal = rStr.copy( 2 ).toUInt32( 16 ); + } + else + { + nVal = rStr.toInt32(); + } + return nVal; +} +inline bool getBoolAttr( + bool * pRet, OUString const & rAttrName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, sal_Int32 uid ) +{ + OUString aValue( + xAttributes->getValueByUidName( uid, rAttrName ) ); + if (!aValue.isEmpty()) + { + if ( aValue == "true" ) + { + *pRet = true; + return true; + } + else if ( aValue == "false" ) + { + *pRet = false; + return true; + } + else + { + throw css::xml::sax::SAXException(rAttrName + ": no boolean value (true|false)!", css::uno::Reference< css::uno::XInterface >(), css::uno::Any() ); + } + } + return false; +} + +inline bool getStringAttr( + OUString * pRet, OUString const & rAttrName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, sal_Int32 uid ) +{ + *pRet = xAttributes->getValueByUidName( uid, rAttrName ); + return (!pRet->isEmpty()); +} + +inline bool getLongAttr( + sal_Int32 * pRet, OUString const & rAttrName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, + sal_Int32 uid ) +{ + OUString aValue( + xAttributes->getValueByUidName( uid, rAttrName ) ); + if (!aValue.isEmpty()) + { + *pRet = toInt32( aValue ); + return true; + } + return false; +} + +// Library import + +struct LibraryImport + : public ::cppu::WeakImplHelper< css::xml::input::XRoot > +{ + friend class LibrariesElement; + friend class LibraryElement; + + LibDescriptorArray* mpLibArray; + LibDescriptor* const mpLibDesc; // Single library mode + + sal_Int32 XMLNS_LIBRARY_UID; + sal_Int32 XMLNS_XLINK_UID; + +public: + explicit LibraryImport( LibDescriptorArray* pLibArray ) + : mpLibArray(pLibArray) + , mpLibDesc(nullptr) + , XMLNS_LIBRARY_UID(0) + , XMLNS_XLINK_UID(0) + { + } + + // Single library mode + explicit LibraryImport(LibDescriptor* pLibDesc) + : mpLibArray(nullptr) + , mpLibDesc(pLibDesc) + , XMLNS_LIBRARY_UID(0) + , XMLNS_XLINK_UID(0) + { + } + + virtual ~LibraryImport() override; + + // XRoot + virtual void SAL_CALL startDocument( + css::uno::Reference< css::xml::input::XNamespaceMapping > const & xNamespaceMapping ) override; + virtual void SAL_CALL endDocument() override; + virtual void SAL_CALL processingInstruction( + OUString const & rTarget, OUString const & rData ) override; + virtual void SAL_CALL setDocumentLocator( + css::uno::Reference< css::xml::sax::XLocator > const & xLocator ) override; + virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startRootElement( + sal_Int32 nUid, OUString const & rLocalName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override; +}; + +class LibElementBase + : public ::cppu::WeakImplHelper< css::xml::input::XElement > +{ +protected: + rtl::Reference<LibraryImport> mxImport; + rtl::Reference<LibElementBase> mxParent; +private: + OUString const _aLocalName; + css::uno::Reference< css::xml::input::XAttributes > _xAttributes; + +public: + LibElementBase( + OUString const & rLocalName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, + LibElementBase * pParent, LibraryImport * pImport ); + virtual ~LibElementBase() override; + + // XElement + virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL getParent() override; + virtual OUString SAL_CALL getLocalName() override; + virtual sal_Int32 SAL_CALL getUid() override; + virtual css::uno::Reference< css::xml::input::XAttributes > SAL_CALL getAttributes() override; + virtual void SAL_CALL ignorableWhitespace( + OUString const & rWhitespaces ) override; + virtual void SAL_CALL characters( OUString const & rChars ) override; + virtual void SAL_CALL processingInstruction( + OUString const & rTarget, OUString const & rData ) override; + virtual void SAL_CALL endElement() override; + virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement( + sal_Int32 nUid, OUString const & rLocalName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override; +}; + +class LibrariesElement : public LibElementBase +{ + friend class LibraryElement; + + std::vector< LibDescriptor > mLibDescriptors; + +public: + virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement( + sal_Int32 nUid, OUString const & rLocalName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override; + virtual void SAL_CALL endElement() override; + + LibrariesElement( + OUString const & rLocalName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, + LibraryImport * pImport ) + : LibElementBase( rLocalName, xAttributes, nullptr, pImport ) + {} +}; + +class LibraryElement : public LibElementBase +{ + std::vector< OUString > mElements; + +public: + + virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement( + sal_Int32 nUid, OUString const & rLocalName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override; + virtual void SAL_CALL endElement() override; + + LibraryElement( + OUString const & rLocalName, + css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, + LibElementBase * pParent, LibraryImport * pImport ) + : LibElementBase( rLocalName, xAttributes, pParent, pImport ) + {} +}; + +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmlscript/source/xmllib_imexp/xmllib_export.cxx b/xmlscript/source/xmllib_imexp/xmllib_export.cxx new file mode 100644 index 000000000..f7a3b17dd --- /dev/null +++ b/xmlscript/source/xmllib_imexp/xmllib_export.cxx @@ -0,0 +1,136 @@ +/* -*- 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 <rtl/ref.hxx> +#include <xmlscript/xmllib_imexp.hxx> +#include <xmlscript/xml_helper.hxx> +#include <xmlscript/xmlns.h> +#include <com/sun/star/xml/sax/XWriter.hpp> + +using namespace com::sun::star::uno; +using namespace com::sun::star; + +namespace xmlscript +{ + +const char aTrueStr[] = "true"; +const char aFalseStr[] = "false"; + +void +exportLibraryContainer( + Reference< xml::sax::XWriter > const & xOut, + const LibDescriptorArray* pLibArray ) +{ + xOut->startDocument(); + + xOut->unknown( + "<!DOCTYPE library:libraries PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\"" + " \"libraries.dtd\">" ); + xOut->ignorableWhitespace( OUString() ); + + OUString aLibrariesName( XMLNS_LIBRARY_PREFIX ":libraries" ); + XMLElement* pLibsElement = new XMLElement( aLibrariesName ); + Reference< xml::sax::XAttributeList > xAttributes( pLibsElement ); + + pLibsElement->addAttribute( "xmlns:" XMLNS_LIBRARY_PREFIX, XMLNS_LIBRARY_URI ); + pLibsElement->addAttribute( "xmlns:" XMLNS_XLINK_PREFIX, XMLNS_XLINK_URI ); + + xOut->ignorableWhitespace( OUString() ); + xOut->startElement( aLibrariesName, xAttributes ); + + OUString sTrueStr(aTrueStr); + OUString sFalseStr(aFalseStr); + + int nLibCount = pLibArray->mnLibCount; + for( sal_Int32 i = 0 ; i < nLibCount ; i++ ) + { + LibDescriptor& rLib = pLibArray->mpLibs[i]; + + rtl::Reference<XMLElement> pLibElement(new XMLElement( XMLNS_LIBRARY_PREFIX ":library" )); + + pLibElement->addAttribute( XMLNS_LIBRARY_PREFIX ":name", rLib.aName ); + + if( !rLib.aStorageURL.isEmpty() ) + { + pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":href", rLib.aStorageURL ); + pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":type", "simple" ); + } + + pLibElement->addAttribute( XMLNS_LIBRARY_PREFIX ":link", rLib.bLink ? sTrueStr : sFalseStr ); + + if( rLib.bLink ) + { + pLibElement->addAttribute( XMLNS_LIBRARY_PREFIX ":readonly", rLib.bReadOnly ? sTrueStr : sFalseStr ); + } + + pLibElement->dump( xOut.get() ); + } + + xOut->ignorableWhitespace( OUString() ); + xOut->endElement( aLibrariesName ); + + xOut->endDocument(); +} + +void +exportLibrary( + css::uno::Reference< css::xml::sax::XWriter > const & xOut, + const LibDescriptor& rLib ) +{ + xOut->startDocument(); + + xOut->unknown( + "<!DOCTYPE library:library PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\"" + " \"library.dtd\">" ); + xOut->ignorableWhitespace( OUString() ); + + rtl::Reference<XMLElement> pLibElement = new XMLElement( XMLNS_LIBRARY_PREFIX ":library" ); + + pLibElement->addAttribute( "xmlns:" XMLNS_LIBRARY_PREFIX, XMLNS_LIBRARY_URI ); + + pLibElement->addAttribute( XMLNS_LIBRARY_PREFIX ":name", rLib.aName ); + + OUString sTrueStr(aTrueStr); + OUString sFalseStr(aFalseStr); + + pLibElement->addAttribute( XMLNS_LIBRARY_PREFIX ":readonly", rLib.bReadOnly ? sTrueStr : sFalseStr ); + + pLibElement->addAttribute( XMLNS_LIBRARY_PREFIX ":passwordprotected", rLib.bPasswordProtected ? sTrueStr : sFalseStr ); + + if( rLib.bPreload ) + pLibElement->addAttribute( XMLNS_LIBRARY_PREFIX ":preload", sTrueStr ); + + for( const auto& rElementName : rLib.aElementNames ) + { + rtl::Reference<XMLElement> pElement(new XMLElement( XMLNS_LIBRARY_PREFIX ":element" )); + + pElement->addAttribute( XMLNS_LIBRARY_PREFIX ":name", + rElementName ); + + pLibElement->addSubElement( pElement.get() ); + } + + pLibElement->dump( xOut.get() ); + + xOut->endDocument(); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmlscript/source/xmllib_imexp/xmllib_import.cxx b/xmlscript/source/xmllib_imexp/xmllib_import.cxx new file mode 100644 index 000000000..5e063430e --- /dev/null +++ b/xmlscript/source/xmllib_imexp/xmllib_import.cxx @@ -0,0 +1,257 @@ +/* -*- 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 <sal/log.hxx> + +#include "imp_share.hxx" +#include <xml_import.hxx> +#include <xmlscript/xmlns.h> + +using namespace css; +using namespace css::uno; + +namespace xmlscript +{ + +Reference< xml::input::XElement > LibElementBase::getParent() +{ + return mxParent.get(); +} + +OUString LibElementBase::getLocalName() +{ + return _aLocalName; +} + +sal_Int32 LibElementBase::getUid() +{ + return mxImport->XMLNS_LIBRARY_UID; +} + +Reference< xml::input::XAttributes > LibElementBase::getAttributes() +{ + return _xAttributes; +} + +void LibElementBase::ignorableWhitespace( + OUString const & /*rWhitespaces*/ ) +{ +} + +void LibElementBase::characters( OUString const & /*rChars*/ ) +{ + // not used, all characters ignored +} + +void LibElementBase::processingInstruction( + OUString const & /*rTarget*/, OUString const & /*rData*/ ) +{ +} + +void LibElementBase::endElement() +{ +} +Reference< xml::input::XElement > LibElementBase::startChildElement( + sal_Int32 /*nUid*/, OUString const & /*rLocalName*/, + Reference< xml::input::XAttributes > const & /*xAttributes*/ ) +{ + throw xml::sax::SAXException("unexpected element!", Reference< XInterface >(), Any() ); +} + +LibElementBase::LibElementBase( + OUString const & rLocalName, + Reference< xml::input::XAttributes > const & xAttributes, + LibElementBase * pParent, LibraryImport * pImport ) + : mxImport( pImport ) + , mxParent( pParent ) + , _aLocalName( rLocalName ) + , _xAttributes( xAttributes ) +{ +} + +LibElementBase::~LibElementBase() +{ + SAL_INFO("xmlscript.xmllib", "LibElementBase::~LibElementBase(): " << _aLocalName ); +} + +// XRoot + +void LibraryImport::startDocument( + Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping ) +{ + XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri( XMLNS_LIBRARY_URI ); + XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri( XMLNS_XLINK_URI ); +} + +void LibraryImport::endDocument() +{ +} + +void LibraryImport::processingInstruction( + OUString const & /*rTarget*/, OUString const & /*rData*/ ) +{ +} + +void LibraryImport::setDocumentLocator( + Reference< xml::sax::XLocator > const & /*xLocator*/ ) +{ +} + +Reference< xml::input::XElement > LibraryImport::startRootElement( + sal_Int32 nUid, OUString const & rLocalName, + Reference< xml::input::XAttributes > const & xAttributes ) +{ + if (XMLNS_LIBRARY_UID != nUid) + { + throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() ); + } + else if ( mpLibArray && rLocalName == "libraries" ) + { + return new LibrariesElement( rLocalName, xAttributes, this ); + } + else if ( mpLibDesc && rLocalName == "library" ) + { + LibDescriptor& aDesc = *mpLibDesc; + aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = false; + + aDesc.aName = xAttributes->getValueByUidName(XMLNS_LIBRARY_UID, "name" ); + getBoolAttr( &aDesc.bReadOnly, "readonly", xAttributes, XMLNS_LIBRARY_UID ); + getBoolAttr( &aDesc.bPasswordProtected, "passwordprotected", xAttributes, XMLNS_LIBRARY_UID ); + getBoolAttr( &aDesc.bPreload, "preload", xAttributes, XMLNS_LIBRARY_UID ); + + return new LibraryElement( rLocalName, xAttributes, nullptr, this ); + } + else + { + throw xml::sax::SAXException( "illegal root element (expected libraries) given: " + rLocalName, Reference< XInterface >(), Any() ); + } +} + +LibraryImport::~LibraryImport() +{ + SAL_INFO("xmlscript.xmllib", "LibraryImport::~LibraryImport()." ); +} + +// libraries +Reference< xml::input::XElement > LibrariesElement::startChildElement( + sal_Int32 nUid, OUString const & rLocalName, + Reference< xml::input::XAttributes > const & xAttributes ) +{ + if (mxImport->XMLNS_LIBRARY_UID != nUid) + { + throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() ); + } + // library + else if ( rLocalName == "library" ) + { + LibDescriptor aDesc; + aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = false; + + aDesc.aName = xAttributes->getValueByUidName(mxImport->XMLNS_LIBRARY_UID, "name" ); + aDesc.aStorageURL = xAttributes->getValueByUidName( mxImport->XMLNS_XLINK_UID, "href" ); + getBoolAttr(&aDesc.bLink, "link", xAttributes, mxImport->XMLNS_LIBRARY_UID ); + getBoolAttr(&aDesc.bReadOnly, "readonly", xAttributes, mxImport->XMLNS_LIBRARY_UID ); + getBoolAttr(&aDesc.bPasswordProtected, "passwordprotected", xAttributes, mxImport->XMLNS_LIBRARY_UID ); + + mLibDescriptors.push_back( aDesc ); + return new LibraryElement( rLocalName, xAttributes, this, mxImport.get() ); + } + else + { + throw xml::sax::SAXException( "expected styles of bulletinboard element!", Reference< XInterface >(), Any() ); + } +} + +void LibrariesElement::endElement() +{ + sal_Int32 nLibCount = mxImport->mpLibArray->mnLibCount = static_cast<sal_Int32>(mLibDescriptors.size()); + mxImport->mpLibArray->mpLibs.reset( new LibDescriptor[ nLibCount ] ); + + for( sal_Int32 i = 0 ; i < nLibCount ; i++ ) + { + const LibDescriptor& rLib = mLibDescriptors[i]; + mxImport->mpLibArray->mpLibs[i] = rLib; + } +} + +// library +Reference< xml::input::XElement > LibraryElement::startChildElement( + sal_Int32 nUid, OUString const & rLocalName, + Reference< xml::input::XAttributes > const & xAttributes ) +{ + if (mxImport->XMLNS_LIBRARY_UID != nUid) + { + throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() ); + } + // library + else if ( rLocalName == "element" ) + { + OUString aValue( xAttributes->getValueByUidName(mxImport->XMLNS_LIBRARY_UID, "name" ) ); + if (!aValue.isEmpty()) + mElements.push_back( aValue ); + + return new LibElementBase( rLocalName, xAttributes, this, mxImport.get() ); + } + else + { + throw xml::sax::SAXException( "expected styles or bulletinboard element!", Reference< XInterface >(), Any() ); + } +} + +void LibraryElement::endElement() +{ + sal_Int32 nElementCount = mElements.size(); + Sequence< OUString > aElementNames( nElementCount ); + OUString* pElementNames = aElementNames.getArray(); + for( sal_Int32 i = 0 ; i < nElementCount ; i++ ) + pElementNames[i] = mElements[i]; + + LibDescriptor* pLib = mxImport->mpLibDesc; + if( !pLib ) + pLib = &static_cast< LibrariesElement* >( mxParent.get() )->mLibDescriptors.back(); + pLib->aElementNames = aElementNames; +} + +Reference< css::xml::sax::XDocumentHandler > +importLibraryContainer( LibDescriptorArray* pLibArray ) +{ + return ::xmlscript::createDocumentHandler(new LibraryImport(pLibArray)); +} + +css::uno::Reference< css::xml::sax::XDocumentHandler > +importLibrary( LibDescriptor& rLib ) +{ + return ::xmlscript::createDocumentHandler(new LibraryImport(&rLib)); +} + +LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount ) +{ + mnLibCount = nLibCount; + mpLibs.reset( new LibDescriptor[ mnLibCount ] ); +} + +LibDescriptorArray::~LibDescriptorArray() +{ +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |