diff options
Diffstat (limited to '')
-rw-r--r-- | include/sax/fastattribs.hxx | 250 | ||||
-rw-r--r-- | include/sax/fastparser.hxx | 79 | ||||
-rw-r--r-- | include/sax/fshelper.hxx | 179 | ||||
-rw-r--r-- | include/sax/saxdllapi.h | 33 | ||||
-rw-r--r-- | include/sax/tools/converter.hxx | 313 | ||||
-rw-r--r-- | include/sax/tools/documenthandleradapter.hxx | 214 |
6 files changed, 1068 insertions, 0 deletions
diff --git a/include/sax/fastattribs.hxx b/include/sax/fastattribs.hxx new file mode 100644 index 0000000000..fb6f47eb7f --- /dev/null +++ b/include/sax/fastattribs.hxx @@ -0,0 +1,250 @@ +/* -*- 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_SAX_FASTATTRIBS_HXX +#define INCLUDED_SAX_FASTATTRIBS_HXX + +#include <com/sun/star/xml/sax/XFastAttributeList.hpp> +#include <com/sun/star/xml/sax/XFastTokenHandler.hpp> +#include <com/sun/star/util/XCloneable.hpp> + +#include <cppuhelper/implbase.hxx> +#include <o3tl/string_view.hxx> +#include <sax/saxdllapi.h> + +#include <string_view> +#include <vector> + +namespace com::sun::star::xml::sax { class XFastTokenHandler; } +namespace com::sun::star::xml { struct Attribute; } +namespace com::sun::star::xml { struct FastAttribute; } + +namespace sax_fastparser +{ + +struct UnknownAttribute +{ + OUString maNamespaceURL; + OString maName; + OString maValue; + + UnknownAttribute( OUString sNamespaceURL, OString aName, OString value ); + UnknownAttribute( OString sName, OString value ); + + void FillAttribute( css::xml::Attribute* pAttrib ) const; +}; + +/// A native C++ interface to tokenisation +class SAX_DLLPUBLIC FastTokenHandlerBase : + public cppu::WeakImplHelper< css::xml::sax::XFastTokenHandler > +{ + public: + virtual ~FastTokenHandlerBase(); + virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const = 0; + + /** + * Client method to attempt the use of this interface if possible. + * @xTokenHandler - the token lookup interface + * @str - string buffer to lookup + * + * @return Tokenized form of str + */ + static sal_Int32 getTokenFromChars( + const FastTokenHandlerBase *pTokenHandler, + std::string_view str ); +}; + + +class SAX_DLLPUBLIC FastAttributeList final : public cppu::WeakImplHelper< css::xml::sax::XFastAttributeList, css::util::XCloneable > +{ +public: + FastAttributeList( FastTokenHandlerBase *pTokenHandler ); + FastAttributeList( const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ); + virtual ~FastAttributeList() override; + + void clear(); + void reserve( sal_Int32 nNumTokens ) + { + maAttributeValues.reserve(nNumTokens+1); + maAttributeTokens.reserve(nNumTokens); + } + void add( const FastAttributeList& ); + void add( const css::uno::Reference<css::xml::sax::XFastAttributeList>& ); + void add( sal_Int32 nToken, std::string_view value ); + void add( sal_Int32 nToken, std::u16string_view sValue ); // Converts to UTF-8 + template <typename C, typename T1, typename T2> + void add( sal_Int32 nToken, rtl::StringConcat<C, T1, T2>&& value) { add(nToken, Concat2View(value)); } + template <typename Val, typename... Rest, std::enable_if_t<(sizeof...(Rest) > 0), int> = 0> + void add( sal_Int32 nToken, Val&& val, Rest&&... rest ) + { + add(nToken, std::forward<Val>(val)); + add(std::forward<Rest>(rest)...); + } + void addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, std::string_view sValue ); + void addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, std::u16string_view sValue ); + // note: rQName is *namespace-prefixed* + void addUnknown( const OUString& rNamespaceURL, const OString& rQName, const OString& value ); + void addUnknown( const OString& rName, const OString& value ); + const std::vector< sal_Int32 >& getFastAttributeTokens() const { return maAttributeTokens; } + const char* getFastAttributeValue(size_t nIndex) const { return mpChunk + maAttributeValues[nIndex]; } + sal_Int32 AttributeValueLength(size_t i) const { return maAttributeValues[i + 1] - maAttributeValues[i] - 1; } + size_t size() const { return maAttributeValues.size(); } + + // performance sensitive shortcuts to avoid allocation ... + bool getAsInteger( sal_Int32 nToken, sal_Int32 &rInt) const; + bool getAsDouble( sal_Int32 nToken, double &rDouble) const; + bool getAsView( sal_Int32 nToken, std::string_view& rPos ) const; + sal_Int32 getAsIntegerByIndex( sal_Int32 nTokenIndex ) const + { + return o3tl::toInt32(getAsViewByIndex(nTokenIndex)); + } + std::string_view getAsViewByIndex( sal_Int32 nTokenIndex ) const + { + return std::string_view(getFastAttributeValue(nTokenIndex), AttributeValueLength(nTokenIndex)); + } + OUString getValueByIndex( sal_Int32 nTokenIndex ) const + { + return OStringToOUString(getAsViewByIndex(nTokenIndex), RTL_TEXTENCODING_UTF8); + } + + // XFastAttributeList + virtual sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) override; + virtual ::sal_Int32 SAL_CALL getValueToken( ::sal_Int32 Token ) override; + virtual ::sal_Int32 SAL_CALL getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) override; + virtual OUString SAL_CALL getValue( ::sal_Int32 Token ) override; + virtual OUString SAL_CALL getOptionalValue( ::sal_Int32 Token ) override; + virtual css::uno::Sequence< css::xml::Attribute > SAL_CALL getUnknownAttributes( ) override; + virtual css::uno::Sequence< css::xml::FastAttribute > SAL_CALL getFastAttributes() override; + + // XCloneable + virtual ::css::uno::Reference< ::css::util::XCloneable > SAL_CALL createClone() override; + + sal_Int32 getAttributeIndex( ::sal_Int32 Token ) + { + for (size_t i=0; i<maAttributeTokens.size(); ++i) + if (maAttributeTokens[i] == Token) + return i; + return -1; + } + + /// Use for fast iteration and conversion of attributes + class FastAttributeIter { + const FastAttributeList &mrList; + size_t mnIdx; + + public: + FastAttributeIter(const FastAttributeList &rList, size_t nIdx) + : mrList(rList), mnIdx(nIdx) + { + } + + FastAttributeIter& operator++ () + { + mnIdx++; + return *this; + } + bool operator!=( const FastAttributeIter& rhs ) const + { + return mnIdx != rhs.mnIdx; + } + + const FastAttributeIter& operator*() const + { + return *this; + } + + sal_Int32 getToken() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return mrList.maAttributeTokens[mnIdx]; + } + bool isEmpty() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return mrList.AttributeValueLength(mnIdx) < 1; + } + sal_Int32 toInt32() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return mrList.getAsIntegerByIndex(mnIdx); + } + double toDouble() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return o3tl::toDouble(mrList.getAsViewByIndex(mnIdx)); + } + bool toBoolean() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return rtl_str_toBoolean(mrList.getFastAttributeValue(mnIdx)); + } + OUString toString() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return mrList.getValueByIndex(mnIdx); + } + const char* toCString() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return mrList.getFastAttributeValue(mnIdx); + } + sal_Int32 getLength() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return mrList.AttributeValueLength(mnIdx); + } + std::string_view toView() const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return mrList.getAsViewByIndex(mnIdx); + } + bool isString(const char *str) const + { + assert(mnIdx < mrList.maAttributeTokens.size()); + return !strcmp(str, mrList.getFastAttributeValue(mnIdx)); + } + }; + FastAttributeIter begin() const { return FastAttributeIter(*this, 0); } + FastAttributeIter end() const { return FastAttributeIter(*this, maAttributeTokens.size()); } + FastAttributeIter find( sal_Int32 nToken ) const; + +private: + char *mpChunk; ///< buffer to store all attribute values - null terminated strings + sal_Int32 mnChunkLength; ///< size of allocated memory for mpChunk + // maAttributeValues stores pointers, relative to mpChunk, for each attribute value string + // length of the string is maAttributeValues[n+1] - maAttributeValues[n] - 1 + // maAttributeValues[0] == 0 + std::vector< sal_Int32 > maAttributeValues; + std::vector< sal_Int32 > maAttributeTokens; + std::vector< UnknownAttribute > maUnknownAttributes; + FastTokenHandlerBase * mpTokenHandler; +}; + +inline FastAttributeList& castToFastAttributeList( + const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) +{ + assert( dynamic_cast <FastAttributeList *> ( xAttrList.get() ) != nullptr ); + return *static_cast <FastAttributeList *> ( xAttrList.get() ); +} + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/sax/fastparser.hxx b/include/sax/fastparser.hxx new file mode 100644 index 0000000000..fe388fce21 --- /dev/null +++ b/include/sax/fastparser.hxx @@ -0,0 +1,79 @@ +/* -*- 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_SAX_FASTPARSER_HXX +#define INCLUDED_SAX_FASTPARSER_HXX + +#include <com/sun/star/xml/sax/XFastParser.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <cppuhelper/implbase.hxx> +#include <com/sun/star/lang/XInitialization.hpp> + +#include <sax/saxdllapi.h> +#include <memory> + +namespace com::sun::star::xml::sax { + class XFastDocumentHandler; + class XFastTokenHandler; +} + +namespace sax_fastparser { + + +class FastSaxParserImpl; + +// This class implements the external Parser interface +class SAX_DLLPUBLIC FastSaxParser final + : public ::cppu::WeakImplHelper< + css::lang::XInitialization, + css::xml::sax::XFastParser, + css::lang::XServiceInfo > +{ + std::unique_ptr<FastSaxParserImpl> mpImpl; + +public: + FastSaxParser(); + virtual ~FastSaxParser() override; + + // css::lang::XInitialization: + virtual void SAL_CALL initialize(css::uno::Sequence<css::uno::Any> const& rArguments) override; + + // XFastParser + virtual void SAL_CALL parseStream( const css::xml::sax::InputSource& aInputSource ) override; + virtual void SAL_CALL setFastDocumentHandler( const css::uno::Reference< css::xml::sax::XFastDocumentHandler >& Handler ) override; + virtual void SAL_CALL setTokenHandler( const css::uno::Reference< css::xml::sax::XFastTokenHandler >& Handler ) override; + virtual void SAL_CALL registerNamespace( const OUString& NamespaceURL, sal_Int32 NamespaceToken ) override; + virtual OUString SAL_CALL getNamespaceURL( const OUString& rPrefix ) override; + virtual void SAL_CALL setErrorHandler( const css::uno::Reference< css::xml::sax::XErrorHandler >& Handler ) override; + virtual void SAL_CALL setEntityResolver( const css::uno::Reference< css::xml::sax::XEntityResolver >& Resolver ) override; + virtual void SAL_CALL setLocale( const css::lang::Locale& rLocale ) override; + virtual void SAL_CALL setNamespaceHandler( const css::uno::Reference< css::xml::sax::XFastNamespaceHandler >& Handler) override; + virtual void SAL_CALL setCustomEntityNames( const ::css::uno::Sequence< ::css::beans::Pair<::rtl::OUString, ::rtl::OUString> >& replacements ) override; + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName( ) override; + virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override; + virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override; +}; + +} + +#endif // _SAX_FASTPARSER_HXX_ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/sax/fshelper.hxx b/include/sax/fshelper.hxx new file mode 100644 index 0000000000..74e2ed4e32 --- /dev/null +++ b/include/sax/fshelper.hxx @@ -0,0 +1,179 @@ +/* -*- 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_SAX_FSHELPER_HXX +#define INCLUDED_SAX_FSHELPER_HXX + +#include <com/sun/star/xml/sax/XFastAttributeList.hpp> +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <rtl/ustring.hxx> +#include <rtl/ref.hxx> +#include <sax/saxdllapi.h> +#include <optional> +#include <memory> +#include <string_view> +#include <utility> + +namespace com::sun::star::io { class XOutputStream; } +namespace sax_fastparser { class FastAttributeList; } + +constexpr sal_Int32 FSNS(sal_Int32 namespc, sal_Int32 element) { return (namespc << 16) | element; } + +namespace sax_fastparser { + +enum class MergeMarks { APPEND = 0, PREPEND = 1, POSTPONE = 2}; + +class FastSaxSerializer; + +class SAX_DLLPUBLIC FastSerializerHelper +{ +public: + + FastSerializerHelper( const css::uno::Reference< css::io::XOutputStream >& xOutputStream, bool bWriteHeader ); + + ~FastSerializerHelper(); + + void startDocument(); + void endDocument(); + + /// Start an element. After the first argument there can be a number of (attribute, value) pairs. + template<typename... Args> + void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, Args &&... args) + { + if (value) + pushAttributeValue(attribute, value); + startElement(elementTokenId, std::forward<Args>(args)...); + } + template<typename... Args> + void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, + const std::optional<OString>& value, Args&&... args) + { + if (value) + pushAttributeValue(attribute, *value); + startElement(elementTokenId, std::forward<Args>(args)...); + } + template<typename... Args> + void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, + const std::optional<OUString>& value, Args&&... args) + { + std::optional<OString> opt; + if (value) + opt = value->toUtf8(); + startElement(elementTokenId, attribute, opt, std::forward<Args>(args)...); + } + void startElement(sal_Int32 elementTokenId); + + /// Start an element. After the first two arguments there can be a number of (attribute, value) pairs. + template<typename... Args> + void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, Args &&... args) + { + startElement(FSNS(namespaceTokenId, elementTokenId), std::forward<Args>(args)...); + } + + /// Create a single element. After the first argument there can be a number of (attribute, value) pairs. + template<typename... Args> + void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, Args &&... args) + { + if (value) + pushAttributeValue(attribute, value); + singleElement(elementTokenId, std::forward<Args>(args)...); + } + template<typename... Args> + void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, + const std::optional<OString>& value, Args&&... args) + { + if (value) + pushAttributeValue(attribute, *value); + singleElement(elementTokenId, std::forward<Args>(args)...); + } + template<typename... Args> + void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, + const std::optional<OUString>& value, Args&&... args) + { + std::optional<OString> opt; + if (value) + opt = value->toUtf8(); + singleElement(elementTokenId, attribute, opt, std::forward<Args>(args)...); + } + void singleElement(sal_Int32 elementTokenId); + + /// Create a single element. After the first two arguments there can be a number of (attribute, value) pairs. + template<typename... Args> + void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, Args &&... args) + { + singleElement(FSNS(namespaceTokenId, elementTokenId), std::forward<Args>(args)...); + } + + void endElement(sal_Int32 elementTokenId); + void endElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId) + { endElement( FSNS( namespaceTokenId, elementTokenId ) ); } + + void singleElement(sal_Int32 elementTokenId, const rtl::Reference<FastAttributeList>& xAttrList); + + void startElement(sal_Int32 elementTokenId, const rtl::Reference<FastAttributeList>& xAttrList); + + FastSerializerHelper* write(const char* value); + FastSerializerHelper* write(const OString& value); + FastSerializerHelper* write(std::u16string_view value); + FastSerializerHelper* write(sal_Int32 value); + FastSerializerHelper* write(sal_Int64 value); + FastSerializerHelper* write(double value); + + FastSerializerHelper* writeEscaped(const char* value); + FastSerializerHelper* writeEscaped(std::u16string_view value); + + FastSerializerHelper* writeId(sal_Int32 tokenId); + + css::uno::Reference< css::io::XOutputStream > const & getOutputStream() const; + + static rtl::Reference<FastAttributeList> createAttrList(); + + void mark(sal_Int32 nTag, + const css::uno::Sequence< sal_Int32 >& rOrder = + css::uno::Sequence< sal_Int32 >() ); + void mergeTopMarks(sal_Int32 nTag, + MergeMarks eMergeType = MergeMarks::APPEND ); + + void setAllowXEscape(bool bSet); + +private: + void pushAttributeValue( sal_Int32 attribute, const char* value ); + void pushAttributeValue( sal_Int32 attribute, const OString& value ); + + std::unique_ptr<FastSaxSerializer> mpSerializer; +}; + +typedef std::shared_ptr< FastSerializerHelper > FSHelperPtr; + +// Helpers to make intention to pass optional attributes to *Element functions explicit, instead of +// using `(condition) ? value.toUtf8().getStr() : nullptr` syntax. +inline const char* UseIf(const char* s, bool bUse) { return bUse ? s : nullptr; } +// OString, OUString +template<class TString> +std::optional<TString> UseIf(const TString& s, bool bUse) +{ + return bUse ? std::optional<TString>(s) : std::optional<TString>(); +} + +} + +#endif // INCLUDED_SAX_FSHELPER_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/sax/saxdllapi.h b/include/sax/saxdllapi.h new file mode 100644 index 0000000000..9b56b94ce8 --- /dev/null +++ b/include/sax/saxdllapi.h @@ -0,0 +1,33 @@ +/* -*- 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_SAX_SAXDLLAPI_H +#define INCLUDED_SAX_SAXDLLAPI_H + +#include <sal/types.h> + +#if defined SAX_DLLIMPLEMENTATION +#define SAX_DLLPUBLIC SAL_DLLPUBLIC_EXPORT +#else +#define SAX_DLLPUBLIC SAL_DLLPUBLIC_IMPORT +#endif + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/sax/tools/converter.hxx b/include/sax/tools/converter.hxx new file mode 100644 index 0000000000..d797eace30 --- /dev/null +++ b/include/sax/tools/converter.hxx @@ -0,0 +1,313 @@ +/* -*- 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_SAX_TOOLS_CONVERTER_HXX +#define INCLUDED_SAX_TOOLS_CONVERTER_HXX + +#include <sal/config.h> + +#include <optional> +#include <type_traits> + +#include <sax/saxdllapi.h> + +#include <rtl/strbuf.hxx> +#include <sal/types.h> +#include <rtl/ustrbuf.hxx> +#include <com/sun/star/util/MeasureUnit.hpp> +#include <tools/color.hxx> +#include <unotools/saveopt.hxx> + +namespace com::sun::star { + namespace uno { + class Any; + } + namespace util { + struct Date; + struct DateTime; + struct Duration; + } +} + +namespace sax { + +/** the Converter converts values of various types from + their internal representation to the textual form used in xml + and back. + + All unit types are expressed as css::util::MeasureUnit +*/ + + +class SAX_DLLPUBLIC Converter +{ +public: + /** convert string to measure using optional min and max values*/ + static bool convertMeasure( sal_Int32& rValue, + std::u16string_view rString, + sal_Int16 nTargetUnit = css::util::MeasureUnit::MM_100TH, + sal_Int32 nMin = SAL_MIN_INT32, + sal_Int32 nMax = SAL_MAX_INT32 ); + + /** convert string to measure using optional min and max values*/ + static bool convertMeasure( sal_Int32& rValue, + std::string_view rString, + sal_Int16 nTargetUnit = css::util::MeasureUnit::MM_100TH, + sal_Int32 nMin = SAL_MIN_INT32, + sal_Int32 nMax = SAL_MAX_INT32 ); + + /** convert measure to string */ + static void convertMeasure( OUStringBuffer& rBuffer, + sal_Int32 nMeasure, + sal_Int16 SourceUnit, + sal_Int16 nTargetUnit ); + + /** convert string to boolean */ + static bool convertBool( bool& rBool, + std::u16string_view rString ); + + /** convert string to boolean */ + static bool convertBool( bool& rBool, + std::string_view rString ); + + /** convert boolean to string */ + static void convertBool( OUStringBuffer& rBuffer, + bool bValue ); + + /** convert string to percent */ + static bool convertPercent( sal_Int32& rValue, + std::u16string_view rString ); + + /** convert string to percent */ + static bool convertPercent( sal_Int32& rValue, + std::string_view rString ); + + /** convert percent to string */ + static void convertPercent( OUStringBuffer& rBuffer, + sal_Int32 nValue ); + + /** convert string to pixel measure unit */ + static bool convertMeasurePx( sal_Int32& rValue, + std::u16string_view rString ); + + /** convert string to pixel measure unit */ + static bool convertMeasurePx( sal_Int32& rValue, + std::string_view rString ); + + /** convert pixel measure unit to string */ + static void convertMeasurePx( OUStringBuffer& rBuffer, + sal_Int32 nValue ); + + /** convert string to rgb color */ + static bool convertColor( sal_Int32& rColor, + std::u16string_view rValue ); + static bool convertColor( sal_Int32& rColor, + std::string_view rValue ); + static bool convertColor( ::Color& rColor, + std::u16string_view rValue ) + { + sal_Int32 n(rColor); + bool b = convertColor( n, rValue ); + if (b) rColor = Color(ColorTransparency, n); + return b; + } + static bool convertColor( ::Color& rColor, + std::string_view rValue ) + { + sal_Int32 n(rColor); + bool b = convertColor( n, rValue ); + if (b) rColor = Color(ColorTransparency, n); + return b; + } + + /** convert color to string */ + static void convertColor( OUStringBuffer &rBuffer, + sal_Int32 nColor ); + static void convertColor( OUStringBuffer &rBuffer, + ::Color nColor ) + { convertColor( rBuffer, sal_Int32(nColor) ); } + + /** convert string to number with optional min and max values */ + static bool convertNumber( sal_Int32& rValue, + std::u16string_view aString, + sal_Int32 nMin = SAL_MIN_INT32, + sal_Int32 nMax = SAL_MAX_INT32 ); + + /** convert string to number with optional min and max values */ + static bool convertNumber( sal_Int32& rValue, + std::string_view aString, + sal_Int32 nMin = SAL_MIN_INT32, + sal_Int32 nMax = SAL_MAX_INT32 ); + + /** convert string to number with optional min and max values */ + static bool convertNumber64(sal_Int64& rValue, + std::u16string_view aString, + sal_Int64 nMin = SAL_MIN_INT64, + sal_Int64 nMax = SAL_MAX_INT64); + + /** convert string to number with optional min and max values */ + static bool convertNumber64(sal_Int64& rValue, + std::string_view aString, + sal_Int64 nMin = SAL_MIN_INT64, + sal_Int64 nMax = SAL_MAX_INT64); + + /** convert double number to string (using ::rtl::math) and + DO convert from source unit to target unit */ + static void convertDouble( OUStringBuffer& rBuffer, + double fNumber, + bool bWriteUnits, + sal_Int16 nSourceUnit, + sal_Int16 nTargetUnit ); + + /** convert double number to string (using ::rtl::math) without unit conversion */ + static void convertDouble( OUStringBuffer& rBuffer, double fNumber); + + /** convert string to double number (using ::rtl::math) and DO convert from + source unit to target unit. */ + static bool convertDouble( double& rValue, + std::u16string_view rString, + sal_Int16 nSourceUnit, + sal_Int16 nTargetUnit ); + + /** convert string to double number (using ::rtl::math) and DO convert from + source unit to target unit. */ + static bool convertDouble( double& rValue, + std::string_view rString, + sal_Int16 nSourceUnit, + sal_Int16 nTargetUnit ); + + /** convert string to double number (using ::rtl::math) without unit conversion */ + static bool convertDouble(double& rValue, std::u16string_view rString); + + /** convert string to double number (using ::rtl::math) without unit conversion */ + static bool convertDouble(double& rValue, std::string_view rString); + + /** convert number, 10th of degrees with range [0..3600] to SVG angle */ + static void convertAngle(OUStringBuffer& rBuffer, sal_Int16 nAngle, + SvtSaveOptions::ODFSaneDefaultVersion nVersion); + + /** convert SVG angle to number, 10th of degrees with range [0..3600] */ + static bool convertAngle(sal_Int16& rAngle, std::u16string_view rString, + bool isWrongOOo10thDegAngle); + + /** convert SVG angle to number, 10th of degrees with range [0..3600] */ + static bool convertAngle(sal_Int16& rAngle, std::string_view rString, + bool isWrongOOo10thDegAngle); + + /** convert double to XMLSchema-2 "duration" string; negative durations allowed */ + static void convertDuration(OUStringBuffer& rBuffer, + const double fTime); + + /** convert util::Duration to XMLSchema-2 "duration" string */ + static void convertDuration(OUStringBuffer& rBuffer, + const css::util::Duration& rDuration); + + /** convert XMLSchema-2 "duration" string to double; negative durations allowed */ + static bool convertDuration(double & rfTime, + std::string_view rString); + + /** convert XMLSchema-2 "duration" string to util::Duration */ + static bool convertDuration(css::util::Duration& rDuration, + std::u16string_view rString); + + /** convert XMLSchema-2 "duration" string to util::Duration */ + static bool convertDuration(css::util::Duration& rDuration, + std::string_view rString); + + /** convert util::Date to XMLSchema-2 "date" string */ + static void convertDate( OUStringBuffer& rBuffer, + const css::util::Date& rDate, + sal_Int16 const* pTimeZoneOffset); + + /** convert util::DateTime to XMLSchema-2 "date" or "dateTime" string */ + static void convertDateTime( OUStringBuffer& rBuffer, + const css::util::DateTime& rDateTime, + sal_Int16 const* pTimeZoneOffset, + bool bAddTimeIf0AM = false ); + + /** convert util::DateTime to XMLSchema-2 "time" or "dateTime" string */ + static void convertTimeOrDateTime(OUStringBuffer& rBuffer, + const css::util::DateTime& rDateTime); + + /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */ + static bool parseDateTime( css::util::DateTime& rDateTime, + std::u16string_view rString ); + + /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */ + static bool parseDateTime( css::util::DateTime& rDateTime, + std::string_view rString ); + + /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */ + static bool parseTimeOrDateTime(css::util::DateTime& rDateTime, + std::u16string_view rString); + + /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */ + static bool parseTimeOrDateTime(css::util::DateTime& rDateTime, + std::string_view rString); + + /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or + util::Date */ + static bool parseDateOrDateTime( + css::util::Date * pDate, + css::util::DateTime & rDateTime, + bool & rbDateTime, + std::optional<sal_Int16> * pTimeZoneOffset, + std::u16string_view rString ); + + /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or + util::Date */ + static bool parseDateOrDateTime( + css::util::Date * pDate, + css::util::DateTime & rDateTime, + bool & rbDateTime, + std::optional<sal_Int16> * pTimeZoneOffset, + std::string_view rString ); + + /** gets the position of the first comma after npos in the string + rStr. Commas inside '"' pairs are not matched */ + static sal_Int32 indexOfComma( std::u16string_view rStr, + sal_Int32 nPos ); + + static double GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit); + static double GetConversionFactor(OStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit); + static sal_Int16 GetUnitFromString(std::u16string_view rString, sal_Int16 nDefaultUnit); + static sal_Int16 GetUnitFromString(std::string_view rString, sal_Int16 nDefaultUnit); + + /** convert an Any to string (typesafe) */ + static bool convertAny(OUStringBuffer& rsValue, + OUStringBuffer& rsType , + const css::uno::Any& rValue); + + /** convert specified byte sequence to xsd:hexBinary string **/ + static void convertBytesToHexBinary(OUStringBuffer& rBuffer, const void* pBytes, + sal_Int32 nBytes); + + template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0> + static void convertNumberToHexBinary(OUStringBuffer& rBuffer, T n) + { + convertBytesToHexBinary(rBuffer, &n, sizeof(n)); + } + +}; + +} + +#endif // INCLUDED_SAX_TOOLS_CONVERTER_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/sax/tools/documenthandleradapter.hxx b/include/sax/tools/documenthandleradapter.hxx new file mode 100644 index 0000000000..0296abfd66 --- /dev/null +++ b/include/sax/tools/documenthandleradapter.hxx @@ -0,0 +1,214 @@ +/* -*- 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/. + */ + +#ifndef INCLUDED_SAX_TOOLS_DOCUMENTHANDLERADAPTER_HXX +#define INCLUDED_SAX_TOOLS_DOCUMENTHANDLERADAPTER_HXX + +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> + +namespace sax +{ + /** + * DocumentHandlerAdapter provides a base class for simple decorators to XDocumentHandlers. + * It forwards all method calls to a delegate. An inheriting class only needs to override the + * methods it actually wants to modify. + * + * See filters/source/odfflatxml/FlatXml.cxx for an example. + */ + class DocumentHandlerAdapter : public css::xml::sax::XDocumentHandler + { + public: + // XDocumentHandler + virtual void SAL_CALL + startDocument() override + { + m_handler->startDocument(); + } + + virtual void SAL_CALL + endDocument() override + { + m_handler->endDocument(); + } + + virtual void SAL_CALL + startElement(const OUString& aName, + const css::uno::Reference< css::xml::sax::XAttributeList > & xAttribs) override + { + m_handler->startElement(aName, xAttribs); + } + + virtual void SAL_CALL + endElement(const OUString& aName) override + { + m_handler->endElement(aName); + } + + virtual void SAL_CALL + characters(const OUString& aChars) override + { + m_handler->characters(aChars); + } + + virtual void SAL_CALL + ignorableWhitespace(const OUString& aWhitespaces) override + { + m_handler->ignorableWhitespace(aWhitespaces); + } + virtual void SAL_CALL + processingInstruction(const OUString& aTarget, const OUString& aData) override + { + m_handler->processingInstruction(aTarget, aData); + } + virtual void SAL_CALL + setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator > & xLocator) override + { + m_handler->setDocumentLocator(xLocator); + } + DocumentHandlerAdapter() : + m_handler(css::uno::Reference< css::xml::sax::XDocumentHandler > (nullptr, css::uno::UNO_QUERY)) + { + } + ; + + protected: + void + setDelegate(const css::uno::Reference< css::xml::sax::XDocumentHandler >& delegate) + { + m_handler = delegate; + } + const css::uno::Reference< css::xml::sax::XDocumentHandler >& + getDelegate() const + { + return m_handler; + } + virtual + ~DocumentHandlerAdapter() + { + + } + + private: + css::uno::Reference< css::xml::sax::XDocumentHandler > m_handler; + + }; + + /** + * ExtendedDocumentHandlerAdapter provides a base class for simple decorators to XExtendedDocumentHandlers. + * It forwards all method calls to a delegate. An inheriting class only needs to override the + * methods it actually wants to modify. + */ + class ExtendedDocumentHandlerAdapter : public css::xml::sax::XExtendedDocumentHandler + + { + + public: + // XDocumentHandler + virtual void SAL_CALL + startDocument() override + { + m_handler->startDocument(); + } + + virtual void SAL_CALL + endDocument() override + { + m_handler->endDocument(); + } + + virtual void SAL_CALL + startElement(const OUString& aName, + const css::uno::Reference< css::xml::sax::XAttributeList > & xAttribs) override + { + m_handler->startElement(aName, xAttribs); + } + + virtual void SAL_CALL + endElement(const OUString& aName) override + { + m_handler->endElement(aName); + } + + virtual void SAL_CALL + characters(const OUString& aChars) override + { + m_handler->characters(aChars); + } + + virtual void SAL_CALL + ignorableWhitespace(const OUString& aWhitespaces) override + { + m_handler->ignorableWhitespace(aWhitespaces); + } + virtual void SAL_CALL + processingInstruction(const OUString& aTarget, const OUString& aData) override + { + m_handler->processingInstruction(aTarget, aData); + } + virtual void SAL_CALL + setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator > & xLocator) override + { + m_handler->setDocumentLocator(xLocator); + } + // XExtendedDocumentHandler + virtual void SAL_CALL + startCDATA() override + { + m_handler->startCDATA(); + } + virtual void SAL_CALL + endCDATA() override + { + m_handler->endCDATA(); + } + virtual void SAL_CALL + comment(const OUString& sComment) override + { + m_handler->comment(sComment); + } + virtual void SAL_CALL + unknown(const OUString& sString) override + { + m_handler->unknown(sString); + } + virtual void SAL_CALL + allowLineBreak() override + { + m_handler->allowLineBreak(); + } + protected: + ExtendedDocumentHandlerAdapter() : + m_handler(css::uno::Reference< css::xml::sax::XExtendedDocumentHandler > (nullptr, css::uno::UNO_QUERY)) + { + } + + void + setDelegate(const css::uno::Reference< css::xml::sax::XExtendedDocumentHandler >& delegate) + { + m_handler = delegate; + } + const css::uno::Reference< css::xml::sax::XExtendedDocumentHandler >& + getDelegate() const + { + return m_handler; + } + virtual + ~ExtendedDocumentHandlerAdapter() + { + + } + + private: + css::uno::Reference< css::xml::sax::XExtendedDocumentHandler > m_handler; + }; +} +#endif // INCLUDED_SAX_TOOLS_DOCUMENTHANDLERADAPTER_HXX +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |