Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/script/XMLEventImportHelper.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
21
#include <XMLEventImportHelper.hxx>
22
#include <tools/debug.hxx>
23
#include <xmloff/xmlimp.hxx>
24
#include <xmloff/namespacemap.hxx>
25
#include <xmloff/xmlnamespace.hxx>
26
#include <xmloff/xmlerror.hxx>
27
28
using ::com::sun::star::uno::Reference;
29
30
XMLEventImportHelper::XMLEventImportHelper() :
31
19
    pEventNameMap(new NameMap)
32
19
{
33
19
}
34
35
XMLEventImportHelper::~XMLEventImportHelper()
36
19
{
37
    // delete factories
38
19
    aFactoryMap.clear();
39
40
    // delete name map
41
19
    pEventNameMap.reset();
42
19
}
43
44
void XMLEventImportHelper::RegisterFactory(
45
    const OUString& rLanguage,
46
    std::unique_ptr<XMLEventContextFactory> pFactory )
47
57
{
48
57
    assert(pFactory);
49
57
    aFactoryMap[rLanguage] = std::move(pFactory);
50
57
}
51
52
void XMLEventImportHelper::AddTranslationTable(
53
    const XMLEventNameTranslation* pTransTable )
54
19
{
55
19
    if (nullptr == pTransTable)
56
0
        return;
57
58
    // put translation table into map
59
19
    for(const XMLEventNameTranslation* pTrans = pTransTable;
60
1.15k
        !pTrans->sAPIName.isEmpty();
61
1.14k
        pTrans++)
62
1.14k
    {
63
1.14k
        XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName );
64
65
        // check for conflicting entries
66
1.14k
        DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(),
67
1.14k
                   "conflicting event translations");
68
69
        // assign new translation
70
1.14k
        (*pEventNameMap)[aName] = pTrans->sAPIName;
71
1.14k
    }
72
    // else? ignore!
73
19
}
74
75
void XMLEventImportHelper::PushTranslationTable()
76
0
{
77
    // save old map and install new one
78
0
    aEventNameMapVector.push_back(std::move(pEventNameMap));
79
0
    pEventNameMap.reset( new NameMap );
80
0
}
81
82
void XMLEventImportHelper::PopTranslationTable()
83
0
{
84
0
    DBG_ASSERT(!aEventNameMapVector.empty(),
85
0
               "no translation tables left to pop");
86
0
    if ( !aEventNameMapVector.empty() )
87
0
    {
88
        // delete current and install old map
89
0
        pEventNameMap = std::move(aEventNameMapVector.back());
90
0
        aEventNameMapVector.pop_back();
91
0
    }
92
0
}
93
94
95
SvXMLImportContext* XMLEventImportHelper::CreateContext(
96
    SvXMLImport& rImport,
97
    const Reference<css::xml::sax::XFastAttributeList> & xAttrList,
98
    XMLEventsImportContext* rEvents,
99
    const OUString& rXmlEventName,
100
    const OUString& rLanguage)
101
96
{
102
96
    SvXMLImportContext* pContext = nullptr;
103
104
    // translate event name from xml to api
105
96
    OUString sMacroName;
106
96
    sal_uInt16 nMacroPrefix =
107
96
        rImport.GetNamespaceMap().GetKeyByAttrValueQName(rXmlEventName,
108
96
                                                        &sMacroName );
109
96
    XMLEventName aEventName( nMacroPrefix, sMacroName );
110
96
    NameMap::iterator aNameIter = pEventNameMap->find(aEventName);
111
96
    if (aNameIter != pEventNameMap->end())
112
11
    {
113
11
        OUString aScriptLanguage;
114
11
        sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap().
115
11
                GetKeyByAttrValueQName(rLanguage, &aScriptLanguage);
116
11
        if( XML_NAMESPACE_OOO != nScriptPrefix )
117
0
            aScriptLanguage = rLanguage ;
118
119
        // check for factory
120
11
        FactoryMap::iterator aFactoryIterator =
121
11
            aFactoryMap.find(aScriptLanguage);
122
11
        if (aFactoryIterator != aFactoryMap.end())
123
11
        {
124
            // delegate to factory
125
11
            pContext = aFactoryIterator->second->CreateContext(
126
11
                rImport, xAttrList,
127
11
                rEvents, aNameIter->second);
128
11
        }
129
11
    }
130
131
    // default context (if no context was created above)
132
96
    if( nullptr == pContext )
133
85
    {
134
85
        pContext = new SvXMLImportContext(rImport);
135
136
85
        rImport.SetError(XMLERROR_FLAG_ERROR | XMLERROR_ILLEGAL_EVENT,
137
85
                         { rXmlEventName, rLanguage });
138
139
85
    }
140
141
96
    return pContext;
142
96
}
143
144
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */