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>
49 #if OSL_DEBUG_LEVEL > 2
50 void HelpParser::Dump(XMLHashMap
* rElem_in
)
52 for(XMLHashMap::iterator pos
= rElem_in
->begin();pos
!= rElem_in
->end(); ++pos
)
54 Dump(pos
->second
,pos
->first
);
58 void HelpParser::Dump(LangHashMap
* rElem_in
,const OString
& sKey_in
)
62 fprintf(stdout
,"+------------%s-----------+\n",sKey_in
.getStr() );
63 for(LangHashMap::iterator posn
=rElem_in
->begin();posn
!=rElem_in
->end();++posn
)
66 y
=posn
->second
->ToOString();
67 fprintf(stdout
,"key=%s value=%s\n",x
.getStr(),y
.getStr());
69 fprintf(stdout
,"+--------------------------+\n");
73 HelpParser::HelpParser( const OString
&rHelpFile
)
74 : sHelpFile( rHelpFile
)
77 /*****************************************************************************/
78 bool HelpParser::CreatePO(
79 /*****************************************************************************/
80 const OString
&rPOFile_in
, const OString
&sHelpFile
,
81 XMLFile
*pXmlFile
, const OString
&rGsi1
){
82 SimpleXMLParser aParser
;
83 //TODO: explicit BOM handling?
85 std::unique_ptr
<XMLFile
> file ( aParser
.Execute( sHelpFile
, pXmlFile
) );
87 if(file
.get() == NULL
)
92 aParser
.GetError().m_sMessage
.getStr());
96 if( !file
->CheckExportStatus() ){
100 PoOfstream
aPoOutput( rPOFile_in
, PoOfstream::APP
);
102 if (!aPoOutput
.isOpen()) {
103 fprintf(stdout
,"Can't open file %s\n",rPOFile_in
.getStr());
107 XMLHashMap
* aXMLStrHM
= file
->GetStrings();
109 std::vector
<OString
> order
= file
->getOrder();
110 std::vector
<OString
>::iterator pos
;
111 XMLHashMap::iterator posm
;
113 for( pos
= order
.begin(); pos
!= order
.end() ; ++pos
)
115 posm
= aXMLStrHM
->find( *pos
);
116 LangHashMap
* pElem
= posm
->second
;
118 XMLElement
* pXMLElement
= (*pElem
)[ "en-US" ];
120 if( pXMLElement
!= NULL
)
123 pXMLElement
->ToOString().
124 replaceAll("\n",OString()).
125 replaceAll("\t",OString()).trim());
127 common::writePoEntry(
128 "Helpex", aPoOutput
, sHelpFile
, rGsi1
,
129 posm
->first
, pXMLElement
->GetOldref(), OString(), data
);
135 fprintf(stdout
,"\nDBG: NullPointer in HelpParser::CreatePO, File %s\n", sHelpFile
.getStr());
143 bool HelpParser::Merge( const OString
&rPOFile
, const OString
&rDestinationFile
,
144 const OString
& rLanguage
, MergeDataFile
* pMergeDataFile
)
149 SimpleXMLParser aParser
;
151 //TODO: explicit BOM handling?
153 XMLFile
* xmlfile
= ( aParser
.Execute( sHelpFile
, new XMLFile( OString('0') ) ) );
156 SAL_WARN("l10ntools", "could not parse " << sHelpFile
);
159 bool hasNoError
= MergeSingleFile( xmlfile
, pMergeDataFile
, rLanguage
, rDestinationFile
);
164 bool HelpParser::MergeSingleFile( XMLFile
* file
, MergeDataFile
* pMergeDataFile
, const OString
& sLanguage
,
165 OString
const & sPath
)
169 XMLHashMap
* aXMLStrHM
= file
->GetStrings();
170 static ResData
pResData("","");
171 pResData
.sResTyp
= "help";
173 std::vector
<OString
> order
= file
->getOrder();
174 std::vector
<OString
>::iterator pos
;
175 XMLHashMap::iterator posm
;
177 for( pos
= order
.begin(); pos
!= order
.end() ; ++pos
) // Merge every l10n related string in the same order as export
179 posm
= aXMLStrHM
->find( *pos
);
180 LangHashMap
* aLangHM
= posm
->second
;
181 #if OSL_DEBUG_LEVEL > 2
182 printf("*********************DUMPING HASHMAP***************************************");
184 printf("DBG: sHelpFile = %s\n",sHelpFile
.getStr() );
187 pResData
.sGId
= posm
->first
;
188 pResData
.sFilename
= sHelpFile
;
190 ProcessHelp( aLangHM
, sLanguage
, &pResData
, pMergeDataFile
);
197 /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
198 void HelpParser::ProcessHelp( LangHashMap
* aLangHM
, const OString
& sCur
, ResData
*pResData
, MergeDataFile
* pMergeDataFile
){
200 XMLElement
* pXMLElement
= NULL
;
201 MergeEntrys
*pEntrys
= NULL
;
207 if( !sCur
.equalsIgnoreAsciiCase("en-US") ){
208 pXMLElement
= (*aLangHM
)[ "en-US" ];
209 if( pXMLElement
== NULL
)
211 printf("Error: Can't find en-US entry\n");
213 if( pXMLElement
!= NULL
)
215 sLId
= pXMLElement
->GetOldref();
216 pResData
->sId
= sLId
;
221 pXMLElement
->ToOString().
228 // re-add spaces to the beginning of translated string,
229 // important for indentation of Basic code examples
230 sal_Int32 nPreSpaces
= 0;
231 sal_Int32 nLen
= sSourceText
.getLength();
232 while ( (nPreSpaces
< nLen
) && (*(sSourceText
.getStr()+nPreSpaces
) == ' ') )
236 sNewText
= MergeEntrys::GetQTZText(*pResData
, sSourceText
);
239 else if( pMergeDataFile
)
241 pEntrys
= pMergeDataFile
->GetMergeEntrys( pResData
);
244 pEntrys
->GetText( sNewText
, STRING_TYP_TEXT
, sCur
, true );
245 if (helper::isWellFormedXML(XMLUtil::QuotHTML(sNewText
)))
247 sNewdata
= sSourceText
.copy(0,nPreSpaces
) + sNewText
;
251 if (!sNewdata
.isEmpty())
253 if( pXMLElement
!= NULL
)
255 XMLData
*data
= new XMLData( sNewdata
, NULL
, true ); // Add new one
256 pXMLElement
->RemoveAndDeleteAllChildren();
257 pXMLElement
->AddChild( data
);
258 aLangHM
->erase( sCur
);
265 "Can't find GID=" << pResData
->sGId
.getStr() << " LID="
266 << pResData
->sId
.getStr() << " TYP=" << pResData
->sResTyp
.getStr());
268 pXMLElement
->ChangeLanguageTag(sCur
);
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */