LibreOffice Module xmloff (master)  1
vcl_date_handler.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 #include "vcl_date_handler.hxx"
21 
22 #include <rtl/ustrbuf.hxx>
23 
24 #include <com/sun/star/util/DateTime.hpp>
25 #include <com/sun/star/util/Date.hpp>
26 
27 #include <sax/tools/converter.hxx>
28 
29 #include <osl/diagnose.h>
30 #include <tools/date.hxx>
31 
32 namespace xmloff
33 {
34 
35  using ::com::sun::star::uno::Any;
36  using ::com::sun::star::uno::makeAny;
37  using ::com::sun::star::util::DateTime;
38  using ::com::sun::star::util::Date;
39 
40  //= VCLDateHandler
42  {
43  }
44 
45  OUString VCLDateHandler::getAttributeValue( const Any& i_propertyValue ) const
46  {
47  Date aDate;
48  OSL_VERIFY( i_propertyValue >>= aDate );
49 
50  DateTime aDateTime; // default-inited to 0
51  aDateTime.Day = aDate.Day;
52  aDateTime.Month = aDate.Month;
53  aDateTime.Year = aDate.Year;
54 
55  OUStringBuffer aBuffer;
56  ::sax::Converter::convertDateTime( aBuffer, aDateTime, nullptr );
57  return aBuffer.makeStringAndClear();
58  }
59 
60  bool VCLDateHandler::getPropertyValues( const OUString& i_attributeValue, PropertyValues& o_propertyValues ) const
61  {
62  DateTime aDateTime;
63  Date aDate;
64  if (::sax::Converter::parseDateTime( aDateTime, i_attributeValue ))
65  {
66  aDate.Day = aDateTime.Day;
67  aDate.Month = aDateTime.Month;
68  aDate.Year = aDateTime.Year;
69  }
70  else
71  {
72  // compatibility format, before we wrote those values in XML-schema compatible form
73  sal_Int32 nVCLDate(0);
74  if (!::sax::Converter::convertNumber(nVCLDate, i_attributeValue))
75  {
76  OSL_ENSURE( false, "VCLDateHandler::getPropertyValues: unknown date format (no XML-schema date, no legacy integer)!" );
77  return false;
78  }
79  aDate = ::Date(nVCLDate).GetUNODate();
80  }
81 
82  const Any aPropertyValue( makeAny( aDate ) );
83 
84  OSL_ENSURE( o_propertyValues.size() == 1, "VCLDateHandler::getPropertyValues: date strings represent exactly one property - not more, not less!" );
85  for ( auto& prop : o_propertyValues )
86  {
87  prop.second = aPropertyValue;
88  }
89  return true;
90  }
91 
92 } // namespace xmloff
93 
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::map< PropertyId, css::uno::Any > PropertyValues
static bool parseDateTime(css::util::DateTime &rDateTime, std::u16string_view rString)
css::util::Date GetUNODate() const
static void convertDateTime(OUStringBuffer &rBuffer, const css::util::DateTime &rDateTime, sal_Int16 const *pTimeZoneOffset, bool bAddTimeIf0AM=false)
virtual bool getPropertyValues(const OUString &i_attributeValue, PropertyValues &o_propertyValues) const override
retrieves the values of the properties controlled by an XML attributed, described by a given attribut...
std::unique_ptr< char[]> aBuffer
virtual OUString getAttributeValue(const css::uno::Any &i_propertyValue) const override
is a convenience method for XML attributes whose value comprises of only one UNO API property ...
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)