1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
37 using com::sun::star::beans::XPropertySet
;
38 using com::sun::star::uno::Reference
;
39 using com::sun::star::uno::makeAny
;
40 using com::sun::star::uno::UNO_QUERY
;
41 using com::sun::star::uno::UNO_QUERY_THROW
;
42 using com::sun::star::container::XNameContainer
;
43 using com::sun::star::xml::sax::XAttributeList
;
44 using com::sun::star::xforms::XModel2
;
45 using namespace xmloff::token
;
50 static struct SvXMLTokenMapEntry aAttributeMap
[] =
52 TOKEN_MAP_ENTRY( NONE
, NODESET
),
53 TOKEN_MAP_ENTRY( NONE
, ID
),
54 TOKEN_MAP_ENTRY( NONE
, READONLY
),
55 TOKEN_MAP_ENTRY( NONE
, RELEVANT
),
56 TOKEN_MAP_ENTRY( NONE
, REQUIRED
),
57 TOKEN_MAP_ENTRY( NONE
, CONSTRAINT
),
58 TOKEN_MAP_ENTRY( NONE
, CALCULATE
),
59 TOKEN_MAP_ENTRY( NONE
, TYPE
),
63 // helper function; see below
64 static void lcl_fillNamespaceContainer( const SvXMLNamespaceMap
&,
65 Reference
<XNameContainer
>& );
67 XFormsBindContext::XFormsBindContext(
70 const OUString
& rLocalName
,
71 const Reference
<XModel2
>& xModel
) :
72 TokenContext( rImport
, nPrefix
, rLocalName
, aAttributeMap
, aEmptyMap
),
76 // attach binding to model
77 mxBinding
= mxModel
->createBinding();
78 DBG_ASSERT( mxBinding
.is(), "can't create binding" );
79 mxModel
->getBindings()->insert( makeAny( mxBinding
) );
82 XFormsBindContext::~XFormsBindContext()
86 void XFormsBindContext::HandleAttribute( sal_uInt16 nToken
,
87 const OUString
& rValue
)
92 xforms_setValue( mxBinding
, "BindingExpression", rValue
);
95 xforms_setValue( mxBinding
, "BindingID", rValue
);
98 xforms_setValue( mxBinding
, "ReadonlyExpression", rValue
);
101 xforms_setValue( mxBinding
, "RelevantExpression", rValue
);
104 xforms_setValue( mxBinding
, "RequiredExpression", rValue
);
107 xforms_setValue( mxBinding
, "ConstraintExpression", rValue
);
110 xforms_setValue( mxBinding
, "CalculateExpression", rValue
);
113 xforms_setValue( mxBinding
, "Type",
114 makeAny( xforms_getTypeName( mxModel
->getDataTypeRepository(),
115 GetImport().GetNamespaceMap(),
119 OSL_FAIL( "should not happen" );
124 void XFormsBindContext::StartElement(
125 const Reference
<XAttributeList
>& xAttributeList
)
127 // we need to register the namespaces
128 Reference
<XNameContainer
> xContainer(
129 mxBinding
->getPropertyValue( "BindingNamespaces" ),
132 DBG_ASSERT( xContainer
.is(), "binding should have a namespace container" );
133 if( xContainer
.is() )
134 lcl_fillNamespaceContainer( GetImport().GetNamespaceMap(), xContainer
);
136 // call super-class for attribute handling
137 TokenContext::StartElement( xAttributeList
);
140 /** will be called for each child element */
141 SvXMLImportContext
* XFormsBindContext::HandleChild(
145 const Reference
<XAttributeList
>& )
147 OSL_FAIL( "no children supported" );
152 static void lcl_fillNamespaceContainer(
153 const SvXMLNamespaceMap
& aMap
,
154 Reference
<XNameContainer
>& xContainer
)
156 sal_uInt16 nKeyIter
= aMap
.GetFirstKey();
159 // get prefix and namespace
160 const OUString
& sPrefix
= aMap
.GetPrefixByKey( nKeyIter
);
161 const OUString
& sNamespace
= aMap
.GetNameByKey( nKeyIter
);
163 // as a hack, we will ignore our own 'default' namespaces
164 DBG_ASSERT( !sPrefix
.isEmpty(), "no prefix?" );
165 if( sPrefix
.getStr()[0] != sal_Unicode( '_' ) &&
166 nKeyIter
>= XML_OLD_NAMESPACE_META_IDX
)
168 // insert prefix (use replace if already known)
169 if( xContainer
->hasByName( sPrefix
) )
170 xContainer
->replaceByName( sPrefix
, makeAny( sNamespace
) );
172 xContainer
->insertByName( sPrefix
, makeAny( sNamespace
) );
176 nKeyIter
= aMap
.GetNextKey( nKeyIter
);
178 while( nKeyIter
!= XML_NAMESPACE_UNKNOWN
);
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */