Drop obsolete variable strings
[LibreOffice.git] / l10ntools / source / helpmerge.cxx
blobabab8a5a03a1c911fd0d23ac69c5dbb6eb0cba86
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 _WIN32
39 #if !defined WIN32_LEAN_AND_MEAN
40 # define WIN32_LEAN_AND_MEAN
41 #endif
42 #include <windows.h>
43 #undef CopyFile
44 #include <direct.h>
45 #endif
47 #include <export.hxx>
48 #include <common.hxx>
49 #include <helper.hxx>
50 #include <po.hxx>
52 #if OSL_DEBUG_LEVEL > 2
53 void HelpParser::Dump(XMLHashMap* rElem_in)
55 for (auto const& pos : *rElem_in)
57 Dump(pos.second,pos.first);
61 void HelpParser::Dump(LangHashMap* rElem_in,const OString & sKey_in)
63 OString x;
64 OString y;
65 fprintf(stdout,"+------------%s-----------+\n",sKey_in.getStr() );
66 for (auto const& posn : *rElem_in)
68 x=posn.first;
69 y=posn.second->ToOString();
70 fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
72 fprintf(stdout,"+--------------------------+\n");
74 #endif
76 HelpParser::HelpParser( const OString &rHelpFile )
77 : sHelpFile( rHelpFile )
78 {};
80 /*****************************************************************************/
81 bool HelpParser::CreatePO(
82 /*****************************************************************************/
83 const OString &rPOFile_in, const OString &sHelpFile,
84 XMLFile* pXmlFile, const OString &rGsi1){
85 SimpleXMLParser aParser;
86 //TODO: explicit BOM handling?
88 if (!aParser.Execute( sHelpFile, pXmlFile ))
90 printf(
91 "%s: %s\n",
92 sHelpFile.getStr(),
93 aParser.GetError().m_sMessage.getStr());
94 exit(-1);
96 pXmlFile->Extract();
97 if( !pXmlFile->CheckExportStatus() ){
98 return true;
101 PoOfstream aPoOutput( rPOFile_in, PoOfstream::APP );
103 if (!aPoOutput.isOpen()) {
104 fprintf(stdout,"Can't open file %s\n",rPOFile_in.getStr());
105 return false;
108 XMLHashMap* aXMLStrHM = pXmlFile->GetStrings();
110 std::vector<OString> order = pXmlFile->getOrder();
112 for (auto const& pos : order)
114 auto posm = aXMLStrHM->find(pos);
115 LangHashMap* pElem = posm->second;
117 XMLElement* pXMLElement = (*pElem)[ "en-US" ];
119 if( pXMLElement != nullptr )
121 OString data(
122 pXMLElement->ToOString().
123 replaceAll("\n",OString()).
124 replaceAll("\t",OString()).trim());
126 common::writePoEntry(
127 "Helpex", aPoOutput, sHelpFile, rGsi1,
128 posm->first, OString(), OString(), data);
130 pXMLElement=nullptr;
132 else
134 // If this is something totally unexpected, wouldn't an assert() be in order?
135 // On the other hand, if this is expected, why the printf?
136 fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreatePO, File %s\n", sHelpFile.getStr());
139 aPoOutput.close();
141 return true;
144 bool HelpParser::Merge( const OString &rDestinationFile,
145 const OString& rLanguage , MergeDataFile* pMergeDataFile )
147 SimpleXMLParser aParser;
149 //TODO: explicit BOM handling?
151 std::unique_ptr<XMLFile> xmlfile(new XMLFile( OString('0') ));
152 if (!aParser.Execute( sHelpFile, xmlfile.get()))
154 SAL_WARN("l10ntools", "could not parse " << sHelpFile);
155 return false;
157 MergeSingleFile( xmlfile.get() , pMergeDataFile , rLanguage , rDestinationFile );
158 return true;
161 void HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile* pMergeDataFile , const OString& sLanguage ,
162 OString const & sPath )
164 file->Extract();
166 XMLHashMap* aXMLStrHM = file->GetStrings();
167 static ResData s_ResData("","");
168 s_ResData.sResTyp = "help";
170 std::vector<OString> order = file->getOrder();
172 for (auto const& pos : order) // Merge every l10n related string in the same order as export
174 auto posm = aXMLStrHM->find(pos);
175 LangHashMap* aLangHM = posm->second;
176 #if OSL_DEBUG_LEVEL > 2
177 printf("*********************DUMPING HASHMAP***************************************");
178 Dump(aXMLStrHM);
179 printf("DBG: sHelpFile = %s\n",sHelpFile.getStr() );
180 #endif
182 s_ResData.sGId = posm->first;
183 s_ResData.sFilename = sHelpFile;
185 ProcessHelp( aLangHM , sLanguage, &s_ResData , pMergeDataFile );
188 file->Write(sPath);
191 /* ProcessHelp method: search for en-US entry and replace it with the current language*/
192 void HelpParser::ProcessHelp( LangHashMap* aLangHM , const OString& sCur , ResData *pResData , MergeDataFile* pMergeDataFile ){
194 XMLElement* pXMLElement = nullptr;
195 MergeEntrys *pEntrys = nullptr;
197 if( sCur.equalsIgnoreAsciiCase("en-US") )
198 return;
200 pXMLElement = (*aLangHM)[ "en-US" ];
201 if( pXMLElement == nullptr )
203 printf("Error: Can't find en-US entry\n");
205 if( pXMLElement == nullptr )
206 return;
208 OString sNewText;
209 OString sNewdata;
210 OString sSourceText(
211 pXMLElement->ToOString().
212 replaceAll(
213 "\n",
214 OString()).
215 replaceAll(
216 "\t",
217 OString()));
218 // re-add spaces to the beginning of translated string,
219 // important for indentation of Basic code examples
220 sal_Int32 nPreSpaces = 0;
221 sal_Int32 nLen = sSourceText.getLength();
222 while ( (nPreSpaces < nLen) && (sSourceText[nPreSpaces] == ' ') )
223 nPreSpaces++;
224 if( sCur == "qtz" )
226 sNewText = MergeEntrys::GetQTZText(*pResData, sSourceText);
227 sNewdata = sNewText;
229 else if( pMergeDataFile )
231 pEntrys = pMergeDataFile->GetMergeEntrys( pResData );
232 if( pEntrys != nullptr)
234 pEntrys->GetText( sNewText, sCur, true );
235 if (helper::isWellFormedXML(XMLUtil::QuotHTML(sNewText)))
237 sNewdata = sSourceText.copy(0,nPreSpaces) + sNewText;
241 if (!sNewdata.isEmpty())
243 if( pXMLElement != nullptr )
245 XMLData *data = new XMLData( sNewdata , nullptr ); // Add new one
246 pXMLElement->RemoveAndDeleteAllChildren();
247 pXMLElement->AddChild( data );
248 aLangHM->erase( sCur );
251 else
253 SAL_WARN(
254 "l10ntools",
255 "Can't find GID=" << pResData->sGId << " TYP=" << pResData->sResTyp);
257 pXMLElement->ChangeLanguageTag(sCur);
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */