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 '\\'
83 using namespace ::rtl
;
84 using namespace ::osl
;
87 * @descr read lwp unicode string from stream to OUString per aEncoding
89 sal_uInt16
LwpTools::QuickReadUnicode(LwpObjectStream
* pObjStrm
,
90 OUString
& str
, sal_uInt16 strlen
, rtl_TextEncoding aEncoding
)
91 //strlen: length of bytes
94 sal_uInt16 readLen
= 0;
95 OUStringBuffer
strBuf(128);
97 if( !IsUnicodePacked(pObjStrm
, strlen
) )
104 strlen
>1023?len
=1023 :len
=strlen
;
105 len
= pObjStrm
->QuickRead(buf
, len
);
107 strBuf
.append( OUString(buf
, len
, aEncoding
) );
112 str
= strBuf
.makeStringAndClear();
118 sal_Unicode unibuf
[1024];
122 sal_Bool flag
= sal_False
; //switch if unicode part reached
123 sal_uInt16 sublen
= 0;
125 while(readLen
<strlen
)
127 if(!flag
) //Not unicode string
130 readbyte
= pObjStrm
->QuickReaduInt8(&bFailure
);
132 readLen
+=sizeof(readbyte
);
137 if(sublen
>0) //add it to the strBuf
139 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
145 buf
[sublen
++] = readbyte
;
147 if(sublen
>=1023 || readLen
==strlen
) //add it to the strBuf
149 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
153 else //unicode string
156 readword
= pObjStrm
->QuickReaduInt16(&bFailure
);
158 readLen
+=sizeof(readword
);
160 if(readword
== 0x0000)
165 unibuf
[sublen
] = sal_Unicode('\0');
166 strBuf
.append( OUString(unibuf
) );
172 unibuf
[sublen
++] = readword
;
174 if(sublen
>=1023 || readLen
==strlen
)
176 unibuf
[sublen
] = sal_Unicode('\0');
177 strBuf
.append( OUString(unibuf
) );
182 str
= strBuf
.makeStringAndClear();
188 * @descr Judge if the data (len) in object stream is lwp unicode packed
190 sal_Bool
LwpTools::IsUnicodePacked(LwpObjectStream
* pObjStrm
, sal_uInt16 len
)
193 sal_uInt16 oldpos
= pObjStrm
->GetPos();
195 for (sal_uInt16 i
= 0; i
< len
; i
++)
197 byte
= pObjStrm
->QuickReaduInt8();
200 pObjStrm
->Seek(oldpos
);
204 pObjStrm
->Seek(oldpos
);
208 sal_Bool
LwpTools::isFileUrl(const OString
&fileName
)
210 if (fileName
.indexOf("file://") == 0 )
215 OUString
LwpTools::convertToFileUrl(const OString
&fileName
)
217 if ( isFileUrl(fileName
) )
219 return OStringToOUString(fileName
, osl_getThreadTextEncoding());
222 OUString uUrlFileName
;
223 OUString
uFileName(fileName
.getStr(), fileName
.getLength(), osl_getThreadTextEncoding());
224 if ( fileName
.indexOf('.') == 0 || fileName
.indexOf(SEPARATOR
) < 0 )
226 OUString uWorkingDir
;
227 OSL_VERIFY( osl_getProcessWorkingDir(&uWorkingDir
.pData
) == osl_Process_E_None
);
228 OSL_VERIFY( FileBase::getAbsoluteFileURL(uWorkingDir
, uFileName
, uUrlFileName
) == FileBase::E_None
);
231 OSL_VERIFY( FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
) == FileBase::E_None
);
237 OUString
LwpTools::DateTimeToOUString(LtTm
& dt
)
240 buf
.append(dt
.tm_year
);
241 buf
.append( A2OUSTR("-") );
242 buf
.append(dt
.tm_mon
);
243 buf
.append( A2OUSTR("-") );
244 buf
.append(dt
.tm_mday
);
245 buf
.append( A2OUSTR("T") );
246 buf
.append(dt
.tm_hour
);
247 buf
.append( A2OUSTR(":") );
248 buf
.append(dt
.tm_min
);
249 buf
.append( A2OUSTR(":") );
250 buf
.append(dt
.tm_sec
);
252 return buf
.makeStringAndClear();
256 * @descr get the system date format
258 XFDateStyle
* LwpTools::GetSystemDateStyle(sal_Bool bLongFormat
)
260 icu::DateFormat::EStyle style
;
262 style
= icu::DateFormat::FULL
;//system full date format
264 style
= icu::DateFormat::SHORT
;//system short date format
266 //1 get locale for system
267 icu::Locale
aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
268 //2 get icu format pattern by locale
269 icu::DateFormat
* fmt
= icu::DateFormat::createDateInstance(style
,aLocale
);
273 UErrorCode status
= U_ZERO_ERROR
;
274 UChar
* pattern
= NULL
;
276 nLengthNeed
= udat_toPattern((void *const *)fmt
,sal_False
,NULL
,nLength
,&status
);
277 if (status
== U_BUFFER_OVERFLOW_ERROR
)
279 status
= U_ZERO_ERROR
;
280 nLength
= nLengthNeed
+1;
281 pattern
= (UChar
*)malloc(sizeof(UChar
)*nLength
);
282 udat_toPattern((void *const *)fmt
,sal_False
,pattern
,nLength
,&status
);
286 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
287 // as patter letter,each represent a element in date/time and its repeat numbers represent
288 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
289 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
290 // we parse pattern string letter by letter and get the time format.
293 XFDateStyle
* pDateStyle
= new XFDateStyle
;
295 for (int32_t i
=0;i
<nLengthNeed
;)
297 cSymbol
= pattern
[i
];
312 pDateStyle
->AddEra();
327 pDateStyle
->AddYear(sal_False
);
329 pDateStyle
->AddYear();
344 pDateStyle
->AddMonth(sal_False
,sal_False
);
346 pDateStyle
->AddMonth(sal_True
,sal_False
);
348 pDateStyle
->AddMonth(sal_False
,sal_True
);
350 pDateStyle
->AddMonth(sal_True
,sal_True
);
365 pDateStyle
->AddMonthDay(sal_False
);
367 pDateStyle
->AddMonthDay();
382 pDateStyle
->AddHour(sal_False
);
384 pDateStyle
->AddHour();
399 pDateStyle
->AddHour(sal_False
);
401 pDateStyle
->AddHour();
416 pDateStyle
->AddMinute(sal_False
);
418 pDateStyle
->AddMinute();
433 pDateStyle
->AddSecond(sal_False
,0);
435 pDateStyle
->AddSecond(sal_True
,0);
450 pDateStyle->AddSecond(sal_False);
452 pDateStyle->AddSecond();*/
467 pDateStyle
->AddWeekDay(sal_False
);
469 pDateStyle
->AddWeekDay();
484 pDateStyle->AddWeekDay(sal_False);
486 pDateStyle->AddWeekDay();*/
501 pDateStyle->AddWeekDay(sal_False);
503 pDateStyle->AddWeekDay();*/
518 pDateStyle->AddWeekDay(sal_False);
520 pDateStyle->AddWeekDay();*/
535 pDateStyle->AddWeekDay(sal_False);
537 pDateStyle->AddWeekDay();*/
551 pDateStyle
->AddAmPm(sal_True
);
579 pDateStyle
->AddHour(sal_False
);
581 pDateStyle
->AddHour();
612 pDateStyle
->AddText(OUString(A2OUSTR("'")));
617 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
621 //UChar buffer[1024];
622 sal_Unicode buffer
[1024];
627 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
628 cTmp
=='\'' || cTmp
=='"' )
638 pDateStyle
->AddText(OUString(buffer
));//keep for all parsed
648 * @descr get the system time format
650 XFTimeStyle
* LwpTools::GetSystemTimeStyle()
652 //1 get locale for system
653 icu::Locale
aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
654 //2 get icu format pattern by locale
655 icu::DateFormat
* fmt
= icu::DateFormat::createTimeInstance(icu::DateFormat::DEFAULT
,aLocale
);
659 UErrorCode status
= U_ZERO_ERROR
;
660 UChar
* pattern
= NULL
;
661 nLengthNeed
= udat_toPattern((void *const *)fmt
,false,NULL
,nLength
,&status
);
662 if (status
== U_BUFFER_OVERFLOW_ERROR
)
664 status
= U_ZERO_ERROR
;
665 nLength
= nLengthNeed
+1;
666 pattern
= (UChar
*)malloc(sizeof(UChar
)*nLength
);
667 udat_toPattern((void *const *)fmt
,false,pattern
,nLength
,&status
);
672 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
673 // as patter letter,each represent a element in date/time and its repeat numbers represent
674 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
675 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
676 // we parse pattern string letter by letter and get the time format.
677 // for time format ,for there is not date info,we can only parse the letter representing time.
680 XFTimeStyle
* pTimeStyle
= new XFTimeStyle
;
682 for (int32_t i
=0;i
<nLengthNeed
;)
684 cSymbol
= pattern
[i
];
700 pTimeStyle
->AddHour(sal_False
);
702 pTimeStyle
->AddHour();
717 pTimeStyle
->AddHour(sal_False
);
719 pTimeStyle
->AddHour();
734 pTimeStyle
->AddMinute(sal_False
);
736 pTimeStyle
->AddMinute();
751 pTimeStyle
->AddSecond(sal_False
,0);
753 pTimeStyle
->AddSecond(sal_True
,0);
768 pDateStyle->AddSecond(sal_False);
770 pDateStyle->AddSecond();*/
784 pTimeStyle
->SetAmPm(sal_True
);
812 pTimeStyle
->AddHour(sal_False
);
814 pTimeStyle
->AddHour();
832 pTimeStyle
->AddText(OUString(A2OUSTR("'")));
837 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
841 sal_Unicode buffer
[1024];
843 //strBuffer.append(cSymbol);
847 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
848 cTmp
=='\'' || cTmp
=='"' )
857 pTimeStyle
->AddText(OUString(buffer
));//keep for all parsed
867 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */