From 71075740aee2e15f574d19d452f0a586e25a6bd0 Mon Sep 17 00:00:00 2001 From: Szymon Kłos Date: Fri, 7 Apr 2023 09:48:41 +0200 Subject: Make encodeForXml accessible for other modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and share similar code Change-Id: I7729a46d40845893f577c273c1ab340f69ebb51b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151230 Tested-by: Jenkins CollaboraOffice Reviewed-by: Szymon Kłos Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151754 Tested-by: Jenkins Reviewed-by: Caolán McNamara --- .../registry/configuration/dp_configuration.cxx | 36 +------------ include/comphelper/xmlencode.hxx | 62 ++++++++++++++++++++++ include/vcl/pdfwriter.hxx | 2 - sd/source/filter/html/htmlex.cxx | 5 +- sw/source/filter/html/css1atr.cxx | 3 +- sw/source/filter/html/htmlfldw.cxx | 3 +- vcl/source/gdi/pdfwriter_impl.cxx | 39 ++------------ 7 files changed, 75 insertions(+), 75 deletions(-) create mode 100644 include/comphelper/xmlencode.hxx diff --git a/desktop/source/deployment/registry/configuration/dp_configuration.cxx b/desktop/source/deployment/registry/configuration/dp_configuration.cxx index 1f4ba053ec55..622814248670 100644 --- a/desktop/source/deployment/registry/configuration/dp_configuration.cxx +++ b/desktop/source/deployment/registry/configuration/dp_configuration.cxx @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -566,39 +567,6 @@ BackendImpl::PackageImpl::isRegistered_( } -OUString encodeForXml( std::u16string_view text ) -{ - // encode conforming xml: - size_t len = text.size(); - OUStringBuffer buf; - for ( size_t pos = 0; pos < len; ++pos ) - { - sal_Unicode c = text[ pos ]; - switch (c) { - case '<': - buf.append( "<" ); - break; - case '>': - buf.append( ">" ); - break; - case '&': - buf.append( "&" ); - break; - case '\'': - buf.append( "'" ); - break; - case '\"': - buf.append( """ ); - break; - default: - buf.append( c ); - break; - } - } - return buf.makeStringAndClear(); -} - - OUString replaceOrigin( OUString const & url, std::u16string_view destFolder, Reference< XCommandEnvironment > const & xCmdEnv, Reference< XComponentContext > const & xContext, bool & out_replaced) { @@ -651,7 +619,7 @@ OUString replaceOrigin( if (origin.isEmpty()) { // encode only once origin = OUStringToOString( - encodeForXml( url.subView( 0, url.lastIndexOf( '/' ) ) ), + comphelper::string::encodeForXml( url.subView( 0, url.lastIndexOf( '/' ) ) ), // xxx todo: encode always for UTF-8? => lookup doc-header? RTL_TEXTENCODING_UTF8 ); } diff --git a/include/comphelper/xmlencode.hxx b/include/comphelper/xmlencode.hxx new file mode 100644 index 000000000000..160de5c9cf42 --- /dev/null +++ b/include/comphelper/xmlencode.hxx @@ -0,0 +1,62 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * 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 + +namespace comphelper::string +{ +inline OUString encodeForXml(std::u16string_view rStr) +{ + // encode conforming xml: + sal_Int32 len = rStr.length(); + OUStringBuffer buf(len + 16); // it's going to be at least len + for (sal_Int32 pos = 0; pos < len; ++pos) + { + sal_Unicode c = rStr[pos]; + switch (c) + { + case '<': + buf.append("<"); + break; + case '>': + buf.append(">"); + break; + case '&': + buf.append("&"); + break; + case '\'': + buf.append("'"); + break; + case '\"': + buf.append("""); + break; + default: + buf.append(c); + break; + } + } + + return buf.makeStringAndClear(); +} + +} /* Namespace */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/vcl/pdfwriter.hxx b/include/vcl/pdfwriter.hxx index 966798e814aa..0f4eccb42728 100644 --- a/include/vcl/pdfwriter.hxx +++ b/include/vcl/pdfwriter.hxx @@ -1236,8 +1236,6 @@ The following structure describes the permissions used in PDF security static OString GetDateTime(); }; -VCL_DLLPUBLIC void escapeStringXML( const OUString& rStr, OUString &rValue); - } #endif // INCLUDED_VCL_PDFWRITER_HXX diff --git a/sd/source/filter/html/htmlex.cxx b/sd/source/filter/html/htmlex.cxx index 7c526539d9ee..c3f1625dad59 100644 --- a/sd/source/filter/html/htmlex.cxx +++ b/sd/source/filter/html/htmlex.cxx @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -289,10 +290,10 @@ OUString HtmlState::SetLink( const OUString& aLink, const OUString& aTarget ) if (!aLink.isEmpty()) { - aStr += ""; mbLink = true; diff --git a/sw/source/filter/html/css1atr.cxx b/sw/source/filter/html/css1atr.cxx index 0f39024e11e3..3af5f38498e4 100644 --- a/sw/source/filter/html/css1atr.cxx +++ b/sw/source/filter/html/css1atr.cxx @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -1076,7 +1077,7 @@ void SwHTMLWriter::PrepareFontList( const SvxFontItem& rFontItem, while( nStrPos != -1 ) { OUString aName = rName.getToken( 0, ';', nStrPos ); - aName = comphelper::string::strip(aName, ' '); + aName = comphelper::string::encodeForXml(comphelper::string::strip(aName, ' ')); if( aName.isEmpty() ) continue; diff --git a/sw/source/filter/html/htmlfldw.cxx b/sw/source/filter/html/htmlfldw.cxx index 6ea4aba211ab..04f5dad8dbdc 100644 --- a/sw/source/filter/html/htmlfldw.cxx +++ b/sw/source/filter/html/htmlfldw.cxx @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -511,7 +512,7 @@ SwHTMLWriter& OutHTML_SwFormatField( SwHTMLWriter& rWrt, const SfxPoolItem& rHt OString sOut = "<" OOO_STRING_SVTOOLS_HTML_comment " " + - OUStringToOString(sComment, RTL_TEXTENCODING_UTF8) + + OUStringToOString(comphelper::string::encodeForXml(sComment), RTL_TEXTENCODING_UTF8) + " -->"; rWrt.Strm().WriteOString( sOut ); } diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx index 718433aafc02..48ed2ffe3be5 100644 --- a/vcl/source/gdi/pdfwriter_impl.cxx +++ b/vcl/source/gdi/pdfwriter_impl.cxx @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -5885,43 +5886,11 @@ sal_Int32 PDFWriterImpl::emitOutputIntent() return nOIObject; } -// formats the string for the XML stream -void escapeStringXML(const OUString& rStr, OUString &rValue) +static void lcl_assignMeta(std::u16string_view aValue, OString& aMeta) { - const sal_Unicode* pUni = rStr.getStr(); - int nLen = rStr.getLength(); - for( ; nLen; nLen--, pUni++ ) + if (!aValue.empty()) { - switch( *pUni ) - { - case u'&': - rValue += "&"; - break; - case u'<': - rValue += "<"; - break; - case u'>': - rValue += ">"; - break; - case u'\'': - rValue += "'"; - break; - case u'"': - rValue += """; - break; - default: - rValue += OUStringChar( *pUni ); - break; - } - } -} - -static void lcl_assignMeta(const OUString& aValue, OString& aMeta) -{ - if (!aValue.isEmpty()) - { - OUString aTempString; - escapeStringXML(aValue, aTempString); + OUString aTempString = comphelper::string::encodeForXml(aValue); aMeta = OUStringToOString(aTempString, RTL_TEXTENCODING_UTF8); } } -- cgit