1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*63bba73cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*63bba73cSAndrew Rist * distributed with this work for additional information
6*63bba73cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*63bba73cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*63bba73cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*63bba73cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist * KIND, either express or implied. See the License for the
17*63bba73cSAndrew Rist * specific language governing permissions and limitations
18*63bba73cSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*63bba73cSAndrew Rist *************************************************************/
21*63bba73cSAndrew Rist
22*63bba73cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "XFormsBindContext.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include "xformsapi.hxx"
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
32cdf0e10cSrcweir #include "xmloff/xmlerror.hxx"
33cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
34cdf0e10cSrcweir #include <xmloff/xmltkmap.hxx>
35cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
36cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
37cdf0e10cSrcweir
38cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
39cdf0e10cSrcweir #include <com/sun/star/xforms/XModel.hpp>
40cdf0e10cSrcweir
41cdf0e10cSrcweir #include <tools/debug.hxx>
42cdf0e10cSrcweir
43cdf0e10cSrcweir using rtl::OUString;
44cdf0e10cSrcweir using com::sun::star::beans::XPropertySet;
45cdf0e10cSrcweir using com::sun::star::uno::Reference;
46cdf0e10cSrcweir using com::sun::star::uno::makeAny;
47cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
48cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY_THROW;
49cdf0e10cSrcweir using com::sun::star::container::XNameContainer;
50cdf0e10cSrcweir using com::sun::star::xml::sax::XAttributeList;
51cdf0e10cSrcweir using com::sun::star::xforms::XModel;
52cdf0e10cSrcweir using namespace xmloff::token;
53cdf0e10cSrcweir
54cdf0e10cSrcweir
55cdf0e10cSrcweir
56cdf0e10cSrcweir
57cdf0e10cSrcweir static struct SvXMLTokenMapEntry aAttributeMap[] =
58cdf0e10cSrcweir {
59cdf0e10cSrcweir TOKEN_MAP_ENTRY( NONE, NODESET ),
60cdf0e10cSrcweir TOKEN_MAP_ENTRY( NONE, ID ),
61cdf0e10cSrcweir TOKEN_MAP_ENTRY( NONE, READONLY ),
62cdf0e10cSrcweir TOKEN_MAP_ENTRY( NONE, RELEVANT ),
63cdf0e10cSrcweir TOKEN_MAP_ENTRY( NONE, REQUIRED ),
64cdf0e10cSrcweir TOKEN_MAP_ENTRY( NONE, CONSTRAINT ),
65cdf0e10cSrcweir TOKEN_MAP_ENTRY( NONE, CALCULATE ),
66cdf0e10cSrcweir TOKEN_MAP_ENTRY( NONE, TYPE ),
67cdf0e10cSrcweir XML_TOKEN_MAP_END
68cdf0e10cSrcweir };
69cdf0e10cSrcweir
70cdf0e10cSrcweir // helper function; see below
71cdf0e10cSrcweir void lcl_fillNamespaceContainer( const SvXMLNamespaceMap&,
72cdf0e10cSrcweir Reference<XNameContainer>& );
73cdf0e10cSrcweir
XFormsBindContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XPropertySet> & xModel)74cdf0e10cSrcweir XFormsBindContext::XFormsBindContext(
75cdf0e10cSrcweir SvXMLImport& rImport,
76cdf0e10cSrcweir sal_uInt16 nPrefix,
77cdf0e10cSrcweir const OUString& rLocalName,
78cdf0e10cSrcweir const Reference<XPropertySet>& xModel ) :
79cdf0e10cSrcweir TokenContext( rImport, nPrefix, rLocalName, aAttributeMap, aEmptyMap ),
80cdf0e10cSrcweir mxModel( xModel, UNO_QUERY_THROW ),
81cdf0e10cSrcweir mxBinding( NULL )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir // attach binding to model
84cdf0e10cSrcweir mxBinding = mxModel->createBinding();
85cdf0e10cSrcweir DBG_ASSERT( mxBinding.is(), "can't create binding" );
86cdf0e10cSrcweir mxModel->getBindings()->insert( makeAny( mxBinding ) );
87cdf0e10cSrcweir }
88cdf0e10cSrcweir
~XFormsBindContext()89cdf0e10cSrcweir XFormsBindContext::~XFormsBindContext()
90cdf0e10cSrcweir {
91cdf0e10cSrcweir }
92cdf0e10cSrcweir
HandleAttribute(sal_uInt16 nToken,const OUString & rValue)93cdf0e10cSrcweir void XFormsBindContext::HandleAttribute( sal_uInt16 nToken,
94cdf0e10cSrcweir const OUString& rValue )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir switch( nToken )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir case XML_NODESET:
99cdf0e10cSrcweir lcl_setValue( mxBinding, OUSTRING("BindingExpression"), rValue );
100cdf0e10cSrcweir break;
101cdf0e10cSrcweir case XML_ID:
102cdf0e10cSrcweir lcl_setValue( mxBinding, OUSTRING("BindingID"), rValue );
103cdf0e10cSrcweir break;
104cdf0e10cSrcweir case XML_READONLY:
105cdf0e10cSrcweir lcl_setValue( mxBinding, OUSTRING("ReadonlyExpression"), rValue );
106cdf0e10cSrcweir break;
107cdf0e10cSrcweir case XML_RELEVANT:
108cdf0e10cSrcweir lcl_setValue( mxBinding, OUSTRING("RelevantExpression"), rValue );
109cdf0e10cSrcweir break;
110cdf0e10cSrcweir case XML_REQUIRED:
111cdf0e10cSrcweir lcl_setValue( mxBinding, OUSTRING("RequiredExpression"), rValue );
112cdf0e10cSrcweir break;
113cdf0e10cSrcweir case XML_CONSTRAINT:
114cdf0e10cSrcweir lcl_setValue( mxBinding, OUSTRING("ConstraintExpression"), rValue );
115cdf0e10cSrcweir break;
116cdf0e10cSrcweir case XML_CALCULATE:
117cdf0e10cSrcweir lcl_setValue( mxBinding, OUSTRING("CalculateExpression"), rValue );
118cdf0e10cSrcweir break;
119cdf0e10cSrcweir case XML_TYPE:
120cdf0e10cSrcweir lcl_setValue( mxBinding, OUSTRING("Type"),
121cdf0e10cSrcweir makeAny( lcl_getTypeName( mxModel->getDataTypeRepository(),
122cdf0e10cSrcweir GetImport().GetNamespaceMap(),
123cdf0e10cSrcweir rValue ) ) );
124cdf0e10cSrcweir break;
125cdf0e10cSrcweir default:
126cdf0e10cSrcweir DBG_ERROR( "should not happen" );
127cdf0e10cSrcweir break;
128cdf0e10cSrcweir }
129cdf0e10cSrcweir }
130cdf0e10cSrcweir
StartElement(const Reference<XAttributeList> & xAttributeList)131cdf0e10cSrcweir void XFormsBindContext::StartElement(
132cdf0e10cSrcweir const Reference<XAttributeList>& xAttributeList )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir // we need to register the namespaces
135cdf0e10cSrcweir Reference<XNameContainer> xContainer(
136cdf0e10cSrcweir mxBinding->getPropertyValue( OUSTRING("BindingNamespaces") ),
137cdf0e10cSrcweir UNO_QUERY );
138cdf0e10cSrcweir
139cdf0e10cSrcweir DBG_ASSERT( xContainer.is(), "binding should have a namespace container" );
140cdf0e10cSrcweir if( xContainer.is() )
141cdf0e10cSrcweir lcl_fillNamespaceContainer( GetImport().GetNamespaceMap(), xContainer);
142cdf0e10cSrcweir
143cdf0e10cSrcweir // call super-class for attribute handling
144cdf0e10cSrcweir TokenContext::StartElement( xAttributeList );
145cdf0e10cSrcweir }
146cdf0e10cSrcweir
147cdf0e10cSrcweir /** will be called for each child element */
HandleChild(sal_uInt16,sal_uInt16,const OUString &,const Reference<XAttributeList> &)148cdf0e10cSrcweir SvXMLImportContext* XFormsBindContext::HandleChild(
149cdf0e10cSrcweir sal_uInt16,
150cdf0e10cSrcweir sal_uInt16,
151cdf0e10cSrcweir const OUString&,
152cdf0e10cSrcweir const Reference<XAttributeList>& )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir DBG_ERROR( "no children supported" );
155cdf0e10cSrcweir return NULL;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir
158cdf0e10cSrcweir
lcl_fillNamespaceContainer(const SvXMLNamespaceMap & aMap,Reference<XNameContainer> & xContainer)159cdf0e10cSrcweir void lcl_fillNamespaceContainer(
160cdf0e10cSrcweir const SvXMLNamespaceMap& aMap,
161cdf0e10cSrcweir Reference<XNameContainer>& xContainer )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir sal_uInt16 nKeyIter = aMap.GetFirstKey();
164cdf0e10cSrcweir do
165cdf0e10cSrcweir {
166cdf0e10cSrcweir // get prefix and namespace
167cdf0e10cSrcweir const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter );
168cdf0e10cSrcweir const OUString& sNamespace = aMap.GetNameByKey( nKeyIter );
169cdf0e10cSrcweir
170cdf0e10cSrcweir // as a hack, we will ignore our own 'default' namespaces
171cdf0e10cSrcweir DBG_ASSERT( sPrefix.getLength() > 0, "no prefix?" );
172cdf0e10cSrcweir if( sPrefix.getStr()[0] != sal_Unicode( '_' ) &&
173cdf0e10cSrcweir nKeyIter >= XML_OLD_NAMESPACE_META_IDX )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir // insert prefix (use replace if already known)
176cdf0e10cSrcweir if( xContainer->hasByName( sPrefix ) )
177cdf0e10cSrcweir xContainer->replaceByName( sPrefix, makeAny( sNamespace ) );
178cdf0e10cSrcweir else
179cdf0e10cSrcweir xContainer->insertByName( sPrefix, makeAny( sNamespace ) );
180cdf0e10cSrcweir }
181cdf0e10cSrcweir
182cdf0e10cSrcweir // proceed to next
183cdf0e10cSrcweir nKeyIter = aMap.GetNextKey( nKeyIter );
184cdf0e10cSrcweir }
185cdf0e10cSrcweir while( nKeyIter != XML_NAMESPACE_UNKNOWN );
186cdf0e10cSrcweir }
187