CppunitTest_sc_tiledrendering2: move to tiledrendering folder
[LibreOffice.git] / l10ntools / source / helpmerge.cxx
blobfa0358a89da0c49d94755790d4b4e22ff2819126
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>
24 #include <sal/log.hxx>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <helpmerge.hxx>
29 #include <utility>
30 #include <vector>
32 #ifdef _WIN32
33 #if !defined WIN32_LEAN_AND_MEAN
34 # define WIN32_LEAN_AND_MEAN
35 #endif
36 #include <windows.h>
37 #undef CopyFile
38 #include <direct.h>
39 #endif
41 #include <export.hxx>
42 #include <common.hxx>
43 #include <helper.hxx>
44 #include <po.hxx>
46 #if OSL_DEBUG_LEVEL > 2
47 void HelpParser::Dump(XMLHashMap* rElem_in)
49 for (auto const& pos : *rElem_in)
51 Dump(pos.second,pos.first);
55 void HelpParser::Dump(LangHashMap* rElem_in,const OString & sKey_in)
57 OString x;
58 OString y;
59 fprintf(stdout,"+------------%s-----------+\n",sKey_in.getStr() );
60 for (auto const& posn : *rElem_in)
62 x=posn.first;
63 y=posn.second->ToOString();
64 fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
66 fprintf(stdout,"+--------------------------+\n");
68 #endif
70 HelpParser::HelpParser( OString _sHelpFile )
71 : sHelpFile(std::move( _sHelpFile ))
72 {};
74 /*****************************************************************************/
75 bool HelpParser::CreatePO(
76 /*****************************************************************************/
77 const OString &rPOFile_in, const OString &sHelpFile,
78 XMLFile* pXmlFile, std::string_view rGsi1){
79 SimpleXMLParser aParser;
80 //TODO: explicit BOM handling?
82 if (!aParser.Execute( sHelpFile, pXmlFile ))
84 printf(
85 "%s: %s\n",
86 sHelpFile.getStr(),
87 aParser.GetError().m_sMessage.getStr());
88 exit(-1);
90 pXmlFile->Extract();
91 if( !pXmlFile->CheckExportStatus() ){
92 return true;
95 PoOfstream aPoOutput( rPOFile_in, PoOfstream::APP );
97 if (!aPoOutput.isOpen()) {
98 fprintf(stdout,"Can't open file %s\n",rPOFile_in.getStr());
99 return false;
102 XMLHashMap* aXMLStrHM = pXmlFile->GetStrings();
104 std::vector<OString> order = pXmlFile->getOrder();
106 for (auto const& pos : order)
108 auto posm = aXMLStrHM->find(pos);
109 LangHashMap* pElem = posm->second;
111 XMLElement* pXMLElement = (*pElem)[ "en-US"_ostr ];
113 if( pXMLElement != nullptr )
115 OString data(
116 pXMLElement->ToOString().
117 replaceAll("\n"_ostr,OString()).
118 replaceAll("\t"_ostr,OString()).trim());
120 common::writePoEntry(
121 "Helpex"_ostr, aPoOutput, sHelpFile, rGsi1,
122 posm->first, OString(), OString(), data);
124 pXMLElement=nullptr;
126 else
128 // If this is something totally unexpected, wouldn't an assert() be in order?
129 // On the other hand, if this is expected, why the printf?
130 fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreatePO, File %s\n", sHelpFile.getStr());
133 aPoOutput.close();
135 return true;
138 bool HelpParser::Merge( const OString &rDestinationFile,
139 const OString& rLanguage , MergeDataFile* pMergeDataFile )
141 SimpleXMLParser aParser;
143 //TODO: explicit BOM handling?
145 XMLFile xmlfile( OString('0') );
146 if (!aParser.Execute( sHelpFile, &xmlfile))
148 SAL_WARN("l10ntools", "could not parse " << sHelpFile);
149 return false;
151 MergeSingleFile( &xmlfile , pMergeDataFile , rLanguage , rDestinationFile );
152 return true;
155 void HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile* pMergeDataFile , const OString& sLanguage ,
156 OString const & sPath )
158 file->Extract();
160 XMLHashMap* aXMLStrHM = file->GetStrings();
161 static ResData s_ResData(""_ostr,""_ostr);
162 s_ResData.sResTyp = "help"_ostr;
164 std::vector<OString> order = file->getOrder();
166 for (auto const& pos : order) // Merge every l10n related string in the same order as export
168 auto posm = aXMLStrHM->find(pos);
169 LangHashMap* aLangHM = posm->second;
170 #if OSL_DEBUG_LEVEL > 2
171 printf("*********************DUMPING HASHMAP***************************************");
172 Dump(aXMLStrHM);
173 printf("DBG: sHelpFile = %s\n",sHelpFile.getStr() );
174 #endif
176 s_ResData.sGId = posm->first;
177 s_ResData.sFilename = sHelpFile;
179 ProcessHelp( aLangHM , sLanguage, &s_ResData , pMergeDataFile );
182 file->Write(sPath);
185 /* ProcessHelp method: search for en-US entry and replace it with the current language*/
186 void HelpParser::ProcessHelp( LangHashMap* aLangHM , const OString& sCur , ResData *pResData , MergeDataFile* pMergeDataFile ){
188 XMLElement* pXMLElement = nullptr;
190 if( sCur.equalsIgnoreAsciiCase("en-US") )
191 return;
193 pXMLElement = (*aLangHM)[ "en-US"_ostr ];
194 if( pXMLElement == nullptr )
196 printf("Error: Can't find en-US entry\n");
197 return;
200 OString sNewText;
201 OString sNewdata;
202 OString sSourceText(
203 pXMLElement->ToOString().
204 replaceAll(
205 "\n"_ostr,
206 OString()).
207 replaceAll(
208 "\t"_ostr,
209 OString()));
210 // re-add spaces to the beginning of translated string,
211 // important for indentation of Basic code examples
212 sal_Int32 nPreSpaces = 0;
213 sal_Int32 nLen = sSourceText.getLength();
214 while ( (nPreSpaces < nLen) && (sSourceText[nPreSpaces] == ' ') )
215 nPreSpaces++;
216 if( sCur == "qtz" )
218 sNewText = MergeEntrys::GetQTZText(*pResData, sSourceText);
219 sNewdata = sNewText;
221 else if( pMergeDataFile )
223 MergeEntrys *pEntrys = pMergeDataFile->GetMergeEntrys( pResData );
224 if( pEntrys != nullptr)
226 pEntrys->GetText( sNewText, sCur, true );
227 if (helper::isWellFormedXML(XMLUtil::QuotHTML(sNewText)))
229 sNewdata = sSourceText.subView(0,nPreSpaces) + sNewText;
233 if (!sNewdata.isEmpty())
235 XMLData *data = new XMLData( sNewdata , nullptr ); // Add new one
236 pXMLElement->RemoveAndDeleteAllChildren();
237 pXMLElement->AddChild( data );
238 aLangHM->erase( sCur );
240 else
242 SAL_WARN(
243 "l10ntools",
244 "Can't find GID=" << pResData->sGId << " TYP=" << pResData->sResTyp);
246 pXMLElement->ChangeLanguageTag(sCur);
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */