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 "xformsapi.hxx"
22 :
23 : #include <com/sun/star/uno/Reference.hxx>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <com/sun/star/container/XNameAccess.hpp>
27 : #include <com/sun/star/xforms/XFormsSupplier.hpp>
28 : #include <com/sun/star/xforms/XDataTypeRepository.hpp>
29 : #include <com/sun/star/xforms/Model.hpp>
30 : #include <com/sun/star/xforms/XModel2.hpp>
31 : #include <com/sun/star/container/XNameContainer.hpp>
32 : #include <com/sun/star/xsd/DataTypeClass.hpp>
33 :
34 : #include <comphelper/processfactory.hxx>
35 : #include <tools/debug.hxx>
36 :
37 : #include <xmloff/xmltoken.hxx>
38 : #include <xmloff/nmspmap.hxx>
39 : #include <xmloff/xmlnmspe.hxx>
40 : #include <xmloff/xmltkmap.hxx>
41 :
42 : using com::sun::star::uno::Reference;
43 : using com::sun::star::uno::Sequence;
44 : using com::sun::star::uno::UNO_QUERY;
45 : using com::sun::star::uno::UNO_QUERY_THROW;
46 : using com::sun::star::beans::XPropertySet;
47 : using com::sun::star::container::XNameAccess;
48 : using com::sun::star::lang::XMultiServiceFactory;
49 : using com::sun::star::xforms::XFormsSupplier;
50 : using com::sun::star::xforms::XDataTypeRepository;
51 : using com::sun::star::xforms::Model;
52 : using com::sun::star::xforms::XModel2;
53 : using com::sun::star::container::XNameContainer;
54 : using com::sun::star::uno::makeAny;
55 : using com::sun::star::uno::Any;
56 : using com::sun::star::uno::Exception;
57 :
58 : using namespace com::sun::star;
59 : using namespace xmloff::token;
60 :
61 0 : Reference<XModel2> xforms_createXFormsModel()
62 : {
63 0 : Reference<XModel2> xModel = Model::create( comphelper::getProcessComponentContext() );
64 :
65 0 : return xModel;
66 : }
67 :
68 0 : void xforms_addXFormsModel(
69 : const Reference<frame::XModel>& xDocument,
70 : const Reference<xforms::XModel2>& xModel )
71 : {
72 0 : bool bSuccess = false;
73 : try
74 : {
75 0 : Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
76 0 : if( xSupplier.is() )
77 : {
78 0 : Reference<XNameContainer> xForms = xSupplier->getXForms();
79 0 : if( xForms.is() )
80 : {
81 0 : OUString sName;
82 0 : xModel->getPropertyValue("ID") >>= sName;
83 0 : xForms->insertByName( sName, makeAny( xModel ) );
84 0 : bSuccess = true;
85 0 : }
86 0 : }
87 : }
88 0 : catch( const Exception& )
89 : {
90 : ; // no success!
91 : }
92 :
93 : // TODO: implement proper error handling
94 : DBG_ASSERT( bSuccess, "can't import model" );
95 : (void)bSuccess;
96 0 : }
97 :
98 0 : static Reference<XPropertySet> lcl_findXFormsBindingOrSubmission(
99 : Reference<frame::XModel>& xDocument,
100 : const OUString& rBindingID,
101 : bool bBinding )
102 : {
103 : // find binding by iterating over all models, and look for the
104 : // given binding ID
105 :
106 0 : Reference<XPropertySet> xRet;
107 : try
108 : {
109 : // get supplier
110 0 : Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
111 0 : if( xSupplier.is() )
112 : {
113 : // get XForms models
114 0 : Reference<XNameContainer> xForms = xSupplier->getXForms();
115 0 : if( xForms.is() )
116 : {
117 : // iterate over all models
118 0 : Sequence<OUString> aNames = xForms->getElementNames();
119 0 : const OUString* pNames = aNames.getConstArray();
120 0 : sal_Int32 nNames = aNames.getLength();
121 0 : for( sal_Int32 n = 0; (n < nNames) && !xRet.is(); n++ )
122 : {
123 : Reference<xforms::XModel2> xModel(
124 0 : xForms->getByName( pNames[n] ), UNO_QUERY );
125 0 : if( xModel.is() )
126 : {
127 : // ask model for bindings
128 : Reference<XNameAccess> xBindings(
129 : bBinding
130 0 : ? xModel->getBindings()
131 0 : : xModel->getSubmissions(),
132 0 : UNO_QUERY_THROW );
133 :
134 : // finally, ask binding for name
135 0 : if( xBindings->hasByName( rBindingID ) )
136 0 : xRet.set( xBindings->getByName( rBindingID ),
137 0 : UNO_QUERY );
138 : }
139 0 : }
140 0 : }
141 0 : }
142 : }
143 0 : catch( const Exception& )
144 : {
145 : ; // no success!
146 : }
147 :
148 : // TODO: if (!xRet.is()) rImport.SetError(...);
149 :
150 0 : return xRet;
151 : }
152 :
153 0 : Reference<XPropertySet> xforms_findXFormsBinding(
154 : Reference<frame::XModel>& xDocument,
155 : const OUString& rBindingID )
156 : {
157 0 : return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, true );
158 : }
159 :
160 0 : Reference<XPropertySet> xforms_findXFormsSubmission(
161 : Reference<frame::XModel>& xDocument,
162 : const OUString& rBindingID )
163 : {
164 0 : return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, false );
165 : }
166 :
167 0 : void xforms_setValue( Reference<XPropertySet>& xPropertySet,
168 : const OUString& rName,
169 : const Any rAny )
170 : {
171 0 : xPropertySet->setPropertyValue( rName, rAny );
172 0 : }
173 :
174 : #define TOKEN_MAP_ENTRY(NAMESPACE,TOKEN) { XML_NAMESPACE_##NAMESPACE, xmloff::token::XML_##TOKEN, xmloff::token::XML_##TOKEN }
175 : static const SvXMLTokenMapEntry aTypes[] =
176 : {
177 : TOKEN_MAP_ENTRY( XSD, STRING ),
178 : TOKEN_MAP_ENTRY( XSD, DECIMAL ),
179 : TOKEN_MAP_ENTRY( XSD, DOUBLE ),
180 : TOKEN_MAP_ENTRY( XSD, FLOAT ),
181 : TOKEN_MAP_ENTRY( XSD, BOOLEAN ),
182 : TOKEN_MAP_ENTRY( XSD, ANYURI ),
183 : TOKEN_MAP_ENTRY( XSD, DATETIME_XSD ),
184 : TOKEN_MAP_ENTRY( XSD, DATE ),
185 : TOKEN_MAP_ENTRY( XSD, TIME ),
186 : TOKEN_MAP_ENTRY( XSD, YEAR ),
187 : TOKEN_MAP_ENTRY( XSD, MONTH ),
188 : TOKEN_MAP_ENTRY( XSD, DAY ),
189 : XML_TOKEN_MAP_END
190 : };
191 :
192 0 : sal_uInt16 xforms_getTypeClass(
193 : const Reference<XDataTypeRepository>&
194 : #ifdef DBG_UTIL
195 : xRepository
196 : #endif
197 : ,
198 : const SvXMLNamespaceMap& rNamespaceMap,
199 : const OUString& rXMLName )
200 : {
201 : // translate name into token for local name
202 0 : OUString sLocalName;
203 0 : sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName(rXMLName, &sLocalName);
204 0 : SvXMLTokenMap aMap( aTypes );
205 0 : sal_uInt16 mnToken = aMap.Get( nPrefix, sLocalName );
206 :
207 0 : sal_uInt16 nTypeClass = com::sun::star::xsd::DataTypeClass::STRING;
208 0 : if( mnToken != XML_TOK_UNKNOWN )
209 : {
210 : // we found an XSD name: then get the proper API name for it
211 : DBG_ASSERT( xRepository.is(), "can't find type without repository");
212 0 : switch( mnToken )
213 : {
214 : case XML_STRING:
215 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::STRING;
216 0 : break;
217 : case XML_ANYURI:
218 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::anyURI;
219 0 : break;
220 : case XML_DECIMAL:
221 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::DECIMAL;
222 0 : break;
223 : case XML_DOUBLE:
224 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::DOUBLE;
225 0 : break;
226 : case XML_FLOAT:
227 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::FLOAT;
228 0 : break;
229 : case XML_BOOLEAN:
230 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::BOOLEAN;
231 0 : break;
232 : case XML_DATETIME_XSD:
233 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::DATETIME;
234 0 : break;
235 : case XML_DATE:
236 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::DATE;
237 0 : break;
238 : case XML_TIME:
239 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::TIME;
240 0 : break;
241 : case XML_YEAR:
242 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::gYear;
243 0 : break;
244 : case XML_DAY:
245 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::gDay;
246 0 : break;
247 : case XML_MONTH:
248 0 : nTypeClass = com::sun::star::xsd::DataTypeClass::gMonth;
249 0 : break;
250 :
251 : /* data types not yet supported:
252 : nTypeClass = com::sun::star::xsd::DataTypeClass::DURATION;
253 : nTypeClass = com::sun::star::xsd::DataTypeClass::gYearMonth;
254 : nTypeClass = com::sun::star::xsd::DataTypeClass::gMonthDay;
255 : nTypeClass = com::sun::star::xsd::DataTypeClass::hexBinary;
256 : nTypeClass = com::sun::star::xsd::DataTypeClass::base64Binary;
257 : nTypeClass = com::sun::star::xsd::DataTypeClass::QName;
258 : nTypeClass = com::sun::star::xsd::DataTypeClass::NOTATION;
259 : */
260 : }
261 : }
262 :
263 0 : return nTypeClass;
264 : }
265 :
266 :
267 0 : OUString xforms_getTypeName(
268 : const Reference<XDataTypeRepository>& xRepository,
269 : const SvXMLNamespaceMap& rNamespaceMap,
270 : const OUString& rXMLName )
271 : {
272 0 : OUString sLocalName;
273 0 : sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName(rXMLName, &sLocalName);
274 0 : SvXMLTokenMap aMap( aTypes );
275 0 : sal_uInt16 mnToken = aMap.Get( nPrefix, sLocalName );
276 : return ( mnToken == XML_TOK_UNKNOWN )
277 : ? rXMLName
278 0 : : xforms_getBasicTypeName( xRepository, rNamespaceMap, rXMLName );
279 : }
280 :
281 0 : OUString xforms_getBasicTypeName(
282 : const Reference<XDataTypeRepository>& xRepository,
283 : const SvXMLNamespaceMap& rNamespaceMap,
284 : const OUString& rXMLName )
285 : {
286 0 : OUString sTypeName = rXMLName;
287 : try
288 : {
289 0 : sTypeName =
290 0 : xRepository->getBasicDataType(
291 0 : xforms_getTypeClass( xRepository, rNamespaceMap, rXMLName ) )
292 0 : ->getName();
293 : }
294 0 : catch( const Exception& )
295 : {
296 : OSL_FAIL( "exception during type creation" );
297 : }
298 0 : return sTypeName;
299 : }
300 :
301 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|