Version 3.4.2.3, tag libreoffice-3.4.2.3 (3.4.2-rc3)
[LibreOffice.git] / l10ntools / inc / export.hxx
blob999eac78ec0aa0893393c7db81ab0bb97eaec2da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #ifndef _EXPORT_HXX
30 #define _EXPORT_HXX
32 #ifndef L10NTOOLS_DIRECTORY_HXX
33 #define L10NTOOLS_DIRECTORY_HXX
34 #include <l10ntools/directory.hxx>
35 #endif
37 #include <tools/string.hxx>
38 #include <tools/stream.hxx>
39 #include <tools/fsys.hxx>
40 #include <osl/file.hxx>
41 #include <osl/file.h>
43 #include <boost/unordered_map.hpp>
44 #include <iterator> /* std::iterator*/
45 #include <set> /* std::set*/
46 #include <vector> /* std::vector*/
47 #include <queue>
48 #include <string>
50 #include <unistd.h>
51 #ifdef WNT
52 #include <direct.h>
53 #endif
55 #define NO_TRANSLATE_ISO "x-no-translate"
57 #define JAPANESE_ISO "ja"
60 struct eqstr{
61 sal_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 return rtl_str_hashCode_WithLength(rName.GetBuffer(), rName.Len() );
83 class PFormEntrys;
84 class MergeData;
85 typedef std::set<ByteString , lessByteString > ByteStringSet;
87 typedef boost::unordered_map<ByteString , ByteString , hashByteString,equalByteString>
88 ByteStringHashMap;
90 typedef boost::unordered_map<ByteString , bool , hashByteString,equalByteString>
91 ByteStringBoolHashMap;
93 typedef boost::unordered_map<ByteString , PFormEntrys* , hashByteString,equalByteString>
94 PFormEntrysHashMap;
96 typedef boost::unordered_map<ByteString , MergeData* , hashByteString,equalByteString>
97 MergeDataHashMap;
99 #define SOURCE_LANGUAGE ByteString("en-US")
100 #define LIST_REFID "LIST_REFID"
102 typedef ByteStringHashMap ExportListEntry;
103 typedef ::std::vector< ExportListEntry* > ExportListBase;
106 // class ExportList
109 class ExportList
111 private:
112 ExportListBase maList;
113 sal_uLong nSourceLanguageListEntryCount;
115 public:
116 ExportList() { nSourceLanguageListEntryCount = 0; }
117 sal_uLong GetSourceLanguageListEntryCount() { return nSourceLanguageListEntryCount; }
118 void NewSourceLanguageListEntry() { nSourceLanguageListEntryCount++; }
119 size_t size() const { return maList.size(); }
120 void push_back( ExportListEntry* item ) { maList.push_back( item ); }
122 ExportListEntry* operator [] ( size_t i )
124 return ( i < maList.size() ) ? maList[ i ] : NULL;
128 #define REFID_NONE 0xFFFF
131 // struct ResData
134 /******************************************************************************
135 * Purpose: holds mandatory data to export a single res (used with ResStack)
136 ******************************************************************************/
138 #define ID_LEVEL_NULL 0x0000
139 #define ID_LEVEL_AUTOID 0x0001
140 #define ID_LEVEL_TEXT 0x0002
141 #define ID_LEVEL_FIELDNAME 0x0003
142 #define ID_LEVEL_ACCESSPATH 0x0004
143 #define ID_LEVEL_IDENTIFIER 0x0005
144 #define ID_LEVEL_LISTINDEX 0x0006
146 class ResData
148 public:
149 ~ResData();
150 sal_Bool SetId( const ByteString &rId, sal_uInt16 nLevel );
152 sal_uInt16 nWidth;
153 sal_uInt16 nChildIndex;
154 sal_uInt16 nIdLevel;
155 sal_Bool bChild;
156 sal_Bool bChildWithText;
158 sal_Bool bText;
159 sal_Bool bHelpText;
160 sal_Bool bQuickHelpText;
161 sal_Bool bTitle;
162 sal_Bool bList;
164 sal_Bool bRestMerged;
166 ByteString sResTyp;
167 ByteString sId;
168 ByteString sGId;
169 ByteString sHelpId;
170 ByteString sFilename;
172 ByteStringHashMap sText;
173 sal_uInt16 nTextRefId;
175 ByteStringHashMap sHelpText;
176 sal_uInt16 nHelpTextRefId;
178 ByteStringHashMap sQuickHelpText;
179 sal_uInt16 nQuickHelpTextRefId;
181 ByteStringHashMap sTitle;
182 sal_uInt16 nTitleRefId;
184 ByteString sTextTyp;
185 ByteStringHashMap aFallbackData;
186 ByteStringHashMap aMergedLanguages;
188 ExportList *pStringList;
189 ExportList *pUIEntries;
190 ExportList *pItemList;
191 ExportList *pFilterList;
192 ExportList *pPairedList;
194 ByteString sPForm;
196 void Dump();
197 void addFallbackData( ByteString& sId , const ByteString& sText );
198 bool getFallbackData( ByteString& sId , ByteString& sText);
200 void addMergedLanguage( ByteString& sLang );
201 bool isMerged( ByteString& sLang );
202 ResData( const ByteString &rPF, const ByteString &rGId )
204 nWidth( 0 ),
205 nChildIndex( 0 ),
206 nIdLevel( ID_LEVEL_NULL ),
207 bChild( sal_False ),
208 bChildWithText( sal_False ),
209 bText( sal_False ),
210 bHelpText( sal_False ),
211 bQuickHelpText( sal_False ),
212 bTitle( sal_False ),
213 bList( sal_False ),
214 bRestMerged( sal_False ),
215 sGId( rGId ),
216 nTextRefId( REFID_NONE ),
217 nHelpTextRefId( REFID_NONE ),
218 nQuickHelpTextRefId( REFID_NONE ),
219 nTitleRefId( REFID_NONE ),
220 sTextTyp( "Text" ),
221 pStringList( NULL ),
222 pUIEntries( NULL ),
223 pItemList( NULL ),
224 pFilterList( NULL ),
225 pPairedList( NULL ),
226 sPForm( rPF )
228 sGId.EraseAllChars( '\r' );
229 sPForm.EraseAllChars( '\r' );
231 ResData( const ByteString &rPF, const ByteString &rGId , const ByteString &rFilename )
233 nChildIndex( 0 ),
234 nIdLevel( ID_LEVEL_NULL ),
235 bChild( sal_False ),
236 bChildWithText( sal_False ),
237 bText( sal_False ),
238 bHelpText( sal_False ),
239 bQuickHelpText( sal_False ),
240 bTitle( sal_False ),
241 bList( sal_False ),
242 bRestMerged( sal_False ),
243 sGId( rGId ),
244 sFilename( rFilename ),
245 nTextRefId( REFID_NONE ),
246 nHelpTextRefId( REFID_NONE ),
247 nQuickHelpTextRefId( REFID_NONE ),
248 nTitleRefId( REFID_NONE ),
249 sTextTyp( "Text" ),
250 pStringList( NULL ),
251 pUIEntries( NULL ),
252 pItemList( NULL ),
253 pFilterList( NULL ),
254 pPairedList( NULL ),
255 sPForm( rPF )
258 sGId.EraseAllChars( '\r' );
259 sPForm.EraseAllChars( '\r' );
267 // class Export
270 /******************************************************************************
271 * Purpose: syntax check and export of *.src, called from lexer
272 ******************************************************************************/
274 #define LIST_NON 0x0000
275 #define LIST_STRING 0x0001
276 #define LIST_FILTER 0x0002
277 #define LIST_ITEM 0x0004
278 #define LIST_PAIRED 0x0005
279 #define LIST_UIENTRIES 0x0008
280 #define STRING_TYP_TEXT 0x0010
281 #define STRING_TYP_HELPTEXT 0x0020
282 #define STRING_TYP_QUICKHELPTEXT 0x0040
283 #define STRING_TYP_TITLE 0x0080
285 #define MERGE_MODE_NORMAL 0x0000
286 #define MERGE_MODE_LIST 0x0001
288 typedef ::std::vector< ResData* > ResStack;
289 // forwards
290 class WordTransformer;
291 class ParserQueue;
293 class Export
295 private:
296 WordTransformer *pWordTransformer;
298 CharSet aCharSet; // used charset in src
300 SvFileStream aOutput;
302 ResStack aResStack; // stack for parsing recursive
304 ByteString sActPForm; // hold cur. system
306 sal_Bool bDefine; // cur. res. in a define?
307 sal_Bool bNextMustBeDefineEOL; // define but no \ at lineend
308 sal_uLong nLevel; // res. recursiv? how deep?
309 sal_uInt16 nList; // cur. res. is String- or FilterList
310 ByteString nListLang;
311 sal_uLong nListIndex;
312 sal_uLong nListLevel;
313 bool bSkipFile;
314 ByteString sProject;
315 ByteString sRoot;
316 sal_Bool bEnableExport;
317 sal_Bool bMergeMode;
318 ByteString sMergeSrc;
319 ByteString sLastListLine;
320 sal_Bool bError; // any errors while export?
321 sal_Bool bReadOver;
322 sal_Bool bDontWriteOutput;
323 ByteString sLastTextTyp;
324 static bool isInitialized;
325 ByteString sFilename;
328 public:
329 ParserQueue* pParseQueue; // public ?
330 static ByteString sLanguages; // public ?
331 static ByteString sForcedLanguages; // public ?
334 static bool skipProject( ByteString sPrj ) ;
335 static void InitLanguages( bool bMergeMode = false );
336 static void InitForcedLanguages( bool bMergeMode = false );
337 static std::vector<ByteString> GetLanguages();
338 static std::vector<ByteString> GetForcedLanguages();
340 static void SetLanguages( std::vector<ByteString> val );
341 static void RemoveUTF8ByteOrderMarker( ByteString &rString );
342 static bool hasUTF8ByteOrderMarker( const ByteString &rString );
343 static void RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename );
344 static bool fileHasUTF8ByteOrderMarker( const ByteString &rString );
345 static ByteString GetIsoLangByIndex( sal_uInt16 nIndex );
346 static void QuotHTML( ByteString &rString );
347 static bool CopyFile( const ByteString& source , const ByteString& dest );
349 static void QuotHTMLXRM( ByteString &rString );
350 static void UnquotHTML( ByteString &rString );
352 static const char* GetEnv( const char *pVar );
353 static int getCurrentDirectory( rtl::OUString& base_fqurl , rtl::OUString& base );
355 static bool isSourceLanguage( const ByteString &sLanguage );
356 static bool isAllowed( const ByteString &sLanguage );
358 static bool LanguageAllowed( const ByteString &nLanguage );
359 static void Languages( std::vector<ByteString>::const_iterator& begin , std::vector<ByteString>::const_iterator& end );
360 static void getRandomName( const ByteString& sPrefix , ByteString& sRandStr , const ByteString& sPostfix );
361 static void getRandomName( ByteString& sRandStr );
362 static void getCurrentDir( std::string& dir );
364 static void replaceEncoding( ByteString& rString );
366 static ByteString GetFallbackLanguage( const ByteString nLanguage );
367 static void FillInFallbacks( ResData *pResData );
368 static void FillInListFallbacks( ExportList *pList, const ByteString &nSource, const ByteString &nFallback );
369 static ByteString GetTimeStamp();
370 static sal_Bool ConvertLineEnds( ByteString sSource, ByteString sDestination );
371 static ByteString GetNativeFile( ByteString sSource );
372 static DirEntry GetTempFile();
374 static void DumpExportList( ByteString& sListName , ExportList& aList );
375 static ByteString DumpMap( ByteString& sMapName , ByteStringHashMap& aMap );
377 private:
378 static std::vector<ByteString> aLanguages;
379 static std::vector<ByteString> aForcedLanguages;
381 sal_Bool ListExists( ResData *pResData, sal_uInt16 nLst );
383 sal_Bool WriteData( ResData *pResData, sal_Bool bCreateNew = sal_False );// called befor dest. cur ResData
384 sal_Bool WriteExportList( ResData *pResData, ExportList *pExportList,
385 const ByteString &rTyp, sal_Bool bCreateNew = sal_False );
387 ByteString MergePairedList( ByteString& sLine , ByteString& sText );
389 ByteString FullId(); // creates cur. GID
391 bool PairedListFallback( ByteString& sText , ResData& aResData );
393 ByteString GetPairedListID ( const ByteString& sText );
394 ByteString GetPairedListString ( const ByteString& sText );
395 ByteString StripList ( const ByteString& sText );
397 void UnmergeUTF8( ByteString& sOrig );
398 void InsertListEntry( const ByteString &rText, const ByteString &rLine );
399 void CleanValue( ByteString &rValue );
400 ByteString GetText( const ByteString &rSource, int nToken );
402 sal_Bool PrepareTextToMerge( ByteString &rText, sal_uInt16 nTyp,
403 ByteString &nLangIndex, ResData *pResData );
405 void MergeRest( ResData *pResData, sal_uInt16 nMode = MERGE_MODE_NORMAL );
406 void ConvertMergeContent( ByteString &rText );
408 void WriteToMerged( const ByteString &rText , bool bSDFContent );
409 void SetChildWithText();
411 void CutComment( ByteString &rText );
413 public:
414 Export( const ByteString &rOutput, sal_Bool bWrite,
415 const ByteString &rPrj, const ByteString &rPrjRoot , const ByteString& rFile );
416 Export( const ByteString &rOutput, sal_Bool bWrite,
417 const ByteString &rPrj, const ByteString &rPrjRoot,
418 const ByteString &rMergeSource , const ByteString& rFile );
419 ~Export();
421 void Init();
422 int Execute( int nToken, const char * pToken ); // called from lexer
423 void SetError() { bError = sal_True; }
424 sal_Bool GetError() { return bError; }
429 // class PFormEntrys
432 /******************************************************************************
433 * Purpose: holds information of data to merge (one pform)
434 ******************************************************************************/
436 class PFormEntrys : public ByteString
438 friend class MergeDataFile;
439 private:
440 ByteString sHelpText; // empty string
441 ByteStringHashMap sText;
442 ByteStringBoolHashMap bTextFirst;
443 ByteStringHashMap sQuickHelpText;
444 ByteStringBoolHashMap bQuickHelpTextFirst;
445 ByteStringHashMap sTitle;
446 ByteStringBoolHashMap bTitleFirst;
448 public:
449 PFormEntrys( const ByteString &rPForm ) : ByteString( rPForm ) {};
450 ByteString Dump();
451 void InsertEntry(
452 const ByteString &nId ,
453 const ByteString &rText,
454 const ByteString &rQuickHelpText,
455 const ByteString &rTitle
459 sText[ nId ] = rText;
460 bTextFirst[ nId ] = true;
461 sQuickHelpText[ nId ] = rQuickHelpText;
462 bQuickHelpTextFirst[ nId ] = true;
463 sTitle[ nId ] = rTitle;
464 bTitleFirst[ nId ] = true;
466 sal_Bool GetText( ByteString &rReturn, sal_uInt16 nTyp, const ByteString &nLangIndex, sal_Bool bDel = sal_False );
467 sal_Bool GetTransex3Text( ByteString &rReturn, sal_uInt16 nTyp, const ByteString &nLangIndex, sal_Bool bDel = sal_False );
472 // class MergeData
475 /******************************************************************************
476 * Purpose: holds information of data to merge (one ressource)
477 ******************************************************************************/
479 class MergeDataFile;
481 class MergeData
483 friend class MergeDataFile;
484 private:
485 ByteString sTyp;
486 ByteString sGID;
487 ByteString sLID;
488 ByteString sFilename;
489 PFormEntrysHashMap aMap;
490 public:
491 MergeData( const ByteString &rTyp, const ByteString &rGID, const ByteString &rLID , const ByteString &rFilename )
492 : sTyp( rTyp ), sGID( rGID ), sLID( rLID ) , sFilename( rFilename ) {};
493 ~MergeData();
494 PFormEntrys* InsertEntry( const ByteString &rPForm );
495 PFormEntrys* GetPFormEntrys( ResData *pResData );
497 void Insert( const ByteString& rPFO , PFormEntrys* pfEntrys );
498 PFormEntrys* GetPFObject( const ByteString& rPFO );
500 ByteString Dump();
501 sal_Bool operator==( ResData *pData );
505 // class MergeDataFile
508 /******************************************************************************
509 * Purpose: holds information of data to merge
510 ******************************************************************************/
512 class MergeDataFile
514 private:
515 sal_Bool bErrorLog;
516 ByteString sErrorLog;
517 SvFileStream aErrLog;
518 MergeDataHashMap aMap;
519 std::set<ByteString> aLanguageSet;
521 MergeData *GetMergeData( ResData *pResData , bool bCaseSensitve = false );
522 void InsertEntry( const ByteString &rTYP, const ByteString &rGID, const ByteString &rLID,
523 const ByteString &rPFO,
524 const ByteString &nLang, const ByteString &rTEXT,
525 const ByteString &rQHTEXT, const ByteString &rTITLE,
526 const ByteString &sFilename, bool bCaseSensitive
528 ByteString Dump();
529 void WriteError( const ByteString &rLine );
531 public:
532 MergeDataFile( const ByteString &rFileName, const ByteString& rFile , sal_Bool bErrLog, CharSet aCharSet, bool bCaseSensitive = false );
533 ~MergeDataFile();
536 std::vector<ByteString> GetLanguages();
538 PFormEntrys *GetPFormEntrys( ResData *pResData );
539 PFormEntrys *GetPFormEntrysCaseSensitive( ResData *pResData );
541 static ByteString CreateKey( const ByteString& rTYP , const ByteString& rGID , const ByteString& rLID , const ByteString& rFilename , bool bCaseSensitive = false );
545 class QueueEntry
547 public:
548 QueueEntry( int nTypVal , ByteString sLineVal ): nTyp( nTypVal ) , sLine( sLineVal ){};
549 int nTyp;
550 ByteString sLine;
553 class ParserQueue
555 public:
557 ParserQueue( Export& aExportObj );
558 ~ParserQueue();
560 inline void Push( const QueueEntry& aEntry );
561 bool bCurrentIsM; // public ?
562 bool bNextIsM; // public ?
563 bool bLastWasM; // public ?
564 bool bMflag; // public ?
566 void Close();
567 private:
568 // Future / Next
569 std::queue<QueueEntry>* aQueueNext;
570 // Current
571 std::queue<QueueEntry>* aQueueCur;
572 // Ref
573 std::queue<QueueEntry>* aQref;
575 Export& aExport;
576 bool bStart;
577 bool bStartNext;
579 inline void Pop( std::queue<QueueEntry>& aQueue );
582 #endif
584 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */