xref: /aoo41x/main/forms/source/xforms/convert.cxx (revision 24acc546)
1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*24acc546SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*24acc546SAndrew Rist  * distributed with this work for additional information
6*24acc546SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*24acc546SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist  * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*24acc546SAndrew Rist  *
11*24acc546SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*24acc546SAndrew Rist  *
13*24acc546SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist  * software distributed under the License is distributed on an
15*24acc546SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist  * KIND, either express or implied.  See the License for the
17*24acc546SAndrew Rist  * specific language governing permissions and limitations
18*24acc546SAndrew Rist  * under the License.
19*24acc546SAndrew Rist  *
20*24acc546SAndrew Rist  *************************************************************/
21*24acc546SAndrew Rist 
22*24acc546SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "convert.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "unohelper.hxx"
30cdf0e10cSrcweir #include <memory>
31cdf0e10cSrcweir #include <algorithm>
32cdf0e10cSrcweir #include <functional>
33cdf0e10cSrcweir #include <rtl/math.hxx>
34cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
35cdf0e10cSrcweir #include <tools/date.hxx>
36cdf0e10cSrcweir #include <com/sun/star/uno/Type.hxx>
37cdf0e10cSrcweir #include <com/sun/star/xsd/WhiteSpaceTreatment.hpp>
38cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
39cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
40cdf0e10cSrcweir #include <com/sun/star/util/Time.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using xforms::Convert;
43cdf0e10cSrcweir using ::rtl::OUString;
44cdf0e10cSrcweir using ::rtl::OUStringBuffer;
45cdf0e10cSrcweir using com::sun::star::uno::Any;
46cdf0e10cSrcweir using com::sun::star::uno::makeAny;
47cdf0e10cSrcweir using com::sun::star::util::Time;
48cdf0e10cSrcweir using namespace std;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir typedef com::sun::star::util::Date UNODate;
51cdf0e10cSrcweir typedef com::sun::star::util::Time UNOTime;
52cdf0e10cSrcweir typedef com::sun::star::util::DateTime UNODateTime;
53cdf0e10cSrcweir 
Convert()54cdf0e10cSrcweir Convert::Convert()
55cdf0e10cSrcweir     : maMap()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     init();
58cdf0e10cSrcweir }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #define ADD_ENTRY(XCONVERT,TYPE) XCONVERT->maMap[ getCppuType( static_cast<TYPE*>( NULL ) ) ] = Convert_t( &lcl_toXSD_##TYPE, &lcl_toAny_##TYPE )
61cdf0e10cSrcweir 
62cdf0e10cSrcweir namespace
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     // ========================================================================
65cdf0e10cSrcweir     struct StringToken
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir     private:
68cdf0e10cSrcweir         ::rtl::OUString m_sString;
69cdf0e10cSrcweir         sal_Int32       m_nTokenStart;
70cdf0e10cSrcweir         sal_Int32       m_nTokenEnd;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     public:
StringToken__anoncd25293c0111::StringToken73cdf0e10cSrcweir         StringToken() : m_sString(), m_nTokenStart( 0 ), m_nTokenEnd( 0 ) { }
74cdf0e10cSrcweir         StringToken( const ::rtl::OUString& _rString, sal_Int32 _nTokenStart, sal_Int32 _nTokenEnd );
75cdf0e10cSrcweir         StringToken( const StringToken& );
76cdf0e10cSrcweir         StringToken& operator=( const StringToken& );
77cdf0e10cSrcweir 
isEmpty__anoncd25293c0111::StringToken78cdf0e10cSrcweir         inline  bool                isEmpty() const { return m_nTokenEnd <= m_nTokenStart; }
getLength__anoncd25293c0111::StringToken79cdf0e10cSrcweir         inline  sal_Int32           getLength() const { return isEmpty() ? 0 : m_nTokenEnd - m_nTokenStart - 1; }
begin__anoncd25293c0111::StringToken80cdf0e10cSrcweir         inline  const sal_Unicode*  begin() const { return m_sString.getStr() + m_nTokenStart; }
end__anoncd25293c0111::StringToken81cdf0e10cSrcweir         inline  const sal_Unicode*  end() const { return m_sString.getStr() + m_nTokenEnd; }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         bool    toInt32( sal_Int32& _rValue ) const;
84cdf0e10cSrcweir     };
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     // ------------------------------------------------------------------------
StringToken(const::rtl::OUString & _rString,sal_Int32 _nTokenStart,sal_Int32 _nTokenEnd)87cdf0e10cSrcweir     StringToken::StringToken( const ::rtl::OUString& _rString, sal_Int32 _nTokenStart, sal_Int32 _nTokenEnd )
88cdf0e10cSrcweir         :m_sString( _rString )
89cdf0e10cSrcweir         ,m_nTokenStart( _nTokenStart )
90cdf0e10cSrcweir         ,m_nTokenEnd( _nTokenEnd )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         OSL_ENSURE( ( m_nTokenStart >= 0 ) && ( m_nTokenStart <= m_sString.getLength() ), "StringToken::StringToken: invalid token start!" );
93cdf0e10cSrcweir         OSL_ENSURE( ( m_nTokenEnd >= 0 ) && ( m_nTokenEnd <= m_sString.getLength() ), "StringToken::StringToken: invalid token end!" );
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     // ------------------------------------------------------------------------
StringToken(const StringToken & _rRHS)97cdf0e10cSrcweir     StringToken::StringToken( const StringToken& _rRHS )
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         *this = _rRHS;
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     // ------------------------------------------------------------------------
operator =(const StringToken & _rRHS)103cdf0e10cSrcweir     StringToken& StringToken::operator=( const StringToken& _rRHS )
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         if ( this == &_rRHS )
106cdf0e10cSrcweir             return *this;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         m_sString = _rRHS.m_sString;
109cdf0e10cSrcweir         m_nTokenStart = _rRHS.m_nTokenStart;
110cdf0e10cSrcweir         m_nTokenEnd = _rRHS.m_nTokenEnd;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         return *this;
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     // ------------------------------------------------------------------------
toInt32(sal_Int32 & _rValue) const116cdf0e10cSrcweir     bool StringToken::toInt32( sal_Int32& _rValue ) const
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         if ( isEmpty() )
119cdf0e10cSrcweir             return false;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         _rValue = 0;
122cdf0e10cSrcweir         const sal_Unicode* pStr = begin();
123cdf0e10cSrcweir         while ( pStr < end() )
124cdf0e10cSrcweir         {
125cdf0e10cSrcweir             if ( ( *pStr < '0' ) || ( *pStr > '9' ) )
126cdf0e10cSrcweir                 return false;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir             _rValue *= 10;
129cdf0e10cSrcweir             _rValue += ( *pStr - '0' );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir             ++pStr;
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         return true;
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     // ========================================================================
138cdf0e10cSrcweir     class StringTokenizer
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir     private:
141cdf0e10cSrcweir         ::rtl::OUString     m_sString;
142cdf0e10cSrcweir         const sal_Unicode   m_nTokenSeparator;
143cdf0e10cSrcweir         sal_Int32           m_nTokenStart;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     public:
146cdf0e10cSrcweir         /** constructs a tokenizer
147cdf0e10cSrcweir             @param _rString             the string to tokenize
148cdf0e10cSrcweir             @param _nTokenSeparator     the token value. May be 0, in this case the tokenizer
149cdf0e10cSrcweir                                         will recognize exactly one token, being the whole string.
150cdf0e10cSrcweir                                         This may make sense if you want to apply <type>StringToken</type>
151cdf0e10cSrcweir                                         methods to a whole string.
152cdf0e10cSrcweir         */
153cdf0e10cSrcweir         StringTokenizer( const ::rtl::OUString& _rString, sal_Unicode _nTokenSeparator = ';' );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         /// resets the tokenizer to the beginning of the string
156cdf0e10cSrcweir         void    reset();
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         /// determines whether there is a next token
159cdf0e10cSrcweir         bool    hasNextToken() const;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         /// retrieves the next token
162cdf0e10cSrcweir         StringToken
163cdf0e10cSrcweir                 getNextToken();
164cdf0e10cSrcweir     };
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     // ------------------------------------------------------------------------
StringTokenizer(const::rtl::OUString & _rString,sal_Unicode _nTokenSeparator)167cdf0e10cSrcweir     StringTokenizer::StringTokenizer( const ::rtl::OUString& _rString, sal_Unicode _nTokenSeparator )
168cdf0e10cSrcweir         :m_sString( _rString )
169cdf0e10cSrcweir         ,m_nTokenSeparator( _nTokenSeparator )
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         reset();
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir     // ------------------------------------------------------------------------
reset()175cdf0e10cSrcweir     void StringTokenizer::reset()
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         m_nTokenStart = 0;
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     // ------------------------------------------------------------------------
hasNextToken() const181cdf0e10cSrcweir     bool StringTokenizer::hasNextToken() const
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         return ( m_nTokenStart < m_sString.getLength() );
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     // ------------------------------------------------------------------------
getNextToken()187cdf0e10cSrcweir     StringToken StringTokenizer::getNextToken()
188cdf0e10cSrcweir     {
189cdf0e10cSrcweir         OSL_PRECOND( hasNextToken(), "StringTokenizer::getNextToken: there is no next token!" );
190cdf0e10cSrcweir         if ( !hasNextToken() )
191cdf0e10cSrcweir             return StringToken();
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         // determine the end of the current token
194cdf0e10cSrcweir         sal_Int32 nTokenEnd = m_nTokenSeparator ? m_sString.indexOf( m_nTokenSeparator, m_nTokenStart ) : m_sString.getLength();
195cdf0e10cSrcweir         bool bLastToken = !m_nTokenSeparator || ( nTokenEnd == -1 );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         // construct a new token
198cdf0e10cSrcweir         StringToken aToken( m_sString, m_nTokenStart, bLastToken ? m_sString.getLength() : nTokenEnd );
199cdf0e10cSrcweir         // advance
200cdf0e10cSrcweir         m_nTokenStart = bLastToken ? m_sString.getLength() : nTokenEnd + 1;
201cdf0e10cSrcweir         // outta here
202cdf0e10cSrcweir         return aToken;
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     // ========================================================================
206cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toXSD_OUString(const Any & rAny)207cdf0e10cSrcweir     OUString lcl_toXSD_OUString( const Any& rAny )
208cdf0e10cSrcweir     { OUString sStr; rAny >>= sStr; return sStr; }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toAny_OUString(const OUString & rStr)211cdf0e10cSrcweir     Any lcl_toAny_OUString( const OUString& rStr )
212cdf0e10cSrcweir     { Any aAny; aAny <<= rStr; return aAny; }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toXSD_bool(const Any & rAny)215cdf0e10cSrcweir     OUString lcl_toXSD_bool( const Any& rAny )
216cdf0e10cSrcweir     { bool b = false; rAny >>= b; return b ? OUSTRING("true") : OUSTRING("false"); }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toAny_bool(const OUString & rStr)219cdf0e10cSrcweir     Any lcl_toAny_bool( const OUString& rStr )
220cdf0e10cSrcweir     {
221cdf0e10cSrcweir         bool b = ( rStr == OUSTRING("true")  ||  rStr == OUSTRING("1") );
222cdf0e10cSrcweir         return makeAny( b );
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toXSD_double(const Any & rAny)226cdf0e10cSrcweir     OUString lcl_toXSD_double( const Any& rAny )
227cdf0e10cSrcweir     {
228cdf0e10cSrcweir         double f = 0.0;
229cdf0e10cSrcweir         rAny >>= f;
230cdf0e10cSrcweir 
231cdf0e10cSrcweir         return rtl::math::isFinite( f )
232cdf0e10cSrcweir             ? rtl::math::doubleToUString( f, rtl_math_StringFormat_Automatic,
233cdf0e10cSrcweir                                         rtl_math_DecimalPlaces_Max, '.',
234cdf0e10cSrcweir                                         sal_True )
235cdf0e10cSrcweir             : OUString();
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toAny_double(const OUString & rString)239cdf0e10cSrcweir     Any lcl_toAny_double( const OUString& rString )
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir         rtl_math_ConversionStatus eStatus;
242cdf0e10cSrcweir         double f = rtl::math::stringToDouble(
243cdf0e10cSrcweir             rString, sal_Unicode('.'), sal_Unicode(','), &eStatus, NULL );
244cdf0e10cSrcweir         return ( eStatus == rtl_math_ConversionStatus_Ok ) ? makeAny( f ) : Any();
245cdf0e10cSrcweir     }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_appendInt32ToBuffer(const sal_Int32 _nValue,::rtl::OUStringBuffer & _rBuffer,sal_Int16 _nMinDigits)248cdf0e10cSrcweir     void lcl_appendInt32ToBuffer( const sal_Int32 _nValue, ::rtl::OUStringBuffer& _rBuffer, sal_Int16 _nMinDigits )
249cdf0e10cSrcweir     {
250cdf0e10cSrcweir         if ( ( _nMinDigits >= 4 ) && ( _nValue < 1000 ) )
251cdf0e10cSrcweir             _rBuffer.append( (sal_Unicode)'0' );
252cdf0e10cSrcweir         if ( ( _nMinDigits >= 3 ) && ( _nValue < 100 ) )
253cdf0e10cSrcweir             _rBuffer.append( (sal_Unicode)'0' );
254cdf0e10cSrcweir         if ( ( _nMinDigits >= 2 ) && ( _nValue < 10 ) )
255cdf0e10cSrcweir             _rBuffer.append( (sal_Unicode)'0' );
256cdf0e10cSrcweir         _rBuffer.append( _nValue );
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toXSD_UNODate_typed(const UNODate & rDate)260cdf0e10cSrcweir     OUString lcl_toXSD_UNODate_typed( const UNODate& rDate )
261cdf0e10cSrcweir     {
262cdf0e10cSrcweir 
263cdf0e10cSrcweir         ::rtl::OUStringBuffer sInfo;
264cdf0e10cSrcweir         lcl_appendInt32ToBuffer( rDate.Year, sInfo, 4 );
265cdf0e10cSrcweir         sInfo.appendAscii( "-" );
266cdf0e10cSrcweir         lcl_appendInt32ToBuffer( rDate.Month, sInfo, 2 );
267cdf0e10cSrcweir         sInfo.appendAscii( "-" );
268cdf0e10cSrcweir         lcl_appendInt32ToBuffer( rDate.Day, sInfo, 2 );
269cdf0e10cSrcweir 
270cdf0e10cSrcweir         return sInfo.makeStringAndClear();
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toXSD_UNODate(const Any & rAny)274cdf0e10cSrcweir     OUString lcl_toXSD_UNODate( const Any& rAny )
275cdf0e10cSrcweir     {
276cdf0e10cSrcweir         UNODate aDate;
277cdf0e10cSrcweir         OSL_VERIFY( rAny >>= aDate );
278cdf0e10cSrcweir         return lcl_toXSD_UNODate_typed( aDate );
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toUNODate(const OUString & rString)282cdf0e10cSrcweir     UNODate lcl_toUNODate( const OUString& rString )
283cdf0e10cSrcweir     {
284cdf0e10cSrcweir         bool bWellformed = true;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir         UNODate aDate( 1, 1, 1900 );
287cdf0e10cSrcweir 
288cdf0e10cSrcweir         sal_Int32 nToken = 0;
289cdf0e10cSrcweir         StringTokenizer aTokenizer( rString, '-' );
290cdf0e10cSrcweir         while ( aTokenizer.hasNextToken() )
291cdf0e10cSrcweir         {
292cdf0e10cSrcweir             sal_Int32 nTokenValue = 0;
293cdf0e10cSrcweir             if ( !aTokenizer.getNextToken().toInt32( nTokenValue ) )
294cdf0e10cSrcweir             {
295cdf0e10cSrcweir                 bWellformed = false;
296cdf0e10cSrcweir                 break;
297cdf0e10cSrcweir             }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir             if ( nToken == 0 )
300cdf0e10cSrcweir                 aDate.Year = (sal_uInt16)nTokenValue;
301cdf0e10cSrcweir             else if ( nToken == 1 )
302cdf0e10cSrcweir                 aDate.Month = (sal_uInt16)nTokenValue;
303cdf0e10cSrcweir             else if ( nToken == 2 )
304cdf0e10cSrcweir                 aDate.Day = (sal_uInt16)nTokenValue;
305cdf0e10cSrcweir             else
306cdf0e10cSrcweir             {
307cdf0e10cSrcweir                 bWellformed = false;
308cdf0e10cSrcweir                 break;
309cdf0e10cSrcweir             }
310cdf0e10cSrcweir             ++nToken;
311cdf0e10cSrcweir         }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir         // sanity checks
314cdf0e10cSrcweir         if ( ( aDate.Year > 9999 ) || ( aDate.Month < 1 ) || ( aDate.Month > 12 ) || ( aDate.Day < 1 ) || ( aDate.Day > 31 ) )
315cdf0e10cSrcweir             bWellformed = false;
316cdf0e10cSrcweir         else
317cdf0e10cSrcweir         {
318cdf0e10cSrcweir             ::Date aDateCheck( 1, aDate.Month, aDate.Year );
319cdf0e10cSrcweir             if ( aDate.Day > aDateCheck.GetDaysInMonth() )
320cdf0e10cSrcweir                 bWellformed = false;
321cdf0e10cSrcweir         }
322cdf0e10cSrcweir 
323cdf0e10cSrcweir         // all okay?
324cdf0e10cSrcweir         if ( !bWellformed )
325cdf0e10cSrcweir             return UNODate( 1, 1, 1900 );
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         return aDate;
328cdf0e10cSrcweir     }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toAny_UNODate(const OUString & rString)331cdf0e10cSrcweir     Any lcl_toAny_UNODate( const OUString& rString )
332cdf0e10cSrcweir     {
333cdf0e10cSrcweir         return makeAny( lcl_toUNODate( rString ) );
334cdf0e10cSrcweir     }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toXSD_UNOTime_typed(const UNOTime & rTime)337cdf0e10cSrcweir     OUString lcl_toXSD_UNOTime_typed( const UNOTime& rTime )
338cdf0e10cSrcweir     {
339cdf0e10cSrcweir 
340cdf0e10cSrcweir         ::rtl::OUStringBuffer sInfo;
341cdf0e10cSrcweir         lcl_appendInt32ToBuffer( rTime.Hours, sInfo, 2 );
342cdf0e10cSrcweir         sInfo.appendAscii( ":" );
343cdf0e10cSrcweir         lcl_appendInt32ToBuffer( rTime.Minutes, sInfo, 2 );
344cdf0e10cSrcweir         sInfo.appendAscii( ":" );
345cdf0e10cSrcweir         lcl_appendInt32ToBuffer( rTime.Seconds, sInfo, 2 );
346cdf0e10cSrcweir         if ( rTime.HundredthSeconds )
347cdf0e10cSrcweir         {
348cdf0e10cSrcweir             sInfo.appendAscii( "." );
349cdf0e10cSrcweir             lcl_appendInt32ToBuffer( rTime.HundredthSeconds, sInfo, 2 );
350cdf0e10cSrcweir         }
351cdf0e10cSrcweir 
352cdf0e10cSrcweir         return sInfo.makeStringAndClear();
353cdf0e10cSrcweir     }
354cdf0e10cSrcweir 
355cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toXSD_UNOTime(const Any & rAny)356cdf0e10cSrcweir     OUString lcl_toXSD_UNOTime( const Any& rAny )
357cdf0e10cSrcweir     {
358cdf0e10cSrcweir         UNOTime aTime;
359cdf0e10cSrcweir         OSL_VERIFY( rAny >>= aTime );
360cdf0e10cSrcweir         return lcl_toXSD_UNOTime_typed( aTime );
361cdf0e10cSrcweir     }
362cdf0e10cSrcweir 
363cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toUNOTime(const OUString & rString)364cdf0e10cSrcweir     UNOTime lcl_toUNOTime( const OUString& rString )
365cdf0e10cSrcweir     {
366cdf0e10cSrcweir         bool bWellformed = true;
367cdf0e10cSrcweir 
368cdf0e10cSrcweir         UNOTime aTime( 0, 0, 0, 0 );
369cdf0e10cSrcweir 
370cdf0e10cSrcweir         ::rtl::OUString sString( rString );
371cdf0e10cSrcweir         // see if there's a decimal separator for the seconds,
372cdf0e10cSrcweir         // and if so, handle it separately
373cdf0e10cSrcweir         sal_Int32 nDecimalSepPos = rString.indexOf( '.' );
374cdf0e10cSrcweir         if ( nDecimalSepPos == -1 )
375cdf0e10cSrcweir             // ISO 8601 allows for both a comma and a dot
376cdf0e10cSrcweir             nDecimalSepPos = rString.indexOf( ',' );
377cdf0e10cSrcweir         if ( nDecimalSepPos != -1 )
378cdf0e10cSrcweir         {
379cdf0e10cSrcweir             // handle fractional seconds
380cdf0e10cSrcweir             ::rtl::OUString sFractional = sString.copy( nDecimalSepPos + 1 );
381cdf0e10cSrcweir             if ( sFractional.getLength() > 2 )
382cdf0e10cSrcweir                 // our precision is HundrethSeconds - it's all a css.util.Time can hold
383cdf0e10cSrcweir                 sFractional = sFractional.copy( 0, 2 );
384cdf0e10cSrcweir             sal_Int32 nFractional = 0;
385cdf0e10cSrcweir             if ( sFractional.getLength() )
386cdf0e10cSrcweir             {
387cdf0e10cSrcweir                 if ( StringTokenizer( sFractional, 0 ).getNextToken().toInt32( nFractional ) )
388cdf0e10cSrcweir                 {
389cdf0e10cSrcweir                     aTime.HundredthSeconds = (sal_uInt16)nFractional;
390cdf0e10cSrcweir                     if ( nFractional < 10 )
391cdf0e10cSrcweir                         aTime.HundredthSeconds *= 10;
392cdf0e10cSrcweir                 }
393cdf0e10cSrcweir                 else
394cdf0e10cSrcweir                     bWellformed = false;
395cdf0e10cSrcweir             }
396cdf0e10cSrcweir 
397cdf0e10cSrcweir             // strip the fraction before further processing
398cdf0e10cSrcweir             sString = sString.copy( 0, nDecimalSepPos );
399cdf0e10cSrcweir         }
400cdf0e10cSrcweir 
401cdf0e10cSrcweir         // split into the tokens which are separated by colon
402cdf0e10cSrcweir         sal_Int32 nToken = 0;
403cdf0e10cSrcweir         StringTokenizer aTokenizer( sString, ':' );
404cdf0e10cSrcweir         while ( aTokenizer.hasNextToken() )
405cdf0e10cSrcweir         {
406cdf0e10cSrcweir             sal_Int32 nTokenValue = 0;
407cdf0e10cSrcweir             if ( !aTokenizer.getNextToken().toInt32( nTokenValue ) )
408cdf0e10cSrcweir             {
409cdf0e10cSrcweir                 bWellformed = false;
410cdf0e10cSrcweir                 break;
411cdf0e10cSrcweir             }
412cdf0e10cSrcweir 
413cdf0e10cSrcweir             if ( nToken == 0 )
414cdf0e10cSrcweir                 aTime.Hours = (sal_uInt16)nTokenValue;
415cdf0e10cSrcweir             else if ( nToken == 1 )
416cdf0e10cSrcweir                 aTime.Minutes = (sal_uInt16)nTokenValue;
417cdf0e10cSrcweir             else if ( nToken == 2 )
418cdf0e10cSrcweir                 aTime.Seconds = (sal_uInt16)nTokenValue;
419cdf0e10cSrcweir             else
420cdf0e10cSrcweir             {
421cdf0e10cSrcweir                 bWellformed = false;
422cdf0e10cSrcweir                 break;
423cdf0e10cSrcweir             }
424cdf0e10cSrcweir             ++nToken;
425cdf0e10cSrcweir         }
426cdf0e10cSrcweir 
427cdf0e10cSrcweir         // sanity checks
428cdf0e10cSrcweir         // note that Seconds == 60 denotes leap seconds. Normally, they're not allowed everywhere,
429cdf0e10cSrcweir         // but we accept them all the time for simplicity reasons
430cdf0e10cSrcweir         if  (  ( aTime.Hours > 24 )
431cdf0e10cSrcweir             || ( aTime.Minutes > 59 )
432cdf0e10cSrcweir             || ( aTime.Seconds > 60 )
433cdf0e10cSrcweir             )
434cdf0e10cSrcweir             bWellformed = false;
435cdf0e10cSrcweir 
436cdf0e10cSrcweir         if  (   bWellformed
437cdf0e10cSrcweir             &&  ( aTime.Hours == 24 )
438cdf0e10cSrcweir             &&  (   ( aTime.Minutes != 0 )
439cdf0e10cSrcweir                 ||  ( aTime.Seconds != 0 )
440cdf0e10cSrcweir                 ||  ( aTime.HundredthSeconds != 0 )
441cdf0e10cSrcweir                 )
442cdf0e10cSrcweir             )
443cdf0e10cSrcweir             bWellformed = false;
444cdf0e10cSrcweir 
445cdf0e10cSrcweir         // all okay?
446cdf0e10cSrcweir         if ( !bWellformed )
447cdf0e10cSrcweir             return UNOTime( 0, 0, 0, 0 );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir         return aTime;
450cdf0e10cSrcweir     }
451cdf0e10cSrcweir 
452cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toAny_UNOTime(const OUString & rString)453cdf0e10cSrcweir     Any lcl_toAny_UNOTime( const OUString& rString )
454cdf0e10cSrcweir     {
455cdf0e10cSrcweir         return makeAny( lcl_toUNOTime( rString ) );
456cdf0e10cSrcweir     }
457cdf0e10cSrcweir 
458cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toXSD_UNODateTime(const Any & rAny)459cdf0e10cSrcweir     OUString lcl_toXSD_UNODateTime( const Any& rAny )
460cdf0e10cSrcweir     {
461cdf0e10cSrcweir         UNODateTime aDateTime;
462cdf0e10cSrcweir         OSL_VERIFY( rAny >>= aDateTime );
463cdf0e10cSrcweir 
464cdf0e10cSrcweir         UNODate aDate( aDateTime.Day, aDateTime.Month, aDateTime.Year );
465cdf0e10cSrcweir         ::rtl::OUString sDate = lcl_toXSD_UNODate_typed( aDate );
466cdf0e10cSrcweir 
467cdf0e10cSrcweir         UNOTime aTime( aDateTime.HundredthSeconds, aDateTime.Seconds, aDateTime.Minutes, aDateTime.Hours );
468cdf0e10cSrcweir         ::rtl::OUString sTime = lcl_toXSD_UNOTime_typed( aTime );
469cdf0e10cSrcweir 
470cdf0e10cSrcweir         ::rtl::OUStringBuffer sInfo;
471cdf0e10cSrcweir         sInfo.append( sDate );
472cdf0e10cSrcweir         sInfo.append( (sal_Unicode) 'T' );
473cdf0e10cSrcweir         sInfo.append( sTime );
474cdf0e10cSrcweir         return sInfo.makeStringAndClear();
475cdf0e10cSrcweir     }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir     // ------------------------------------------------------------------------
lcl_toAny_UNODateTime(const OUString & rString)478cdf0e10cSrcweir     Any lcl_toAny_UNODateTime( const OUString& rString )
479cdf0e10cSrcweir     {
480cdf0e10cSrcweir         // separate the date from the time part
481cdf0e10cSrcweir         sal_Int32 nDateTimeSep = rString.indexOf( 'T' );
482cdf0e10cSrcweir         if ( nDateTimeSep == -1 )
483cdf0e10cSrcweir             nDateTimeSep = rString.indexOf( 't' );
484cdf0e10cSrcweir 
485cdf0e10cSrcweir         UNODate aDate;
486cdf0e10cSrcweir         UNOTime aTime;
487cdf0e10cSrcweir         if ( nDateTimeSep == -1 )
488cdf0e10cSrcweir         {   // no time part
489cdf0e10cSrcweir             aDate = lcl_toUNODate( rString );
490cdf0e10cSrcweir             aTime = UNOTime( 0, 0, 0, 0 );
491cdf0e10cSrcweir         }
492cdf0e10cSrcweir         else
493cdf0e10cSrcweir         {
494cdf0e10cSrcweir             aDate = lcl_toUNODate( rString.copy( 0, nDateTimeSep ) );
495cdf0e10cSrcweir             aTime = lcl_toUNOTime( rString.copy( nDateTimeSep + 1 ) );
496cdf0e10cSrcweir         }
497cdf0e10cSrcweir         UNODateTime aDateTime(
498cdf0e10cSrcweir             aTime.HundredthSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours,
499cdf0e10cSrcweir             aDate.Day, aDate.Month, aDate.Year
500cdf0e10cSrcweir         );
501cdf0e10cSrcweir         return makeAny( aDateTime );
502cdf0e10cSrcweir     }
503cdf0e10cSrcweir }
504cdf0e10cSrcweir 
505cdf0e10cSrcweir // ============================================================================
init()506cdf0e10cSrcweir void Convert::init()
507cdf0e10cSrcweir {
508cdf0e10cSrcweir     ADD_ENTRY( this, OUString );
509cdf0e10cSrcweir     ADD_ENTRY( this, bool );
510cdf0e10cSrcweir     ADD_ENTRY( this, double );
511cdf0e10cSrcweir     ADD_ENTRY( this, UNODate );
512cdf0e10cSrcweir     ADD_ENTRY( this, UNOTime );
513cdf0e10cSrcweir     ADD_ENTRY( this, UNODateTime );
514cdf0e10cSrcweir }
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 
get()517cdf0e10cSrcweir Convert& Convert::get()
518cdf0e10cSrcweir {
519cdf0e10cSrcweir     // create our Singleton instance on demand
520cdf0e10cSrcweir     static Convert* pConvert = NULL;
521cdf0e10cSrcweir     if( pConvert == NULL )
522cdf0e10cSrcweir         pConvert = new Convert();
523cdf0e10cSrcweir 
524cdf0e10cSrcweir     OSL_ENSURE( pConvert != NULL, "no converter?" );
525cdf0e10cSrcweir     return *pConvert;
526cdf0e10cSrcweir }
527cdf0e10cSrcweir 
hasType(const Type_t & rType)528cdf0e10cSrcweir bool Convert::hasType( const Type_t& rType )
529cdf0e10cSrcweir {
530cdf0e10cSrcweir     return maMap.find( rType ) != maMap.end();
531cdf0e10cSrcweir }
532cdf0e10cSrcweir 
getTypes()533cdf0e10cSrcweir Convert::Types_t Convert::getTypes()
534cdf0e10cSrcweir {
535cdf0e10cSrcweir     Types_t aTypes( maMap.size() );
536cdf0e10cSrcweir     transform( maMap.begin(), maMap.end(), aTypes.getArray(),
537cdf0e10cSrcweir                select1st<Map_t::value_type>() );
538cdf0e10cSrcweir     return aTypes;
539cdf0e10cSrcweir }
540cdf0e10cSrcweir 
toXSD(const Any_t & rAny)541cdf0e10cSrcweir rtl::OUString Convert::toXSD( const Any_t& rAny )
542cdf0e10cSrcweir {
543cdf0e10cSrcweir     Map_t::iterator aIter = maMap.find( rAny.getValueType() );
544cdf0e10cSrcweir     return aIter != maMap.end() ? aIter->second.first( rAny ) : OUString();
545cdf0e10cSrcweir }
546cdf0e10cSrcweir 
toAny(const rtl::OUString & rValue,const Type_t & rType)547cdf0e10cSrcweir Convert::Any_t Convert::toAny( const rtl::OUString& rValue,
548cdf0e10cSrcweir                                const Type_t& rType )
549cdf0e10cSrcweir {
550cdf0e10cSrcweir     Map_t::iterator aIter = maMap.find( rType );
551cdf0e10cSrcweir     return aIter != maMap.end() ? aIter->second.second( rValue ) : Any_t();
552cdf0e10cSrcweir }
553cdf0e10cSrcweir 
554cdf0e10cSrcweir //------------------------------------------------------------------------
convertWhitespace(const::rtl::OUString & _rString,sal_Int16 _nWhitespaceTreatment)555cdf0e10cSrcweir ::rtl::OUString Convert::convertWhitespace( const ::rtl::OUString& _rString, sal_Int16 _nWhitespaceTreatment )
556cdf0e10cSrcweir {
557cdf0e10cSrcweir     ::rtl::OUString sConverted;
558cdf0e10cSrcweir     switch( _nWhitespaceTreatment )
559cdf0e10cSrcweir     {
560cdf0e10cSrcweir     default:
561cdf0e10cSrcweir         OSL_ENSURE( sal_False, "Convert::convertWhitespace: invalid whitespace treatment constant!" );
562cdf0e10cSrcweir         // NO break
563cdf0e10cSrcweir     case com::sun::star::xsd::WhiteSpaceTreatment::Preserve:
564cdf0e10cSrcweir         sConverted = _rString;
565cdf0e10cSrcweir         break;
566cdf0e10cSrcweir     case com::sun::star::xsd::WhiteSpaceTreatment::Replace:
567cdf0e10cSrcweir         sConverted = replaceWhitespace( _rString );
568cdf0e10cSrcweir         break;
569cdf0e10cSrcweir     case com::sun::star::xsd::WhiteSpaceTreatment::Collapse:
570cdf0e10cSrcweir         sConverted = collapseWhitespace( _rString );
571cdf0e10cSrcweir         break;
572cdf0e10cSrcweir     }
573cdf0e10cSrcweir     return sConverted;
574cdf0e10cSrcweir }
575cdf0e10cSrcweir 
576cdf0e10cSrcweir //------------------------------------------------------------------------
replaceWhitespace(const::rtl::OUString & _rString)577cdf0e10cSrcweir ::rtl::OUString Convert::replaceWhitespace( const ::rtl::OUString& _rString )
578cdf0e10cSrcweir {
579cdf0e10cSrcweir     OUStringBuffer aBuffer( _rString );
580cdf0e10cSrcweir     sal_Int32 nLength = aBuffer.getLength();
581cdf0e10cSrcweir     const sal_Unicode* pBuffer = aBuffer.getStr();
582cdf0e10cSrcweir     for( sal_Int32 i = 0; i < nLength; i++ )
583cdf0e10cSrcweir     {
584cdf0e10cSrcweir         sal_Unicode c = pBuffer[i];
585cdf0e10cSrcweir         if( c == sal_Unicode(0x08) ||
586cdf0e10cSrcweir             c == sal_Unicode(0x0A) ||
587cdf0e10cSrcweir             c == sal_Unicode(0x0D) )
588cdf0e10cSrcweir             aBuffer.setCharAt( i, sal_Unicode(0x20) );
589cdf0e10cSrcweir     }
590cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
591cdf0e10cSrcweir }
592cdf0e10cSrcweir 
593cdf0e10cSrcweir //------------------------------------------------------------------------
collapseWhitespace(const::rtl::OUString & _rString)594cdf0e10cSrcweir ::rtl::OUString Convert::collapseWhitespace( const ::rtl::OUString& _rString )
595cdf0e10cSrcweir {
596cdf0e10cSrcweir     sal_Int32 nLength = _rString.getLength();
597cdf0e10cSrcweir     OUStringBuffer aBuffer( nLength );
598cdf0e10cSrcweir     const sal_Unicode* pStr = _rString.getStr();
599cdf0e10cSrcweir     bool bStrip = true;
600cdf0e10cSrcweir     for( sal_Int32 i = 0; i < nLength; i++ )
601cdf0e10cSrcweir     {
602cdf0e10cSrcweir         sal_Unicode c = pStr[i];
603cdf0e10cSrcweir         if( c == sal_Unicode(0x08) ||
604cdf0e10cSrcweir             c == sal_Unicode(0x0A) ||
605cdf0e10cSrcweir             c == sal_Unicode(0x0D) ||
606cdf0e10cSrcweir             c == sal_Unicode(0x20) )
607cdf0e10cSrcweir         {
608cdf0e10cSrcweir             if( ! bStrip )
609cdf0e10cSrcweir             {
610cdf0e10cSrcweir                 aBuffer.append( sal_Unicode(0x20) );
611cdf0e10cSrcweir                 bStrip = true;
612cdf0e10cSrcweir             }
613cdf0e10cSrcweir         }
614cdf0e10cSrcweir         else
615cdf0e10cSrcweir         {
616cdf0e10cSrcweir             bStrip = false;
617cdf0e10cSrcweir             aBuffer.append( c );
618cdf0e10cSrcweir         }
619cdf0e10cSrcweir     }
620cdf0e10cSrcweir     if( aBuffer[ aBuffer.getLength() - 1 ] == sal_Unicode( 0x20 ) )
621cdf0e10cSrcweir         aBuffer.setLength( aBuffer.getLength() - 1 );
622cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
623cdf0e10cSrcweir }
624