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