diff options
author | Szymon Kłos <szymon.klos@collabora.com> | 2023-04-07 09:48:41 +0200 |
---|---|---|
committer | Caolán McNamara <caolan.mcnamara@collabora.com> | 2023-06-14 13:27:04 +0200 |
commit | 71075740aee2e15f574d19d452f0a586e25a6bd0 (patch) | |
tree | b93c1fcfc73ef99400818a20e4b045069c50ebea | |
parent | 427769a751d3f45e41e5a0f4aed385bb2727998c (diff) |
Make encodeForXml accessible for other modules
and share similar code
Change-Id: I7729a46d40845893f577c273c1ab340f69ebb51b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151230
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Szymon Kłos <szymon.klos@collabora.com>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151754
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
-rw-r--r-- | desktop/source/deployment/registry/configuration/dp_configuration.cxx | 36 | ||||
-rw-r--r-- | include/comphelper/xmlencode.hxx | 62 | ||||
-rw-r--r-- | include/vcl/pdfwriter.hxx | 2 | ||||
-rw-r--r-- | sd/source/filter/html/htmlex.cxx | 5 | ||||
-rw-r--r-- | sw/source/filter/html/css1atr.cxx | 3 | ||||
-rw-r--r-- | sw/source/filter/html/htmlfldw.cxx | 3 | ||||
-rw-r--r-- | vcl/source/gdi/pdfwriter_impl.cxx | 39 |
7 files changed, 75 insertions, 75 deletions
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 <unotools/ucbhelper.hxx> #include <xmlscript/xml_helper.hxx> #include <comphelper/lok.hxx> +#include <comphelper/xmlencode.hxx> #include <svl/inettype.hxx> #include <o3tl/string_view.hxx> #include <com/sun/star/configuration/Update.hpp> @@ -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 <rtl/ustring.hxx> + +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 <rtl/tencinfo.h> #include <comphelper/processfactory.hxx> #include <comphelper/propertyvalue.hxx> +#include <comphelper/xmlencode.hxx> #include <o3tl/safeint.hxx> #include <osl/file.hxx> #include <unotools/pathoptions.hxx> @@ -289,10 +290,10 @@ OUString HtmlState::SetLink( const OUString& aLink, const OUString& aTarget ) if (!aLink.isEmpty()) { - aStr += "<a href=\"" + aLink; + aStr += "<a href=\"" + comphelper::string::encodeForXml(aLink); if (!aTarget.isEmpty()) { - aStr += "\" target=\"" + aTarget; + aStr += "\" target=\"" + comphelper::string::encodeForXml(aTarget); } 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 <hintids.hxx> #include <comphelper/string.hxx> +#include <comphelper/xmlencode.hxx> #include <vcl/svapp.hxx> #include <svl/whiter.hxx> #include <editeng/boxitem.hxx> @@ -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 <com/sun/star/i18n/XBreakIterator.hpp> #include <comphelper/string.hxx> +#include <comphelper/xmlencode.hxx> #include <svtools/htmlkywd.hxx> #include <svtools/htmlout.hxx> #include <osl/diagnose.h> @@ -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 <com/sun/star/util/URLTransformer.hpp> #include <comphelper/processfactory.hxx> #include <comphelper/string.hxx> +#include <comphelper/xmlencode.hxx> #include <cppuhelper/implbase.hxx> #include <i18nlangtag/languagetag.hxx> #include <o3tl/numeric.hxx> @@ -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); } } |