Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / l10ntools / inc / export.hxx
blobbf1809bfb54e32bd1b7b382e5ff9074c01bb2873
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 #include "sal/config.h"
34 #include <cstddef>
35 #include <fstream>
37 #ifndef L10NTOOLS_DIRECTORY_HXX
38 #define L10NTOOLS_DIRECTORY_HXX
39 #include <l10ntools/directory.hxx>
40 #endif
42 #include <osl/file.hxx>
43 #include <osl/file.h>
45 #include <boost/unordered_map.hpp>
46 #include <iterator> /* std::iterator*/
47 #include <set> /* std::set*/
48 #include <vector> /* std::vector*/
49 #include <queue>
50 #include <string>
52 #include <unistd.h>
53 #ifdef WNT
54 #include <direct.h>
55 #endif
57 #define NO_TRANSLATE_ISO "x-no-translate"
59 class PFormEntrys;
60 class MergeData;
62 typedef boost::unordered_map<rtl::OString, rtl::OString, rtl::OStringHash>
63 OStringHashMap;
65 typedef boost::unordered_map<rtl::OString, bool, rtl::OStringHash>
66 OStringBoolHashMap;
68 typedef boost::unordered_map<rtl::OString, PFormEntrys*, rtl::OStringHash>
69 PFormEntrysHashMap;
71 typedef boost::unordered_map<rtl::OString, MergeData*, rtl::OStringHash>
72 MergeDataHashMap;
74 #define SOURCE_LANGUAGE rtl::OString(RTL_CONSTASCII_STRINGPARAM("en-US"))
75 #define X_COMMENT rtl::OString(RTL_CONSTASCII_STRINGPARAM("x-comment"))
76 #define LIST_REFID "LIST_REFID"
78 typedef OStringHashMap ExportListEntry;
79 typedef ::std::vector< ExportListEntry* > ExportListBase;
82 // class ExportList
85 class ExportList
87 private:
88 ExportListBase maList;
89 std::size_t nSourceLanguageListEntryCount;
91 public:
92 ExportList() { nSourceLanguageListEntryCount = 0; }
93 std::size_t GetSourceLanguageListEntryCount() { return nSourceLanguageListEntryCount; }
94 void NewSourceLanguageListEntry() { nSourceLanguageListEntryCount++; }
95 size_t size() const { return maList.size(); }
96 void push_back( ExportListEntry* item ) { maList.push_back( item ); }
98 ExportListEntry* operator [] ( size_t i )
100 return ( i < maList.size() ) ? maList[ i ] : NULL;
104 #define REFID_NONE 0xFFFF
107 // struct ResData
110 /******************************************************************************
111 * Purpose: holds mandatory data to export a single res (used with ResStack)
112 ******************************************************************************/
114 #define ID_LEVEL_NULL 0x0000
115 #define ID_LEVEL_AUTOID 0x0001
116 #define ID_LEVEL_TEXT 0x0002
117 #define ID_LEVEL_FIELDNAME 0x0003
118 #define ID_LEVEL_ACCESSPATH 0x0004
119 #define ID_LEVEL_IDENTIFIER 0x0005
120 #define ID_LEVEL_LISTINDEX 0x0006
122 class ResData
124 public:
125 ~ResData();
126 sal_Bool SetId(const rtl::OString &rId, sal_uInt16 nLevel);
128 sal_Int32 nWidth;
129 sal_uInt16 nChildIndex;
130 sal_uInt16 nIdLevel;
131 sal_Bool bChild;
132 sal_Bool bChildWithText;
134 sal_Bool bText;
135 sal_Bool bHelpText;
136 sal_Bool bQuickHelpText;
137 sal_Bool bTitle;
138 sal_Bool bList;
140 sal_Bool bRestMerged;
142 rtl::OString sResTyp;
143 rtl::OString sId;
144 rtl::OString sGId;
145 rtl::OString sHelpId;
146 rtl::OString sFilename;
148 OStringHashMap sText;
149 sal_uInt16 nTextRefId;
151 OStringHashMap sHelpText;
152 sal_uInt16 nHelpTextRefId;
154 OStringHashMap sQuickHelpText;
155 sal_uInt16 nQuickHelpTextRefId;
157 OStringHashMap sTitle;
158 sal_uInt16 nTitleRefId;
160 rtl::OString sTextTyp;
162 ExportList *pStringList;
163 ExportList *pUIEntries;
164 ExportList *pItemList;
165 ExportList *pFilterList;
166 ExportList *pPairedList;
168 rtl::OString sPForm;
170 ResData(const rtl::OString &rPF, const rtl::OString &rGId)
172 nWidth( 0 ),
173 nChildIndex( 0 ),
174 nIdLevel( ID_LEVEL_NULL ),
175 bChild( sal_False ),
176 bChildWithText( sal_False ),
177 bText( sal_False ),
178 bHelpText( sal_False ),
179 bQuickHelpText( sal_False ),
180 bTitle( sal_False ),
181 bList( sal_False ),
182 bRestMerged( sal_False ),
183 sGId( rGId ),
184 nTextRefId( REFID_NONE ),
185 nHelpTextRefId( REFID_NONE ),
186 nQuickHelpTextRefId( REFID_NONE ),
187 nTitleRefId( REFID_NONE ),
188 sTextTyp( "Text" ),
189 pStringList( NULL ),
190 pUIEntries( NULL ),
191 pItemList( NULL ),
192 pFilterList( NULL ),
193 pPairedList( NULL ),
194 sPForm( rPF )
196 sGId = sGId.replaceAll("\r", rtl::OString());
197 sPForm = sPForm.replaceAll("\r", rtl::OString());
200 ResData(const rtl::OString &rPF, const rtl::OString &rGId , const rtl::OString &rFilename)
202 nWidth( 0 ),
203 nChildIndex( 0 ),
204 nIdLevel( ID_LEVEL_NULL ),
205 bChild( sal_False ),
206 bChildWithText( sal_False ),
207 bText( sal_False ),
208 bHelpText( sal_False ),
209 bQuickHelpText( sal_False ),
210 bTitle( sal_False ),
211 bList( sal_False ),
212 bRestMerged( sal_False ),
213 sGId( rGId ),
214 sFilename( rFilename ),
215 nTextRefId( REFID_NONE ),
216 nHelpTextRefId( REFID_NONE ),
217 nQuickHelpTextRefId( REFID_NONE ),
218 nTitleRefId( REFID_NONE ),
219 sTextTyp( "Text" ),
220 pStringList( NULL ),
221 pUIEntries( NULL ),
222 pItemList( NULL ),
223 pFilterList( NULL ),
224 pPairedList( NULL ),
225 sPForm( rPF )
227 sGId = sGId.replaceAll("\r", rtl::OString());
228 sPForm = sPForm.replaceAll("\r", rtl::OString());
234 // class Export
237 /******************************************************************************
238 * Purpose: syntax check and export of *.src, called from lexer
239 ******************************************************************************/
241 #define LIST_NON 0x0000
242 #define LIST_STRING 0x0001
243 #define LIST_FILTER 0x0002
244 #define LIST_ITEM 0x0004
245 #define LIST_PAIRED 0x0005
246 #define LIST_UIENTRIES 0x0008
247 #define STRING_TYP_TEXT 0x0010
248 #define STRING_TYP_HELPTEXT 0x0020
249 #define STRING_TYP_QUICKHELPTEXT 0x0040
250 #define STRING_TYP_TITLE 0x0080
252 #define MERGE_MODE_NORMAL 0x0000
253 #define MERGE_MODE_LIST 0x0001
255 typedef ::std::vector< ResData* > ResStack;
256 // forwards
257 class WordTransformer;
258 class ParserQueue;
260 class Export
262 private:
263 WordTransformer *pWordTransformer;
265 std::ofstream aOutput;
267 ResStack aResStack; // stack for parsing recursive
269 rtl::OString sActPForm; // hold cur. system
271 sal_Bool bDefine; // cur. res. in a define?
272 sal_Bool bNextMustBeDefineEOL; // define but no \ at lineend
273 std::size_t nLevel; // res. recursiv? how deep?
274 sal_uInt16 nList; // cur. res. is String- or FilterList
275 rtl::OString m_sListLang;
276 std::size_t nListIndex;
277 std::size_t nListLevel;
278 bool bSkipFile;
279 rtl::OString sProject;
280 rtl::OString sRoot;
281 sal_Bool bMergeMode;
282 rtl::OString sMergeSrc;
283 rtl::OString sLastListLine;
284 sal_Bool bError; // any errors while export?
285 sal_Bool bReadOver;
286 sal_Bool bDontWriteOutput;
287 rtl::OString sLastTextTyp;
288 static bool isInitialized;
289 rtl::OString sFilename;
292 public:
293 ParserQueue* pParseQueue; // public ?
294 static rtl::OString sLanguages; // public ?
295 static rtl::OString sForcedLanguages; // public ?
297 static void InitLanguages( bool bMergeMode = false );
298 static void InitForcedLanguages( bool bMergeMode = false );
299 static std::vector<rtl::OString> GetLanguages();
300 static std::vector<rtl::OString> GetForcedLanguages();
302 static void SetLanguages( std::vector<rtl::OString> val );
303 static void RemoveUTF8ByteOrderMarker( rtl::OString &rString );
304 static bool hasUTF8ByteOrderMarker( const rtl::OString &rString );
305 static rtl::OString QuoteHTML( rtl::OString const &rString );
307 static rtl::OString UnquoteHTML( rtl::OString const &rString );
309 static bool isSourceLanguage(const rtl::OString &rLanguage);
310 static bool isAllowed(const rtl::OString &rLanguage);
312 static void Languages( std::vector<rtl::OString>::const_iterator& begin , std::vector<rtl::OString>::const_iterator& end );
314 private:
315 static std::vector<rtl::OString> aLanguages;
316 static std::vector<rtl::OString> aForcedLanguages;
318 sal_Bool WriteData( ResData *pResData, sal_Bool bCreateNew = sal_False );// called befor dest. cur ResData
319 sal_Bool WriteExportList( ResData *pResData, ExportList *pExportList,
320 const rtl::OString &rTyp, sal_Bool bCreateNew = sal_False );
322 rtl::OString MergePairedList( rtl::OString const & sLine , rtl::OString const & sText );
324 rtl::OString FullId(); // creates cur. GID
326 rtl::OString GetPairedListID(const rtl::OString & rText);
327 rtl::OString GetPairedListString(const rtl::OString& rText);
328 rtl::OString StripList(const rtl::OString& rText);
330 void InsertListEntry(const rtl::OString &rText, const rtl::OString &rLine);
331 void CleanValue( rtl::OString &rValue );
332 rtl::OString GetText(const rtl::OString &rSource, int nToken);
334 sal_Bool PrepareTextToMerge(rtl::OString &rText, sal_uInt16 nTyp,
335 rtl::OString &rLangIndex, ResData *pResData);
336 void ResData2Output( PFormEntrys *pEntry, sal_uInt16 nType, const rtl::OString& rTextType );
337 void MergeRest( ResData *pResData, sal_uInt16 nMode = MERGE_MODE_NORMAL );
338 void ConvertMergeContent( rtl::OString &rText );
340 void WriteToMerged(const rtl::OString &rText , bool bSDFContent);
341 void SetChildWithText();
343 void CutComment( rtl::OString &rText );
345 public:
346 Export(const rtl::OString &rOutput);
347 Export(const rtl::OString &rMergeSource, const rtl::OString &rOutput);
348 ~Export();
350 void Init();
351 int Execute( int nToken, const char * pToken ); // called from lexer
352 void SetError() { bError = sal_True; }
353 sal_Bool GetError() { return bError; }
358 // class PFormEntrys
361 /******************************************************************************
362 * Purpose: holds information of data to merge (one pform)
363 ******************************************************************************/
365 class PFormEntrys
367 friend class MergeDataFile;
368 private:
369 rtl::OString data_; //TODO
370 rtl::OString sHelpText; // empty string
371 OStringHashMap sText;
372 OStringBoolHashMap bTextFirst;
373 OStringHashMap sQuickHelpText;
374 OStringBoolHashMap bQuickHelpTextFirst;
375 OStringHashMap sTitle;
376 OStringBoolHashMap bTitleFirst;
378 public:
379 PFormEntrys( const rtl::OString &rPForm ) : data_( rPForm ) {};
380 void InsertEntry(const rtl::OString &rId, const rtl::OString &rText,
381 const rtl::OString &rQuickHelpText, const rtl::OString &rTitle)
384 sText[ rId ] = rText;
385 bTextFirst[ rId ] = true;
386 sQuickHelpText[ rId ] = rQuickHelpText;
387 bQuickHelpTextFirst[ rId ] = true;
388 sTitle[ rId ] = rTitle;
389 bTitleFirst[ rId ] = true;
391 sal_Bool GetText( rtl::OString &rReturn, sal_uInt16 nTyp, const rtl::OString &nLangIndex, sal_Bool bDel = sal_False );
392 sal_Bool GetTransex3Text( rtl::OString &rReturn, sal_uInt16 nTyp, const rtl::OString &nLangIndex, sal_Bool bDel = sal_False );
397 // class MergeData
400 /******************************************************************************
401 * Purpose: holds information of data to merge (one ressource)
402 ******************************************************************************/
404 class MergeDataFile;
406 class MergeData
408 friend class MergeDataFile;
409 private:
410 rtl::OString sTyp;
411 rtl::OString sGID;
412 rtl::OString sLID;
413 rtl::OString sFilename;
414 PFormEntrysHashMap aMap;
415 public:
416 MergeData( const rtl::OString &rTyp, const rtl::OString &rGID, const rtl::OString &rLID , const rtl::OString &rFilename )
417 : sTyp( rTyp ), sGID( rGID ), sLID( rLID ) , sFilename( rFilename ) {};
418 ~MergeData();
419 PFormEntrys* GetPFormEntries();
421 void Insert( PFormEntrys* pfEntrys );
422 PFormEntrys* GetPFObject( const rtl::OString &rPFO );
424 sal_Bool operator==( ResData *pData );
428 // class MergeDataFile
431 /******************************************************************************
432 * Purpose: holds information of data to merge
433 ******************************************************************************/
435 class MergeDataFile
437 private:
438 sal_Bool bErrorLog;
439 rtl::OString sErrorLog;
440 MergeDataHashMap aMap;
441 std::set<rtl::OString> aLanguageSet;
443 MergeData *GetMergeData( ResData *pResData , bool bCaseSensitve = false );
444 void InsertEntry(const rtl::OString &rTYP, const rtl::OString &rGID,
445 const rtl::OString &rLID, const rtl::OString &rPFO,
446 const rtl::OString &nLang, const rtl::OString &rTEXT,
447 const rtl::OString &rQHTEXT, const rtl::OString &rTITLE,
448 const rtl::OString &sFilename, bool bCaseSensitive);
449 public:
450 explicit MergeDataFile(const rtl::OString &rFileName,
451 const rtl::OString& rFile, bool bErrLog, bool bCaseSensitive = false);
452 ~MergeDataFile();
455 std::vector<rtl::OString> GetLanguages();
457 PFormEntrys *GetPFormEntrys( ResData *pResData );
458 PFormEntrys *GetPFormEntrysCaseSensitive( ResData *pResData );
460 static rtl::OString CreateKey(const rtl::OString& rTYP, const rtl::OString& rGID,
461 const rtl::OString& rLID, const rtl::OString& rFilename , bool bCaseSensitive = false);
465 class QueueEntry
467 public:
468 QueueEntry(int nTypVal, const rtl::OString &rLineVal)
469 : nTyp(nTypVal), sLine(rLineVal)
472 int nTyp;
473 rtl::OString sLine;
476 class ParserQueue
478 public:
480 ParserQueue( Export& aExportObj );
481 ~ParserQueue();
483 inline void Push( const QueueEntry& aEntry );
484 bool bCurrentIsM; // public ?
485 bool bNextIsM; // public ?
486 bool bLastWasM; // public ?
487 bool bMflag; // public ?
489 void Close();
490 private:
491 // Future / Next
492 std::queue<QueueEntry>* aQueueNext;
493 // Current
494 std::queue<QueueEntry>* aQueueCur;
495 // Ref
496 std::queue<QueueEntry>* aQref;
498 Export& aExport;
499 bool bStart;
500 bool bStartNext;
502 inline void Pop( std::queue<QueueEntry>& aQueue );
505 #endif
507 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */