1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
56 /*************************************************************************
58 * For LWP filter architecture prototype
59 ************************************************************************/
60 /*************************************************************************
63 ************************************************************************/
65 #include "lwptools.hxx"
66 #include <rtl/ustrbuf.hxx>
67 #include <osl/process.h>
68 #include <osl/thread.h>
69 #include <osl/file.hxx>
70 #include <vcl/svapp.hxx>
71 #include <vcl/settings.hxx>
72 #include <unicode/datefmt.h>
73 #include <unicode/udat.h>
74 #include <i18nlangtag/languagetagicu.hxx>
79 #define SEPARATOR '\\'
82 using namespace ::rtl
;
83 using namespace ::osl
;
86 * @descr read lwp unicode string from stream to OUString per aEncoding
88 sal_uInt16
LwpTools::QuickReadUnicode(LwpObjectStream
* pObjStrm
,
89 OUString
& str
, sal_uInt16 strlen
, rtl_TextEncoding aEncoding
)
90 //strlen: length of bytes
93 sal_uInt16 readLen
= 0;
94 OUStringBuffer
strBuf(128);
96 if( !IsUnicodePacked(pObjStrm
, strlen
) )
103 strlen
>1023?len
=1023 :len
=strlen
;
104 len
= pObjStrm
->QuickRead(buf
, len
);
106 strBuf
.append( OUString(buf
, len
, aEncoding
) );
111 str
= strBuf
.makeStringAndClear();
117 sal_Unicode unibuf
[1024];
121 bool flag
= false; //switch if unicode part reached
122 sal_uInt16 sublen
= 0;
124 while(readLen
<strlen
)
126 if(!flag
) //Not unicode string
129 readbyte
= pObjStrm
->QuickReaduInt8(&bFailure
);
131 readLen
+=sizeof(readbyte
);
136 if(sublen
>0) //add it to the strBuf
138 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
144 buf
[sublen
++] = readbyte
;
146 if(sublen
>=1023 || readLen
==strlen
) //add it to the strBuf
148 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
152 else //unicode string
155 readword
= pObjStrm
->QuickReaduInt16(&bFailure
);
157 readLen
+=sizeof(readword
);
159 if(readword
== 0x0000)
164 unibuf
[sublen
] = '\0';
165 strBuf
.append( OUString(unibuf
) );
171 unibuf
[sublen
++] = readword
;
173 if(sublen
>=1023 || readLen
==strlen
)
175 unibuf
[sublen
] = '\0';
176 strBuf
.append( OUString(unibuf
) );
181 str
= strBuf
.makeStringAndClear();
187 * @descr Judge if the data (len) in object stream is lwp unicode packed
189 bool LwpTools::IsUnicodePacked(LwpObjectStream
* pObjStrm
, sal_uInt16 len
)
192 sal_uInt16 oldpos
= pObjStrm
->GetPos();
194 for (sal_uInt16 i
= 0; i
< len
; i
++)
196 byte
= pObjStrm
->QuickReaduInt8();
199 pObjStrm
->Seek(oldpos
);
203 pObjStrm
->Seek(oldpos
);
207 bool LwpTools::isFileUrl(const OString
&fileName
)
209 if (fileName
.startsWith("file://") )
214 OUString
LwpTools::convertToFileUrl(const OString
&fileName
)
216 if ( isFileUrl(fileName
) )
218 return OStringToOUString(fileName
, osl_getThreadTextEncoding());
221 OUString uUrlFileName
;
222 OUString
uFileName(fileName
.getStr(), fileName
.getLength(), osl_getThreadTextEncoding());
223 if ( fileName
.startsWith(".") || fileName
.indexOf(SEPARATOR
) < 0 )
225 OUString uWorkingDir
;
226 OSL_VERIFY( osl_getProcessWorkingDir(&uWorkingDir
.pData
) == osl_Process_E_None
);
227 OSL_VERIFY( FileBase::getAbsoluteFileURL(uWorkingDir
, uFileName
, uUrlFileName
) == FileBase::E_None
);
230 OSL_VERIFY( FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
) == FileBase::E_None
);
236 OUString
LwpTools::DateTimeToOUString(LtTm
& dt
)
238 OUString aResult
= OUString::number(dt
.tm_year
) + "-" + OUString::number(dt
.tm_mon
) + "-" + OUString::number(dt
.tm_mday
) +
239 "T" + OUString::number(dt
.tm_hour
) + ":" + OUString::number(dt
.tm_min
) + ":" + OUString::number(dt
.tm_sec
);
245 * @descr get the system date format
247 XFDateStyle
* LwpTools::GetSystemDateStyle(bool bLongFormat
)
249 icu::DateFormat::EStyle style
;
251 style
= icu::DateFormat::FULL
;//system full date format
253 style
= icu::DateFormat::SHORT
;//system short date format
255 //1 get locale for system
256 icu::Locale
aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
257 //2 get icu format pattern by locale
258 icu::DateFormat
* fmt
= icu::DateFormat::createDateInstance(style
,aLocale
);
262 UErrorCode status
= U_ZERO_ERROR
;
263 UChar
* pattern
= NULL
;
265 nLengthNeed
= udat_toPattern((void *const *)fmt
,sal_False
,NULL
,nLength
,&status
);
266 if (status
== U_BUFFER_OVERFLOW_ERROR
)
268 status
= U_ZERO_ERROR
;
269 nLength
= nLengthNeed
+1;
270 pattern
= (UChar
*)malloc(sizeof(UChar
)*nLength
);
271 udat_toPattern((void *const *)fmt
,sal_False
,pattern
,nLength
,&status
);
275 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
276 // as patter letter,each represent a element in date/time and its repeat numbers represent
277 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
278 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
279 // we parse pattern string letter by letter and get the time format.
282 XFDateStyle
* pDateStyle
= new XFDateStyle
;
284 for (int32_t i
=0;i
<nLengthNeed
;)
286 cSymbol
= pattern
[i
];
301 pDateStyle
->AddEra();
316 pDateStyle
->AddYear(false);
318 pDateStyle
->AddYear();
333 pDateStyle
->AddMonth(false,false);
335 pDateStyle
->AddMonth(true,false);
337 pDateStyle
->AddMonth(false,true);
339 pDateStyle
->AddMonth(true,true);
354 pDateStyle
->AddMonthDay(false);
356 pDateStyle
->AddMonthDay();
371 pDateStyle
->AddHour(false);
373 pDateStyle
->AddHour();
388 pDateStyle
->AddHour(false);
390 pDateStyle
->AddHour();
405 pDateStyle
->AddMinute(false);
407 pDateStyle
->AddMinute();
422 pDateStyle
->AddSecond(false,0);
424 pDateStyle
->AddSecond(true,0);
439 pDateStyle->AddSecond(sal_False);
441 pDateStyle->AddSecond();*/
456 pDateStyle
->AddWeekDay(false);
458 pDateStyle
->AddWeekDay();
473 pDateStyle->AddWeekDay(sal_False);
475 pDateStyle->AddWeekDay();*/
490 pDateStyle->AddWeekDay(sal_False);
492 pDateStyle->AddWeekDay();*/
507 pDateStyle->AddWeekDay(sal_False);
509 pDateStyle->AddWeekDay();*/
524 pDateStyle->AddWeekDay(sal_False);
526 pDateStyle->AddWeekDay();*/
540 pDateStyle
->AddAmPm(true);
568 pDateStyle
->AddHour(false);
570 pDateStyle
->AddHour();
601 pDateStyle
->AddText("'");
606 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
610 //UChar buffer[1024];
611 sal_Unicode buffer
[1024];
616 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
617 cTmp
=='\'' || cTmp
=='"' )
627 pDateStyle
->AddText(OUString(buffer
));//keep for all parsed
637 * @descr get the system time format
639 XFTimeStyle
* LwpTools::GetSystemTimeStyle()
641 //1 get locale for system
642 icu::Locale
aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
643 //2 get icu format pattern by locale
644 icu::DateFormat
* fmt
= icu::DateFormat::createTimeInstance(icu::DateFormat::DEFAULT
,aLocale
);
648 UErrorCode status
= U_ZERO_ERROR
;
649 UChar
* pattern
= NULL
;
650 nLengthNeed
= udat_toPattern((void *const *)fmt
,false,NULL
,nLength
,&status
);
651 if (status
== U_BUFFER_OVERFLOW_ERROR
)
653 status
= U_ZERO_ERROR
;
654 nLength
= nLengthNeed
+1;
655 pattern
= (UChar
*)malloc(sizeof(UChar
)*nLength
);
656 udat_toPattern((void *const *)fmt
,false,pattern
,nLength
,&status
);
661 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
662 // as patter letter,each represent a element in date/time and its repeat numbers represent
663 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
664 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
665 // we parse pattern string letter by letter and get the time format.
666 // for time format ,for there is not date info,we can only parse the letter representing time.
669 XFTimeStyle
* pTimeStyle
= new XFTimeStyle
;
671 for (int32_t i
=0;i
<nLengthNeed
;)
673 cSymbol
= pattern
[i
];
689 pTimeStyle
->AddHour(false);
691 pTimeStyle
->AddHour();
706 pTimeStyle
->AddHour(false);
708 pTimeStyle
->AddHour();
723 pTimeStyle
->AddMinute(false);
725 pTimeStyle
->AddMinute();
740 pTimeStyle
->AddSecond(false,0);
742 pTimeStyle
->AddSecond(true,0);
757 pDateStyle->AddSecond(sal_False);
759 pDateStyle->AddSecond();*/
773 pTimeStyle
->SetAmPm(true);
801 pTimeStyle
->AddHour(false);
803 pTimeStyle
->AddHour();
821 pTimeStyle
->AddText("'");
826 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
830 sal_Unicode buffer
[1024];
832 //strBuffer.append(cSymbol);
836 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
837 cTmp
=='\'' || cTmp
=='"' )
846 pTimeStyle
->AddText(OUString(buffer
));//keep for all parsed
856 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */