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::XFastAttributeList;
43 using com::sun::star::xforms::XModel2;
44 using namespace xmloff::token;
45 
46 // helper function; see below
48  Reference<XNameContainer> const & );
49 
51  SvXMLImport& rImport,
52  const Reference<XModel2>& xModel ) :
53  TokenContext( rImport ),
54  mxModel( xModel )
55 {
56  // attach binding to model
57  mxBinding = mxModel->createBinding();
58  SAL_WARN_IF( !mxBinding.is(), "xmloff", "can't create binding" );
59  mxModel->getBindings()->insert( makeAny( mxBinding ) );
60 }
61 
63 {
64  switch( aIter.getToken() & TOKEN_MASK )
65  {
66  case XML_NODESET:
67  xforms_setValue( mxBinding, "BindingExpression", aIter.toString() );
68  break;
69  case XML_ID:
70  xforms_setValue( mxBinding, "BindingID", aIter.toString() );
71  break;
72  case XML_READONLY:
73  xforms_setValue( mxBinding, "ReadonlyExpression", aIter.toString() );
74  break;
75  case XML_RELEVANT:
76  xforms_setValue( mxBinding, "RelevantExpression", aIter.toString() );
77  break;
78  case XML_REQUIRED:
79  xforms_setValue( mxBinding, "RequiredExpression", aIter.toString() );
80  break;
81  case XML_CONSTRAINT:
82  xforms_setValue( mxBinding, "ConstraintExpression", aIter.toString() );
83  break;
84  case XML_CALCULATE:
85  xforms_setValue( mxBinding, "CalculateExpression", aIter.toString() );
86  break;
87  case XML_TYPE:
88  xforms_setValue( mxBinding, "Type",
89  xforms_getTypeName( mxModel->getDataTypeRepository(),
91  aIter.toString() ) );
92  break;
93  default:
94  assert( false && "should not happen" );
95  break;
96  }
97 }
98 
100  sal_Int32 nElement,
101  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
102 {
103  // we need to register the namespaces
104  Reference<XNameContainer> xContainer(
105  mxBinding->getPropertyValue( "BindingNamespaces" ),
106  UNO_QUERY );
107 
108  SAL_WARN_IF( !xContainer.is(), "xmloff", "binding should have a namespace container" );
109  if( xContainer.is() )
111 
112  // call super-class for attribute handling
113  TokenContext::startFastElement( nElement, xAttrList );
114 }
115 
118  sal_Int32,
120 {
121  assert( false && "no children supported" );
122  return nullptr;
123 }
124 
125 
127  const SvXMLNamespaceMap& aMap,
128  Reference<XNameContainer> const & xContainer )
129 {
130  sal_uInt16 nKeyIter = aMap.GetFirstKey();
131  do
132  {
133  // get prefix and namespace
134  const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter );
135  const OUString& sNamespace = aMap.GetNameByKey( nKeyIter );
136 
137  // as a hack, we will ignore our own 'default' namespaces
138  SAL_WARN_IF( sPrefix.isEmpty(), "xmloff", "no prefix?" );
139  if( !sPrefix.startsWith("_") &&
140  nKeyIter >= XML_NAMESPACE_META_SO52)
141  {
142  // insert prefix (use replace if already known)
143  if( xContainer->hasByName( sPrefix ) )
144  xContainer->replaceByName( sPrefix, makeAny( sNamespace ) );
145  else
146  xContainer->insertByName( sPrefix, makeAny( sNamespace ) );
147  }
148 
149  // proceed to next
150  nKeyIter = aMap.GetNextKey( nKeyIter );
151  }
152  while( nKeyIter != XML_NAMESPACE_UNKNOWN );
153 }
154 
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XFormsBindContext(SvXMLImport &rImport, const css::uno::Reference< css::xforms::XModel2 > &xModel)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
sal_uInt16 GetFirstKey() const
const css::uno::Reference< css::xforms::XModel2 > mxModel
css::uno::Reference< css::frame::XModel2 > mxModel
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:398
virtual void HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter &aIter) override
will be called for each attribute
const sal_uInt16 XML_NAMESPACE_UNKNOWN
handle attributes through an SvXMLTokenMap
void xforms_setValue(css::uno::Reference< css::beans::XPropertySet > &xPropSet, const OUString &rName, const T &aValue)
Definition: xformsapi.hxx:52
virtual SvXMLImportContext * HandleChild(sal_Int32 nElementToken, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
will be called for each child element
static void lcl_fillNamespaceContainer(const SvXMLNamespaceMap &, Reference< XNameContainer > const &)
constexpr sal_uInt16 XML_NAMESPACE_META_SO52
OUString sPrefix
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
css::uno::Reference< css::beans::XPropertySet > mxBinding
Any makeAny(Color const &value)
#define SAL_WARN_IF(condition, area, stream)
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
call HandleAttribute for each attribute in the token map; create a warning for all others...
Handling of tokens in XML:
OUString xforms_getTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:263
constexpr sal_Int32 TOKEN_MASK
Definition: xmlimp.hxx:94
const OUString & GetPrefixByKey(sal_uInt16 nKey) const
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
call HandleAttribute for each attribute in the token map; create a warning for all others...
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)