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"
25 #include <osl/file.hxx>
26 #include <sal/log.hxx>
30 #include "helpmerge.hxx"
32 #include <sys/types.h>
36 #include <rtl/strbuf.hxx>
48 #if OSL_DEBUG_LEVEL > 2
49 void HelpParser::Dump(XMLHashMap
* rElem_in
)
51 for(XMLHashMap::iterator pos
= rElem_in
->begin();pos
!= rElem_in
->end(); ++pos
)
53 Dump(pos
->second
,pos
->first
);
57 void HelpParser::Dump(LangHashMap
* rElem_in
,const OString
& sKey_in
)
61 fprintf(stdout
,"+------------%s-----------+\n",sKey_in
.getStr() );
62 for(LangHashMap::iterator posn
=rElem_in
->begin();posn
!=rElem_in
->end();++posn
)
65 y
=posn
->second
->ToOString();
66 fprintf(stdout
,"key=%s value=%s\n",x
.getStr(),y
.getStr());
68 fprintf(stdout
,"+--------------------------+\n");
72 HelpParser::HelpParser( const OString
&rHelpFile
)
73 : sHelpFile( rHelpFile
)
76 /*****************************************************************************/
77 bool HelpParser::CreatePO(
78 /*****************************************************************************/
79 const OString
&rPOFile_in
, const OString
&sHelpFile
,
80 XMLFile
*pXmlFile
, const OString
&rGsi1
){
81 SimpleXMLParser aParser
;
83 OStringToOUString(sHelpFile
, RTL_TEXTENCODING_ASCII_US
));
84 //TODO: explicit BOM handling?
86 std::auto_ptr
<XMLFile
> file ( aParser
.Execute( sXmlFile
, pXmlFile
) );
88 if(file
.get() == NULL
)
94 aParser
.GetError().sMessage
, RTL_TEXTENCODING_ASCII_US
).
99 if( !file
->CheckExportStatus() ){
103 PoOfstream
aPoOutput( rPOFile_in
, PoOfstream::APP
);
105 if (!aPoOutput
.isOpen()) {
106 fprintf(stdout
,"Can't open file %s\n",rPOFile_in
.getStr());
110 XMLHashMap
* aXMLStrHM
= file
->GetStrings();
112 XMLElement
* pXMLElement
= NULL
;
114 std::vector
<OString
> order
= file
->getOrder();
115 std::vector
<OString
>::iterator pos
;
116 XMLHashMap::iterator posm
;
118 for( pos
= order
.begin(); pos
!= order
.end() ; ++pos
)
120 posm
= aXMLStrHM
->find( *pos
);
121 pElem
= posm
->second
;
123 pXMLElement
= (*pElem
)[ "en-US" ];
125 if( pXMLElement
!= NULL
)
128 OUStringToOString( pXMLElement
->ToOUString(), RTL_TEXTENCODING_UTF8
).
129 replaceAll("\n",OString()).
130 replaceAll("\t",OString()).trim());
132 common::writePoEntry(
133 "Helpex", aPoOutput
, sHelpFile
, rGsi1
,
134 posm
->first
, pXMLElement
->GetOldref(), OString(), data
);
140 fprintf(stdout
,"\nDBG: NullPointer in HelpParser::CreatePO, File %s\n", sHelpFile
.getStr());
148 bool HelpParser::Merge( const OString
&rPOFile
, const OString
&rDestinationFile
,
149 const OString
& rLanguage
, MergeDataFile
* pMergeDataFile
)
154 SimpleXMLParser aParser
;
157 OStringToOUString(sHelpFile
, RTL_TEXTENCODING_ASCII_US
));
158 //TODO: explicit BOM handling?
160 XMLFile
* xmlfile
= ( aParser
.Execute( sXmlFile
, new XMLFile( OUString('0') ) ) );
161 bool hasNoError
= MergeSingleFile( xmlfile
, pMergeDataFile
, rLanguage
, rDestinationFile
);
166 bool HelpParser::MergeSingleFile( XMLFile
* file
, MergeDataFile
* pMergeDataFile
, const OString
& sLanguage
,
167 OString
const & sPath
)
171 XMLHashMap
* aXMLStrHM
= file
->GetStrings();
172 LangHashMap
* aLangHM
;
173 static ResData
pResData("","");
174 pResData
.sResTyp
= "help";
176 std::vector
<OString
> order
= file
->getOrder();
177 std::vector
<OString
>::iterator pos
;
178 XMLHashMap::iterator posm
;
180 for( pos
= order
.begin(); pos
!= order
.end() ; ++pos
) // Merge every l10n related string in the same order as export
182 posm
= aXMLStrHM
->find( *pos
);
183 aLangHM
= posm
->second
;
184 #if OSL_DEBUG_LEVEL > 2
185 printf("*********************DUMPING HASHMAP***************************************");
187 printf("DBG: sHelpFile = %s\n",sHelpFile
.getStr() );
190 pResData
.sGId
= posm
->first
;
191 pResData
.sFilename
= sHelpFile
;
193 ProcessHelp( aLangHM
, sLanguage
, &pResData
, pMergeDataFile
);
200 /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
201 void HelpParser::ProcessHelp( LangHashMap
* aLangHM
, const OString
& sCur
, ResData
*pResData
, MergeDataFile
* pMergeDataFile
){
203 XMLElement
* pXMLElement
= NULL
;
204 MergeEntrys
*pEntrys
= NULL
;
210 if( !sCur
.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")) ){
211 pXMLElement
= (*aLangHM
)[ "en-US" ];
212 if( pXMLElement
== NULL
)
214 printf("Error: Can't find en-US entry\n");
216 if( pXMLElement
!= NULL
)
218 sLId
= pXMLElement
->GetOldref();
219 pResData
->sId
= sLId
;
223 OUString
sSourceText(
224 pXMLElement
->ToOUString().
231 // re-add spaces to the beginning of translated string,
232 // important for indentation of Basic code examples
233 sal_Int32 nPreSpaces
= 0;
234 sal_Int32 nLen
= sSourceText
.getLength();
235 while ( (nPreSpaces
< nLen
) && (*(sSourceText
.getStr()+nPreSpaces
) == ' ') )
239 const OString sOriginText
= OUStringToOString(sSourceText
, RTL_TEXTENCODING_UTF8
);
240 sNewText
= MergeEntrys::GetQTZText(*pResData
, sOriginText
);
241 sNewdata
= OStringToOUString(sNewText
, RTL_TEXTENCODING_UTF8
);
243 else if( pMergeDataFile
)
245 pEntrys
= pMergeDataFile
->GetMergeEntrys( pResData
);
248 pEntrys
->GetText( sNewText
, STRING_TYP_TEXT
, sCur
, true );
249 OUString sTemp
= OStringToOUString(sNewText
, RTL_TEXTENCODING_UTF8
);
250 if (helper::isWellFormedXML(OUStringToOString(XMLUtil::QuotHTML(sTemp
),RTL_TEXTENCODING_UTF8
)))
252 sNewdata
= sSourceText
.copy(0,nPreSpaces
) + sTemp
;
256 if (!sNewdata
.isEmpty())
258 if( pXMLElement
!= NULL
)
260 XMLData
*data
= new XMLData( sNewdata
, NULL
, true ); // Add new one
261 pXMLElement
->RemoveAndDeleteAllChildren();
262 pXMLElement
->AddChild( data
);
263 aLangHM
->erase( sCur
);
266 else if( pResData
== NULL
)
268 fprintf(stdout
,"Can't find GID=%s LID=%s TYP=%s\n",
269 pResData
->sGId
.getStr(), pResData
->sId
.getStr(),
270 pResData
->sResTyp
.getStr());
272 pXMLElement
->ChangeLanguageTag(
273 OStringToOUString(sCur
, RTL_TEXTENCODING_ASCII_US
));
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */