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