xref: /trunk/main/l10ntools/source/helpmerge.cxx (revision 7a8fdded)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_l10ntools.hxx"
26 #include <tools/fsys.hxx>
27 #include <osl/file.hxx>
28 // local includes
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "helpmerge.hxx"
32 #include "utf8conv.hxx"
33 #include <algorithm>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <iostream>
37 #include <fstream>
38 #include <vector>
39 #include "rtl/strbuf.hxx"
40 #ifdef WNT
41 #include <direct.h>
42 //#include <WinBase.h>
43 #include "tools/prewin.h"
44 #include <windows.h>
45 #include "tools/postwin.h"
46 #endif
47 
48 /*****************************************************************************/
FillInFallbacks(LangHashMap & rElem_out,ByteString sLangIdx_in)49 void HelpParser::FillInFallbacks( LangHashMap& rElem_out, ByteString sLangIdx_in ){
50 /*****************************************************************************/
51     static const ByteString ENGLISH_LANGUAGECODE( "en-US" );
52     static const ByteString GERMAN_LANGUAGECODE ( "de"    );
53     ByteString sCur;
54     XMLElement* pTmp     = NULL;
55     XMLElement* pTmp2    = NULL;
56 
57     sCur = sLangIdx_in;
58     ByteString sFallback( sCur );
59     GetIsoFallback( sFallback );
60     if( (rElem_out.find( sFallback ) != rElem_out.end()) && rElem_out[ sFallback ] != NULL ){
61 	    pTmp2 = rElem_out[ sFallback ];
62         pTmp = new XMLElement( *pTmp2 )  ; // Copy
63         pTmp->SetPos( pTmp2->GetPos()+1 );
64         pTmp->ChangeLanguageTag( String( sLangIdx_in , RTL_TEXTENCODING_ASCII_US) );
65         rElem_out[ sLangIdx_in ] = pTmp;
66         pTmp2 = NULL;
67     }
68     else if( (rElem_out.find( ENGLISH_LANGUAGECODE ) != rElem_out.end()) && rElem_out[ ENGLISH_LANGUAGECODE ] != NULL ){// No English
69 	    pTmp2 = rElem_out[ ENGLISH_LANGUAGECODE ];
70         pTmp = new XMLElement( *pTmp2 )  ; // Copy
71         pTmp->SetPos( pTmp2->GetPos()+1 );
72         pTmp->ChangeLanguageTag( String( sLangIdx_in , RTL_TEXTENCODING_ASCII_US) );
73         rElem_out[ sCur ] = pTmp;
74         pTmp2 = NULL;
75     }
76     else if( (rElem_out.find( GERMAN_LANGUAGECODE ) != rElem_out.end() ) && rElem_out[ GERMAN_LANGUAGECODE ] != NULL ){// No English
77 	    pTmp2 = rElem_out[ GERMAN_LANGUAGECODE ];
78         pTmp = new XMLElement( *pTmp2 ); // Copy
79         pTmp->SetPos( pTmp2->GetPos()+1 );
80         pTmp->ChangeLanguageTag( String( sLangIdx_in , RTL_TEXTENCODING_ASCII_US ) );
81         rElem_out[ sCur ] = pTmp;
82         pTmp2 = NULL;
83 	}else{
84         fprintf(stdout,"ERROR: No Fallback found for language %s:\n",sCur.GetBuffer());
85         rElem_out[ sCur ]=new XMLElement(); // Use dummy element
86 	}
87 }
88 
89 /*****************************************************************************/
Dump(XMLHashMap * rElem_in)90 void HelpParser::Dump(XMLHashMap* rElem_in) {
91 /*****************************************************************************/
92     for(XMLHashMap::iterator pos = rElem_in->begin();pos != rElem_in->end(); ++pos){
93 	    Dump(pos->second,pos->first);
94     }
95 }
96 /*****************************************************************************/
Dump(LangHashMap * rElem_in,const ByteString sKey_in)97 void HelpParser::Dump(LangHashMap* rElem_in,const ByteString sKey_in) {
98 /*****************************************************************************/
99     ByteString x;
100     OString y;
101     fprintf(stdout,"+------------%s-----------+\n",sKey_in.GetBuffer() );
102     for(LangHashMap::iterator posn=rElem_in->begin();posn!=rElem_in->end();++posn){
103         x=posn->first;
104         y=posn->second->ToOString();
105         fprintf(stdout,"key=%s value=%s\n",x.GetBuffer(),y.getStr());
106     }
107     fprintf(stdout,"+--------------------------+\n");
108 }
109 
HelpParser(const ByteString & rHelpFile,bool rUTF8,bool rHasInputList)110 HelpParser::HelpParser( const ByteString &rHelpFile, bool rUTF8 , bool rHasInputList  )
111         : sHelpFile( rHelpFile ),
112           bUTF8    ( rUTF8     ),
113           bHasInputList( rHasInputList )
114           {};
115 
116 /*****************************************************************************/
CreateSDF(const ByteString & rSDFFile_in,const ByteString & rPrj_in,const ByteString & rRoot_in,const ByteString & sHelpFile,XMLFile * pXmlFile,const ByteString & rGsi1)117 bool HelpParser::CreateSDF(
118 /*****************************************************************************/
119 	const ByteString &rSDFFile_in, const ByteString &rPrj_in,const ByteString &rRoot_in,
120     const ByteString &sHelpFile, XMLFile *pXmlFile, const ByteString &rGsi1){
121     // GSI File constants
122     static const String GSI_SEQUENCE1( String::CreateFromAscii("\t0\t")	);
123     static const String GSI_SEQUENCE2( String::CreateFromAscii("\t\t\t0\t")		);
124     static const String GSI_TAB      ( String::CreateFromAscii("\t")			);
125     static const String GSI_SEQUENCE4( String::CreateFromAscii("\t\t\t\t")		);
126 	static const String ret			 ( String::CreateFromAscii("\n")			);
127     static const String ret_char	 ( String::CreateFromAscii("")				);
128     static const String tab			 ( String::CreateFromAscii("\t")			);
129     static const String tab_char	 ( String::CreateFromAscii("")				);
130 
131     SimpleXMLParser aParser;
132     String sUsedTempFile;
133     String sXmlFile;
134 
135     if( Export::fileHasUTF8ByteOrderMarker( sHelpFile ) ){
136         DirEntry aTempFile = Export::GetTempFile();
137         DirEntry aSourceFile( String( sHelpFile , RTL_TEXTENCODING_ASCII_US ) );
138         aSourceFile.CopyTo( aTempFile , FSYS_ACTION_COPYFILE );
139         String sTempFile = aTempFile.GetFull();
140         Export::RemoveUTF8ByteOrderMarkerFromFile( ByteString( sTempFile , RTL_TEXTENCODING_ASCII_US ) );
141         sUsedTempFile = sTempFile;
142         sXmlFile = sTempFile;
143     }else{
144         sUsedTempFile = String::CreateFromAscii("");
145         sXmlFile = String( sHelpFile , RTL_TEXTENCODING_ASCII_US );
146     }
147 
148 //    ByteString fullFilePath;
149     //DirEntry aFile( sXmlFile );
150     //makeAbsolutePath( sHelpFile , rRoot_in);
151     ByteString fullFilePath = rPrj_in;
152     fullFilePath.Append( "\\" );
153     fullFilePath.Append( makeAbsolutePath( sHelpFile , rRoot_in ) );
154 	fullFilePath.SearchAndReplaceAll( "\\", "/" );
155 
156     String strFullPath( fullFilePath.GetBuffer() , RTL_TEXTENCODING_ASCII_US );
157 
158     //printf( "%s\n", fullFilePath.GetBuffer() );
159     std::auto_ptr <XMLFile> file ( aParser.Execute( strFullPath , sXmlFile, pXmlFile ) );
160 
161 	if(file.get() == NULL){
162 		printf("%s\n",ByteString(aParser.GetError().sMessage,RTL_TEXTENCODING_ASCII_US).GetBuffer());
163         exit(-1);
164 		//return false;
165 	}
166     file->Extract();
167     if( !file->CheckExportStatus() ){
168         return true;
169     }
170     SvFileStream aSDFStream( String( rSDFFile_in, RTL_TEXTENCODING_ASCII_US ),
171 		STREAM_STD_WRITE | STREAM_TRUNC );
172 
173 	if ( !aSDFStream.IsOpen()) {
174         fprintf(stdout,"Can't open file %s\n",rSDFFile_in.GetBuffer());
175         return false;
176 	}
177 
178     ByteString sActFileName = makeAbsolutePath( sHelpFile , rRoot_in );
179 
180 	XMLHashMap*  aXMLStrHM   = file->GetStrings();
181 	LangHashMap* pElem;
182 	XMLElement*  pXMLElement  = NULL;
183 
184 	//Dump(aXMLStrHM);
185 
186 	ByteString sTimeStamp( Export::GetTimeStamp() );
187     OUString sOUTimeStamp( sTimeStamp.GetBuffer() , sTimeStamp.Len() , RTL_TEXTENCODING_ASCII_US );
188 
189     OUStringBuffer sBuffer;
190     const OUString sOUPrj( rPrj_in.GetBuffer() , rPrj_in.Len() , RTL_TEXTENCODING_ASCII_US );
191     const OUString sOUActFileName(sActFileName.GetBuffer() , sActFileName.Len() , RTL_TEXTENCODING_ASCII_US );
192     const OUString sOUGsi1( rGsi1.GetBuffer() , rGsi1.Len() , RTL_TEXTENCODING_ASCII_US );
193 
194     Export::InitLanguages( false );
195     std::vector<ByteString> aLanguages = Export::GetLanguages();
196 
197     std::vector<ByteString> order = file->getOrder();
198     std::vector<ByteString>::iterator pos;
199     XMLHashMap::iterator posm;
200 
201     for( pos = order.begin(); pos != order.end() ; ++pos )
202 	{
203         posm = aXMLStrHM->find( *pos );
204         pElem = posm->second;
205         ByteString sCur;
206 
207         for( unsigned int n = 0; n < aLanguages.size(); n++ )
208 		{
209 			sCur = aLanguages[ n ];
210 			if(pElem->find( sCur )==pElem->end())
211 			{
212 				FillInFallbacks( *pElem , sCur );
213             }
214             pXMLElement = (*pElem)[ sCur ];
215 
216 			if( pXMLElement != NULL )
217 			{
218 				OUString data = pXMLElement->ToOUString();
219    				String sTmp = String(data.getStr());
220 				sTmp.SearchAndReplaceAll(ret,ret_char);    // Remove \n
221 				sTmp.SearchAndReplaceAll(tab,tab_char);    // Remove \t
222 
223                 data = OUString( sTmp );
224                 sBuffer.append( sOUPrj );
225                 sBuffer.append( GSI_TAB );				//"\t";
226                 if ( rRoot_in.Len())
227 					sBuffer.append( sOUActFileName );
228    				sBuffer.append( GSI_SEQUENCE1 );		//"\t0\t";
229    				sBuffer.append( sOUGsi1 );		        //"help";
230    				sBuffer.append( GSI_TAB );              //"\t";
231                 ByteString sID = posm->first;			// ID
232                 sBuffer.append( OUString( sID.GetBuffer() , sID.Len() , RTL_TEXTENCODING_UTF8 ) );
233                 sBuffer.append( GSI_TAB ); //"\t";
234    				ByteString sOldRef = pXMLElement->GetOldref(); // oldref
235                 sBuffer.append( OUString(sOldRef.GetBuffer() , sOldRef.Len() , RTL_TEXTENCODING_UTF8 ) );
236    				sBuffer.append( GSI_SEQUENCE2 );		//"\t\t\t0\t";
237                 sBuffer.append( OUString( sCur.GetBuffer() , sCur.Len() , RTL_TEXTENCODING_UTF8 ) );
238    				sBuffer.append( GSI_TAB );				//"\t";
239 				sBuffer.append( data );
240   				sBuffer.append( GSI_SEQUENCE4 );		//"\t\t\t\t";
241                 sBuffer.append( sOUTimeStamp );
242 				ByteString sOut( sBuffer.makeStringAndClear().getStr() , RTL_TEXTENCODING_UTF8 );
243                 //if( !sCur.EqualsIgnoreCaseAscii("de") ||( sCur.EqualsIgnoreCaseAscii("de") && !Export::isMergingGermanAllowed( rPrj_in ) ) )
244 				//{
245 				if( data.getLength() > 0 ) aSDFStream.WriteLine( sOut );
246                 //}
247                 pXMLElement=NULL;
248 			}else fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreateSDF , Language %s\n",sCur.GetBuffer() );
249 		}
250 
251 	}
252     //Dump(aXMLStrHM);
253 	aSDFStream.Close();
254 
255     if( !sUsedTempFile.EqualsIgnoreCaseAscii( "" ) ){
256         DirEntry aTempFile( sUsedTempFile );
257         aTempFile.Kill();
258     }
259     return sal_True;
260 }
261 
makeAbsolutePath(const ByteString & sHelpFile,const ByteString & rRoot_in)262 ByteString HelpParser::makeAbsolutePath( const ByteString& sHelpFile , const ByteString& rRoot_in )
263 {
264   	DirEntry aEntry( String( sHelpFile, RTL_TEXTENCODING_ASCII_US ));
265 	aEntry.ToAbs();
266 	String sFullEntry = aEntry.GetFull();
267 	aEntry += DirEntry( String( "..", RTL_TEXTENCODING_ASCII_US ));
268 	aEntry += DirEntry( rRoot_in );
269 	ByteString sPrjEntry( aEntry.GetFull(), gsl_getSystemTextEncoding());
270 	ByteString sActFileName(
271 	sFullEntry.Copy( sPrjEntry.Len() + 1 ), gsl_getSystemTextEncoding());
272 
273 	sActFileName.SearchAndReplaceAll( "/", "\\" );
274     return sActFileName;
275 }
Merge(const ByteString & rSDFFile,const ByteString & rDestinationFile,ByteString & sLanguage,MergeDataFile & aMergeDataFile)276 bool HelpParser::Merge( const ByteString &rSDFFile, const ByteString &rDestinationFile  ,
277 		ByteString& sLanguage , MergeDataFile& aMergeDataFile )
278 {
279 
280     (void) rSDFFile;
281     bool hasNoError = true;
282 
283     SimpleXMLParser aParser;
284 
285     String sUsedTempFile;
286     String sXmlFile;
287 
288     if( Export::fileHasUTF8ByteOrderMarker( sHelpFile ) ){
289         DirEntry aTempFile = Export::GetTempFile();
290         DirEntry aSourceFile( String( sHelpFile , RTL_TEXTENCODING_ASCII_US ) );
291         aSourceFile.CopyTo( aTempFile , FSYS_ACTION_COPYFILE );
292         String sTempFile = aTempFile.GetFull();
293         Export::RemoveUTF8ByteOrderMarkerFromFile( ByteString( sTempFile , RTL_TEXTENCODING_ASCII_US ) );
294         sUsedTempFile = sTempFile;
295         sXmlFile = sTempFile;
296     }else{
297         sUsedTempFile = String::CreateFromAscii("");
298         sXmlFile = String( sHelpFile , RTL_TEXTENCODING_ASCII_US );
299     }
300 
301     OUString sOUHelpFile( sXmlFile );
302 	String fullFilePath;
303     DirEntry aFile( sXmlFile );
304 
305 	XMLFile* xmlfile = ( aParser.Execute( aFile.GetFull() , sOUHelpFile, new XMLFile( '0' ) ) );
306     if (xmlfile == NULL) {
307         printf("%s\n",ByteString(aParser.GetError().sMessage,
308                                  RTL_TEXTENCODING_ASCII_US).GetBuffer());
309         exit(-1);
310     }
311 	printf("Dest file %s\n",rDestinationFile.GetBuffer());
312 	hasNoError = MergeSingleFile( xmlfile , aMergeDataFile , sLanguage , rDestinationFile );
313 	delete xmlfile;
314     if( !sUsedTempFile.EqualsIgnoreCaseAscii( "" ) ){
315         DirEntry aTempFile( sUsedTempFile );
316         aTempFile.Kill();
317     }
318 	return hasNoError;
319 }
320 
ByteStringEqual(const ByteString & rKey1,const ByteString & rKey2)321 bool ByteStringEqual( const ByteString& rKey1, const ByteString& rKey2 )  {
322     return rKey1.CompareTo( rKey2 )==COMPARE_EQUAL;
323 };
ByteStringLess(const ByteString & rKey1,const ByteString & rKey2)324 bool ByteStringLess( const ByteString& rKey1, const ByteString& rKey2 )  {
325      return rKey1.CompareTo( rKey2 )==COMPARE_LESS;
326 }
327 
parse_languages(std::vector<ByteString> & aLanguages,MergeDataFile & aMergeDataFile)328 void HelpParser::parse_languages( std::vector<ByteString>& aLanguages , MergeDataFile& aMergeDataFile ){
329     std::vector<ByteString> aTmp;
330 
331     const ByteString DE		("de");
332 	const ByteString ENUS	("en-US");
333 	static const ByteString ALL( "ALL" );
334 
335 	Export::InitLanguages( false );
336 
337 	if( Export::sLanguages.EqualsIgnoreCaseAscii( ALL ) )
338 	{
339 		aLanguages = aMergeDataFile.GetLanguages();
340         aLanguages.push_back( DE );
341         aLanguages.push_back( ENUS );
342 
343         if( !Export::sForcedLanguages.Equals("") )
344 		{
345 			std::vector<ByteString> aFL = Export::GetForcedLanguages();
346             std::copy( aFL.begin() ,
347                        aFL.end() ,
348                        back_inserter( aLanguages )
349                      );
350             std::sort(   aLanguages.begin() , aLanguages.end() , ByteStringLess );
351             std::vector<ByteString>::iterator unique_iter =  std::unique( aLanguages.begin() , aLanguages.end() , ByteStringEqual );
352             std::copy( aLanguages.begin() , unique_iter , back_inserter( aTmp ) );
353             aLanguages = aTmp;
354         }
355     }
356     else{
357         aLanguages = Export::GetLanguages();
358     }
359 
360 }
361 
Merge(const ByteString & rSDFFile,const ByteString & rPathX,const ByteString & rPathY,bool bISO,const std::vector<ByteString> & aLanguages,MergeDataFile & aMergeDataFile,bool bCreateDir)362 bool HelpParser::Merge(
363 	const ByteString &rSDFFile, const ByteString &rPathX , const ByteString &rPathY , bool bISO ,
364 	const std::vector<ByteString>& aLanguages , MergeDataFile& aMergeDataFile , bool bCreateDir )
365 {
366 
367 
368     (void) rSDFFile ;
369     bool hasNoError = true;
370     SimpleXMLParser aParser;
371     String sUsedTempFile;
372     String sXmlFile;
373 
374 	if( Export::fileHasUTF8ByteOrderMarker( sHelpFile ) )
375 	{
376         DirEntry aTempFile = Export::GetTempFile();
377         DirEntry aSourceFile( String( sHelpFile , RTL_TEXTENCODING_ASCII_US ) );
378         aSourceFile.CopyTo( aTempFile , FSYS_ACTION_COPYFILE );
379         String sTempFile = aTempFile.GetFull();
380         Export::RemoveUTF8ByteOrderMarkerFromFile( ByteString( sTempFile , RTL_TEXTENCODING_ASCII_US ) );
381         sUsedTempFile = sTempFile;
382         sXmlFile = sTempFile;
383     }
384 	else
385 	{
386         sUsedTempFile = String::CreateFromAscii("");
387         sXmlFile = String( sHelpFile , RTL_TEXTENCODING_ASCII_US );
388     }
389 
390 
391 	OUString sOUHelpFile( sXmlFile );
392   	String fullFilePath;
393     DirEntry aFile( sXmlFile );
394 
395 	XMLFile* xmlfile = ( aParser.Execute( aFile.GetFull() , sOUHelpFile, new XMLFile( '0' ) ) );
396 
397 	if( xmlfile == NULL)
398 	{
399 		printf("%s\n",ByteString(aParser.GetError().sMessage,RTL_TEXTENCODING_UTF8).GetBuffer());
400 		exit(-1);
401 		//return false;
402 	}
403 	xmlfile->Extract();
404 
405 
406     ByteString sCur;
407     for( unsigned int n = 0; n < aLanguages.size(); n++ ){
408 		sCur = aLanguages[ n ];
409 
410         ByteString sFilepath;
411         if( bISO )	sFilepath = GetOutpath( rPathX , sCur , rPathY );
412         else        sFilepath = rPathX;
413         if( bCreateDir ) MakeDir( sFilepath );
414 
415         XMLFile* file = new XMLFile( *xmlfile );
416         sFilepath.Append( sHelpFile );
417 		hasNoError = MergeSingleFile( file , aMergeDataFile , sCur , sFilepath );
418 		delete file;
419 
420 		if( !hasNoError ) return false;			// Stop on error
421      }
422 
423     if( !sUsedTempFile.EqualsIgnoreCaseAscii( "" ) )
424 	{
425         DirEntry aTempFile( sUsedTempFile );
426         aTempFile.Kill();
427     }
428 	delete xmlfile;
429 	return hasNoError;
430 }
431 
MergeSingleFile(XMLFile * file,MergeDataFile & aMergeDataFile,const ByteString & sLanguage,ByteString sPath)432 bool HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile& aMergeDataFile , const ByteString& sLanguage ,
433 								  ByteString sPath )
434 {
435 	file->Extract();
436 
437    	XMLHashMap*   aXMLStrHM     = file->GetStrings();
438 	LangHashMap*  aLangHM;
439 	static	ResData	pResData( "","","");
440 	pResData.sResTyp   = "help";
441 
442     ByteString sTmp             = Export::sLanguages;
443 
444     sTmp.EraseLeadingAndTrailingChars();
445 
446     for(XMLHashMap::iterator pos=aXMLStrHM->begin();pos!=aXMLStrHM->end();++pos)    // Merge every l10n related string
447     {
448 
449         aLangHM             = pos->second;
450         //printf("*********************DUMPING HASHMAP***************************************");
451 		//Dump( aXMLStrHM );
452 		//printf("DBG: sHelpFile = %s\n",sHelpFile.GetBuffer() );
453 
454         pResData.sGId      =  pos->first;
455 		pResData.sFilename  =  sHelpFile;
456 
457         ProcessHelp( aLangHM , sLanguage, &pResData , aMergeDataFile );
458      }
459 
460 
461     // Init temp and target file
462 	ByteString sTempFile;
463 	ByteString sTargetFile( sPath );
464     ByteString sTempFileCopy;
465 
466 	static const ByteString INPATH = Export::GetEnv( "INPATH" );
467 	Export::getRandomName( sPath , sTempFile , INPATH );
468   	Export::getRandomName( sPath , sTempFileCopy , INPATH );
469 	// Write in the temp file
470 	bool hasNoError = file->Write ( sTempFile );
471 	if( !hasNoError )
472     {
473         cerr << "ERROR: file->Write failed\n";
474         return false;
475     }
476 
477     DirEntry aTmp( sTempFile );
478     DirEntry aTmp2( sTempFileCopy );
479     DirEntry aTar( sTargetFile );
480 
481     if( !Export::CopyFile( sTempFile , sTempFileCopy ) )
482     {
483 #if defined(UNX) || defined(OS2)
484         sleep( 3 );
485 #else
486         Sleep( 3 );
487 #endif
488         if( !Export::CopyFile( sTempFile , sTempFileCopy ) )
489         {
490             cerr << "ERROR: Can not copy file from " << sTempFile.GetBuffer() << " to " << sTempFileCopy.GetBuffer() << "\n";
491             return false;
492         }
493     }
494     //remove( sTargetFile.GetBuffer() );
495 
496     FileStat aFSTest( aTar );
497     if( aFSTest.GetSize() < 1 )
498     {
499         remove( sTargetFile.GetBuffer() );
500     }
501     int rc;
502 #if defined(UNX) || defined(OS2)
503     rc = rename( sTempFile.GetBuffer() , sTargetFile.GetBuffer() );
504 #else
505     rc = MoveFileEx( sTempFile.GetBuffer() , sTargetFile.GetBuffer(), MOVEFILE_REPLACE_EXISTING );
506 #endif
507     FileStat aFS( aTar );
508 
509     //cout << "mv " << sTempFile.GetBuffer() << " " << sTargetFile.GetBuffer() << "\n";
510     //cout << "rc -> " << rc << " filesize -> " << aFS.GetSize() << "\n";
511 // Windows rename returns -1 if the file already exits
512 //#ifdef UNX
513     if( rc < 0 || aFS.GetSize() < 1 )
514 //#else
515 //    if( aFS.GetSize() < 1 )
516 //#endif
517     {
518 #if defined(UNX) || defined(OS2)
519         sleep( 3 );
520 #else
521         Sleep( 3 );
522 #endif
523         aFSTest.Update( aTar );
524         if( aFSTest.GetSize() < 1 )
525         {
526             remove( sTargetFile.GetBuffer() );
527         }
528 #if defined(UNX) || defined(OS2)
529         rc = rename( sTempFileCopy.GetBuffer() , sTargetFile.GetBuffer() );
530 #else
531         rc = MoveFileEx( sTempFileCopy.GetBuffer() , sTargetFile.GetBuffer() , MOVEFILE_REPLACE_EXISTING );
532 #endif
533         aFS.Update( aTar );
534 
535         //cout << "mv2 " << sTempFileCopy.GetBuffer() << " " << sTargetFile.GetBuffer() << "\n";
536         //cout << "rc -> " << rc << " filesize -> " << aFS.GetSize() << "\n";
537 
538 // Windows rename returns -1 if the file already exits
539 //#ifdef WNT
540 //        if( aFS.GetSize() < 1 )
541 //#else
542         if( rc < 0 || aFS.GetSize() < 1 )
543 //#endif
544         {
545             cerr << "ERROR: helpex Can't rename file " << sTempFileCopy.GetBuffer() << " to " << sTargetFile.GetBuffer() << " rename rc=" << rc << " filesize=" << aFS.GetSize() << "\n";
546             aTmp.Kill();
547             aTmp2.Kill();
548             if( aFS.GetSize() < 1 )
549                 aTar.Kill();
550             return false;
551         }
552     }
553     aTmp.Kill();
554     aTmp2.Kill();
555 
556 	return true;
557 }
558 
GetOutpath(const ByteString & rPathX,const ByteString & sCur,const ByteString & rPathY)559 ByteString HelpParser::GetOutpath( const ByteString& rPathX , const ByteString& sCur , const ByteString& rPathY ){
560     ByteString testpath = rPathX;
561     static const ByteString sDelimiter( DirEntry::GetAccessDelimiter(), RTL_TEXTENCODING_ASCII_US );
562     testpath.EraseTrailingChars( '/' );
563     testpath.EraseTrailingChars( '\\' );
564     testpath += sDelimiter;
565     testpath += sCur;
566     testpath += sDelimiter;
567     ByteString sRelativePath( rPathY );
568     sRelativePath.EraseLeadingChars( '/' );
569     sRelativePath.EraseLeadingChars( '\\' );
570     testpath += sRelativePath;
571 	testpath += sDelimiter;
572     return testpath;
573 }
MakeDir(const ByteString & sPath)574 void HelpParser::MakeDir( const ByteString& sPath ){
575     ByteString sTPath( sPath );
576     ByteString sDelimiter( DirEntry::GetAccessDelimiter(), RTL_TEXTENCODING_ASCII_US );
577     sTPath.SearchAndReplaceAll( sDelimiter , '/' );
578     sal_uInt16 cnt = sTPath.GetTokenCount( '/' );
579     ByteString sCreateDir;
580     for( sal_uInt16 i = 0 ; i < cnt ; i++ )
581     {
582         sCreateDir += sTPath.GetToken( i , '/' );
583         sCreateDir += sDelimiter;
584 #ifdef WNT
585         _mkdir( sCreateDir.GetBuffer() );
586 #else
587         mkdir( sCreateDir.GetBuffer() , S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
588 #endif
589     }
590 }
591 
592 
593 /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
ProcessHelp(LangHashMap * aLangHM,const ByteString & sCur,ResData * pResData,MergeDataFile & aMergeDataFile)594 void HelpParser::ProcessHelp( LangHashMap* aLangHM , const ByteString& sCur , ResData *pResData , MergeDataFile& aMergeDataFile ){
595 
596 	XMLElement*   pXMLElement = NULL;
597    	PFormEntrys   *pEntrys    = NULL;
598     XMLData       *data       = NULL;
599     XMLParentNode *parent     = NULL;
600 
601     String        sNewdata;
602 	ByteString sLId;
603     ByteString sGId;
604 
605     pEntrys = NULL;
606 
607 #ifdef MERGE_SOURCE_LANGUAGES
608     if( true ){                  // Merge en-US!
609 #else
610     if( !sCur.EqualsIgnoreCaseAscii("en-US") ){
611 #endif
612         pXMLElement = (*aLangHM)[ "en-US" ];
613         if( pXMLElement == NULL )
614 		{
615             printf("Error: Can't find en-US entry\n");
616         }
617         if( pXMLElement != NULL )
618 		{
619             parent  = pXMLElement->GetParent();
620             sLId    = pXMLElement->GetOldref();
621             pResData->sId     =  sLId;
622 
623             pEntrys = aMergeDataFile.GetPFormEntrys( pResData );
624             if( pEntrys != NULL)
625 			{
626                 ByteString sNewText;
627 		        pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur , true );
628 		        sNewdata = String(  sNewText , RTL_TEXTENCODING_UTF8 );
629                 if ( sNewdata.Len())
630 				{
631                     if( pXMLElement != NULL )
632 					{
633 				        data   = new XMLData( sNewdata , NULL , true ); // Add new one
634                         pXMLElement->RemoveAndDeleteAllChilds();
635 						pXMLElement->AddChild( data );
636                         aLangHM->erase( sCur );
637 				    }
638 			    }
639             }else if( pResData == NULL ){fprintf(stdout,"Can't find GID=%s LID=%s TYP=%s\n",pResData->sGId.GetBuffer(),pResData->sId.GetBuffer(),pResData->sResTyp.GetBuffer());}
640             pXMLElement->ChangeLanguageTag( String( sCur , RTL_TEXTENCODING_ASCII_US) );
641         }
642 
643     }
644 }
645 /* Process() Method merges */
646 void HelpParser::Process( LangHashMap* aLangHM , const ByteString& sCur , ResData *pResData , MergeDataFile& aMergeDataFile ){
647 
648 	XMLElement*   pXMLElement = NULL;
649    	PFormEntrys   *pEntrys    = NULL;
650     XMLData       *data       = NULL;
651     XMLParentNode *parent     = NULL;
652     XMLDefault    *xmldefault = NULL;
653 
654 	short		  curLang     = 0;
655     String        sNewdata;
656     bool          isFallback  = false;
657 	ByteString sLId;
658     ByteString sGId;
659 
660     pEntrys = NULL;
661 
662 #ifdef MERGE_SOURCE_LANGUAGES
663     if( true ){                  // Merge en-US!
664 #else
665     if( !sCur.EqualsIgnoreCaseAscii("en-US") ){
666 #endif
667         pXMLElement = (*aLangHM)[ sCur ];
668         if( pXMLElement == NULL )
669 		{
670             FillInFallbacks( *aLangHM , sCur );
671             pXMLElement =   ( *aLangHM )[ sCur ];
672             isFallback = true;
673         }
674         if( pXMLElement != NULL )
675 		{
676             parent  = pXMLElement->GetParent();
677             sLId    = pXMLElement->GetOldref();
678             pResData->sId     =  sLId;
679 
680             pEntrys = aMergeDataFile.GetPFormEntrys( pResData );
681             if( pEntrys != NULL)
682 			{
683                 ByteString sNewText;
684 		        pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur , true );
685 		        sNewdata = String(  sNewText , RTL_TEXTENCODING_UTF8 );
686                 if ( sNewdata.Len())
687 				{
688 			        printf("Entries found\n");
689                     if( pXMLElement != NULL )
690 					{
691 				        data   = new XMLData( sNewdata , NULL , true ); // Add new one
692 					    if( pXMLElement->ToOUString().compareTo( OUString(data->GetData()) ) != 0 )
693 						{
694 					        pXMLElement->RemoveAndDeleteAllChilds();
695 						    pXMLElement->AddChild( data );
696                         }
697                         if( isFallback )
698 						{
699 					        xmldefault = new XMLDefault( String::CreateFromAscii("\n") , NULL );
700                             int pos = parent->GetPosition( pXMLElement->GetId() );
701                             if( pos != -1 ){
702 						        parent->AddChild(xmldefault , pos+1 );
703                                 parent->AddChild(pXMLElement , pos+2 );
704                             }
705 							else fprintf(stdout,"ERROR: Can't find reference Element of id %s language %d\n",pXMLElement->GetId().GetBuffer(),curLang);
706                         }
707 
708                         aLangHM->erase( sCur );
709 				    }
710 			    }
711 			    delete pResData;
712             }else if( pResData == NULL ){fprintf(stdout,"Can't find GID=%s LID=%s TYP=%s\n",pResData->sGId.GetBuffer(),pResData->sId.GetBuffer(),pResData->sResTyp.GetBuffer());}
713         }
714 
715     }
716 }
717 
718