LibreOffice Module xmloff (master)  1
SchemaRestrictionContext.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 
22 #include "xformsapi.hxx"
23 
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/namespacemap.hxx>
26 #include <xmloff/xmlnamespace.hxx>
27 #include <xmloff/xmltkmap.hxx>
28 #include <xmloff/xmlimp.hxx>
29 
30 #include <sax/tools/converter.hxx>
31 
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/util/Date.hpp>
34 #include <com/sun/star/util/Time.hpp>
35 #include <com/sun/star/util/DateTime.hpp>
36 #include <com/sun/star/util/Duration.hpp>
37 #include <com/sun/star/xforms/XDataTypeRepository.hpp>
38 #include <com/sun/star/xsd/DataTypeClass.hpp>
39 #include <com/sun/star/xsd/WhiteSpaceTreatment.hpp>
40 
41 #include <sal/log.hxx>
42 #include <tools/diagnose_ex.h>
43 
44 
46 using com::sun::star::uno::Exception;
47 using com::sun::star::uno::Any;
49 using namespace com::sun::star;
50 using com::sun::star::util::Duration;
51 using com::sun::star::xml::sax::XFastAttributeList;
52 using com::sun::star::xforms::XDataTypeRepository;
53 using namespace xmloff::token;
54 
55 
57  SvXMLImport& rImport,
59  const OUString& sTypeName ) :
60  TokenContext( rImport ),
61  mxRepository( rRepository ),
62  msTypeName( sTypeName )
63 {
64  SAL_WARN_IF( !mxRepository.is(), "xmloff", "need repository" );
65 }
66 
68 {
69  // only do something if we don't have a data type already
70  if( mxDataType.is() )
71  return;
72 
73  SAL_WARN_IF( msBaseName.isEmpty(), "xmloff", "no base name?" );
74  SAL_WARN_IF( !mxRepository.is(), "xmloff", "no repository?" );
75 
76  try
77  {
78  mxDataType =
79  mxRepository->cloneDataType(
81  GetImport().GetNamespaceMap(),
82  msBaseName ),
83  msTypeName );
84  }
85  catch( const Exception& )
86  {
87  TOOLS_WARN_EXCEPTION("xmloff", "exception during type creation");
88  }
89  SAL_WARN_IF( !mxDataType.is(), "xmloff", "can't create type" );
90 }
91 
93 {
94  switch (aIter.getToken() & TOKEN_MASK)
95  {
96  case XML_BASE:
97  msBaseName = aIter.toString();
98  break;
99  }
100 }
101 
102 typedef Any (*convert_t)( const OUString& );
103 
104 static Any xforms_string( const OUString& rValue )
105 {
106  return makeAny( rValue );
107 }
108 
109 static Any xforms_int32( const OUString& rValue )
110 {
111  sal_Int32 nValue;
112  bool bSuccess = ::sax::Converter::convertNumber( nValue, rValue );
113  return bSuccess ? makeAny( nValue ) : Any();
114 }
115 
116 static Any xforms_int16( const OUString& rValue )
117 {
118  sal_Int32 nValue;
119  bool bSuccess = ::sax::Converter::convertNumber( nValue, rValue );
120  return bSuccess ? makeAny( static_cast<sal_Int16>( nValue ) ) : Any();
121 }
122 
123 static Any xforms_whitespace( const OUString& rValue )
124 {
125  Any aValue;
126  if( IsXMLToken( rValue, XML_PRESERVE ) )
127  aValue <<= css::xsd::WhiteSpaceTreatment::Preserve;
128  else if( IsXMLToken( rValue, XML_REPLACE ) )
129  aValue <<= css::xsd::WhiteSpaceTreatment::Replace;
130  else if( IsXMLToken( rValue, XML_COLLAPSE ) )
131  aValue <<= css::xsd::WhiteSpaceTreatment::Collapse;
132  return aValue;
133 }
134 
135 static Any xforms_double( const OUString& rValue )
136 {
137  double fValue;
138  bool bSuccess = ::sax::Converter::convertDouble( fValue, rValue );
139  return bSuccess ? makeAny( fValue ) : Any();
140 }
141 
142 static Any xforms_date( const OUString& rValue )
143 {
144  Any aAny;
145 
146  // parse ISO date
147  sal_Int32 nPos1 = rValue.indexOf( '-' );
148  sal_Int32 nPos2 = rValue.indexOf( '-', nPos1 + 1 );
149  if( nPos1 > 0 && nPos2 > 0 )
150  {
151  util::Date aDate;
152  aDate.Year = static_cast<sal_uInt16>(
153  rValue.copy( 0, nPos1 ).toInt32() );
154  aDate.Month = static_cast<sal_uInt16>(
155  rValue.copy( nPos1 + 1, nPos2 - nPos1 - 1 ).toInt32() );
156  aDate.Day = static_cast<sal_uInt16>(
157  rValue.copy( nPos2 + 1 ).toInt32() );
158  aAny <<= aDate;
159  }
160  return aAny;
161 }
162 
163 static Any xforms_dateTime( const OUString& rValue )
164 {
165  util::DateTime aDateTime;
166  bool const bSuccess = ::sax::Converter::parseDateTime(aDateTime, rValue);
167  return bSuccess ? makeAny( aDateTime ) : Any();
168 }
169 
170 static Any xforms_time( const OUString& rValue )
171 {
172  Any aAny;
173  Duration aDuration;
174  if (::sax::Converter::convertDuration( aDuration, rValue ))
175  {
176  css::util::Time aTime;
177  aTime.Hours = aDuration.Hours;
178  aTime.Minutes = aDuration.Minutes;
179  aTime.Seconds = aDuration.Seconds;
180  aTime.NanoSeconds = aDuration.NanoSeconds;
181  aAny <<= aTime;
182  }
183  return aAny;
184 }
185 
186 
188  sal_Int32 nElementToken,
189  const Reference<XFastAttributeList>& xAttrList )
190 {
191  // find value
192  OUString sValue;
193  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
194  {
195  if( ( aIter.getToken() & TOKEN_MASK) == XML_VALUE )
196  {
197  sValue = aIter.toString();
198  break;
199  }
200  }
201 
202  // determine property name + suitable converter
203  OUString sPropertyName;
204  convert_t pConvert = nullptr;
205  switch( nElementToken & TOKEN_MASK )
206  {
207  case XML_LENGTH:
208  sPropertyName = "Length";
209  pConvert = &xforms_int32;
210  break;
211  case XML_MINLENGTH:
212  sPropertyName = "MinLength";
213  pConvert = &xforms_int32;
214  break;
215  case XML_MAXLENGTH:
216  sPropertyName = "MaxLength";
217  pConvert = &xforms_int32;
218  break;
219  case XML_TOTALDIGITS:
220  sPropertyName = "TotalDigits";
221  pConvert = &xforms_int32;
222  break;
223  case XML_FRACTIONDIGITS:
224  sPropertyName = "FractionDigits";
225  pConvert = &xforms_int32;
226  break;
227  case XML_PATTERN:
228  sPropertyName = "Pattern";
229  pConvert = &xforms_string;
230  break;
231  case XML_WHITESPACE:
232  sPropertyName = "WhiteSpace";
233  pConvert = &xforms_whitespace;
234  break;
235  case XML_MININCLUSIVE:
236  case XML_MINEXCLUSIVE:
237  case XML_MAXINCLUSIVE:
238  case XML_MAXEXCLUSIVE:
239  {
240  // these attributes are mapped to different properties.
241  // To determine the property name, we use an attribute
242  // dependent prefix and a type dependent suffix. The
243  // converter is only type dependent.
244 
245  // first, attribute-dependent prefix
246  switch( nElementToken & TOKEN_MASK )
247  {
248  case XML_MININCLUSIVE:
249  sPropertyName = "MinInclusive";
250  break;
251  case XML_MINEXCLUSIVE:
252  sPropertyName = "MinExclusive";
253  break;
254  case XML_MAXINCLUSIVE:
255  sPropertyName = "MaxInclusive";
256  break;
257  case XML_MAXEXCLUSIVE:
258  sPropertyName = "MaxExclusive";
259  break;
260  }
261 
262  // second, type-dependent suffix + converter
264  GetImport().GetNamespaceMap(),
265  msBaseName ) )
266  {
267  case css::xsd::DataTypeClass::DECIMAL:
268  case css::xsd::DataTypeClass::DOUBLE:
269  case css::xsd::DataTypeClass::FLOAT:
270  sPropertyName += "Double";
271  pConvert = &xforms_double;
272  break;
273  case css::xsd::DataTypeClass::DATETIME:
274  sPropertyName += "DateTime";
275  pConvert = &xforms_dateTime;
276  break;
277  case css::xsd::DataTypeClass::DATE:
278  sPropertyName += "Date";
279  pConvert = &xforms_date;
280  break;
281  case css::xsd::DataTypeClass::TIME:
282  sPropertyName += "Time";
283  pConvert = &xforms_time;
284  break;
285  case css::xsd::DataTypeClass::gYear:
286  case css::xsd::DataTypeClass::gDay:
287  case css::xsd::DataTypeClass::gMonth:
288  sPropertyName += "Int";
289  pConvert = &xforms_int16;
290  break;
291 
292  case css::xsd::DataTypeClass::STRING:
293  case css::xsd::DataTypeClass::anyURI:
294  case css::xsd::DataTypeClass::BOOLEAN:
295  // invalid: These shouldn't have min/max-inclusive
296  break;
297 
298  /* data types not yet supported:
299  case css::xsd::DataTypeClass::DURATION:
300  case css::xsd::DataTypeClass::gYearMonth:
301  case css::xsd::DataTypeClass::gMonthDay:
302  case css::xsd::DataTypeClass::hexBinary:
303  case css::xsd::DataTypeClass::base64Binary:
304  case css::xsd::DataTypeClass::QName:
305  case css::xsd::DataTypeClass::NOTATION:
306  */
307  }
308  }
309  break;
310 
311  default:
312  OSL_FAIL( "unknown facet" );
313  }
314 
315  // finally, set the property
316  CreateDataType();
317  if( mxDataType.is()
318  && !sPropertyName.isEmpty()
319  && pConvert != nullptr
320  && mxDataType->getPropertySetInfo()->hasPropertyByName(sPropertyName) )
321  {
322  try
323  {
324  mxDataType->setPropertyValue( sPropertyName, pConvert( sValue ) );
325  }
326  catch( const Exception& )
327  {
328  ; // can't set property? Then ignore.
329  }
330  }
331 
332  return new SvXMLImportContext( GetImport() );
333 }
334 
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Any(* convert_t)(const OUString &)
css::uno::Reference< css::xforms::XDataTypeRepository > mxRepository
static Any xforms_whitespace(const OUString &rValue)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3511
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
static bool parseDateTime(css::util::DateTime &rDateTime, std::u16string_view rString)
static void convertDouble(OUStringBuffer &rBuffer, double fNumber, bool bWriteUnits, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit)
static Any xforms_int16(const OUString &rValue)
static Any xforms_time(const OUString &rValue)
handle attributes through an SvXMLTokenMap
virtual void HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter &aIter) override
will be called for each attribute
SchemaRestrictionContext(SvXMLImport &rImport, css::uno::Reference< css::xforms::XDataTypeRepository > const &rRepository, const OUString &sTypeName)
OUString xforms_getBasicTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:277
#define TOOLS_WARN_EXCEPTION(area, stream)
static Any xforms_string(const OUString &rValue)
SvXMLImportContext(SvXMLImport &rImport)
A contexts constructor does anything that is required if an element starts.
Definition: xmlictxt.cxx:30
static void convertDuration(OUStringBuffer &rBuffer, const double fTime)
static Any xforms_date(const OUString &rValue)
static Any xforms_dateTime(const OUString &rValue)
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
Any makeAny(Color const &value)
#define SAL_WARN_IF(condition, area, stream)
css::uno::Reference< css::beans::XPropertySet > mxDataType
Handling of tokens in XML:
static Any xforms_int32(const OUString &rValue)
constexpr sal_Int32 TOKEN_MASK
Definition: xmlimp.hxx:94
virtual SvXMLImportContext * HandleChild(sal_Int32 nElementToken, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
will be called for each child element
sal_uInt16 xforms_getTypeClass(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:192
sal_Int16 nValue
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
static Any xforms_double(const OUString &rValue)
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)