Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/bastyp/frmhtmlw.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <svtools/htmlkywd.hxx>
21
22
#include <rtl/tencinfo.h>
23
#include <sal/log.hxx>
24
25
#include <svl/urihelper.hxx>
26
#include <tools/stream.hxx>
27
#include <tools/debug.hxx>
28
#include <unotools/resmgr.hxx>
29
#include <svtools/htmlout.hxx>
30
31
#include <sfx2/frmdescr.hxx>
32
#include <sfx2/frmhtmlw.hxx>
33
#include <strings.hxx>
34
35
#include <comphelper/processfactory.hxx>
36
#include <comphelper/string.hxx>
37
38
#include <com/sun/star/script/Converter.hpp>
39
#include <com/sun/star/document/XDocumentProperties.hpp>
40
#include <com/sun/star/beans/XPropertySet.hpp>
41
42
#include <rtl/bootstrap.hxx>
43
#include <rtl/strbuf.hxx>
44
#include <sax/tools/converter.hxx>
45
46
using namespace ::com::sun::star;
47
48
char const sHTML_SC_yes[] =  "YES";
49
char const sHTML_SC_no[] =       "NO";
50
51
void SfxFrameHTMLWriter::OutMeta( SvStream& rStrm,
52
                                  const char *pIndent,
53
                                  std::u16string_view rName,
54
                                  std::u16string_view rContent,
55
                                  bool bHTTPEquiv,
56
                                  OUString *pNonConvertableChars  )
57
0
{
58
0
    rStrm.WriteOString( SAL_NEWLINE_STRING );
59
0
    if( pIndent )
60
0
        rStrm.WriteOString( pIndent );
61
62
0
    OStringBuffer sOut;
63
0
    sOut.append("<" OOO_STRING_SVTOOLS_HTML_meta " ")
64
0
        .append(bHTTPEquiv ? OOO_STRING_SVTOOLS_HTML_O_httpequiv : OOO_STRING_SVTOOLS_HTML_O_name).append("=\"");
65
0
    rStrm.WriteOString( sOut );
66
0
    sOut.setLength(0);
67
68
0
    HTMLOutFuncs::Out_String( rStrm, rName, pNonConvertableChars );
69
70
0
    sOut.append("\" " OOO_STRING_SVTOOLS_HTML_O_content "=\"");
71
0
    rStrm.WriteOString( sOut );
72
0
    sOut.setLength(0);
73
74
0
    HTMLOutFuncs::Out_String( rStrm, rContent, pNonConvertableChars ).WriteOString( "\"/>" );
75
0
}
76
77
void SfxFrameHTMLWriter::Out_DocInfo( SvStream& rStrm, const OUString& rBaseURL,
78
        const uno::Reference<document::XDocumentProperties> & i_xDocProps,
79
        const char *pIndent,
80
        OUString *pNonConvertableChars    )
81
0
{
82
0
    OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_content_type, u"text/html; charset=utf-8", true,
83
0
                 pNonConvertableChars );
84
85
    // Title (regardless if empty)
86
0
    rStrm.WriteOString( SAL_NEWLINE_STRING );
87
0
    if( pIndent )
88
0
        rStrm.WriteOString( pIndent );
89
0
    HTMLOutFuncs::Out_AsciiTag( rStrm, OOO_STRING_SVTOOLS_HTML_title );
90
0
    if( i_xDocProps.is() )
91
0
    {
92
0
        const OUString aTitle = i_xDocProps->getTitle();
93
0
        if( !aTitle.isEmpty() )
94
0
            HTMLOutFuncs::Out_String( rStrm, aTitle, pNonConvertableChars );
95
0
    }
96
0
    HTMLOutFuncs::Out_AsciiTag( rStrm, OOO_STRING_SVTOOLS_HTML_title, false );
97
98
    // Target-Frame
99
0
    if( i_xDocProps.is() )
100
0
    {
101
0
        const OUString aTarget = i_xDocProps->getDefaultTarget();
102
0
        if( !aTarget.isEmpty() )
103
0
        {
104
0
            rStrm.WriteOString( SAL_NEWLINE_STRING );
105
0
            if( pIndent )
106
0
                rStrm.WriteOString( pIndent );
107
108
0
            rStrm.WriteOString( "<" OOO_STRING_SVTOOLS_HTML_base " "
109
0
                OOO_STRING_SVTOOLS_HTML_O_target "=\"" );
110
0
            HTMLOutFuncs::Out_String( rStrm, aTarget, pNonConvertableChars )
111
0
               .WriteOString( "\">" );
112
0
        }
113
0
    }
114
115
    // Who we are
116
0
    OUString sGenerator(Translate::ExpandVariables(STR_HTML_GENERATOR));
117
0
    OUString os( u"$_OS"_ustr );
118
0
    ::rtl::Bootstrap::expandMacros(os);
119
0
    sGenerator = sGenerator.replaceFirst( "%1", os );
120
0
    OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_generator, sGenerator, false, pNonConvertableChars );
121
122
0
    if( !i_xDocProps.is() )
123
0
        return;
124
125
    // Reload
126
0
    if( (i_xDocProps->getAutoloadSecs() != 0) ||
127
0
        !i_xDocProps->getAutoloadURL().isEmpty() )
128
0
    {
129
0
        OUString sContent = OUString::number(
130
0
                            i_xDocProps->getAutoloadSecs() );
131
132
0
        const OUString aReloadURL = i_xDocProps->getAutoloadURL();
133
0
        if( !aReloadURL.isEmpty() )
134
0
        {
135
0
            sContent += ";URL=" + URIHelper::simpleNormalizedMakeRelative(
136
0
                          rBaseURL, aReloadURL);
137
0
        }
138
139
0
        OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_refresh, sContent, true,
140
0
                 pNonConvertableChars );
141
0
    }
142
143
    // Author
144
0
    const OUString aAuthor = i_xDocProps->getAuthor();
145
0
    if( !aAuthor.isEmpty() )
146
0
        OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_author, aAuthor, false,
147
0
                 pNonConvertableChars );
148
149
    // created
150
0
    ::util::DateTime uDT = i_xDocProps->getCreationDate();
151
0
    OUStringBuffer aBuffer;
152
0
    ::sax::Converter::convertTimeOrDateTime(aBuffer, uDT);
153
154
0
    OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_created, aBuffer.makeStringAndClear(), false,
155
0
             pNonConvertableChars );
156
157
    // changedby
158
0
    const OUString aChangedBy = i_xDocProps->getModifiedBy();
159
0
    if( !aChangedBy.isEmpty() )
160
0
        OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_changedby, aChangedBy, false,
161
0
                 pNonConvertableChars );
162
163
    // changed
164
0
    uDT = i_xDocProps->getModificationDate();
165
0
    ::sax::Converter::convertTimeOrDateTime(aBuffer, uDT);
166
167
0
    OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_changed, aBuffer.makeStringAndClear(), false,
168
0
             pNonConvertableChars );
169
170
    // Subject
171
0
    const OUString aTheme = i_xDocProps->getSubject();
172
0
    if( !aTheme.isEmpty() )
173
0
        OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_classification, aTheme, false,
174
0
                 pNonConvertableChars );
175
176
    // Description
177
0
    const OUString aComment = i_xDocProps->getDescription();
178
0
    if( !aComment.isEmpty() )
179
0
        OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_description, aComment, false,
180
0
                 pNonConvertableChars);
181
182
    // Keywords
183
0
    OUString Keywords = ::comphelper::string::convertCommaSeparated(
184
0
        i_xDocProps->getKeywords());
185
0
    if( !Keywords.isEmpty() )
186
0
        OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_keywords, Keywords, false,
187
0
                 pNonConvertableChars);
188
189
0
    uno::Reference < script::XTypeConverter > xConverter( script::Converter::create(
190
0
        ::comphelper::getProcessComponentContext() ) );
191
0
    uno::Reference<beans::XPropertySet> xUserDefinedProps(
192
0
        i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
193
0
    uno::Reference<beans::XPropertySetInfo> xPropInfo =
194
0
        xUserDefinedProps->getPropertySetInfo();
195
0
    DBG_ASSERT(xPropInfo.is(), "UserDefinedProperties Info is null");
196
0
    const uno::Sequence<beans::Property> props = xPropInfo->getProperties();
197
0
    for (const auto& rProp : props)
198
0
    {
199
0
        try
200
0
        {
201
0
            OUString name = rProp.Name;
202
0
            uno::Any aStr = xConverter->convertToSimpleType(
203
0
                    xUserDefinedProps->getPropertyValue(name),
204
0
                    uno::TypeClass_STRING);
205
0
            OUString str;
206
0
            aStr >>= str;
207
0
            OUString valstr(comphelper::string::stripEnd(str, ' '));
208
0
            OutMeta( rStrm, pIndent, name, valstr, false,
209
0
                     pNonConvertableChars );
210
0
        }
211
0
        catch (const uno::Exception&)
212
0
        {
213
            // may happen with concurrent modification...
214
0
            SAL_INFO("sfx", "SfxFrameHTMLWriter::Out_DocInfo: exception");
215
0
        }
216
0
    }
217
0
}
218
219
void SfxFrameHTMLWriter::Out_FrameDescriptor(
220
    SvStream& rOut, const OUString& rBaseURL, const uno::Reference < beans::XPropertySet >& xSet )
221
0
{
222
0
    try
223
0
    {
224
0
        OStringBuffer sOut;
225
0
        OUString aStr;
226
0
        uno::Any aAny = xSet->getPropertyValue(u"FrameURL"_ustr);
227
0
        if ( (aAny >>= aStr) && !aStr.isEmpty() )
228
0
        {
229
0
            OUString aURL = INetURLObject( aStr ).GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
230
0
            if( !aURL.isEmpty() )
231
0
            {
232
0
                aURL = URIHelper::simpleNormalizedMakeRelative(
233
0
                    rBaseURL, aURL );
234
0
                sOut.append(" " OOO_STRING_SVTOOLS_HTML_O_src "=\"");
235
0
                rOut.WriteOString( sOut );
236
0
                sOut.setLength(0);
237
0
                HTMLOutFuncs::Out_String( rOut, aURL );
238
0
                sOut.append('\"');
239
0
            }
240
0
        }
241
242
0
        aAny = xSet->getPropertyValue(u"FrameName"_ustr);
243
0
        if ( (aAny >>= aStr) && !aStr.isEmpty() )
244
0
        {
245
0
            sOut.append(" " OOO_STRING_SVTOOLS_HTML_O_name "=\"");
246
0
            rOut.WriteOString( sOut );
247
0
            sOut.setLength(0);
248
0
            HTMLOutFuncs::Out_String( rOut, aStr );
249
0
            sOut.append('\"');
250
0
        }
251
252
0
        sal_Int32 nVal = SIZE_NOT_SET;
253
0
        aAny = xSet->getPropertyValue(u"FrameMarginWidth"_ustr);
254
0
        if ( (aAny >>= nVal) && nVal != SIZE_NOT_SET )
255
0
        {
256
0
            sOut.append(" " OOO_STRING_SVTOOLS_HTML_O_marginwidth
257
0
                    "=" + OString::number(nVal));
258
0
        }
259
0
        aAny = xSet->getPropertyValue(u"FrameMarginHeight"_ustr);
260
0
        if ( (aAny >>= nVal) && nVal != SIZE_NOT_SET )
261
0
        {
262
0
            sOut.append(" " OOO_STRING_SVTOOLS_HTML_O_marginheight
263
0
                    "=" + OString::number(nVal));
264
0
        }
265
266
0
        bool bVal = true;
267
0
        aAny = xSet->getPropertyValue(u"FrameIsAutoScroll"_ustr);
268
0
        if ( (aAny >>= bVal) && !bVal )
269
0
        {
270
0
            aAny = xSet->getPropertyValue(u"FrameIsScrollingMode"_ustr);
271
0
            if ( aAny >>= bVal )
272
0
            {
273
0
                const char *pStr = bVal ? sHTML_SC_yes : sHTML_SC_no;
274
0
                sOut.append(OString::Concat(" " OOO_STRING_SVTOOLS_HTML_O_scrolling) +
275
0
                        pStr);
276
0
            }
277
0
        }
278
279
        // frame border (MS+Netscape-Extension)
280
0
        aAny = xSet->getPropertyValue(u"FrameIsAutoBorder"_ustr);
281
0
        if ( (aAny >>= bVal) && !bVal )
282
0
        {
283
0
            aAny = xSet->getPropertyValue(u"FrameIsBorder"_ustr);
284
0
            if ( aAny >>= bVal )
285
0
            {
286
0
                const char* pStr = bVal ? sHTML_SC_yes : sHTML_SC_no;
287
0
                sOut.append(OString::Concat(" " OOO_STRING_SVTOOLS_HTML_O_frameborder
288
0
                        "=") + pStr);
289
0
            }
290
0
        }
291
0
        rOut.WriteOString( sOut );
292
0
        sOut.setLength(0);
293
0
    }
294
0
    catch (const uno::Exception&)
295
0
    {
296
0
    }
297
0
}
298
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */