Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/transform/DocumentTContext.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 <com/sun/star/beans/XPropertySetInfo.hpp>
21
#include <xmloff/namespacemap.hxx>
22
#include <xmloff/xmltoken.hxx>
23
#include <xmloff/xmlnamespace.hxx>
24
25
#include "TransformerBase.hxx"
26
#include "MutableAttrList.hxx"
27
28
#include "DocumentTContext.hxx"
29
30
31
using namespace ::xmloff::token;
32
using namespace ::com::sun::star::uno;
33
using namespace ::com::sun::star::xml::sax;
34
using namespace ::com::sun::star::beans;
35
36
XMLDocumentTransformerContext::XMLDocumentTransformerContext( XMLTransformerBase& rImp,
37
                                                const OUString& rQName ) :
38
0
    XMLTransformerContext( rImp, rQName )
39
0
{
40
0
}
41
42
void XMLDocumentTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
43
0
{
44
0
    Reference< XAttributeList > xAttrList( rAttrList );
45
46
0
    bool bMimeFound = false;
47
0
    OUString aClass;
48
0
    OUString aClassQName(
49
0
                    GetTransformer().GetNamespaceMap().GetQNameByKey(
50
0
                                XML_NAMESPACE_OFFICE, GetXMLToken(XML_CLASS ) ) );
51
52
0
    rtl::Reference<XMLMutableAttributeList> pMutableAttrList;
53
0
    sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
54
0
    for( sal_Int16 i=0; i < nAttrCount; i++ )
55
0
    {
56
0
        const OUString aAttrName = xAttrList->getNameByIndex( i );
57
0
        OUString aLocalName;
58
0
        sal_uInt16 nPrefix =
59
0
            GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName,
60
0
                                                                 &aLocalName );
61
0
        if( XML_NAMESPACE_OFFICE == nPrefix &&
62
0
            IsXMLToken( aLocalName, XML_MIMETYPE ) )
63
0
        {
64
0
            const OUString aValue = xAttrList->getValueByIndex( i );
65
0
            static constexpr std::string_view aTmp[]
66
0
            {
67
0
                "application/vnd.oasis.openoffice.",
68
0
                "application/x-vnd.oasis.openoffice.",
69
0
                "application/vnd.oasis.opendocument.",
70
0
                "application/x-vnd.oasis.document."
71
0
            };
72
0
            for (const auto & rPrefix : aTmp)
73
0
            {
74
0
                if( aValue.matchAsciiL( rPrefix.data(), rPrefix.size() ) )
75
0
                {
76
0
                    aClass = aValue.copy( rPrefix.size() );
77
0
                    break;
78
0
                }
79
0
            }
80
81
0
            if( !pMutableAttrList )
82
0
            {
83
0
                pMutableAttrList = new XMLMutableAttributeList( xAttrList );
84
0
                xAttrList = pMutableAttrList;
85
0
            }
86
0
            pMutableAttrList->SetValueByIndex( i, aClass );
87
0
            pMutableAttrList->RenameAttributeByIndex(i, aClassQName );
88
0
            bMimeFound = true;
89
0
            break;
90
0
        }
91
0
    }
92
93
0
    if( !bMimeFound )
94
0
    {
95
0
        const Reference< XPropertySet > rPropSet =
96
0
            GetTransformer().GetPropertySet();
97
98
0
        if( rPropSet.is() )
99
0
        {
100
0
            Reference< XPropertySetInfo > xPropSetInfo(
101
0
                rPropSet->getPropertySetInfo() );
102
0
            OUString aPropName(u"Class"_ustr);
103
0
            if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName( aPropName ) )
104
0
            {
105
0
                Any aAny = rPropSet->getPropertyValue( aPropName );
106
0
                aAny >>= aClass;
107
0
            }
108
0
        }
109
110
0
        if( !aClass.isEmpty() )
111
0
        {
112
0
            if( !pMutableAttrList )
113
0
            {
114
0
                pMutableAttrList = new XMLMutableAttributeList( xAttrList );
115
0
                xAttrList = pMutableAttrList;
116
0
            }
117
118
0
            pMutableAttrList->AddAttribute( aClassQName, aClass );
119
0
        }
120
0
    }
121
0
    XMLTransformerContext::StartElement( xAttrList );
122
0
}
123
124
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */