diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2019-09-29 19:40:09 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-10-07 21:31:17 +0200 |
commit | 36c965311f53dd9feae63ed4f8ab6de4cc449b99 (patch) | |
tree | 1598e4cd1cbe7b46cdd7facdd540fb88ca051a75 /linguistic | |
parent | be634d021229408bf9e30b4cb75be5debf9a3ffd (diff) |
use FastParser in linguistic
Change-Id: Iee2254dc13ccc31d266009ba1eb00f2517484cc7
Reviewed-on: https://gerrit.libreoffice.org/79814
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'linguistic')
-rw-r--r-- | linguistic/Library_lng.mk | 1 | ||||
-rw-r--r-- | linguistic/source/convdicxml.cxx | 181 | ||||
-rw-r--r-- | linguistic/source/convdicxml.hxx | 46 |
3 files changed, 120 insertions, 108 deletions
diff --git a/linguistic/Library_lng.mk b/linguistic/Library_lng.mk index d00f7637e52c..39a908f7bd69 100644 --- a/linguistic/Library_lng.mk +++ b/linguistic/Library_lng.mk @@ -38,6 +38,7 @@ $(eval $(call gb_Library_use_libraries,lng,\ cppuhelper \ i18nlangtag \ sal \ + sax \ svl \ tl \ ucbhelper \ diff --git a/linguistic/source/convdicxml.cxx b/linguistic/source/convdicxml.cxx index f7320100dc3b..55f51404a929 100644 --- a/linguistic/source/convdicxml.cxx +++ b/linguistic/source/convdicxml.cxx @@ -82,10 +82,8 @@ class ConvDicXMLImportContext : public SvXMLImportContext { public: - ConvDicXMLImportContext( - ConvDicXMLImport &rImport, - sal_uInt16 nPrfx, const OUString& rLName ) : - SvXMLImportContext( rImport, nPrfx, rLName ) + ConvDicXMLImportContext( ConvDicXMLImport &rImport ) : + SvXMLImportContext( rImport ) { } @@ -96,7 +94,8 @@ public: // SvXMLImportContext virtual void Characters( const OUString &rChars ) override; - virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList > &rxAttrList) override; + virtual css::uno::Reference<XFastContextHandler> SAL_CALL createFastChildContext( + sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override; }; @@ -107,17 +106,17 @@ class ConvDicXMLDictionaryContext_Impl : sal_Int16 nConversionType; public: - ConvDicXMLDictionaryContext_Impl( ConvDicXMLImport &rImport, - sal_uInt16 nPrefix, const OUString& rLName) : - ConvDicXMLImportContext( rImport, nPrefix, rLName ) + ConvDicXMLDictionaryContext_Impl( ConvDicXMLImport &rImport ) : + ConvDicXMLImportContext( rImport ) { nLanguage = LANGUAGE_NONE; nConversionType = -1; } // SvXMLImportContext - virtual void StartElement( const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override; - virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList > &rxAttrList ) override; + virtual void SAL_CALL startFastElement( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& Attribs ) override; + virtual css::uno::Reference<XFastContextHandler> SAL_CALL createFastChildContext( + sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override; }; @@ -127,16 +126,15 @@ class ConvDicXMLEntryTextContext_Impl : OUString aLeftText; public: - ConvDicXMLEntryTextContext_Impl( - ConvDicXMLImport &rImport, - sal_uInt16 nPrefix, const OUString& rLName ) : - ConvDicXMLImportContext( rImport, nPrefix, rLName ) + ConvDicXMLEntryTextContext_Impl( ConvDicXMLImport &rImport ) : + ConvDicXMLImportContext( rImport ) { } // SvXMLImportContext - virtual void StartElement( const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override; - virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList > &rxAttrList ) override; + virtual void SAL_CALL startFastElement( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& Attribs ) override; + virtual css::uno::Reference<XFastContextHandler> SAL_CALL createFastChildContext( + sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override; const OUString & GetLeftText() const { return aLeftText; } }; @@ -151,16 +149,14 @@ class ConvDicXMLRightTextContext_Impl : public: ConvDicXMLRightTextContext_Impl( ConvDicXMLImport &rImport, - sal_uInt16 nPrefix, const OUString& rLName, ConvDicXMLEntryTextContext_Impl &rParentContext ) : - ConvDicXMLImportContext( rImport, nPrefix, rLName ), + ConvDicXMLImportContext( rImport ), rEntryContext( rParentContext ) { } // SvXMLImportContext - virtual void EndElement() override; - virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList > &rxAttrList ) override; + virtual void SAL_CALL endFastElement( sal_Int32 nElement ) override; virtual void Characters( const OUString &rChars ) override; const OUString & GetRightText() const { return aRightText; } @@ -180,99 +176,95 @@ void ConvDicXMLImportContext::Characters(const OUString & /*rChars*/) } -SvXMLImportContextRef ConvDicXMLImportContext::CreateChildContext( - sal_uInt16 nPrefix, const OUString& rLocalName, - const uno::Reference< xml::sax::XAttributeList > & /*rxAttrList*/ ) +css::uno::Reference<XFastContextHandler> ConvDicXMLImportContext::createFastChildContext( + sal_Int32 Element, + const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/ ) { - SvXMLImportContext *pContext = nullptr; - if ( nPrefix == XML_NAMESPACE_TCD && rLocalName == "text-conversion-dictionary" ) - pContext = new ConvDicXMLDictionaryContext_Impl( GetConvDicImport(), nPrefix, rLocalName ); + if ( Element == ConvDicXMLToken::TEXT_CONVERSION_DICTIONARY ) + return new ConvDicXMLDictionaryContext_Impl( GetConvDicImport() ); else - pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); - return pContext; + return new SvXMLImportContext( GetImport() ); } -void ConvDicXMLDictionaryContext_Impl::StartElement( - const uno::Reference< xml::sax::XAttributeList > &rxAttrList ) +void ConvDicXMLDictionaryContext_Impl::startFastElement( sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& rxAttrList ) { - sal_Int16 nAttrCount = rxAttrList.is() ? rxAttrList->getLength() : 0; - for (sal_Int16 i = 0; i < nAttrCount; ++i) + if ( rxAttrList.is() ) { - OUString aAttrName = rxAttrList->getNameByIndex(i); - OUString aLocalName; - sal_uInt16 nPrefix = GetImport().GetNamespaceMap(). - GetKeyByAttrName( aAttrName, &aLocalName ); - OUString aValue = rxAttrList->getValueByIndex(i); - - if ( nPrefix == XML_NAMESPACE_TCD && aLocalName == "lang" ) - nLanguage = LanguageTag::convertToLanguageType( aValue ); - else if ( nPrefix == XML_NAMESPACE_TCD && aLocalName == "conversion-type" ) - nConversionType = GetConversionTypeFromText( aValue ); + sax_fastparser::FastAttributeList *pAttribList = + sax_fastparser::FastAttributeList::castToFastAttributeList( rxAttrList ); + + for (auto &aIter : *pAttribList) + { + switch (aIter.getToken()) + { + case XML_NAMESPACE_TCD | XML_LANG: + nLanguage = LanguageTag::convertToLanguageType( aIter.toString() ); + break; + case XML_NAMESPACE_TCD | XML_CONVERSION_TYPE: + nConversionType = GetConversionTypeFromText( aIter.toString() ); + break; + default: + ; + } + } } GetConvDicImport().SetLanguage( nLanguage ); GetConvDicImport().SetConversionType( nConversionType ); } -SvXMLImportContextRef ConvDicXMLDictionaryContext_Impl::CreateChildContext( - sal_uInt16 nPrefix, const OUString& rLocalName, - const uno::Reference< xml::sax::XAttributeList > & /*rxAttrList*/ ) +css::uno::Reference<XFastContextHandler> ConvDicXMLDictionaryContext_Impl::createFastChildContext( + sal_Int32 Element, + const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/ ) { - SvXMLImportContext *pContext = nullptr; - if ( nPrefix == XML_NAMESPACE_TCD && rLocalName == "entry" ) - pContext = new ConvDicXMLEntryTextContext_Impl( GetConvDicImport(), nPrefix, rLocalName ); + if ( Element == ConvDicXMLToken::ENTRY ) + return new ConvDicXMLEntryTextContext_Impl( GetConvDicImport() ); else - pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); - return pContext; + return new SvXMLImportContext(GetImport()); } - -SvXMLImportContextRef ConvDicXMLEntryTextContext_Impl::CreateChildContext( - sal_uInt16 nPrefix, const OUString& rLocalName, - const uno::Reference< xml::sax::XAttributeList > & /*rxAttrList*/ ) +css::uno::Reference<XFastContextHandler> ConvDicXMLEntryTextContext_Impl::createFastChildContext( + sal_Int32 Element, + const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/ ) { - SvXMLImportContext *pContext = nullptr; - if ( nPrefix == XML_NAMESPACE_TCD && rLocalName == "right-text" ) - pContext = new ConvDicXMLRightTextContext_Impl( GetConvDicImport(), nPrefix, rLocalName, *this ); + if ( Element == ConvDicXMLToken::RIGHT_TEXT ) + return new ConvDicXMLRightTextContext_Impl( GetConvDicImport(), *this ); else - pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); - return pContext; + return new SvXMLImportContext(GetImport()); } -void ConvDicXMLEntryTextContext_Impl::StartElement( - const uno::Reference< xml::sax::XAttributeList >& rxAttrList ) +void ConvDicXMLEntryTextContext_Impl::startFastElement( + sal_Int32 /*Element*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& rxAttrList ) { - sal_Int16 nAttrCount = rxAttrList.is() ? rxAttrList->getLength() : 0; - for (sal_Int16 i = 0; i < nAttrCount; ++i) + if ( rxAttrList.is() ) { - OUString aAttrName = rxAttrList->getNameByIndex(i); - OUString aLocalName; - sal_uInt16 nPrefix = GetImport().GetNamespaceMap(). - GetKeyByAttrName( aAttrName, &aLocalName ); - OUString aValue = rxAttrList->getValueByIndex(i); - - if ( nPrefix == XML_NAMESPACE_TCD && aLocalName == "left-text" ) - aLeftText = aValue; + sax_fastparser::FastAttributeList *pAttribList = + sax_fastparser::FastAttributeList::castToFastAttributeList( rxAttrList ); + + for (auto &aIter : *pAttribList) + { + switch (aIter.getToken()) + { + case XML_NAMESPACE_TCD | XML_LEFT_TEXT: + aLeftText = aIter.toString(); + break; + default: + ; + } + } } } -SvXMLImportContextRef ConvDicXMLRightTextContext_Impl::CreateChildContext( - sal_uInt16 nPrefix, const OUString& rLocalName, - const uno::Reference< xml::sax::XAttributeList > & /*rxAttrList*/ ) -{ - // leaf: return default (empty) context - SvXMLImportContext *pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); - return pContext; -} - void ConvDicXMLRightTextContext_Impl::Characters( const OUString &rChars ) { aRightText += rChars; } -void ConvDicXMLRightTextContext_Impl::EndElement() +void ConvDicXMLRightTextContext_Impl::endFastElement( sal_Int32 /*nElement*/ ) { ConvDic *pDic = GetConvDicImport().GetDic(); if (pDic) @@ -359,25 +351,24 @@ void ConvDicXMLExport::ExportContent_() } } -void SAL_CALL ConvDicXMLImport::startDocument() + //!! see comment for pDic member +ConvDicXMLImport::ConvDicXMLImport( ConvDic *pConvDic ) : + SvXMLImport ( comphelper::getProcessComponentContext(), "com.sun.star.lingu2.ConvDicXMLImport", SvXMLImportFlags::ALL ), + pDic ( pConvDic ) { - // register namespace at first possible opportunity - GetNamespaceMap().Add( "tcd", - XML_NAMESPACE_TCD_STRING, XML_NAMESPACE_TCD ); - SvXMLImport::startDocument(); + nLanguage = LANGUAGE_NONE; + nConversionType = -1; + GetNamespaceMap().Add( GetXMLToken(XML_NP_TCD), GetXMLToken(XML_N_TCD), XML_NAMESPACE_TCD); } -SvXMLImportContext * ConvDicXMLImport::CreateDocumentContext( - sal_uInt16 nPrefix, - const OUString &rLocalName, - const uno::Reference < xml::sax::XAttributeList > & /*rxAttrList*/ ) +SvXMLImportContext * ConvDicXMLImport::CreateFastContext( + sal_Int32 Element, + const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) { - SvXMLImportContext *pContext = nullptr; - if ( nPrefix == XML_NAMESPACE_TCD && rLocalName == "text-conversion-dictionary" ) - pContext = new ConvDicXMLDictionaryContext_Impl( *this, nPrefix, rLocalName ); + if( Element == ConvDicXMLToken::TEXT_CONVERSION_DICTIONARY ) + return new ConvDicXMLDictionaryContext_Impl( *this ); else - pContext = new SvXMLImportContext( *this, nPrefix, rLocalName ); - return pContext; + return SvXMLImport::CreateFastContext( Element, xAttrList ); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/linguistic/source/convdicxml.hxx b/linguistic/source/convdicxml.hxx index 9a2b3c776c37..70c9ab1e7691 100644 --- a/linguistic/source/convdicxml.hxx +++ b/linguistic/source/convdicxml.hxx @@ -24,9 +24,12 @@ #include <com/sun/star/util/MeasureUnit.hpp> #include <com/sun/star/lang/XServiceInfo.hpp> #include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/xml/sax/FastToken.hpp> #include <comphelper/processfactory.hxx> #include <xmloff/xmlexp.hxx> #include <xmloff/xmlimp.hxx> +#include <xmloff/xmltoken.hxx> +#include <xmloff/xmlnmspe.hxx> #include <cppuhelper/interfacecontainer.h> #include <rtl/ustring.hxx> #include <linguistic/misc.hxx> @@ -34,6 +37,8 @@ class ConvDic; +using namespace css::xml::sax; +using namespace ::xmloff::token; class ConvDicXMLExport : public SvXMLExport @@ -62,6 +67,29 @@ public: }; +enum ConvDicXMLToken : sal_Int32 +{ + TEXT_CONVERSION_DICTIONARY = FastToken::NAMESPACE | XML_NAMESPACE_TCD | XML_BLOCK_LIST, + RIGHT_TEXT = FastToken::NAMESPACE | XML_NAMESPACE_TCD | XML_RIGHT_TEXT, + ENTRY = FastToken::NAMESPACE | XML_NAMESPACE_TCD | XML_ENTRY, +}; + +class ConvDicXMLTokenHandler : public + cppu::WeakImplHelper< css::xml::sax::XFastTokenHandler >, + public sax_fastparser::FastTokenHandlerBase +{ +public: + explicit ConvDicXMLTokenHandler(); + virtual ~ConvDicXMLTokenHandler() override; + + //XFastTokenHandler + virtual sal_Int32 SAL_CALL getTokenFromUTF8( const css::uno::Sequence< sal_Int8 >& Identifier ) override; + virtual css::uno::Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 Token ) override; + + // Much faster direct C++ shortcut to the method that matters + virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const override; +}; + class ConvDicXMLImport : public SvXMLImport { ConvDic *pDic; // conversion dictionary to be used @@ -77,19 +105,7 @@ class ConvDicXMLImport : public SvXMLImport public: //!! see comment for pDic member - explicit ConvDicXMLImport( ConvDic *pConvDic ) : - SvXMLImport ( comphelper::getProcessComponentContext(), "com.sun.star.lingu2.ConvDicXMLImport", SvXMLImportFlags::ALL ), - pDic ( pConvDic ) - { - nLanguage = LANGUAGE_NONE; - nConversionType = -1; - } - - virtual void SAL_CALL startDocument() override; - - virtual SvXMLImportContext * CreateDocumentContext( - sal_uInt16 nPrefix, const OUString &rLocalName, - const css::uno::Reference < css::xml::sax::XAttributeList > &rxAttrList ) override; + explicit ConvDicXMLImport( ConvDic *pConvDic ); ConvDic * GetDic() { return pDic; } LanguageType GetLanguage() const { return nLanguage; } @@ -97,6 +113,10 @@ public: void SetLanguage( LanguageType nLang ) { nLanguage = nLang; } void SetConversionType( sal_Int16 nType ) { nConversionType = nType; } + +private: + virtual SvXMLImportContext *CreateFastContext( sal_Int32 Element, + const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override; }; |