fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / l10ntools / source / helpmerge.cxx
blobdd31a3a04d526fb9932fe819cf645961945a4b31
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
22 #include <fstream>
23 #include <functional>
24 #include <memory>
26 #include <osl/file.hxx>
27 #include <sal/log.hxx>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "helpmerge.hxx"
32 #include <algorithm>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <iostream>
36 #include <vector>
37 #include <rtl/strbuf.hxx>
38 #ifdef WNT
39 #include <windows.h>
40 #undef CopyFile
41 #include <direct.h>
42 #endif
44 #include "export.hxx"
45 #include "common.hxx"
46 #include "helper.hxx"
47 #include "po.hxx"
49 #if OSL_DEBUG_LEVEL > 2
50 void HelpParser::Dump(XMLHashMap* rElem_in)
52 for(XMLHashMap::iterator pos = rElem_in->begin();pos != rElem_in->end(); ++pos)
54 Dump(pos->second,pos->first);
58 void HelpParser::Dump(LangHashMap* rElem_in,const OString & sKey_in)
60 OString x;
61 OString y;
62 fprintf(stdout,"+------------%s-----------+\n",sKey_in.getStr() );
63 for(LangHashMap::iterator posn=rElem_in->begin();posn!=rElem_in->end();++posn)
65 x=posn->first;
66 y=posn->second->ToOString();
67 fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
69 fprintf(stdout,"+--------------------------+\n");
71 #endif
73 HelpParser::HelpParser( const OString &rHelpFile )
74 : sHelpFile( rHelpFile )
75 {};
77 /*****************************************************************************/
78 bool HelpParser::CreatePO(
79 /*****************************************************************************/
80 const OString &rPOFile_in, const OString &sHelpFile,
81 XMLFile *pXmlFile, const OString &rGsi1){
82 SimpleXMLParser aParser;
83 //TODO: explicit BOM handling?
85 std::unique_ptr <XMLFile> file ( aParser.Execute( sHelpFile, pXmlFile ) );
87 if(file.get() == NULL)
89 printf(
90 "%s: %s\n",
91 sHelpFile.getStr(),
92 aParser.GetError().m_sMessage.getStr());
93 exit(-1);
95 file->Extract();
96 if( !file->CheckExportStatus() ){
97 return true;
100 PoOfstream aPoOutput( rPOFile_in, PoOfstream::APP );
102 if (!aPoOutput.isOpen()) {
103 fprintf(stdout,"Can't open file %s\n",rPOFile_in.getStr());
104 return false;
107 XMLHashMap* aXMLStrHM = file->GetStrings();
109 std::vector<OString> order = file->getOrder();
110 std::vector<OString>::iterator pos;
111 XMLHashMap::iterator posm;
113 for( pos = order.begin(); pos != order.end() ; ++pos )
115 posm = aXMLStrHM->find( *pos );
116 LangHashMap* pElem = posm->second;
118 XMLElement* pXMLElement = (*pElem)[ "en-US" ];
120 if( pXMLElement != NULL )
122 OString data(
123 pXMLElement->ToOString().
124 replaceAll("\n",OString()).
125 replaceAll("\t",OString()).trim());
127 common::writePoEntry(
128 "Helpex", aPoOutput, sHelpFile, rGsi1,
129 posm->first, pXMLElement->GetOldref(), OString(), data);
131 pXMLElement=NULL;
133 else
135 fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreatePO, File %s\n", sHelpFile.getStr());
138 aPoOutput.close();
140 return true;
143 bool HelpParser::Merge( const OString &rPOFile, const OString &rDestinationFile,
144 const OString& rLanguage , MergeDataFile* pMergeDataFile )
147 (void) rPOFile;
149 SimpleXMLParser aParser;
151 //TODO: explicit BOM handling?
153 XMLFile* xmlfile = ( aParser.Execute( sHelpFile, new XMLFile( OString('0') ) ) );
154 if (!xmlfile)
156 SAL_WARN("l10ntools", "could not parse " << sHelpFile);
157 return false;
159 bool hasNoError = MergeSingleFile( xmlfile , pMergeDataFile , rLanguage , rDestinationFile );
160 delete xmlfile;
161 return hasNoError;
164 bool HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile* pMergeDataFile , const OString& sLanguage ,
165 OString const & sPath )
167 file->Extract();
169 XMLHashMap* aXMLStrHM = file->GetStrings();
170 static ResData pResData("","");
171 pResData.sResTyp = "help";
173 std::vector<OString> order = file->getOrder();
174 std::vector<OString>::iterator pos;
175 XMLHashMap::iterator posm;
177 for( pos = order.begin(); pos != order.end() ; ++pos ) // Merge every l10n related string in the same order as export
179 posm = aXMLStrHM->find( *pos );
180 LangHashMap* aLangHM = posm->second;
181 #if OSL_DEBUG_LEVEL > 2
182 printf("*********************DUMPING HASHMAP***************************************");
183 Dump(aXMLStrHM);
184 printf("DBG: sHelpFile = %s\n",sHelpFile.getStr() );
185 #endif
187 pResData.sGId = posm->first;
188 pResData.sFilename = sHelpFile;
190 ProcessHelp( aLangHM , sLanguage, &pResData , pMergeDataFile );
193 file->Write(sPath);
194 return true;
197 /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
198 void HelpParser::ProcessHelp( LangHashMap* aLangHM , const OString& sCur , ResData *pResData , MergeDataFile* pMergeDataFile ){
200 XMLElement* pXMLElement = NULL;
201 MergeEntrys *pEntrys = NULL;
203 OString sLId;
205 pEntrys = NULL;
207 if( !sCur.equalsIgnoreAsciiCase("en-US") ){
208 pXMLElement = (*aLangHM)[ "en-US" ];
209 if( pXMLElement == NULL )
211 printf("Error: Can't find en-US entry\n");
213 if( pXMLElement != NULL )
215 sLId = pXMLElement->GetOldref();
216 pResData->sId = sLId;
218 OString sNewText;
219 OString sNewdata;
220 OString sSourceText(
221 pXMLElement->ToOString().
222 replaceAll(
223 OString("\n"),
224 OString()).
225 replaceAll(
226 OString("\t"),
227 OString()));
228 // re-add spaces to the beginning of translated string,
229 // important for indentation of Basic code examples
230 sal_Int32 nPreSpaces = 0;
231 sal_Int32 nLen = sSourceText.getLength();
232 while ( (nPreSpaces < nLen) && (*(sSourceText.getStr()+nPreSpaces) == ' ') )
233 nPreSpaces++;
234 if( sCur == "qtz" )
236 sNewText = MergeEntrys::GetQTZText(*pResData, sSourceText);
237 sNewdata = sNewText;
239 else if( pMergeDataFile )
241 pEntrys = pMergeDataFile->GetMergeEntrys( pResData );
242 if( pEntrys != NULL)
244 pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur, true );
245 if (helper::isWellFormedXML(XMLUtil::QuotHTML(sNewText)))
247 sNewdata = sSourceText.copy(0,nPreSpaces) + sNewText;
251 if (!sNewdata.isEmpty())
253 if( pXMLElement != NULL )
255 XMLData *data = new XMLData( sNewdata , NULL , true ); // Add new one
256 pXMLElement->RemoveAndDeleteAllChildren();
257 pXMLElement->AddChild( data );
258 aLangHM->erase( sCur );
261 else
263 SAL_WARN(
264 "l10ntools",
265 "Can't find GID=" << pResData->sGId.getStr() << " LID="
266 << pResData->sId.getStr() << " TYP=" << pResData->sResTyp.getStr());
268 pXMLElement->ChangeLanguageTag(sCur);
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */