1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * either of the following licenses
6 * - GNU Lesser General Public License Version 2.1
7 * - Sun Industry Standards Source License Version 1.1
9 * Sun Microsystems Inc., October, 2000
11 * GNU Lesser General Public License Version 2.1
12 * =============================================
13 * Copyright 2000 by Sun Microsystems, Inc.
14 * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License version 2.1, as published by the Free Software Foundation.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 * Sun Industry Standards Source License Version 1.1
32 * =================================================
33 * The contents of this file are subject to the Sun Industry Standards
34 * Source License Version 1.1 (the "License"); You may not use this file
35 * except in compliance with the License. You may obtain a copy of the
36 * License at http://www.openoffice.org/license.html.
38 * Software provided under this License is provided on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
40 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
41 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
42 * See the License for the specific provisions governing your rights and
43 * obligations concerning the Software.
45 * The Initial Developer of the Original Code is: IBM Corporation
47 * Copyright: 2008 by IBM Corporation
49 * All Rights Reserved.
51 * Contributor(s): _______________________________________
54 ************************************************************************/
55 /*************************************************************************
57 * For LWP filter architecture prototype
58 ************************************************************************/
59 /*************************************************************************
62 ************************************************************************/
64 #include "lwptools.hxx"
65 #include <rtl/ustrbuf.hxx>
66 #include <osl/process.h>
67 #include <osl/thread.h>
68 #include <osl/file.hxx>
69 #include <vcl/svapp.hxx>
70 #include <vcl/settings.hxx>
71 #include <unicode/datefmt.h>
72 #include <unicode/udat.h>
77 #define SEPARATOR '\\'
81 using namespace ::rtl
;
82 using namespace ::osl
;
85 * @descr read lwp unicode string from stream to OUString per aEncoding
87 sal_uInt16
LwpTools::QuickReadUnicode(LwpObjectStream
* pObjStrm
,
88 OUString
& str
, sal_uInt16 strlen
, rtl_TextEncoding aEncoding
)
89 //strlen: length of bytes
92 sal_uInt16 readLen
= 0;
93 OUStringBuffer
strBuf(128);
96 if( !IsUnicodePacked(pObjStrm
, strlen
) )
102 strlen
>1023?len
=1023 :len
=strlen
;
103 len
= pObjStrm
->QuickRead(buf
, len
);
105 strBuf
.append( OUString(buf
, len
, aEncoding
) );
110 str
= strBuf
.makeStringAndClear();
116 sal_Unicode unibuf
[1024];
120 BOOL flag
= sal_False
; //switch if unicode part reached
121 sal_uInt16 sublen
= 0;
123 while(readLen
<strlen
)
125 if(!flag
) //Not unicode string
127 len
= pObjStrm
->QuickRead(&readbyte
, sizeof(readbyte
));
134 if(sublen
>0) //add it to the strBuf
136 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
142 buf
[sublen
++] = readbyte
;
144 if(sublen
>=1023 || readLen
==strlen
) //add it to the strBuf
146 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
150 else //unicode string
152 len
= pObjStrm
->QuickRead(&readword
, sizeof(readword
));
156 if(readword
== 0x0000)
161 unibuf
[sublen
] = sal_Unicode('\0');
162 strBuf
.append( OUString(unibuf
) );
168 unibuf
[sublen
++] = readword
;
170 if(sublen
>=1023 || readLen
==strlen
)
172 unibuf
[sublen
] = sal_Unicode('\0');
173 strBuf
.append( OUString(unibuf
) );
180 // unibuf[sublen] = sal_Unicode('\0');
181 // strBuf.append( OUString(unibuf) );
184 str
= strBuf
.makeStringAndClear();
190 * @descr Judge if the data (len) in object stream is lwp unicode packed
192 BOOL
LwpTools::IsUnicodePacked(LwpObjectStream
* pObjStrm
, sal_uInt16 len
)
195 sal_uInt16 oldpos
= pObjStrm
->GetPos();
197 for (sal_uInt16 i
= 0; i
< len
; i
++)
199 pObjStrm
->QuickRead(&byte
, sizeof(byte
));
202 pObjStrm
->Seek(oldpos
);
206 pObjStrm
->Seek(oldpos
);
210 sal_Bool
LwpTools::isFileUrl(const OString
&fileName
)
212 if (fileName
.indexOf("file://") == 0 )
217 OUString
LwpTools::convertToFileUrl(const OString
&fileName
)
219 if ( isFileUrl(fileName
) )
221 return OStringToOUString(fileName
, osl_getThreadTextEncoding());
224 OUString uUrlFileName
;
225 OUString
uFileName(fileName
.getStr(), fileName
.getLength(), osl_getThreadTextEncoding());
226 if ( fileName
.indexOf('.') == 0 || fileName
.indexOf(SEPARATOR
) < 0 )
228 OUString uWorkingDir
;
229 OSL_VERIFY( osl_getProcessWorkingDir(&uWorkingDir
.pData
) == osl_Process_E_None
);
230 OSL_VERIFY( FileBase::getAbsoluteFileURL(uWorkingDir
, uFileName
, uUrlFileName
) == FileBase::E_None
);
233 OSL_VERIFY( FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
) == FileBase::E_None
);
239 OUString
LwpTools::DateTimeToOUString(LtTm
& dt
)
241 rtl::OUStringBuffer buf
;
242 buf
.append(dt
.tm_year
);
243 buf
.append( A2OUSTR("-") );
244 buf
.append(dt
.tm_mon
);
245 buf
.append( A2OUSTR("-") );
246 buf
.append(dt
.tm_mday
);
247 buf
.append( A2OUSTR("T") );
248 buf
.append(dt
.tm_hour
);
249 buf
.append( A2OUSTR(":") );
250 buf
.append(dt
.tm_min
);
251 buf
.append( A2OUSTR(":") );
252 buf
.append(dt
.tm_sec
);
254 return buf
.makeStringAndClear();
258 * @descr get the system date format
260 XFDateStyle
* LwpTools::GetSystemDateStyle(sal_Bool bLongFormat
)
262 icu::DateFormat::EStyle style
;
264 style
= icu::DateFormat::FULL
;//system full date format
266 style
= icu::DateFormat::SHORT
;//system short date format
267 /* ::com::sun::star::lang::Locale aLocale=Application::GetSettings().GetLocale();
268 rtl::OUString strLang = aLocale.Language;
269 rtl::OUString strCountry = aLocale.Country;
270 strLang = strLang + A2OUSTR("_");
271 rtl::OUString strLocale = strLang + strCountry;
275 UErrorCode status = U_ZERO_ERROR;
276 UChar* pattern = NULL;
278 UDateFormat* fmt= udat_open(UDAT_FULL, UDAT_FULL,
279 (char*)(OUStringToOString(strLocale,RTL_TEXTENCODING_MS_1252).getStr()), NULL, 0, NULL, 0, &status);
281 nLengthNeed = udat_toPattern(fmt,true,NULL,nLength,&status);
282 if (status == U_BUFFER_OVERFLOW_ERROR)
284 status = U_ZERO_ERROR;
285 nLength = nLengthNeed +1;
286 pattern = (UChar*)malloc(sizeof(UChar)*nLength);
287 udat_toPattern(fmt,true,pattern,nLength,&status);
290 //1 get locale for system
291 ::com::sun::star::lang::Locale aLocale
=Application::GetSettings().GetLocale();
292 rtl::OUString strLang
= aLocale
.Language
;
293 rtl::OUString strCountry
= aLocale
.Country
;
294 icu::Locale
bLocale((char*)(OUStringToOString(strLang
,RTL_TEXTENCODING_MS_1252
).getStr()),
295 (char*)(OUStringToOString(strCountry
,RTL_TEXTENCODING_MS_1252
).getStr()));
296 //2 get icu format pattern by locale
297 icu::DateFormat
* fmt
= icu::DateFormat::createDateInstance(style
,bLocale
);
301 UErrorCode status
= U_ZERO_ERROR
;
302 UChar
* pattern
= NULL
;
304 nLengthNeed
= udat_toPattern((void *const *)fmt
,sal_False
,NULL
,nLength
,&status
);
305 if (status
== U_BUFFER_OVERFLOW_ERROR
)
307 status
= U_ZERO_ERROR
;
308 nLength
= nLengthNeed
+1;
309 pattern
= (UChar
*)malloc(sizeof(UChar
)*nLength
);
310 udat_toPattern((void *const *)fmt
,sal_False
,pattern
,nLength
,&status
);
314 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
315 // as patter letter,each represent a element in date/time and its repeat numbers represent
316 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
317 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
318 // we parse pattern string letter by letter and get the time format.
321 XFDateStyle
* pDateStyle
= new XFDateStyle
;
323 for (int32_t i
=0;i
<nLengthNeed
;)
325 cSymbol
= pattern
[i
];
340 pDateStyle
->AddEra();
355 pDateStyle
->AddYear(sal_False
);
357 pDateStyle
->AddYear();
372 pDateStyle
->AddMonth(sal_False
,sal_False
);
374 pDateStyle
->AddMonth(sal_True
,sal_False
);
376 pDateStyle
->AddMonth(sal_False
,sal_True
);
378 pDateStyle
->AddMonth(sal_True
,sal_True
);
393 pDateStyle
->AddMonthDay(sal_False
);
395 pDateStyle
->AddMonthDay();
410 pDateStyle
->AddHour(sal_False
);
412 pDateStyle
->AddHour();
427 pDateStyle
->AddHour(sal_False
);
429 pDateStyle
->AddHour();
444 pDateStyle
->AddMinute(sal_False
);
446 pDateStyle
->AddMinute();
461 pDateStyle
->AddSecond(sal_False
,0);
463 pDateStyle
->AddSecond(sal_True
,0);
478 pDateStyle->AddSecond(sal_False);
480 pDateStyle->AddSecond();*/
495 pDateStyle
->AddWeekDay(sal_False
);
497 pDateStyle
->AddWeekDay();
512 pDateStyle->AddWeekDay(sal_False);
514 pDateStyle->AddWeekDay();*/
529 pDateStyle->AddWeekDay(sal_False);
531 pDateStyle->AddWeekDay();*/
546 pDateStyle->AddWeekDay(sal_False);
548 pDateStyle->AddWeekDay();*/
563 pDateStyle->AddWeekDay(sal_False);
565 pDateStyle->AddWeekDay();*/
579 pDateStyle
->AddAmPm(sal_True
);
607 pDateStyle
->AddHour(sal_False
);
609 pDateStyle
->AddHour();
640 pDateStyle
->AddText(OUString(A2OUSTR("'")));
645 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
649 //UChar buffer[1024];
650 sal_Unicode buffer
[1024];
655 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
656 cTmp
=='\'' || cTmp
=='"' )
666 pDateStyle
->AddText(OUString(buffer
));//keep for all parsed
676 * @descr get the system time format
678 XFTimeStyle
* LwpTools::GetSystemTimeStyle()
680 /* ::com::sun::star::lang::Locale aLocale=Application::GetSettings().GetLocale();
681 rtl::OUString strLang = aLocale.Language;
682 rtl::OUString strCountry = aLocale.Country;
683 strLang = strLang + A2OUSTR("_");
684 rtl::OUString strLocale = strLang + strCountry;
688 UErrorCode status = U_ZERO_ERROR;
689 UChar* pattern = NULL;
691 UDateFormat* fmt= udat_open(UDAT_FULL, UDAT_FULL,
692 (char*)(OUStringToOString(strLocale,RTL_TEXTENCODING_MS_1252).getStr()), NULL, 0, NULL, 0, &status);
694 nLengthNeed = udat_toPattern(fmt,true,NULL,nLength,&status);
695 if (status == U_BUFFER_OVERFLOW_ERROR)
697 status = U_ZERO_ERROR;
698 nLength = nLengthNeed +1;
699 pattern = (UChar*)malloc(sizeof(UChar)*nLength);
700 udat_toPattern(fmt,true,pattern,nLength,&status);
703 //1 get locale for system
704 ::com::sun::star::lang::Locale aLocale
=Application::GetSettings().GetLocale();
705 rtl::OUString strLang
= aLocale
.Language
;
706 rtl::OUString strCountry
= aLocale
.Country
;
707 icu::Locale
bLocale((char*)(OUStringToOString(strLang
,RTL_TEXTENCODING_MS_1252
).getStr()),
708 (char*)(OUStringToOString(strCountry
,RTL_TEXTENCODING_MS_1252
).getStr()));
710 icu::DateFormat
* fmt
= icu::DateFormat::createTimeInstance(icu::DateFormat::DEFAULT
,bLocale
);
711 //2 get icu format pattern by locale
714 UErrorCode status
= U_ZERO_ERROR
;
715 UChar
* pattern
= NULL
;
716 nLengthNeed
= udat_toPattern((void *const *)fmt
,false,NULL
,nLength
,&status
);
717 if (status
== U_BUFFER_OVERFLOW_ERROR
)
719 status
= U_ZERO_ERROR
;
720 nLength
= nLengthNeed
+1;
721 pattern
= (UChar
*)malloc(sizeof(UChar
)*nLength
);
722 udat_toPattern((void *const *)fmt
,false,pattern
,nLength
,&status
);
727 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
728 // as patter letter,each represent a element in date/time and its repeat numbers represent
729 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
730 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
731 // we parse pattern string letter by letter and get the time format.
732 // for time format ,for there is not date info,we can only parse the letter representing time.
735 XFTimeStyle
* pTimeStyle
= new XFTimeStyle
;
737 for (int32_t i
=0;i
<nLengthNeed
;)
739 cSymbol
= pattern
[i
];
755 pTimeStyle
->AddHour(sal_False
);
757 pTimeStyle
->AddHour();
772 pTimeStyle
->AddHour(sal_False
);
774 pTimeStyle
->AddHour();
789 pTimeStyle
->AddMinute(sal_False
);
791 pTimeStyle
->AddMinute();
806 pTimeStyle
->AddSecond(sal_False
,0);
808 pTimeStyle
->AddSecond(sal_True
,0);
823 pDateStyle->AddSecond(sal_False);
825 pDateStyle->AddSecond();*/
839 pTimeStyle
->SetAmPm(sal_True
);
867 pTimeStyle
->AddHour(sal_False
);
869 pTimeStyle
->AddHour();
887 pTimeStyle
->AddText(OUString(A2OUSTR("'")));
892 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
896 sal_Unicode buffer
[1024];
898 //strBuffer.append(cSymbol);
902 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
903 cTmp
=='\'' || cTmp
=='"' )
912 pTimeStyle
->AddText(OUString(buffer
));//keep for all parsed