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>
26 #include <osl/file.hxx>
27 #include <sal/log.hxx>
31 #include <helpmerge.hxx>
33 #include <sys/types.h>
37 #include <rtl/strbuf.hxx>
39 #if !defined WIN32_LEAN_AND_MEAN
40 # define WIN32_LEAN_AND_MEAN
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
)
65 fprintf(stdout
,"+------------%s-----------+\n",sKey_in
.getStr() );
66 for (auto const& posn
: *rElem_in
)
69 y
=posn
.second
->ToOString();
70 fprintf(stdout
,"key=%s value=%s\n",x
.getStr(),y
.getStr());
72 fprintf(stdout
,"+--------------------------+\n");
76 HelpParser::HelpParser( const OString
&rHelpFile
)
77 : sHelpFile( rHelpFile
)
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
))
93 aParser
.GetError().m_sMessage
.getStr());
97 if( !pXmlFile
->CheckExportStatus() ){
101 PoOfstream
aPoOutput( rPOFile_in
, PoOfstream::APP
);
103 if (!aPoOutput
.isOpen()) {
104 fprintf(stdout
,"Can't open file %s\n",rPOFile_in
.getStr());
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 )
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
);
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());
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
);
157 MergeSingleFile( xmlfile
.get() , pMergeDataFile
, rLanguage
, rDestinationFile
);
161 void HelpParser::MergeSingleFile( XMLFile
* file
, MergeDataFile
* pMergeDataFile
, const OString
& sLanguage
,
162 OString
const & sPath
)
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***************************************");
179 printf("DBG: sHelpFile = %s\n",sHelpFile
.getStr() );
182 s_ResData
.sGId
= posm
->first
;
183 s_ResData
.sFilename
= sHelpFile
;
185 ProcessHelp( aLangHM
, sLanguage
, &s_ResData
, pMergeDataFile
);
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") )
200 pXMLElement
= (*aLangHM
)[ "en-US" ];
201 if( pXMLElement
== nullptr )
203 printf("Error: Can't find en-US entry\n");
205 if( pXMLElement
== nullptr )
211 pXMLElement
->ToOString().
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
] == ' ') )
226 sNewText
= MergeEntrys::GetQTZText(*pResData
, sSourceText
);
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
);
255 "Can't find GID=" << pResData
->sGId
<< " TYP=" << pResData
->sResTyp
);
257 pXMLElement
->ChangeLanguageTag(sCur
);
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */