Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "XFormsBindContext.hxx"
22 :
23 : #include "xformsapi.hxx"
24 :
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>
31 :
32 : #include <com/sun/star/container/XNameContainer.hpp>
33 : #include <com/sun/star/xforms/XModel2.hpp>
34 :
35 : #include <tools/debug.hxx>
36 : #include <osl/diagnose.h>
37 :
38 : using com::sun::star::beans::XPropertySet;
39 : using com::sun::star::uno::Reference;
40 : using com::sun::star::uno::makeAny;
41 : using com::sun::star::uno::UNO_QUERY;
42 : using com::sun::star::uno::UNO_QUERY_THROW;
43 : using com::sun::star::container::XNameContainer;
44 : using com::sun::star::xml::sax::XAttributeList;
45 : using com::sun::star::xforms::XModel2;
46 : using namespace xmloff::token;
47 :
48 :
49 :
50 :
51 : static const struct SvXMLTokenMapEntry aAttributeMap[] =
52 : {
53 : TOKEN_MAP_ENTRY( NONE, NODESET ),
54 : TOKEN_MAP_ENTRY( NONE, ID ),
55 : TOKEN_MAP_ENTRY( NONE, READONLY ),
56 : TOKEN_MAP_ENTRY( NONE, RELEVANT ),
57 : TOKEN_MAP_ENTRY( NONE, REQUIRED ),
58 : TOKEN_MAP_ENTRY( NONE, CONSTRAINT ),
59 : TOKEN_MAP_ENTRY( NONE, CALCULATE ),
60 : TOKEN_MAP_ENTRY( NONE, TYPE ),
61 : XML_TOKEN_MAP_END
62 : };
63 :
64 : // helper function; see below
65 : static void lcl_fillNamespaceContainer( const SvXMLNamespaceMap&,
66 : Reference<XNameContainer>& );
67 :
68 0 : XFormsBindContext::XFormsBindContext(
69 : SvXMLImport& rImport,
70 : sal_uInt16 nPrefix,
71 : const OUString& rLocalName,
72 : const Reference<XModel2>& xModel ) :
73 : TokenContext( rImport, nPrefix, rLocalName, aAttributeMap, aEmptyMap ),
74 : mxModel( xModel ),
75 0 : mxBinding( NULL )
76 : {
77 : // attach binding to model
78 0 : mxBinding = mxModel->createBinding();
79 : DBG_ASSERT( mxBinding.is(), "can't create binding" );
80 0 : mxModel->getBindings()->insert( makeAny( mxBinding ) );
81 0 : }
82 :
83 0 : XFormsBindContext::~XFormsBindContext()
84 : {
85 0 : }
86 :
87 0 : void XFormsBindContext::HandleAttribute( sal_uInt16 nToken,
88 : const OUString& rValue )
89 : {
90 0 : switch( nToken )
91 : {
92 : case XML_NODESET:
93 0 : xforms_setValue( mxBinding, "BindingExpression", rValue );
94 0 : break;
95 : case XML_ID:
96 0 : xforms_setValue( mxBinding, "BindingID", rValue );
97 0 : break;
98 : case XML_READONLY:
99 0 : xforms_setValue( mxBinding, "ReadonlyExpression", rValue );
100 0 : break;
101 : case XML_RELEVANT:
102 0 : xforms_setValue( mxBinding, "RelevantExpression", rValue );
103 0 : break;
104 : case XML_REQUIRED:
105 0 : xforms_setValue( mxBinding, "RequiredExpression", rValue );
106 0 : break;
107 : case XML_CONSTRAINT:
108 0 : xforms_setValue( mxBinding, "ConstraintExpression", rValue );
109 0 : break;
110 : case XML_CALCULATE:
111 0 : xforms_setValue( mxBinding, "CalculateExpression", rValue );
112 0 : break;
113 : case XML_TYPE:
114 : xforms_setValue( mxBinding, "Type",
115 0 : makeAny( xforms_getTypeName( mxModel->getDataTypeRepository(),
116 0 : GetImport().GetNamespaceMap(),
117 0 : rValue ) ) );
118 0 : break;
119 : default:
120 : OSL_FAIL( "should not happen" );
121 0 : break;
122 : }
123 0 : }
124 :
125 0 : void XFormsBindContext::StartElement(
126 : const Reference<XAttributeList>& xAttributeList )
127 : {
128 : // we need to register the namespaces
129 : Reference<XNameContainer> xContainer(
130 0 : mxBinding->getPropertyValue( "BindingNamespaces" ),
131 0 : UNO_QUERY );
132 :
133 : DBG_ASSERT( xContainer.is(), "binding should have a namespace container" );
134 0 : if( xContainer.is() )
135 0 : lcl_fillNamespaceContainer( GetImport().GetNamespaceMap(), xContainer);
136 :
137 : // call super-class for attribute handling
138 0 : TokenContext::StartElement( xAttributeList );
139 0 : }
140 :
141 : /** will be called for each child element */
142 0 : SvXMLImportContext* XFormsBindContext::HandleChild(
143 : sal_uInt16,
144 : sal_uInt16,
145 : const OUString&,
146 : const Reference<XAttributeList>& )
147 : {
148 : OSL_FAIL( "no children supported" );
149 0 : return NULL;
150 : }
151 :
152 :
153 0 : static void lcl_fillNamespaceContainer(
154 : const SvXMLNamespaceMap& aMap,
155 : Reference<XNameContainer>& xContainer )
156 : {
157 0 : sal_uInt16 nKeyIter = aMap.GetFirstKey();
158 0 : do
159 : {
160 : // get prefix and namespace
161 0 : const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter );
162 0 : const OUString& sNamespace = aMap.GetNameByKey( nKeyIter );
163 :
164 : // as a hack, we will ignore our own 'default' namespaces
165 : DBG_ASSERT( !sPrefix.isEmpty(), "no prefix?" );
166 0 : if( !sPrefix.startsWith("_") &&
167 : nKeyIter >= XML_OLD_NAMESPACE_META_IDX )
168 : {
169 : // insert prefix (use replace if already known)
170 0 : if( xContainer->hasByName( sPrefix ) )
171 0 : xContainer->replaceByName( sPrefix, makeAny( sNamespace ) );
172 : else
173 0 : xContainer->insertByName( sPrefix, makeAny( sNamespace ) );
174 : }
175 :
176 : // proceed to next
177 0 : nKeyIter = aMap.GetNextKey( nKeyIter );
178 : }
179 : while( nKeyIter != XML_NAMESPACE_UNKNOWN );
180 0 : }
181 :
182 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|