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 ::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);
95 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
= false; //switch if unicode part reached
121 sal_uInt16 sublen
= 0;
123 while(readLen
<strlen
)
125 if(!flag
) //Not unicode string
128 readbyte
= pObjStrm
->QuickReaduInt8(&bFailure
);
130 readLen
+=sizeof(readbyte
);
135 if(sublen
>0) //add it to the strBuf
137 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
143 buf
[sublen
++] = readbyte
;
145 if(sublen
>=1023 || readLen
==strlen
) //add it to the strBuf
147 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
151 else //unicode string
154 readword
= pObjStrm
->QuickReaduInt16(&bFailure
);
156 readLen
+=sizeof(readword
);
158 if(readword
== 0x0000)
163 unibuf
[sublen
] = '\0';
164 strBuf
.append( OUString(unibuf
) );
170 unibuf
[sublen
++] = readword
;
172 if(sublen
>=1023 || readLen
==strlen
)
174 unibuf
[sublen
] = '\0';
175 strBuf
.append( OUString(unibuf
) );
180 str
= strBuf
.makeStringAndClear();
186 * @descr Judge if the data (len) in object stream is lwp unicode packed
188 bool LwpTools::IsUnicodePacked(LwpObjectStream
* pObjStrm
, sal_uInt16 len
)
191 sal_uInt16 oldpos
= pObjStrm
->GetPos();
193 for (sal_uInt16 i
= 0; i
< len
; i
++)
195 byte
= pObjStrm
->QuickReaduInt8();
198 pObjStrm
->Seek(oldpos
);
202 pObjStrm
->Seek(oldpos
);
206 bool LwpTools::isFileUrl(const OString
&fileName
)
208 if (fileName
.startsWith("file://") )
213 OUString
LwpTools::convertToFileUrl(const OString
&fileName
)
215 if ( isFileUrl(fileName
) )
217 return OStringToOUString(fileName
, osl_getThreadTextEncoding());
220 OUString uUrlFileName
;
221 OUString
uFileName(fileName
.getStr(), fileName
.getLength(), osl_getThreadTextEncoding());
222 if ( fileName
.startsWith(".") || fileName
.indexOf(SEPARATOR
) < 0 )
224 OUString uWorkingDir
;
225 OSL_VERIFY( osl_getProcessWorkingDir(&uWorkingDir
.pData
) == osl_Process_E_None
);
226 OSL_VERIFY( FileBase::getAbsoluteFileURL(uWorkingDir
, uFileName
, uUrlFileName
) == FileBase::E_None
);
229 OSL_VERIFY( FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
) == FileBase::E_None
);
235 OUString
LwpTools::DateTimeToOUString(LtTm
& dt
)
237 OUString aResult
= OUString::number(dt
.tm_year
) + "-" + OUString::number(dt
.tm_mon
) + "-" + OUString::number(dt
.tm_mday
) +
238 "T" + OUString::number(dt
.tm_hour
) + ":" + OUString::number(dt
.tm_min
) + ":" + OUString::number(dt
.tm_sec
);
244 * @descr get the system date format
246 XFDateStyle
* LwpTools::GetSystemDateStyle(bool bLongFormat
)
248 icu::DateFormat::EStyle style
;
250 style
= icu::DateFormat::FULL
;//system full date format
252 style
= icu::DateFormat::SHORT
;//system short date format
254 //1 get locale for system
255 icu::Locale
aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
256 //2 get icu format pattern by locale
257 icu::DateFormat
* fmt
= icu::DateFormat::createDateInstance(style
,aLocale
);
261 UErrorCode status
= U_ZERO_ERROR
;
262 UChar
* pattern
= NULL
;
264 nLengthNeed
= udat_toPattern(reinterpret_cast<void **>(fmt
),sal_False
,NULL
,nLength
,&status
);
265 if (status
== U_BUFFER_OVERFLOW_ERROR
)
267 status
= U_ZERO_ERROR
;
268 nLength
= nLengthNeed
+1;
269 pattern
= static_cast<UChar
*>(malloc(sizeof(UChar
)*nLength
));
270 udat_toPattern(reinterpret_cast<void **>(fmt
),sal_False
,pattern
,nLength
,&status
);
274 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
275 // as patter letter,each represent a element in date/time and its repeat numbers represent
276 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
277 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
278 // we parse pattern string letter by letter and get the time format.
281 XFDateStyle
* pDateStyle
= new XFDateStyle
;
283 for (int32_t i
=0;i
<nLengthNeed
;)
285 cSymbol
= pattern
[i
];
300 pDateStyle
->AddEra();
315 pDateStyle
->AddYear(false);
317 pDateStyle
->AddYear();
332 pDateStyle
->AddMonth(false,false);
334 pDateStyle
->AddMonth(true,false);
336 pDateStyle
->AddMonth(false,true);
338 pDateStyle
->AddMonth(true,true);
353 pDateStyle
->AddMonthDay(false);
355 pDateStyle
->AddMonthDay();
370 pDateStyle
->AddHour(false);
372 pDateStyle
->AddHour();
387 pDateStyle
->AddHour(false);
389 pDateStyle
->AddHour();
404 pDateStyle
->AddMinute(false);
406 pDateStyle
->AddMinute();
421 pDateStyle
->AddSecond(false,0);
423 pDateStyle
->AddSecond(true,0);
438 pDateStyle->AddSecond(sal_False);
440 pDateStyle->AddSecond();*/
455 pDateStyle
->AddWeekDay(false);
457 pDateStyle
->AddWeekDay();
472 pDateStyle->AddWeekDay(sal_False);
474 pDateStyle->AddWeekDay();*/
489 pDateStyle->AddWeekDay(sal_False);
491 pDateStyle->AddWeekDay();*/
506 pDateStyle->AddWeekDay(sal_False);
508 pDateStyle->AddWeekDay();*/
523 pDateStyle->AddWeekDay(sal_False);
525 pDateStyle->AddWeekDay();*/
539 pDateStyle
->AddAmPm(true);
567 pDateStyle
->AddHour(false);
569 pDateStyle
->AddHour();
600 pDateStyle
->AddText("'");
605 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
612 //UChar buffer[1024];
613 sal_Unicode buffer
[1024];
618 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
619 cTmp
=='\'' || cTmp
=='"' )
629 pDateStyle
->AddText(OUString(buffer
));//keep for all parsed
639 * @descr get the system time format
641 XFTimeStyle
* LwpTools::GetSystemTimeStyle()
643 //1 get locale for system
644 icu::Locale
aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
645 //2 get icu format pattern by locale
646 icu::DateFormat
* fmt
= icu::DateFormat::createTimeInstance(icu::DateFormat::DEFAULT
,aLocale
);
650 UErrorCode status
= U_ZERO_ERROR
;
651 UChar
* pattern
= NULL
;
652 nLengthNeed
= udat_toPattern(reinterpret_cast<void **>(fmt
),false,NULL
,nLength
,&status
);
653 if (status
== U_BUFFER_OVERFLOW_ERROR
)
655 status
= U_ZERO_ERROR
;
656 nLength
= nLengthNeed
+1;
657 pattern
= static_cast<UChar
*>(malloc(sizeof(UChar
)*nLength
));
658 udat_toPattern(reinterpret_cast<void **>(fmt
),false,pattern
,nLength
,&status
);
663 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
664 // as patter letter,each represent a element in date/time and its repeat numbers represent
665 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
666 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
667 // we parse pattern string letter by letter and get the time format.
668 // for time format ,for there is not date info,we can only parse the letter representing time.
671 XFTimeStyle
* pTimeStyle
= new XFTimeStyle
;
673 for (int32_t i
=0;i
<nLengthNeed
;)
675 cSymbol
= pattern
[i
];
691 pTimeStyle
->AddHour(false);
693 pTimeStyle
->AddHour();
708 pTimeStyle
->AddHour(false);
710 pTimeStyle
->AddHour();
725 pTimeStyle
->AddMinute(false);
727 pTimeStyle
->AddMinute();
742 pTimeStyle
->AddSecond(false,0);
744 pTimeStyle
->AddSecond(true,0);
759 pDateStyle->AddSecond(sal_False);
761 pDateStyle->AddSecond();*/
775 pTimeStyle
->SetAmPm(true);
803 pTimeStyle
->AddHour(false);
805 pTimeStyle
->AddHour();
823 pTimeStyle
->AddText("'");
828 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
835 sal_Unicode buffer
[1024];
837 //strBuffer.append(cSymbol);
841 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
842 cTmp
=='\'' || cTmp
=='"' )
851 pTimeStyle
->AddText(OUString(buffer
));//keep for all parsed
861 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */