Fix typos
[LibreOffice.git] / l10ntools / source / helpmerge.cxx
blobed5e1c8777aa45184e47a317ed1380942dcdd5e8
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, std::string_view 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 XMLFile xmlfile( OString('0') );
152 if (!aParser.Execute( sHelpFile, &xmlfile))
154 SAL_WARN("l10ntools", "could not parse " << sHelpFile);
155 return false;
157 MergeSingleFile( &xmlfile , 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;
196 if( sCur.equalsIgnoreAsciiCase("en-US") )
197 return;
199 pXMLElement = (*aLangHM)[ "en-US" ];
200 if( pXMLElement == nullptr )
202 printf("Error: Can't find en-US entry\n");
203 return;
206 OString sNewText;
207 OString sNewdata;
208 OString sSourceText(
209 pXMLElement->ToOString().
210 replaceAll(
211 "\n",
212 OString()).
213 replaceAll(
214 "\t",
215 OString()));
216 // re-add spaces to the beginning of translated string,
217 // important for indentation of Basic code examples
218 sal_Int32 nPreSpaces = 0;
219 sal_Int32 nLen = sSourceText.getLength();
220 while ( (nPreSpaces < nLen) && (sSourceText[nPreSpaces] == ' ') )
221 nPreSpaces++;
222 if( sCur == "qtz" )
224 sNewText = MergeEntrys::GetQTZText(*pResData, sSourceText);
225 sNewdata = sNewText;
227 else if( pMergeDataFile )
229 MergeEntrys *pEntrys = pMergeDataFile->GetMergeEntrys( pResData );
230 if( pEntrys != nullptr)
232 pEntrys->GetText( sNewText, sCur, true );
233 if (helper::isWellFormedXML(XMLUtil::QuotHTML(sNewText)))
235 sNewdata = sSourceText.subView(0,nPreSpaces) + sNewText;
239 if (!sNewdata.isEmpty())
241 XMLData *data = new XMLData( sNewdata , nullptr ); // Add new one
242 pXMLElement->RemoveAndDeleteAllChildren();
243 pXMLElement->AddChild( data );
244 aLangHM->erase( sCur );
246 else
248 SAL_WARN(
249 "l10ntools",
250 "Can't find GID=" << pResData->sGId << " TYP=" << pResData->sResTyp);
252 pXMLElement->ChangeLanguageTag(sCur);
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */