1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
24 #include <sal/log.hxx>
28 #include <helpmerge.hxx>
33 #if !defined WIN32_LEAN_AND_MEAN
34 # define WIN32_LEAN_AND_MEAN
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
)
59 fprintf(stdout
,"+------------%s-----------+\n",sKey_in
.getStr() );
60 for (auto const& posn
: *rElem_in
)
63 y
=posn
.second
->ToOString();
64 fprintf(stdout
,"key=%s value=%s\n",x
.getStr(),y
.getStr());
66 fprintf(stdout
,"+--------------------------+\n");
70 HelpParser::HelpParser( OString _sHelpFile
)
71 : sHelpFile(std::move( _sHelpFile
))
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
))
87 aParser
.GetError().m_sMessage
.getStr());
91 if( !pXmlFile
->CheckExportStatus() ){
95 PoOfstream
aPoOutput( rPOFile_in
, PoOfstream::APP
);
97 if (!aPoOutput
.isOpen()) {
98 fprintf(stdout
,"Can't open file %s\n",rPOFile_in
.getStr());
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 )
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
);
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());
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
);
151 MergeSingleFile( &xmlfile
, pMergeDataFile
, rLanguage
, rDestinationFile
);
155 void HelpParser::MergeSingleFile( XMLFile
* file
, MergeDataFile
* pMergeDataFile
, const OString
& sLanguage
,
156 OString
const & sPath
)
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***************************************");
173 printf("DBG: sHelpFile = %s\n",sHelpFile
.getStr() );
176 s_ResData
.sGId
= posm
->first
;
177 s_ResData
.sFilename
= sHelpFile
;
179 ProcessHelp( aLangHM
, sLanguage
, &s_ResData
, pMergeDataFile
);
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") )
193 pXMLElement
= (*aLangHM
)[ "en-US"_ostr
];
194 if( pXMLElement
== nullptr )
196 printf("Error: Can't find en-US entry\n");
203 pXMLElement
->ToOString().
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
] == ' ') )
218 sNewText
= MergeEntrys::GetQTZText(*pResData
, sSourceText
);
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
);
244 "Can't find GID=" << pResData
->sGId
<< " TYP=" << pResData
->sResTyp
);
246 pXMLElement
->ChangeLanguageTag(sCur
);
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */