merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / xforms / XFormsBindContext.cxx
blob0f8b6a61f3b1b7b05a6c16d0c1eb8ed6d334f63f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XFormsBindContext.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
34 #include "XFormsBindContext.hxx"
36 #include "xformsapi.hxx"
38 #include <xmloff/xmlimp.hxx>
39 #include "xmlerror.hxx"
40 #include <xmloff/xmltoken.hxx>
41 #include <xmloff/xmltkmap.hxx>
42 #include "xmlnmspe.hxx"
43 #include <xmloff/nmspmap.hxx>
45 #include <com/sun/star/container/XNameContainer.hpp>
46 #include <com/sun/star/xforms/XModel.hpp>
48 #include <tools/debug.hxx>
50 using rtl::OUString;
51 using com::sun::star::beans::XPropertySet;
52 using com::sun::star::uno::Reference;
53 using com::sun::star::uno::makeAny;
54 using com::sun::star::uno::UNO_QUERY;
55 using com::sun::star::uno::UNO_QUERY_THROW;
56 using com::sun::star::container::XNameContainer;
57 using com::sun::star::xml::sax::XAttributeList;
58 using com::sun::star::xforms::XModel;
59 using namespace xmloff::token;
64 static struct SvXMLTokenMapEntry aAttributeMap[] =
66 TOKEN_MAP_ENTRY( NONE, NODESET ),
67 TOKEN_MAP_ENTRY( NONE, ID ),
68 TOKEN_MAP_ENTRY( NONE, READONLY ),
69 TOKEN_MAP_ENTRY( NONE, RELEVANT ),
70 TOKEN_MAP_ENTRY( NONE, REQUIRED ),
71 TOKEN_MAP_ENTRY( NONE, CONSTRAINT ),
72 TOKEN_MAP_ENTRY( NONE, CALCULATE ),
73 TOKEN_MAP_ENTRY( NONE, TYPE ),
74 XML_TOKEN_MAP_END
77 // helper function; see below
78 void lcl_fillNamespaceContainer( const SvXMLNamespaceMap&,
79 Reference<XNameContainer>& );
81 XFormsBindContext::XFormsBindContext(
82 SvXMLImport& rImport,
83 USHORT nPrefix,
84 const OUString& rLocalName,
85 const Reference<XPropertySet>& xModel ) :
86 TokenContext( rImport, nPrefix, rLocalName, aAttributeMap, aEmptyMap ),
87 mxModel( xModel, UNO_QUERY_THROW ),
88 mxBinding( NULL )
90 // attach binding to model
91 mxBinding = mxModel->createBinding();
92 DBG_ASSERT( mxBinding.is(), "can't create binding" );
93 mxModel->getBindings()->insert( makeAny( mxBinding ) );
96 XFormsBindContext::~XFormsBindContext()
100 void XFormsBindContext::HandleAttribute( sal_uInt16 nToken,
101 const OUString& rValue )
103 switch( nToken )
105 case XML_NODESET:
106 lcl_setValue( mxBinding, OUSTRING("BindingExpression"), rValue );
107 break;
108 case XML_ID:
109 lcl_setValue( mxBinding, OUSTRING("BindingID"), rValue );
110 break;
111 case XML_READONLY:
112 lcl_setValue( mxBinding, OUSTRING("ReadonlyExpression"), rValue );
113 break;
114 case XML_RELEVANT:
115 lcl_setValue( mxBinding, OUSTRING("RelevantExpression"), rValue );
116 break;
117 case XML_REQUIRED:
118 lcl_setValue( mxBinding, OUSTRING("RequiredExpression"), rValue );
119 break;
120 case XML_CONSTRAINT:
121 lcl_setValue( mxBinding, OUSTRING("ConstraintExpression"), rValue );
122 break;
123 case XML_CALCULATE:
124 lcl_setValue( mxBinding, OUSTRING("CalculateExpression"), rValue );
125 break;
126 case XML_TYPE:
127 lcl_setValue( mxBinding, OUSTRING("Type"),
128 makeAny( lcl_getTypeName( mxModel->getDataTypeRepository(),
129 GetImport().GetNamespaceMap(),
130 rValue ) ) );
131 break;
132 default:
133 DBG_ERROR( "should not happen" );
134 break;
138 void XFormsBindContext::StartElement(
139 const Reference<XAttributeList>& xAttributeList )
141 // we need to register the namespaces
142 Reference<XNameContainer> xContainer(
143 mxBinding->getPropertyValue( OUSTRING("BindingNamespaces") ),
144 UNO_QUERY );
146 DBG_ASSERT( xContainer.is(), "binding should have a namespace container" );
147 if( xContainer.is() )
148 lcl_fillNamespaceContainer( GetImport().GetNamespaceMap(), xContainer);
150 // call super-class for attribute handling
151 TokenContext::StartElement( xAttributeList );
154 /** will be called for each child element */
155 SvXMLImportContext* XFormsBindContext::HandleChild(
156 sal_uInt16,
157 sal_uInt16,
158 const OUString&,
159 const Reference<XAttributeList>& )
161 DBG_ERROR( "no children supported" );
162 return NULL;
166 void lcl_fillNamespaceContainer(
167 const SvXMLNamespaceMap& aMap,
168 Reference<XNameContainer>& xContainer )
170 sal_uInt16 nKeyIter = aMap.GetFirstKey();
173 // get prefix and namespace
174 const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter );
175 const OUString& sNamespace = aMap.GetNameByKey( nKeyIter );
177 // as a hack, we will ignore our own 'default' namespaces
178 DBG_ASSERT( sPrefix.getLength() > 0, "no prefix?" );
179 if( sPrefix.getStr()[0] != sal_Unicode( '_' ) &&
180 nKeyIter >= XML_OLD_NAMESPACE_META_IDX )
182 // insert prefix (use replace if already known)
183 if( xContainer->hasByName( sPrefix ) )
184 xContainer->replaceByName( sPrefix, makeAny( sNamespace ) );
185 else
186 xContainer->insertByName( sPrefix, makeAny( sNamespace ) );
189 // proceed to next
190 nKeyIter = aMap.GetNextKey( nKeyIter );
192 while( nKeyIter != XML_NAMESPACE_UNKNOWN );