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/xmlnamespace.hxx>
30 #include <xmloff/namespacemap.hxx>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/xforms/XModel2.hpp>
35 #include <osl/diagnose.h>
36 #include <sal/log.hxx>
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::container::XNameContainer
;
42 using com::sun::star::xml::sax::XFastAttributeList
;
43 using com::sun::star::xforms::XModel2
;
44 using namespace xmloff::token
;
46 // helper function; see below
47 static void lcl_fillNamespaceContainer( const SvXMLNamespaceMap
&,
48 Reference
<XNameContainer
> const & );
50 XFormsBindContext::XFormsBindContext(
52 const Reference
<XModel2
>& xModel
) :
53 TokenContext( rImport
),
56 // attach binding to model
57 mxBinding
= mxModel
->createBinding();
58 SAL_WARN_IF( !mxBinding
.is(), "xmloff", "can't create binding" );
59 mxModel
->getBindings()->insert( makeAny( mxBinding
) );
62 void XFormsBindContext::HandleAttribute( sal_Int32 nAttributeToken
,
63 const OUString
& rValue
)
65 switch( nAttributeToken
& TOKEN_MASK
)
68 xforms_setValue( mxBinding
, "BindingExpression", rValue
);
71 xforms_setValue( mxBinding
, "BindingID", rValue
);
74 xforms_setValue( mxBinding
, "ReadonlyExpression", rValue
);
77 xforms_setValue( mxBinding
, "RelevantExpression", rValue
);
80 xforms_setValue( mxBinding
, "RequiredExpression", rValue
);
83 xforms_setValue( mxBinding
, "ConstraintExpression", rValue
);
86 xforms_setValue( mxBinding
, "CalculateExpression", rValue
);
89 xforms_setValue( mxBinding
, "Type",
90 makeAny( xforms_getTypeName( mxModel
->getDataTypeRepository(),
91 GetImport().GetNamespaceMap(),
95 OSL_FAIL( "should not happen" );
100 void XFormsBindContext::startFastElement(
102 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
104 // we need to register the namespaces
105 Reference
<XNameContainer
> xContainer(
106 mxBinding
->getPropertyValue( "BindingNamespaces" ),
109 SAL_WARN_IF( !xContainer
.is(), "xmloff", "binding should have a namespace container" );
110 if( xContainer
.is() )
111 lcl_fillNamespaceContainer( GetImport().GetNamespaceMap(), xContainer
);
113 // call super-class for attribute handling
114 TokenContext::startFastElement( nElement
, xAttrList
);
117 /** will be called for each child element */
118 SvXMLImportContext
* XFormsBindContext::HandleChild(
120 const Reference
<XFastAttributeList
>& )
122 OSL_FAIL( "no children supported" );
127 static void lcl_fillNamespaceContainer(
128 const SvXMLNamespaceMap
& aMap
,
129 Reference
<XNameContainer
> const & xContainer
)
131 sal_uInt16 nKeyIter
= aMap
.GetFirstKey();
134 // get prefix and namespace
135 const OUString
& sPrefix
= aMap
.GetPrefixByKey( nKeyIter
);
136 const OUString
& sNamespace
= aMap
.GetNameByKey( nKeyIter
);
138 // as a hack, we will ignore our own 'default' namespaces
139 SAL_WARN_IF( sPrefix
.isEmpty(), "xmloff", "no prefix?" );
140 if( !sPrefix
.startsWith("_") &&
141 nKeyIter
>= XML_NAMESPACE_META_SO52
)
143 // insert prefix (use replace if already known)
144 if( xContainer
->hasByName( sPrefix
) )
145 xContainer
->replaceByName( sPrefix
, makeAny( sNamespace
) );
147 xContainer
->insertByName( sPrefix
, makeAny( sNamespace
) );
151 nKeyIter
= aMap
.GetNextKey( nKeyIter
);
153 while( nKeyIter
!= XML_NAMESPACE_UNKNOWN
);
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */