LibreOffice Module xmloff (master)  1
XFormsBindContext.cxx
Go to the documentation of this file.
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/xmltkmap.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <xmloff/namespacemap.hxx>
31 
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/xforms/XModel2.hpp>
34 
35 #include <osl/diagnose.h>
36 #include <sal/log.hxx>
37 
40 using com::sun::star::uno::UNO_QUERY;
41 using com::sun::star::container::XNameContainer;
42 using com::sun::star::xml::sax::XAttributeList;
43 using com::sun::star::xforms::XModel2;
44 using namespace xmloff::token;
45 
46 
48 {
49  TOKEN_MAP_ENTRY( NONE, NODESET ),
52  TOKEN_MAP_ENTRY( NONE, RELEVANT ),
53  TOKEN_MAP_ENTRY( NONE, REQUIRED ),
54  TOKEN_MAP_ENTRY( NONE, CONSTRAINT ),
58 };
59 
60 // helper function; see below
62  Reference<XNameContainer> const & );
63 
65  SvXMLImport& rImport,
66  sal_uInt16 nPrefix,
67  const OUString& rLocalName,
68  const Reference<XModel2>& xModel ) :
69  TokenContext( rImport, nPrefix, rLocalName, aAttributeMap, aEmptyMap ),
70  mxModel( xModel )
71 {
72  // attach binding to model
73  mxBinding = mxModel->createBinding();
74  SAL_WARN_IF( !mxBinding.is(), "xmloff", "can't create binding" );
75  mxModel->getBindings()->insert( makeAny( mxBinding ) );
76 }
77 
79  const OUString& rValue )
80 {
81  switch( nToken )
82  {
83  case XML_NODESET:
84  xforms_setValue( mxBinding, "BindingExpression", rValue );
85  break;
86  case XML_ID:
87  xforms_setValue( mxBinding, "BindingID", rValue );
88  break;
89  case XML_READONLY:
90  xforms_setValue( mxBinding, "ReadonlyExpression", rValue );
91  break;
92  case XML_RELEVANT:
93  xforms_setValue( mxBinding, "RelevantExpression", rValue );
94  break;
95  case XML_REQUIRED:
96  xforms_setValue( mxBinding, "RequiredExpression", rValue );
97  break;
98  case XML_CONSTRAINT:
99  xforms_setValue( mxBinding, "ConstraintExpression", rValue );
100  break;
101  case XML_CALCULATE:
102  xforms_setValue( mxBinding, "CalculateExpression", rValue );
103  break;
104  case XML_TYPE:
105  xforms_setValue( mxBinding, "Type",
106  makeAny( xforms_getTypeName( mxModel->getDataTypeRepository(),
108  rValue ) ) );
109  break;
110  default:
111  OSL_FAIL( "should not happen" );
112  break;
113  }
114 }
115 
117  const Reference<XAttributeList>& xAttributeList )
118 {
119  // we need to register the namespaces
120  Reference<XNameContainer> xContainer(
121  mxBinding->getPropertyValue( "BindingNamespaces" ),
122  UNO_QUERY );
123 
124  SAL_WARN_IF( !xContainer.is(), "xmloff", "binding should have a namespace container" );
125  if( xContainer.is() )
127 
128  // call super-class for attribute handling
129  TokenContext::StartElement( xAttributeList );
130 }
131 
134  sal_uInt16,
135  sal_uInt16,
136  const OUString&,
138 {
139  OSL_FAIL( "no children supported" );
140  return nullptr;
141 }
142 
143 
145  const SvXMLNamespaceMap& aMap,
146  Reference<XNameContainer> const & xContainer )
147 {
148  sal_uInt16 nKeyIter = aMap.GetFirstKey();
149  do
150  {
151  // get prefix and namespace
152  const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter );
153  const OUString& sNamespace = aMap.GetNameByKey( nKeyIter );
154 
155  // as a hack, we will ignore our own 'default' namespaces
156  SAL_WARN_IF( sPrefix.isEmpty(), "xmloff", "no prefix?" );
157  if( !sPrefix.startsWith("_") &&
158  nKeyIter >= XML_NAMESPACE_META_SO52)
159  {
160  // insert prefix (use replace if already known)
161  if( xContainer->hasByName( sPrefix ) )
162  xContainer->replaceByName( sPrefix, makeAny( sNamespace ) );
163  else
164  xContainer->insertByName( sPrefix, makeAny( sNamespace ) );
165  }
166 
167  // proceed to next
168  nKeyIter = aMap.GetNextKey( nKeyIter );
169  }
170  while( nKeyIter != XML_NAMESPACE_UNKNOWN );
171 }
172 
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const struct SvXMLTokenMapEntry aAttributeMap[]
#define ID
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
sal_uInt16 GetFirstKey() const
const css::uno::Reference< css::xforms::XModel2 > mxModel
css::uno::Reference< css::frame::XModel2 > mxModel
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:402
TYPE
virtual SvXMLImportContext * HandleChild(sal_uInt16 nToken, sal_uInt16 nNamespace, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
will be called for each child element
NONE
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
const sal_uInt16 XML_NAMESPACE_UNKNOWN
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
const SvXMLTokenMapEntry aEmptyMap[1]
#define TOKEN_MAP_ENTRY(NAMESPACE, TOKEN)
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
call HandleAttribute for each attribute in the token map; create a warning for all others...
handle attributes through an SvXMLTokenMap
void xforms_setValue(Reference< XPropertySet > const &xPropertySet, const OUString &rName, const Any &rAny)
Definition: xformsapi.cxx:167
static void lcl_fillNamespaceContainer(const SvXMLNamespaceMap &, Reference< XNameContainer > const &)
constexpr sal_uInt16 XML_NAMESPACE_META_SO52
OUString sPrefix
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
call HandleAttribute for each attribute in the token map; create a warning for all others...
sal_uInt16 nToken
Definition: xmltkmap.hxx:40
sal_uInt16 GetNextKey(sal_uInt16 nOldKey) const
const OUString & GetNameByKey(sal_uInt16 nKey) const
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
XFormsBindContext(SvXMLImport &rImport, sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xforms::XModel2 > &xModel)
css::uno::Reference< css::beans::XPropertySet > mxBinding
Any makeAny(Color const &value)
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
OUString xforms_getTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:263
virtual void HandleAttribute(sal_uInt16 nToken, const OUString &rValue) override
will be called for each attribute
const OUString & GetPrefixByKey(sal_uInt16 nKey) const
const bool READONLY
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)