1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lngmerge.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_transex3.hxx"
33 #include <tools/fsys.hxx>
36 #include "lngmerge.hxx"
37 #include "utf8conv.hxx"
43 /*****************************************************************************/
44 LngParser::LngParser( const ByteString
&rLngFile
, BOOL bUTF8
, BOOL bULFFormat
, bool bQuiet_in
)
45 /*****************************************************************************/
54 pLines
= new LngLineList( 100, 100 );
55 DirEntry
aEntry( String( sSource
, RTL_TEXTENCODING_ASCII_US
));
56 if ( aEntry
.Exists()) {
57 SvFileStream
aStream( String( sSource
, RTL_TEXTENCODING_ASCII_US
), STREAM_STD_READ
);
58 if ( aStream
.IsOpen()) {
60 bool bFirstLine
= true;
61 while ( !aStream
.IsEof()) {
62 aStream
.ReadLine( sLine
);
64 if( bFirstLine
){ // Always remove UTF8 BOM from the first line
65 Export::RemoveUTF8ByteOrderMarker( sLine
);
69 pLines
->Insert( new ByteString( sLine
), LIST_APPEND
);
73 nError
= LNG_COULD_NOT_OPEN
;
76 nError
= LNG_FILE_NOTFOUND
;
79 /*****************************************************************************/
80 LngParser::~LngParser()
81 /*****************************************************************************/
83 for ( ULONG i
= 0; i
< pLines
->Count(); i
++ )
84 delete pLines
->GetObject( i
);
88 /*****************************************************************************/
89 void LngParser::FillInFallbacks( ByteStringHashMap Text
)
90 /*****************************************************************************/
93 for( unsigned int n
= 0; n
< aLanguages
.size(); n
++ ){
94 sCur
= aLanguages
[ n
];
96 if( Export::isAllowed( sCur
) ){
97 ByteString sFallbackLang
= Export::GetFallbackLanguage( sCur
);
98 if( sFallbackLang
.Len() ){
99 Text
[ sCur
] = Text
[ sFallbackLang
];
105 /*****************************************************************************/
106 BOOL
LngParser::CreateSDF(
107 const ByteString
&rSDFFile
, const ByteString
&rPrj
,
108 const ByteString
&rRoot
)
109 /*****************************************************************************/
112 Export::InitLanguages( false );
113 aLanguages
= Export::GetLanguages();
114 SvFileStream
aSDFStream( String( rSDFFile
, RTL_TEXTENCODING_ASCII_US
),
115 STREAM_STD_WRITE
| STREAM_TRUNC
);
116 if ( !aSDFStream
.IsOpen()) {
117 nError
= SDF_COULD_NOT_OPEN
;
119 aSDFStream
.SetStreamCharSet( RTL_TEXTENCODING_UTF8
);
121 DirEntry
aEntry( String( sSource
, RTL_TEXTENCODING_ASCII_US
));
123 String sFullEntry
= aEntry
.GetFull();
124 aEntry
+= DirEntry( String( "..", RTL_TEXTENCODING_ASCII_US
));
125 aEntry
+= DirEntry( rRoot
);
126 ByteString
sPrjEntry( aEntry
.GetFull(), gsl_getSystemTextEncoding());
127 ByteString
sActFileName(
128 sFullEntry
.Copy( sPrjEntry
.Len() + 1 ), gsl_getSystemTextEncoding());
129 sActFileName
.SearchAndReplaceAll( "/", "\\" );
134 ByteStringHashMap Text
;
138 while( nPos
< pLines
->Count() ){
139 sLine
= *pLines
->GetObject( nPos
++ );
140 while( nPos
< pLines
->Count() && !isNextGroup( sGroup
, sLine
) ){
141 ReadLine( sLine
, Text
);
143 sLine
= *pLines
->GetObject( nPos
++ );
151 WriteSDF( aSDFStream
, Text
, rPrj
, rRoot
, sActFileName
, sID
);
158 void LngParser::WriteSDF( SvFileStream
&aSDFStream
, ByteStringHashMap
&rText_inout
,
159 const ByteString
&rPrj
, const ByteString
&rRoot
,
160 const ByteString
&sActFileName
, const ByteString
&sID
)
165 ByteString
sTimeStamp( Export::GetTimeStamp());
167 FillInFallbacks( rText_inout
);
168 for( unsigned int n
= 0; n
< aLanguages
.size(); n
++ ){
169 sCur
= aLanguages
[ n
];
170 ByteString sAct
= rText_inout
[ sCur
];
171 if ( !sAct
.Len() && sCur
.Len() )
172 sAct
= rText_inout
[ ByteString("en-US") ];
174 ByteString
sOutput( rPrj
); sOutput
+= "\t";
176 sOutput
+= sActFileName
;
178 sOutput
+= "LngText\t";
179 sOutput
+= sID
; sOutput
+= "\t\t\t\t0\t";
180 sOutput
+= sCur
; sOutput
+= "\t";
181 sOutput
+= sAct
; sOutput
+= "\t\t\t\t";
182 sOutput
+= sTimeStamp
;
183 //if( !sCur.EqualsIgnoreCaseAscii("de") ||( sCur.EqualsIgnoreCaseAscii("de") && !Export::isMergingGermanAllowed( rPrj ) ) )
184 aSDFStream
.WriteLine( sOutput
);
188 bool LngParser::isNextGroup( ByteString
&sGroup_out
, ByteString
&sLine_in
){
189 sLine_in
.EraseLeadingChars( ' ' );
190 sLine_in
.EraseTrailingChars( ' ' );
191 if (( sLine_in
.GetChar( 0 ) == '[' ) &&
192 ( sLine_in
.GetChar( sLine_in
.Len() - 1 ) == ']' )){
193 sGroup_out
= sLine_in
.GetToken( 1, '[' ).GetToken( 0, ']' );
194 sGroup_out
.EraseLeadingChars( ' ' );
195 sGroup_out
.EraseTrailingChars( ' ' );
200 void LngParser::ReadLine( const ByteString
&sLine_in
, ByteStringHashMap
&rText_inout
){
201 //printf("sLine -> '%s'\n",sLine_in.GetBuffer());
202 ByteString sLang
= sLine_in
.GetToken( 0, '=' );
203 sLang
.EraseLeadingChars( ' ' );
204 sLang
.EraseTrailingChars( ' ' );
205 ByteString sText
= sLine_in
.GetToken( 1, '\"' ).GetToken( 0, '\"' );
207 rText_inout
[ sLang
] = sText
;
210 /*****************************************************************************/
211 BOOL
LngParser::Merge(
212 const ByteString
&rSDFFile
, const ByteString
&rDestinationFile
, const ByteString
& rPrj
)
213 /*****************************************************************************/
216 Export::InitLanguages( true );
217 SvFileStream
aDestination(
218 String( rDestinationFile
, RTL_TEXTENCODING_ASCII_US
),
219 STREAM_STD_WRITE
| STREAM_TRUNC
);
220 if ( !aDestination
.IsOpen()) {
221 nError
= LNG_COULD_NOT_OPEN
;
224 // MergeDataFile( const ByteString &rFileName, const ByteString& rFile , BOOL bErrLog, CharSet aCharSet, BOOL bUTF8 );
226 MergeDataFile
aMergeDataFile( rSDFFile
, sSource
, FALSE
, RTL_TEXTENCODING_MS_1252
);//, bDBIsUTF8 );
227 ByteString
sTmp( Export::sLanguages
);
228 if( sTmp
.ToUpperAscii().Equals("ALL") )
229 Export::SetLanguages( aMergeDataFile
.GetLanguages() );
230 aLanguages
= Export::GetLanguages();
236 // seek to next group
237 while ( nPos
< pLines
->Count() && !bGroup
) {
238 ByteString
sLine( *pLines
->GetObject( nPos
));
239 sLine
.EraseLeadingChars( ' ' );
240 sLine
.EraseTrailingChars( ' ' );
241 if (( sLine
.GetChar( 0 ) == '[' ) &&
242 ( sLine
.GetChar( sLine
.Len() - 1 ) == ']' ))
244 sGroup
= sLine
.GetToken( 1, '[' ).GetToken( 0, ']' );
245 sGroup
.EraseLeadingChars( ' ' );
246 sGroup
.EraseTrailingChars( ' ' );
252 while ( nPos
< pLines
->Count()) {
253 ByteStringHashMap Text
;
254 ByteString
sID( sGroup
);
255 ULONG nLastLangPos
= 0;
257 ResData
*pResData
= new ResData( "", sID
, sSource
);
258 pResData
->sResTyp
= "LngText";
259 PFormEntrys
*pEntrys
= aMergeDataFile
.GetPFormEntrys( pResData
);
263 ByteString sLanguagesDone
;
265 while ( nPos
< pLines
->Count() && !bGroup
) {
266 ByteString
sLine( *pLines
->GetObject( nPos
));
267 sLine
.EraseLeadingChars( ' ' );
268 sLine
.EraseTrailingChars( ' ' );
269 if (( sLine
.GetChar( 0 ) == '[' ) &&
270 ( sLine
.GetChar( sLine
.Len() - 1 ) == ']' ))
272 sGroup
= sLine
.GetToken( 1, '[' ).GetToken( 0, ']' );
273 sGroup
.EraseLeadingChars( ' ' );
274 sGroup
.EraseTrailingChars( ' ' );
279 else if ( sLine
.GetTokenCount( '=' ) > 1 ) {
280 ByteString sLang
= sLine
.GetToken( 0, '=' );
281 sLang
.EraseLeadingChars( ' ' );
282 sLang
.EraseTrailingChars( ' ' );
284 ByteString
sSearch( ";" );
288 if (( sLanguagesDone
.Search( sSearch
) != STRING_NOTFOUND
)) {
289 pLines
->Remove( nPos
);
291 if( bULF
&& pEntrys
)
293 // this is a valid text line
294 ByteString sText
= sLine
.GetToken( 1, '\"' ).GetToken( 0, '\"' );
297 pEntrys
->GetText( sNewText
, STRING_TYP_TEXT
, sLang
, TRUE
);
299 if ( sNewText
.Len()) {
300 ByteString
*pLine
= pLines
->GetObject( nPos
);
302 ByteString
sText1( sLang
);
308 Text
[ sLang
] = sNewText
;
313 sLanguagesDone
+= sSearch
;
318 sLanguagesDone
+= sSearch
;
325 if ( nLastLangPos
) {
326 for( unsigned int n
= 0; n
< aLanguages
.size(); n
++ ){
327 sCur
= aLanguages
[ n
];
328 if( //( !sCur.EqualsIgnoreCaseAscii("de") ||
329 //( sCur.EqualsIgnoreCaseAscii("de") && Export::isMergingGermanAllowed( rPrj ) ) )
330 !sCur
.EqualsIgnoreCaseAscii("en-US") && !Text
[ sCur
].Len() && pEntrys
){
333 pEntrys
->GetText( sNewText
, STRING_TYP_TEXT
, sCur
, TRUE
);
334 if (( sNewText
.Len()) &&
335 !(( sCur
.Equals("x-comment") ) && ( sNewText
== "-" )))
346 pLines
->Insert( new ByteString( sLine
), nLastLangPos
);
355 for ( ULONG i
= 0; i
< pLines
->Count(); i
++ )
356 aDestination
.WriteLine( *pLines
->GetObject( i
));
358 aDestination
.Close();