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 
62 void XFormsBindContext::HandleAttribute( sal_Int32 nAttributeToken,
63  const OUString& rValue )
64 {
65  switch( nAttributeToken & TOKEN_MASK )
66  {
67  case XML_NODESET:
68  xforms_setValue( mxBinding, "BindingExpression", rValue );
69  break;
70  case XML_ID:
71  xforms_setValue( mxBinding, "BindingID", rValue );
72  break;
73  case XML_READONLY:
74  xforms_setValue( mxBinding, "ReadonlyExpression", rValue );
75  break;
76  case XML_RELEVANT:
77  xforms_setValue( mxBinding, "RelevantExpression", rValue );
78  break;
79  case XML_REQUIRED:
80  xforms_setValue( mxBinding, "RequiredExpression", rValue );
81  break;
82  case XML_CONSTRAINT:
83  xforms_setValue( mxBinding, "ConstraintExpression", rValue );
84  break;
85  case XML_CALCULATE:
86  xforms_setValue( mxBinding, "CalculateExpression", rValue );
87  break;
88  case XML_TYPE:
89  xforms_setValue( mxBinding, "Type",
90  makeAny( xforms_getTypeName( mxModel->getDataTypeRepository(),
92  rValue ) ) );
93  break;
94  default:
95  OSL_FAIL( "should not happen" );
96  break;
97  }
98 }
99 
101  sal_Int32 nElement,
102  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
103 {
104  // we need to register the namespaces
105  Reference<XNameContainer> xContainer(
106  mxBinding->getPropertyValue( "BindingNamespaces" ),
107  UNO_QUERY );
108 
109  SAL_WARN_IF( !xContainer.is(), "xmloff", "binding should have a namespace container" );
110  if( xContainer.is() )
112 
113  // call super-class for attribute handling
114  TokenContext::startFastElement( nElement, xAttrList );
115 }
116 
119  sal_Int32,
121 {
122  OSL_FAIL( "no children supported" );
123  return nullptr;
124 }
125 
126 
128  const SvXMLNamespaceMap& aMap,
129  Reference<XNameContainer> const & xContainer )
130 {
131  sal_uInt16 nKeyIter = aMap.GetFirstKey();
132  do
133  {
134  // get prefix and namespace
135  const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter );
136  const OUString& sNamespace = aMap.GetNameByKey( nKeyIter );
137 
138  // as a hack, we will ignore our own 'default' namespaces
139  SAL_WARN_IF( sPrefix.isEmpty(), "xmloff", "no prefix?" );
140  if( !sPrefix.startsWith("_") &&
141  nKeyIter >= XML_NAMESPACE_META_SO52)
142  {
143  // insert prefix (use replace if already known)
144  if( xContainer->hasByName( sPrefix ) )
145  xContainer->replaceByName( sPrefix, makeAny( sNamespace ) );
146  else
147  xContainer->insertByName( sPrefix, makeAny( sNamespace ) );
148  }
149 
150  // proceed to next
151  nKeyIter = aMap.GetNextKey( nKeyIter );
152  }
153  while( nKeyIter != XML_NAMESPACE_UNKNOWN );
154 }
155 
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void HandleAttribute(sal_Int32 nAttributeToken, const OUString &rValue) override
will be called for each attribute
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:391
const sal_uInt16 XML_NAMESPACE_UNKNOWN
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
handle attributes through an SvXMLTokenMap
void xforms_setValue(Reference< XPropertySet > const &xPropertySet, const OUString &rName, const Any &rAny)
Definition: xformsapi.cxx:167
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:45
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:91
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)