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