LibreOffice Module l10ntools (master) 1
helpmerge.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <fstream>
23
24#include <sal/log.hxx>
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <helpmerge.hxx>
29#include <utility>
30#include <vector>
31
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
40
41#include <export.hxx>
42#include <common.hxx>
43#include <helper.hxx>
44#include <po.hxx>
45
46#if OSL_DEBUG_LEVEL > 2
47void HelpParser::Dump(XMLHashMap* rElem_in)
48{
49 for (auto const& pos : *rElem_in)
50 {
51 Dump(pos.second,pos.first);
52 }
53}
54
55void HelpParser::Dump(LangHashMap* rElem_in,const OString & sKey_in)
56{
57 OString x;
58 OString y;
59 fprintf(stdout,"+------------%s-----------+\n",sKey_in.getStr() );
60 for (auto const& posn : *rElem_in)
61 {
62 x=posn.first;
63 y=posn.second->ToOString();
64 fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
65 }
66 fprintf(stdout,"+--------------------------+\n");
67}
68#endif
69
70HelpParser::HelpParser( OString _sHelpFile )
71 : sHelpFile(std::move( _sHelpFile ))
72 {};
73
74/*****************************************************************************/
76/*****************************************************************************/
77 const OString &rPOFile_in, const OString &sHelpFile,
78 XMLFile* pXmlFile, std::string_view rGsi1){
79 SimpleXMLParser aParser;
80 //TODO: explicit BOM handling?
81
82 if (!aParser.Execute( sHelpFile, pXmlFile ))
83 {
84 printf(
85 "%s: %s\n",
86 sHelpFile.getStr(),
87 aParser.GetError().m_sMessage.getStr());
88 exit(-1);
89 }
90 pXmlFile->Extract();
91 if( !pXmlFile->CheckExportStatus() ){
92 return true;
93 }
94
95 PoOfstream aPoOutput( rPOFile_in, PoOfstream::APP );
96
97 if (!aPoOutput.isOpen()) {
98 fprintf(stdout,"Can't open file %s\n",rPOFile_in.getStr());
99 return false;
100 }
101
102 XMLHashMap* aXMLStrHM = pXmlFile->GetStrings();
103
104 std::vector<OString> order = pXmlFile->getOrder();
105
106 for (auto const& pos : order)
107 {
108 auto posm = aXMLStrHM->find(pos);
109 LangHashMap* pElem = posm->second;
110
111 XMLElement* pXMLElement = (*pElem)[ "en-US" ];
112
113 if( pXMLElement != nullptr )
114 {
115 OString data(
116 pXMLElement->ToOString().
117 replaceAll("\n",OString()).
118 replaceAll("\t",OString()).trim());
119
121 "Helpex", aPoOutput, sHelpFile, rGsi1,
122 posm->first, OString(), OString(), data);
123
124 pXMLElement=nullptr;
125 }
126 else
127 {
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());
131 }
132 }
133 aPoOutput.close();
134
135 return true;
136}
137
138bool HelpParser::Merge( const OString &rDestinationFile,
139 const OString& rLanguage , MergeDataFile* pMergeDataFile )
140{
141 SimpleXMLParser aParser;
142
143 //TODO: explicit BOM handling?
144
145 XMLFile xmlfile( OString('0') );
146 if (!aParser.Execute( sHelpFile, &xmlfile))
147 {
148 SAL_WARN("l10ntools", "could not parse " << sHelpFile);
149 return false;
150 }
151 MergeSingleFile( &xmlfile , pMergeDataFile , rLanguage , rDestinationFile );
152 return true;
153}
154
155void HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile* pMergeDataFile , const OString& sLanguage ,
156 OString const & sPath )
157{
158 file->Extract();
159
160 XMLHashMap* aXMLStrHM = file->GetStrings();
161 static ResData s_ResData("","");
162 s_ResData.sResTyp = "help";
163
164 std::vector<OString> order = file->getOrder();
165
166 for (auto const& pos : order) // Merge every l10n related string in the same order as export
167 {
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
175
176 s_ResData.sGId = posm->first;
177 s_ResData.sFilename = sHelpFile;
178
179 ProcessHelp( aLangHM , sLanguage, &s_ResData , pMergeDataFile );
180 }
181
182 file->Write(sPath);
183}
184
185/* ProcessHelp method: search for en-US entry and replace it with the current language*/
186void HelpParser::ProcessHelp( LangHashMap* aLangHM , const OString& sCur , ResData *pResData , MergeDataFile* pMergeDataFile ){
187
188 XMLElement* pXMLElement = nullptr;
189
190 if( sCur.equalsIgnoreAsciiCase("en-US") )
191 return;
192
193 pXMLElement = (*aLangHM)[ "en-US" ];
194 if( pXMLElement == nullptr )
195 {
196 printf("Error: Can't find en-US entry\n");
197 return;
198 }
199
200 OString sNewText;
201 OString sNewdata;
202 OString sSourceText(
203 pXMLElement->ToOString().
204 replaceAll(
205 "\n",
206 OString()).
207 replaceAll(
208 "\t",
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" )
217 {
218 sNewText = MergeEntrys::GetQTZText(*pResData, sSourceText);
219 sNewdata = sNewText;
220 }
221 else if( pMergeDataFile )
222 {
223 MergeEntrys *pEntrys = pMergeDataFile->GetMergeEntrys( pResData );
224 if( pEntrys != nullptr)
225 {
226 pEntrys->GetText( sNewText, sCur, true );
228 {
229 sNewdata = sSourceText.subView(0,nPreSpaces) + sNewText;
230 }
231 }
232 }
233 if (!sNewdata.isEmpty())
234 {
235 XMLData *data = new XMLData( sNewdata , nullptr ); // Add new one
236 pXMLElement->RemoveAndDeleteAllChildren();
237 pXMLElement->AddChild( data );
238 aLangHM->erase( sCur );
239 }
240 else
241 {
242 SAL_WARN(
243 "l10ntools",
244 "Can't find GID=" << pResData->sGId << " TYP=" << pResData->sResTyp);
245 }
246 pXMLElement->ChangeLanguageTag(sCur);
247}
248
249/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool Merge(const OString &rDestinationFile_in, const OString &sLanguage, MergeDataFile *pMergeDataFile)
Method merges the String into XMLfile, which must point to an existing file.
Definition: helpmerge.cxx:138
void MergeSingleFile(XMLFile *file, MergeDataFile *pMergeDataFile, const OString &sLanguage, OString const &sPath)
Definition: helpmerge.cxx:155
HelpParser(OString sHelpFile)
Definition: helpmerge.cxx:70
static bool CreatePO(const OString &rPOFile_in, const OString &sHelpFile, XMLFile *pXmlFile, std::string_view rGsi1)
Method append a PO file with the content of a parsed XML file @PRECOND rHelpFile is valid.
Definition: helpmerge.cxx:75
static void ProcessHelp(LangHashMap *aLangHM, const OString &sCur, ResData *pResData, MergeDataFile *pMergeDataFile)
Definition: helpmerge.cxx:186
OString sHelpFile
Definition: helpmerge.hxx:36
Purpose: holds information of data to merge, read from PO file.
Definition: export.hxx:114
MergeEntrys * GetMergeEntrys(ResData *pResData)
Definition: merge.cxx:274
Purpose: holds information of data to merge.
Definition: export.hxx:77
static OString GetQTZText(const ResData &rResData, std::string_view rOrigText)
Generate QTZ string with ResData For executable which works one language and without PO files.
Definition: merge.cxx:109
bool GetText(OString &rReturn, const OString &nLangIndex, bool bDel=false)
Definition: merge.cxx:87
Interface to write po entry to files as output streams.
Definition: po.hxx:101
bool isOpen() const
Definition: po.hxx:116
void close()
Definition: po.cxx:515
@ APP
Definition: po.hxx:109
Purpose: holds mandatory data to export a single res.
Definition: export.hxx:55
OString sGId
Definition: export.hxx:62
OString sFilename
Definition: export.hxx:63
OString sResTyp
Definition: export.hxx:60
validating xml parser, creates a document tree with xml nodes
Definition: xmlparse.hxx:321
const XMLError & GetError() const
returns an error struct
Definition: xmlparse.hxx:355
bool Execute(const OString &rFileName, XMLFile *pXMLFile)
parse a file, return false on critical errors
Definition: xmlparse.cxx:855
Holds character data.
Definition: xmlparse.hxx:240
Hold information of an element node.
Definition: xmlparse.hxx:202
void ChangeLanguageTag(const OString &rValue)
Definition: xmlparse.cxx:586
OString ToOString()
Return a Unicode String representation of this object.
Definition: xmlparse.cxx:631
Holds information of a XML file, is root node of tree.
Definition: xmlparse.hxx:148
void Write(OString const &rFilename)
Definition: xmlparse.cxx:147
const std::vector< OString > & getOrder() const
Definition: xmlparse.hxx:173
XMLHashMap * GetStrings()
Definition: xmlparse.hxx:160
bool CheckExportStatus(XMLChildNode *pCur=nullptr)
Definition: xmlparse.cxx:477
void Extract()
Definition: xmlparse.cxx:327
void RemoveAndDeleteAllChildren()
Definition: xmlparse.cxx:134
void AddChild(XMLChildNode *pChild)
adds a new child
Definition: xmlparse.cxx:127
static OString QuotHTML(const OString &rString)
Quot the XML characters.
Definition: xmlparse.cxx:1062
float y
float x
#define SAL_WARN(area, stream)
void writePoEntry(const OString &rExecutable, PoOfstream &rPoStream, const OString &rSourceFile, std::string_view rResType, const OString &rGroupId, const OString &rLocalId, const OString &rHelpText, const OString &rText, const PoEntry::TYPE eType)
Write out a PoEntry with attention to exceptions.
Definition: common.cxx:110
bool isWellFormedXML(std::string_view text)
Check whether text is a valid XML expression.
Definition: helper.cxx:130
std::basic_string_view< charT, traits > trim(std::basic_string_view< charT, traits > str)
OString m_sMessage
readable error message
Definition: xmlparse.hxx:314
size_t pos
std::unordered_map< OString, LangHashMap * > XMLHashMap
Mapping XML Element string identifier <-> Language Map.
Definition: xmlparse.hxx:143
std::unordered_map< OString, XMLElement * > LangHashMap
Mapping numeric Language code <-> XML Element.
Definition: xmlparse.hxx:140
static OString sLanguage
Definition: xrmmerge.cxx:40