1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XFormsBindContext.cxx,v $
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>
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
),
77 // helper function; see below
78 void lcl_fillNamespaceContainer( const SvXMLNamespaceMap
&,
79 Reference
<XNameContainer
>& );
81 XFormsBindContext::XFormsBindContext(
84 const OUString
& rLocalName
,
85 const Reference
<XPropertySet
>& xModel
) :
86 TokenContext( rImport
, nPrefix
, rLocalName
, aAttributeMap
, aEmptyMap
),
87 mxModel( xModel
, UNO_QUERY_THROW
),
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
)
106 lcl_setValue( mxBinding
, OUSTRING("BindingExpression"), rValue
);
109 lcl_setValue( mxBinding
, OUSTRING("BindingID"), rValue
);
112 lcl_setValue( mxBinding
, OUSTRING("ReadonlyExpression"), rValue
);
115 lcl_setValue( mxBinding
, OUSTRING("RelevantExpression"), rValue
);
118 lcl_setValue( mxBinding
, OUSTRING("RequiredExpression"), rValue
);
121 lcl_setValue( mxBinding
, OUSTRING("ConstraintExpression"), rValue
);
124 lcl_setValue( mxBinding
, OUSTRING("CalculateExpression"), rValue
);
127 lcl_setValue( mxBinding
, OUSTRING("Type"),
128 makeAny( lcl_getTypeName( mxModel
->getDataTypeRepository(),
129 GetImport().GetNamespaceMap(),
133 DBG_ERROR( "should not happen" );
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") ),
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(
159 const Reference
<XAttributeList
>& )
161 DBG_ERROR( "no children supported" );
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
) );
186 xContainer
->insertByName( sPrefix
, makeAny( sNamespace
) );
190 nKeyIter
= aMap
.GetNextKey( nKeyIter
);
192 while( nKeyIter
!= XML_NAMESPACE_UNKNOWN
);