merge the formfield patch from ooo-build
[ooovba.git] / transex3 / source / lngmerge.cxx
blob2f32153e07965dc480022a126c4c0f0e6666976c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lngmerge.cxx,v $
10 * $Revision: 1.29 $
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>
35 // local includes
36 #include "lngmerge.hxx"
37 #include "utf8conv.hxx"
38 #include <iostream>
39 using namespace std;
41 // class LngParser
43 /*****************************************************************************/
44 LngParser::LngParser( const ByteString &rLngFile, BOOL bUTF8, BOOL bULFFormat , bool bQuiet_in )
45 /*****************************************************************************/
47 nError( LNG_OK ),
48 pLines( NULL ),
49 sSource( rLngFile ),
50 bDBIsUTF8( bUTF8 ),
51 bULF( bULFFormat ),
52 bQuiet( bQuiet_in )
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()) {
59 ByteString sLine;
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 );
66 bFirstLine = false;
69 pLines->Insert( new ByteString( sLine ), LIST_APPEND );
72 else
73 nError = LNG_COULD_NOT_OPEN;
75 else
76 nError = LNG_FILE_NOTFOUND;
79 /*****************************************************************************/
80 LngParser::~LngParser()
81 /*****************************************************************************/
83 for ( ULONG i = 0; i < pLines->Count(); i++ )
84 delete pLines->GetObject( i );
85 delete pLines;
88 /*****************************************************************************/
89 void LngParser::FillInFallbacks( ByteStringHashMap Text )
90 /*****************************************************************************/
92 ByteString sCur;
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 );
120 nError = SDF_OK;
121 DirEntry aEntry( String( sSource, RTL_TEXTENCODING_ASCII_US ));
122 aEntry.ToAbs();
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( "/", "\\" );
131 ULONG nPos = 0;
132 BOOL bStart = true;
133 ByteString sGroup;
134 ByteStringHashMap Text;
135 ByteString sID;
136 ByteString sLine;
138 while( nPos < pLines->Count() ){
139 sLine = *pLines->GetObject( nPos++ );
140 while( nPos < pLines->Count() && !isNextGroup( sGroup , sLine ) ){
141 ReadLine( sLine , Text );
142 sID = sGroup;
143 sLine = *pLines->GetObject( nPos++ );
145 if( bStart ){
146 bStart = false;
147 sID = sGroup;
149 else {
151 WriteSDF( aSDFStream , Text , rPrj , rRoot , sActFileName , sID );
154 aSDFStream.Close();
155 return true;
158 void LngParser::WriteSDF( SvFileStream &aSDFStream , ByteStringHashMap &rText_inout ,
159 const ByteString &rPrj , const ByteString &rRoot ,
160 const ByteString &sActFileName , const ByteString &sID )
163 BOOL bExport = true;
164 if ( bExport ) {
165 ByteString sTimeStamp( Export::GetTimeStamp());
166 ByteString sCur;
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";
175 if ( rRoot.Len())
176 sOutput += sActFileName;
177 sOutput += "\t0\t";
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( ' ' );
196 return true;
198 return false;
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, '\"' );
206 if( sLang.Len() )
207 rText_inout[ sLang ] = sText;
210 /*****************************************************************************/
211 BOOL LngParser::Merge(
212 const ByteString &rSDFFile, const ByteString &rDestinationFile , const ByteString& rPrj )
213 /*****************************************************************************/
215 (void) rPrj;
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;
223 nError = LNG_OK;
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();
232 ULONG nPos = 0;
233 BOOL bGroup = FALSE;
234 ByteString sGroup;
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( ' ' );
247 bGroup = TRUE;
249 nPos ++;
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 );
260 // read languages
261 bGroup = FALSE;
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( ' ' );
275 bGroup = TRUE;
276 nPos ++;
277 sLanguagesDone = "";
279 else if ( sLine.GetTokenCount( '=' ) > 1 ) {
280 ByteString sLang = sLine.GetToken( 0, '=' );
281 sLang.EraseLeadingChars( ' ' );
282 sLang.EraseTrailingChars( ' ' );
284 ByteString sSearch( ";" );
285 sSearch += sLang;
286 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, '\"' );
295 if( sLang.Len() ){
296 ByteString sNewText;
297 pEntrys->GetText( sNewText, STRING_TYP_TEXT, sLang, TRUE );
299 if ( sNewText.Len()) {
300 ByteString *pLine = pLines->GetObject( nPos );
302 ByteString sText1( sLang );
303 sText1 += " = \"";
304 sText1 += sNewText;
305 sText1 += "\"";
306 *pLine = sText1;
308 Text[ sLang ] = sNewText;
311 nLastLangPos = nPos;
312 nPos ++;
313 sLanguagesDone += sSearch;
315 else {
316 nLastLangPos = nPos;
317 nPos ++;
318 sLanguagesDone += sSearch;
321 else
322 nPos++;
324 ByteString sCur;
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 ){
332 ByteString sNewText;
333 pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur, TRUE );
334 if (( sNewText.Len()) &&
335 !(( sCur.Equals("x-comment") ) && ( sNewText == "-" )))
337 ByteString sLine;
338 sLine += sCur;
339 sLine += " = \"";
340 sLine += sNewText;
341 sLine += "\"";
343 nLastLangPos++;
344 nPos++;
346 pLines->Insert( new ByteString( sLine ), nLastLangPos );
352 delete pResData;
355 for ( ULONG i = 0; i < pLines->Count(); i++ )
356 aDestination.WriteLine( *pLines->GetObject( i ));
358 aDestination.Close();
359 return TRUE;