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: export.hxx,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 ************************************************************************/
34 #ifndef TRANSEX_DIRECTORY_HXX
35 #define TRANSEX_DIRECTORY_HXX
36 #include <transex3/directory.hxx>
40 // #define MERGE_SOURCE_LANGUAGES <- To merge en-US and de resource
42 #include <tools/string.hxx>
43 #include <tools/list.hxx>
44 #include <tools/stream.hxx>
45 #include <tools/fsys.hxx>
46 #include <osl/file.hxx>
49 #include <hash_map> /* std::hashmap*/
50 #include <iterator> /* std::iterator*/
51 #include <set> /* std::set*/
52 #include <vector> /* std::vector*/
55 #define NO_TRANSLATE_ISO "x-no-translate"
57 #define JAPANESE_ISO "ja"
61 BOOL
operator()(const char* s1
, const char* s2
) const{
62 return strcmp(s1
,s2
)==0;
66 struct equalByteString
{
67 bool operator()( const ByteString
& rKey1
, const ByteString
& rKey2
) const {
68 return rKey1
.CompareTo( rKey2
)==COMPARE_EQUAL
;
71 struct lessByteString
{
72 bool operator()( const ByteString
& rKey1
, const ByteString
& rKey2
) const {
73 return rKey1
.CompareTo( rKey2
)==COMPARE_LESS
;
77 struct hashByteString
{
78 size_t operator()( const ByteString
& rName
) const{
79 std::hash
< const char* > myHash
;
80 return myHash( rName
.GetBuffer() );
86 typedef std::set
<ByteString
, lessByteString
> ByteStringSet
;
88 typedef std::hash_map
<ByteString
, ByteString
, hashByteString
,equalByteString
>
91 typedef std::hash_map
<ByteString
, bool , hashByteString
,equalByteString
>
92 ByteStringBoolHashMap
;
94 typedef std::hash_map
<ByteString
, PFormEntrys
* , hashByteString
,equalByteString
>
97 typedef std::hash_map
<ByteString
, MergeData
* , hashByteString
,equalByteString
>
100 #define SOURCE_LANGUAGE ByteString("en-US")
101 #define LIST_REFID "LIST_REFID"
103 typedef ByteStringHashMap ExportListEntry
;
105 DECLARE_LIST( ExportListBase
, ExportListEntry
* )
111 class ExportList
: public ExportListBase
114 ULONG nSourceLanguageListEntryCount
;
117 ExportList() : ExportListBase() { nSourceLanguageListEntryCount
= 0; }
118 ULONG
GetSourceLanguageListEntryCount() { return nSourceLanguageListEntryCount
; }
119 void NewSourceLanguageListEntry() { nSourceLanguageListEntryCount
++; }
122 #define REFID_NONE 0xFFFF
128 /******************************************************************************
129 * Purpose: holds mandatory data to export a single res (used with ResStack)
130 ******************************************************************************/
132 #define ID_LEVEL_NULL 0x0000
133 #define ID_LEVEL_AUTOID 0x0001
134 #define ID_LEVEL_TEXT 0x0002
135 #define ID_LEVEL_FIELDNAME 0x0003
136 #define ID_LEVEL_ACCESSPATH 0x0004
137 #define ID_LEVEL_IDENTIFIER 0x0005
138 #define ID_LEVEL_LISTINDEX 0x0006
144 BOOL
SetId( const ByteString
&rId
, USHORT nLevel
);
164 ByteString sFilename
;
166 ByteStringHashMap sText
;
169 ByteStringHashMap sHelpText
;
170 USHORT nHelpTextRefId
;
172 ByteStringHashMap sQuickHelpText
;
173 USHORT nQuickHelpTextRefId
;
175 ByteStringHashMap sTitle
;
179 ByteStringHashMap aFallbackData
;
180 ByteStringHashMap aMergedLanguages
;
182 ExportList
*pStringList
;
183 ExportList
*pUIEntries
;
184 ExportList
*pItemList
;
185 ExportList
*pFilterList
;
186 ExportList
*pPairedList
;
191 void addFallbackData( ByteString
& sId
, const ByteString
& sText
);
192 bool getFallbackData( ByteString
& sId
, ByteString
& sText
);
194 void addMergedLanguage( ByteString
& sLang
);
195 bool isMerged( ByteString
& sLang
);
196 ResData( const ByteString
&rPF
, const ByteString
&rGId
)
200 nIdLevel( ID_LEVEL_NULL
),
202 bChildWithText( FALSE
),
205 bQuickHelpText( FALSE
),
208 bRestMerged( FALSE
),
210 nTextRefId( REFID_NONE
),
211 nHelpTextRefId( REFID_NONE
),
212 nQuickHelpTextRefId( REFID_NONE
),
213 nTitleRefId( REFID_NONE
),
222 sGId
.EraseAllChars( '\r' );
223 sPForm
.EraseAllChars( '\r' );
225 ResData( const ByteString
&rPF
, const ByteString
&rGId
, const ByteString
&rFilename
)
228 nIdLevel( ID_LEVEL_NULL
),
230 bChildWithText( FALSE
),
233 bQuickHelpText( FALSE
),
236 bRestMerged( FALSE
),
238 sFilename( rFilename
),
239 nTextRefId( REFID_NONE
),
240 nHelpTextRefId( REFID_NONE
),
241 nQuickHelpTextRefId( REFID_NONE
),
242 nTitleRefId( REFID_NONE
),
252 sGId
.EraseAllChars( '\r' );
253 sPForm
.EraseAllChars( '\r' );
264 /******************************************************************************
265 * Purpose: syntax check and export of *.src, called from lexer
266 ******************************************************************************/
268 #define LIST_NON 0x0000
269 #define LIST_STRING 0x0001
270 #define LIST_FILTER 0x0002
271 #define LIST_ITEM 0x0004
272 #define LIST_PAIRED 0x0005
273 #define LIST_UIENTRIES 0x0008
274 #define STRING_TYP_TEXT 0x0010
275 #define STRING_TYP_HELPTEXT 0x0020
276 #define STRING_TYP_QUICKHELPTEXT 0x0040
277 #define STRING_TYP_TITLE 0x0080
279 #define MERGE_MODE_NORMAL 0x0000
280 #define MERGE_MODE_LIST 0x0001
282 DECLARE_LIST( ResStack
, ResData
* )
284 class WordTransformer
;
290 WordTransformer
*pWordTransformer
;
292 CharSet aCharSet
; // used charset in src
294 SvFileStream aOutput
;
296 ResStack aResStack
; // stack for parsing recursive
298 ByteString sActPForm
; // hold cur. system
300 BOOL bDefine
; // cur. res. in a define?
301 BOOL bNextMustBeDefineEOL
; // define but no \ at lineend
302 ULONG nLevel
; // res. recursiv? how deep?
303 USHORT nList
; // cur. res. is String- or FilterList
304 ByteString nListLang
;
312 ByteString sMergeSrc
;
313 ByteString sLastListLine
;
314 BOOL bError
; // any errors while export?
316 BOOL bDontWriteOutput
;
317 ByteString sLastTextTyp
;
318 static bool isInitialized
;
319 ByteString sFilename
;
323 ParserQueue
* pParseQueue
; // public ?
324 static ByteString sLanguages
; // public ?
325 static ByteString sForcedLanguages
; // public ?
328 static bool skipProject( ByteString sPrj
) ;
329 static ByteString sIsoCode99
;
330 static void InitLanguages( bool bMergeMode
= false );
331 static void InitForcedLanguages( bool bMergeMode
= false );
332 static std::vector
<ByteString
> GetLanguages();
333 static std::vector
<ByteString
> GetForcedLanguages();
335 static void SetLanguages( std::vector
<ByteString
> val
);
336 static void RemoveUTF8ByteOrderMarker( ByteString
&rString
);
337 static bool hasUTF8ByteOrderMarker( const ByteString
&rString
);
338 static void RemoveUTF8ByteOrderMarkerFromFile( const ByteString
&rFilename
);
339 static bool fileHasUTF8ByteOrderMarker( const ByteString
&rString
);
340 static ByteString
GetIsoLangByIndex( USHORT nIndex
);
341 static void QuotHTML( ByteString
&rString
);
342 static bool CopyFile( const ByteString
& source
, const ByteString
& dest
);
344 static void QuotHTMLXRM( ByteString
&rString
);
345 static void UnquotHTML( ByteString
&rString
);
347 static const char* GetEnv( const char *pVar
);
348 static int getCurrentDirectory( rtl::OUString
& base_fqurl
, rtl::OUString
& base
);
350 static bool isSourceLanguage( const ByteString
&sLanguage
);
351 static bool isAllowed( const ByteString
&sLanguage
);
352 //static bool isMergingGermanAllowed( const ByteString& rPrj );
354 static bool LanguageAllowed( const ByteString
&nLanguage
);
355 static void Languages( std::vector
<ByteString
>::const_iterator
& begin
, std::vector
<ByteString
>::const_iterator
& end
);
356 static void getRandomName( const ByteString
& sPrefix
, ByteString
& sRandStr
, const ByteString
& sPostfix
);
357 static void getRandomName( ByteString
& sRandStr
);
359 static void replaceEncoding( ByteString
& rString
);
361 static ByteString
GetFallbackLanguage( const ByteString nLanguage
);
362 static void FillInFallbacks( ResData
*pResData
);
363 static void FillInListFallbacks( ExportList
*pList
, const ByteString
&nSource
, const ByteString
&nFallback
);
364 static ByteString
GetTimeStamp();
365 static BOOL
ConvertLineEnds( ByteString sSource
, ByteString sDestination
);
366 static ByteString
GetNativeFile( ByteString sSource
);
367 static DirEntry
GetTempFile();
369 static void DumpExportList( ByteString
& sListName
, ExportList
& aList
);
370 static ByteString
DumpMap( ByteString
& sMapName
, ByteStringHashMap
& aMap
);
373 static std::vector
<ByteString
> aLanguages
;
374 static std::vector
<ByteString
> aForcedLanguages
;
376 BOOL
ListExists( ResData
*pResData
, USHORT nLst
);
378 BOOL
WriteData( ResData
*pResData
, BOOL bCreateNew
= FALSE
);// called befor dest. cur ResData
379 BOOL
WriteExportList( ResData
*pResData
, ExportList
*pExportList
,
380 const ByteString
&rTyp
, BOOL bCreateNew
= FALSE
);
382 ByteString
MergePairedList( ByteString
& sLine
, ByteString
& sText
);
384 ByteString
FullId(); // creates cur. GID
386 bool PairedListFallback( ByteString
& sText
, ResData
& aResData
);
388 ByteString
GetPairedListID ( const ByteString
& sText
);
389 ByteString
GetPairedListString ( const ByteString
& sText
);
390 ByteString
StripList ( const ByteString
& sText
);
392 void UnmergeUTF8( ByteString
& sOrig
);
393 void InsertListEntry( const ByteString
&rText
, const ByteString
&rLine
);
394 void CleanValue( ByteString
&rValue
);
395 ByteString
GetText( const ByteString
&rSource
, int nToken
);
397 BOOL
PrepareTextToMerge( ByteString
&rText
, USHORT nTyp
,
398 ByteString
&nLangIndex
, ResData
*pResData
);
400 void MergeRest( ResData
*pResData
, USHORT nMode
= MERGE_MODE_NORMAL
);
401 void ConvertMergeContent( ByteString
&rText
);
403 void WriteToMerged( const ByteString
&rText
, bool bSDFContent
);
404 void SetChildWithText();
406 void CutComment( ByteString
&rText
);
409 Export( const ByteString
&rOutput
, BOOL bWrite
,
410 const ByteString
&rPrj
, const ByteString
&rPrjRoot
, const ByteString
& rFile
);
411 Export( const ByteString
&rOutput
, BOOL bWrite
,
412 const ByteString
&rPrj
, const ByteString
&rPrjRoot
,
413 const ByteString
&rMergeSource
, const ByteString
& rFile
);
417 int Execute( int nToken
, const char * pToken
); // called from lexer
418 void SetError() { bError
= TRUE
; }
419 BOOL
GetError() { return bError
; }
427 /******************************************************************************
428 * Purpose: holds information of data to merge (one pform)
429 ******************************************************************************/
431 class PFormEntrys
: public ByteString
433 friend class MergeDataFile
;
435 ByteString sHelpText
; // empty string
436 ByteStringHashMap sText
;
437 ByteStringBoolHashMap bTextFirst
;
438 ByteStringHashMap sQuickHelpText
;
439 ByteStringBoolHashMap bQuickHelpTextFirst
;
440 ByteStringHashMap sTitle
;
441 ByteStringBoolHashMap bTitleFirst
;
444 PFormEntrys( const ByteString
&rPForm
) : ByteString( rPForm
) {};
447 const ByteString
&nId
,
448 const ByteString
&rText
,
449 const ByteString
&rQuickHelpText
,
450 const ByteString
&rTitle
454 sText
[ nId
] = rText
;
455 bTextFirst
[ nId
] = true;
456 sQuickHelpText
[ nId
] = rQuickHelpText
;
457 bQuickHelpTextFirst
[ nId
] = true;
458 sTitle
[ nId
] = rTitle
;
459 bTitleFirst
[ nId
] = true;
461 BOOL
GetText( ByteString
&rReturn
, USHORT nTyp
, const ByteString
&nLangIndex
, BOOL bDel
= FALSE
);
468 /******************************************************************************
469 * Purpose: holds information of data to merge (one ressource)
470 ******************************************************************************/
476 friend class MergeDataFile
;
481 ByteString sFilename
;
482 PFormEntrysHashMap aMap
;
484 MergeData( const ByteString
&rTyp
, const ByteString
&rGID
, const ByteString
&rLID
, const ByteString
&rFilename
)
485 : sTyp( rTyp
), sGID( rGID
), sLID( rLID
) , sFilename( rFilename
) {};
487 PFormEntrys
* InsertEntry( const ByteString
&rPForm
);
488 PFormEntrys
* GetPFormEntrys( ResData
*pResData
);
490 void Insert( const ByteString
& rPFO
, PFormEntrys
* pfEntrys
);
491 PFormEntrys
* GetPFObject( const ByteString
& rPFO
);
494 BOOL
operator==( ResData
*pData
);
498 // class MergeDataFile
501 /******************************************************************************
502 * Purpose: holds information of data to merge
503 ******************************************************************************/
509 ByteString sErrorLog
;
510 SvFileStream aErrLog
;
511 ByteStringSet aLanguageSet
;
512 MergeDataHashMap aMap
;
513 ByteStringHashMap aLanguageMap
;
514 std::vector
<ByteString
> aLanguageList
;
515 ByteStringHashMap aFilenames
;
519 MergeDataFile( const ByteString
&rFileName
, const ByteString
& rFile
, BOOL bErrLog
, CharSet aCharSet
, bool bCaseSensitive
= false );
520 // MergeDataFile( const ByteString &rFileName, const ByteString& rFile , BOOL bErrLog, CharSet aCharSet
525 std::vector
<ByteString
> GetLanguages();
526 MergeData
*GetMergeData( ResData
*pResData
, bool bCaseSensitve
= false );
528 PFormEntrys
*GetPFormEntrys( ResData
*pResData
);
529 PFormEntrys
*GetPFormEntrysCaseSensitive( ResData
*pResData
);
531 void InsertEntry( const ByteString
&rTYP
, const ByteString
&rGID
, const ByteString
&rLID
,
532 const ByteString
&rPFO
,
533 const ByteString
&nLang
, const ByteString
&rTEXT
,
534 const ByteString
&rQHTEXT
, const ByteString
&rTITLE
,
535 const ByteString
&sFilename
, bool bCaseSensitive
537 static USHORT
GetLangIndex( USHORT nId
);
538 static ByteString
CreateKey( const ByteString
& rTYP
, const ByteString
& rGID
, const ByteString
& rLID
, const ByteString
& rFilename
, bool bCaseSensitive
= false );
541 // void WriteErrorLog( const ByteString &rFileName );
542 void WriteError( const ByteString
&rLine
);
549 QueueEntry( int nTypVal
, ByteString sLineVal
): nTyp( nTypVal
) , sLine( sLineVal
){};
558 ParserQueue( Export
& aExportObj
);
561 inline void Push( const QueueEntry
& aEntry
);
562 bool bCurrentIsM
; // public ?
563 bool bNextIsM
; // public ?
564 bool bLastWasM
; // public ?
565 bool bMflag
; // public ?
570 std::queue
<QueueEntry
>* aQueueNext
;
572 std::queue
<QueueEntry
>* aQueueCur
;
574 std::queue
<QueueEntry
>* aQref
;
580 inline void Pop( std::queue
<QueueEntry
>& aQueue
);