bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / xforms / XFormsBindContext.cxx
blobc7eb92855dfe17ce04112a95fd0141f2b265bae5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "XFormsBindContext.hxx"
23 #include "xformsapi.hxx"
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/xmlerror.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmltkmap.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/nmspmap.hxx>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/xforms/XModel2.hpp>
35 #include <tools/debug.hxx>
36 #include <osl/diagnose.h>
38 using com::sun::star::beans::XPropertySet;
39 using com::sun::star::uno::Reference;
40 using com::sun::star::uno::makeAny;
41 using com::sun::star::uno::UNO_QUERY;
42 using com::sun::star::uno::UNO_QUERY_THROW;
43 using com::sun::star::container::XNameContainer;
44 using com::sun::star::xml::sax::XAttributeList;
45 using com::sun::star::xforms::XModel2;
46 using namespace xmloff::token;
51 static const struct SvXMLTokenMapEntry aAttributeMap[] =
53 TOKEN_MAP_ENTRY( NONE, NODESET ),
54 TOKEN_MAP_ENTRY( NONE, ID ),
55 TOKEN_MAP_ENTRY( NONE, READONLY ),
56 TOKEN_MAP_ENTRY( NONE, RELEVANT ),
57 TOKEN_MAP_ENTRY( NONE, REQUIRED ),
58 TOKEN_MAP_ENTRY( NONE, CONSTRAINT ),
59 TOKEN_MAP_ENTRY( NONE, CALCULATE ),
60 TOKEN_MAP_ENTRY( NONE, TYPE ),
61 XML_TOKEN_MAP_END
64 // helper function; see below
65 static void lcl_fillNamespaceContainer( const SvXMLNamespaceMap&,
66 Reference<XNameContainer>& );
68 XFormsBindContext::XFormsBindContext(
69 SvXMLImport& rImport,
70 sal_uInt16 nPrefix,
71 const OUString& rLocalName,
72 const Reference<XModel2>& xModel ) :
73 TokenContext( rImport, nPrefix, rLocalName, aAttributeMap, aEmptyMap ),
74 mxModel( xModel ),
75 mxBinding( NULL )
77 // attach binding to model
78 mxBinding = mxModel->createBinding();
79 DBG_ASSERT( mxBinding.is(), "can't create binding" );
80 mxModel->getBindings()->insert( makeAny( mxBinding ) );
83 XFormsBindContext::~XFormsBindContext()
87 void XFormsBindContext::HandleAttribute( sal_uInt16 nToken,
88 const OUString& rValue )
90 switch( nToken )
92 case XML_NODESET:
93 xforms_setValue( mxBinding, "BindingExpression", rValue );
94 break;
95 case XML_ID:
96 xforms_setValue( mxBinding, "BindingID", rValue );
97 break;
98 case XML_READONLY:
99 xforms_setValue( mxBinding, "ReadonlyExpression", rValue );
100 break;
101 case XML_RELEVANT:
102 xforms_setValue( mxBinding, "RelevantExpression", rValue );
103 break;
104 case XML_REQUIRED:
105 xforms_setValue( mxBinding, "RequiredExpression", rValue );
106 break;
107 case XML_CONSTRAINT:
108 xforms_setValue( mxBinding, "ConstraintExpression", rValue );
109 break;
110 case XML_CALCULATE:
111 xforms_setValue( mxBinding, "CalculateExpression", rValue );
112 break;
113 case XML_TYPE:
114 xforms_setValue( mxBinding, "Type",
115 makeAny( xforms_getTypeName( mxModel->getDataTypeRepository(),
116 GetImport().GetNamespaceMap(),
117 rValue ) ) );
118 break;
119 default:
120 OSL_FAIL( "should not happen" );
121 break;
125 void XFormsBindContext::StartElement(
126 const Reference<XAttributeList>& xAttributeList )
128 // we need to register the namespaces
129 Reference<XNameContainer> xContainer(
130 mxBinding->getPropertyValue( "BindingNamespaces" ),
131 UNO_QUERY );
133 DBG_ASSERT( xContainer.is(), "binding should have a namespace container" );
134 if( xContainer.is() )
135 lcl_fillNamespaceContainer( GetImport().GetNamespaceMap(), xContainer);
137 // call super-class for attribute handling
138 TokenContext::StartElement( xAttributeList );
141 /** will be called for each child element */
142 SvXMLImportContext* XFormsBindContext::HandleChild(
143 sal_uInt16,
144 sal_uInt16,
145 const OUString&,
146 const Reference<XAttributeList>& )
148 OSL_FAIL( "no children supported" );
149 return NULL;
153 static void lcl_fillNamespaceContainer(
154 const SvXMLNamespaceMap& aMap,
155 Reference<XNameContainer>& xContainer )
157 sal_uInt16 nKeyIter = aMap.GetFirstKey();
160 // get prefix and namespace
161 const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter );
162 const OUString& sNamespace = aMap.GetNameByKey( nKeyIter );
164 // as a hack, we will ignore our own 'default' namespaces
165 DBG_ASSERT( !sPrefix.isEmpty(), "no prefix?" );
166 if( !sPrefix.startsWith("_") &&
167 nKeyIter >= XML_OLD_NAMESPACE_META_IDX )
169 // insert prefix (use replace if already known)
170 if( xContainer->hasByName( sPrefix ) )
171 xContainer->replaceByName( sPrefix, makeAny( sNamespace ) );
172 else
173 xContainer->insertByName( sPrefix, makeAny( sNamespace ) );
176 // proceed to next
177 nKeyIter = aMap.GetNextKey( nKeyIter );
179 while( nKeyIter != XML_NAMESPACE_UNKNOWN );
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */