Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/xforms/XFormsBindContext.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 "XFormsBindContext.hxx"
22
23
#include "xformsapi.hxx"
24
25
#include <xmloff/xmlimp.hxx>
26
#include <xmloff/xmlerror.hxx>
27
#include <xmloff/xmltoken.hxx>
28
#include <xmloff/xmlnamespace.hxx>
29
#include <xmloff/namespacemap.hxx>
30
31
#include <com/sun/star/container/XNameContainer.hpp>
32
#include <com/sun/star/xforms/XModel2.hpp>
33
34
#include <sal/log.hxx>
35
36
using com::sun::star::uno::Reference;
37
using com::sun::star::uno::Any;
38
using com::sun::star::uno::UNO_QUERY;
39
using com::sun::star::container::XNameContainer;
40
using com::sun::star::xml::sax::XFastAttributeList;
41
using com::sun::star::xforms::XModel2;
42
using namespace xmloff::token;
43
44
// helper function; see below
45
static void lcl_fillNamespaceContainer( const SvXMLNamespaceMap&,
46
                                 Reference<XNameContainer> const & );
47
48
XFormsBindContext::XFormsBindContext(
49
    SvXMLImport& rImport,
50
    const Reference<XModel2>& xModel ) :
51
0
        TokenContext( rImport ),
52
0
        mxModel( xModel )
53
0
{
54
    // attach binding to model
55
0
    mxBinding = mxModel->createBinding();
56
0
    SAL_WARN_IF( !mxBinding.is(), "xmloff", "can't create binding" );
57
0
    mxModel->getBindings()->insert( Any( mxBinding ) );
58
0
}
59
60
void XFormsBindContext::HandleAttribute( const sax_fastparser::FastAttributeList::FastAttributeIter & aIter )
61
0
{
62
0
    switch( aIter.getToken() & TOKEN_MASK )
63
0
    {
64
0
    case XML_NODESET:
65
0
        xforms_setValue( mxBinding, u"BindingExpression"_ustr, aIter.toString() );
66
0
        break;
67
0
    case XML_ID:
68
0
        xforms_setValue( mxBinding, u"BindingID"_ustr, aIter.toString() );
69
0
        break;
70
0
    case XML_READONLY:
71
0
        xforms_setValue( mxBinding, u"ReadonlyExpression"_ustr, aIter.toString() );
72
0
        break;
73
0
    case XML_RELEVANT:
74
0
        xforms_setValue( mxBinding, u"RelevantExpression"_ustr, aIter.toString() );
75
0
        break;
76
0
    case XML_REQUIRED:
77
0
        xforms_setValue( mxBinding, u"RequiredExpression"_ustr, aIter.toString() );
78
0
        break;
79
0
    case XML_CONSTRAINT:
80
0
        xforms_setValue( mxBinding, u"ConstraintExpression"_ustr, aIter.toString() );
81
0
        break;
82
0
    case XML_CALCULATE:
83
0
        xforms_setValue( mxBinding, u"CalculateExpression"_ustr, aIter.toString() );
84
0
        break;
85
0
    case XML_TYPE:
86
0
        xforms_setValue( mxBinding, u"Type"_ustr,
87
0
                         xforms_getTypeName( mxModel->getDataTypeRepository(),
88
0
                                       GetImport().GetNamespaceMap(),
89
0
                                       aIter.toString() ) );
90
0
        break;
91
0
    default:
92
0
        assert( false && "should not happen" );
93
0
        break;
94
0
    }
95
0
}
96
97
void XFormsBindContext::startFastElement(
98
    sal_Int32 nElement,
99
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
100
0
{
101
    // we need to register the namespaces
102
0
    Reference<XNameContainer> xContainer(
103
0
        mxBinding->getPropertyValue( u"BindingNamespaces"_ustr ),
104
0
        UNO_QUERY );
105
106
0
    SAL_WARN_IF( !xContainer.is(), "xmloff", "binding should have a namespace container" );
107
0
    if( xContainer.is() )
108
0
        lcl_fillNamespaceContainer( GetImport().GetNamespaceMap(), xContainer);
109
110
    // call super-class for attribute handling
111
0
    TokenContext::startFastElement( nElement, xAttrList );
112
0
}
113
114
/** will be called for each child element */
115
SvXMLImportContext* XFormsBindContext::HandleChild(
116
    sal_Int32,
117
    const Reference<XFastAttributeList>& )
118
0
{
119
0
    assert( false && "no children supported" );
120
0
    return nullptr;
121
0
}
122
123
124
static void lcl_fillNamespaceContainer(
125
    const SvXMLNamespaceMap& aMap,
126
    Reference<XNameContainer> const & xContainer )
127
0
{
128
0
    sal_uInt16 nKeyIter = aMap.GetFirstKey();
129
0
    do
130
0
    {
131
        // get prefix and namespace
132
0
        const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter );
133
0
        const OUString& sNamespace = aMap.GetNameByKey( nKeyIter );
134
135
        // as a hack, we will ignore our own 'default' namespaces
136
0
        SAL_WARN_IF( sPrefix.isEmpty(), "xmloff", "no prefix?" );
137
0
        if( !sPrefix.startsWith("_") &&
138
0
            nKeyIter >= XML_NAMESPACE_META_SO52)
139
0
        {
140
            // insert prefix (use replace if already known)
141
0
            if( xContainer->hasByName( sPrefix ) )
142
0
                xContainer->replaceByName( sPrefix, Any( sNamespace ) );
143
0
            else
144
0
                xContainer->insertByName( sPrefix, Any( sNamespace ) );
145
0
        }
146
147
        // proceed to next
148
0
        nKeyIter = aMap.GetNextKey( nKeyIter );
149
0
    }
150
0
    while( nKeyIter != XML_NAMESPACE_UNKNOWN );
151
0
}
152
153
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */