LibreOffice Module xmloff (master)  1
xformsapi.cxx
Go to the documentation of this file.
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/frame/XModel.hpp>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/beans/XPropertySet.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 
35 #include <osl/diagnose.h>
36 #include <sal/log.hxx>
37 
38 #include <xmloff/xmltoken.hxx>
39 #include <xmloff/namespacemap.hxx>
40 #include <xmloff/xmlnamespace.hxx>
41 #include <xmloff/xmltkmap.hxx>
42 
45 using com::sun::star::uno::UNO_QUERY;
46 using com::sun::star::uno::UNO_QUERY_THROW;
48 using com::sun::star::container::XNameAccess;
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;
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 
62 {
64 
65  return xModel;
66 }
67 
69  const Reference<frame::XModel>& xDocument,
70  const Reference<xforms::XModel2>& xModel )
71 {
72  bool bSuccess = false;
73  try
74  {
75  Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
76  if( xSupplier.is() )
77  {
78  Reference<XNameContainer> xForms = xSupplier->getXForms();
79  if( xForms.is() )
80  {
81  OUString sName;
82  xModel->getPropertyValue("ID") >>= sName;
83  xForms->insertByName( sName, makeAny( xModel ) );
84  bSuccess = true;
85  }
86  }
87  }
88  catch( const Exception& )
89  {
90  ; // no success!
91  }
92 
93  // TODO: implement proper error handling
94  SAL_WARN_IF( !bSuccess, "xmloff", "can't import model" );
95 }
96 
98  Reference<frame::XModel> const & xDocument,
99  const OUString& rBindingID,
100  bool bBinding )
101 {
102  // find binding by iterating over all models, and look for the
103  // given binding ID
104 
106  try
107  {
108  // get supplier
109  Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
110  if( xSupplier.is() )
111  {
112  // get XForms models
113  Reference<XNameContainer> xForms = xSupplier->getXForms();
114  if( xForms.is() )
115  {
116  // iterate over all models
117  const Sequence<OUString> aNames = xForms->getElementNames();
118  for( const auto& rName : aNames )
119  {
121  xForms->getByName( rName ), UNO_QUERY );
122  if( xModel.is() )
123  {
124  // ask model for bindings
125  Reference<XNameAccess> xBindings(
126  bBinding
127  ? xModel->getBindings()
128  : xModel->getSubmissions(),
129  UNO_QUERY_THROW );
130 
131  // finally, ask binding for name
132  if( xBindings->hasByName( rBindingID ) )
133  xRet.set( xBindings->getByName( rBindingID ),
134  UNO_QUERY );
135  }
136 
137  if (xRet.is())
138  break;
139  }
140  }
141  }
142  }
143  catch( const Exception& )
144  {
145  ; // no success!
146  }
147 
148  // TODO: if (!xRet.is()) rImport.SetError(...);
149 
150  return xRet;
151 }
152 
154  Reference<frame::XModel> const & xDocument,
155  const OUString& rBindingID )
156 {
157  return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, true );
158 }
159 
161  Reference<frame::XModel> const & xDocument,
162  const OUString& rBindingID )
163 {
164  return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, false );
165 }
166 
167 void xforms_setValue( Reference<XPropertySet> const & xPropertySet,
168  const OUString& rName,
169  const Any& rAny )
170 {
171  xPropertySet->setPropertyValue( rName, rAny );
172 }
173 
174 #define TOKEN_MAP_ENTRY(NAMESPACE,TOKEN) { XML_NAMESPACE_##NAMESPACE, xmloff::token::XML_##TOKEN, xmloff::token::XML_##TOKEN }
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 ),
190 };
191 
193  const Reference<XDataTypeRepository>& xRepository,
194  const SvXMLNamespaceMap& rNamespaceMap,
195  const OUString& rXMLName )
196 {
197  // translate name into token for local name
198  OUString sLocalName;
199  sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName);
200  static const SvXMLTokenMap aMap( aTypes );
201  sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
202 
203  sal_uInt16 nTypeClass = css::xsd::DataTypeClass::STRING;
204  if( nToken != XML_TOK_UNKNOWN )
205  {
206  // we found an XSD name: then get the proper API name for it
207  SAL_WARN_IF( !xRepository.is(), "xmloff", "can't find type without repository");
208  switch( nToken )
209  {
210  case XML_STRING:
211  nTypeClass = css::xsd::DataTypeClass::STRING;
212  break;
213  case XML_ANYURI:
214  nTypeClass = css::xsd::DataTypeClass::anyURI;
215  break;
216  case XML_DECIMAL:
217  nTypeClass = css::xsd::DataTypeClass::DECIMAL;
218  break;
219  case XML_DOUBLE:
220  nTypeClass = css::xsd::DataTypeClass::DOUBLE;
221  break;
222  case XML_FLOAT:
223  nTypeClass = css::xsd::DataTypeClass::FLOAT;
224  break;
225  case XML_BOOLEAN:
226  nTypeClass = css::xsd::DataTypeClass::BOOLEAN;
227  break;
228  case XML_DATETIME_XSD:
229  nTypeClass = css::xsd::DataTypeClass::DATETIME;
230  break;
231  case XML_DATE:
232  nTypeClass = css::xsd::DataTypeClass::DATE;
233  break;
234  case XML_TIME:
235  nTypeClass = css::xsd::DataTypeClass::TIME;
236  break;
237  case XML_YEAR:
238  nTypeClass = css::xsd::DataTypeClass::gYear;
239  break;
240  case XML_DAY:
241  nTypeClass = css::xsd::DataTypeClass::gDay;
242  break;
243  case XML_MONTH:
244  nTypeClass = css::xsd::DataTypeClass::gMonth;
245  break;
246 
247  /* data types not yet supported:
248  nTypeClass = css::xsd::DataTypeClass::DURATION;
249  nTypeClass = css::xsd::DataTypeClass::gYearMonth;
250  nTypeClass = css::xsd::DataTypeClass::gMonthDay;
251  nTypeClass = css::xsd::DataTypeClass::hexBinary;
252  nTypeClass = css::xsd::DataTypeClass::base64Binary;
253  nTypeClass = css::xsd::DataTypeClass::QName;
254  nTypeClass = css::xsd::DataTypeClass::NOTATION;
255  */
256  }
257  }
258 
259  return nTypeClass;
260 }
261 
262 
264  const Reference<XDataTypeRepository>& xRepository,
265  const SvXMLNamespaceMap& rNamespaceMap,
266  const OUString& rXMLName )
267 {
268  OUString sLocalName;
269  sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName);
270  static const SvXMLTokenMap aMap( aTypes );
271  sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
272  return ( nToken == XML_TOK_UNKNOWN )
273  ? rXMLName
274  : xforms_getBasicTypeName( xRepository, rNamespaceMap, rXMLName );
275 }
276 
278  const Reference<XDataTypeRepository>& xRepository,
279  const SvXMLNamespaceMap& rNamespaceMap,
280  const OUString& rXMLName )
281 {
282  OUString sTypeName = rXMLName;
283  try
284  {
285  sTypeName =
286  xRepository->getBasicDataType(
287  xforms_getTypeClass( xRepository, rNamespaceMap, rXMLName ) )
288  ->getName();
289  }
290  catch( const Exception& )
291  {
292  OSL_FAIL( "exception during type creation" );
293  }
294  return sTypeName;
295 }
296 
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
STRING
#define XML_TOK_UNKNOWN
Definition: xmltkmap.hxx:32
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
HashMap_OWString_Interface aMap
DATE
sal_uInt16 GetKeyByAttrValueQName(const OUString &rAttrName, OUString *pLocalName) const
const char * sName
void xforms_setValue(Reference< XPropertySet > const &xPropertySet, const OUString &rName, const Any &rAny)
Definition: xformsapi.cxx:167
Reference< XModel2 > xforms_createXFormsModel()
Definition: xformsapi.cxx:61
Reference< XPropertySet > xforms_findXFormsSubmission(Reference< frame::XModel > const &xDocument, const OUString &rBindingID)
Definition: xformsapi.cxx:160
Reference< XPropertySet > xforms_findXFormsBinding(Reference< frame::XModel > const &xDocument, const OUString &rBindingID)
Definition: xformsapi.cxx:153
OUString xforms_getBasicTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:277
class SAL_NO_VTABLE XPropertySet
void xforms_addXFormsModel(const Reference< frame::XModel > &xDocument, const Reference< xforms::XModel2 > &xModel)
Definition: xformsapi.cxx:68
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
Definition: xmltkmap.cxx:99
#define DOUBLE
const SvXMLTokenMapEntry aTypes[]
Definition: xformsapi.cxx:175
#define TOKEN_MAP_ENTRY(NAMESPACE, TOKEN)
Definition: xformsapi.cxx:174
DefTokenId nToken
Any makeAny(Color const &value)
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
Reference< XComponentContext > getProcessComponentContext()
OUString xforms_getTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:263
Reference< XModel > xModel
static Reference< XPropertySet > lcl_findXFormsBindingOrSubmission(Reference< frame::XModel > const &xDocument, const OUString &rBindingID, bool bBinding)
Definition: xformsapi.cxx:97
sal_uInt16 xforms_getTypeClass(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:192
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)