Stop leaking all ScPostIt instances.
[LibreOffice.git] / l10ntools / source / helpmerge.cxx
bloba8fa684ab443b7f9941bcbadc7ed84b48e0e18fe
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
22 #include <fstream>
23 #include <functional>
25 #include <osl/file.hxx>
26 #include <sal/log.hxx>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include "helpmerge.hxx"
31 #include <algorithm>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <iostream>
35 #include <vector>
36 #include <rtl/strbuf.hxx>
37 #ifdef WNT
38 #include <windows.h>
39 #undef CopyFile
40 #include <direct.h>
41 #endif
43 #include "export.hxx"
44 #include "common.hxx"
45 #include "helper.hxx"
46 #include "po.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)
59 OString x;
60 OString y;
61 fprintf(stdout,"+------------%s-----------+\n",sKey_in.getStr() );
62 for(LangHashMap::iterator posn=rElem_in->begin();posn!=rElem_in->end();++posn)
64 x=posn->first;
65 y=posn->second->ToOString();
66 fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
68 fprintf(stdout,"+--------------------------+\n");
70 #endif
72 HelpParser::HelpParser( const OString &rHelpFile )
73 : sHelpFile( rHelpFile )
74 {};
76 /*****************************************************************************/
77 bool HelpParser::CreatePO(
78 /*****************************************************************************/
79 const OString &rPOFile_in, const OString &sHelpFile,
80 XMLFile *pXmlFile, const OString &rGsi1){
81 SimpleXMLParser aParser;
82 //TODO: explicit BOM handling?
84 std::auto_ptr <XMLFile> file ( aParser.Execute( sHelpFile, pXmlFile ) );
86 if(file.get() == NULL)
88 printf(
89 "%s: %s\n",
90 sHelpFile.getStr(),
91 aParser.GetError().m_sMessage.getStr());
92 exit(-1);
94 file->Extract();
95 if( !file->CheckExportStatus() ){
96 return true;
99 PoOfstream aPoOutput( rPOFile_in, PoOfstream::APP );
101 if (!aPoOutput.isOpen()) {
102 fprintf(stdout,"Can't open file %s\n",rPOFile_in.getStr());
103 return false;
106 XMLHashMap* aXMLStrHM = file->GetStrings();
107 LangHashMap* pElem;
108 XMLElement* pXMLElement = NULL;
110 std::vector<OString> order = file->getOrder();
111 std::vector<OString>::iterator pos;
112 XMLHashMap::iterator posm;
114 for( pos = order.begin(); pos != order.end() ; ++pos )
116 posm = aXMLStrHM->find( *pos );
117 pElem = posm->second;
119 pXMLElement = (*pElem)[ "en-US" ];
121 if( pXMLElement != NULL )
123 OString data(
124 pXMLElement->ToOString().
125 replaceAll("\n",OString()).
126 replaceAll("\t",OString()).trim());
128 common::writePoEntry(
129 "Helpex", aPoOutput, sHelpFile, rGsi1,
130 posm->first, pXMLElement->GetOldref(), OString(), data);
132 pXMLElement=NULL;
134 else
136 fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreatePO, File %s\n", sHelpFile.getStr());
139 aPoOutput.close();
141 return sal_True;
144 bool HelpParser::Merge( const OString &rPOFile, const OString &rDestinationFile,
145 const OString& rLanguage , MergeDataFile* pMergeDataFile )
148 (void) rPOFile;
150 SimpleXMLParser aParser;
152 //TODO: explicit BOM handling?
154 XMLFile* xmlfile = ( aParser.Execute( sHelpFile, new XMLFile( OString('0') ) ) );
155 bool hasNoError = MergeSingleFile( xmlfile , pMergeDataFile , rLanguage , rDestinationFile );
156 delete xmlfile;
157 return hasNoError;
160 bool HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile* pMergeDataFile , const OString& sLanguage ,
161 OString const & sPath )
163 file->Extract();
165 XMLHashMap* aXMLStrHM = file->GetStrings();
166 LangHashMap* aLangHM;
167 static ResData pResData("","");
168 pResData.sResTyp = "help";
170 std::vector<OString> order = file->getOrder();
171 std::vector<OString>::iterator pos;
172 XMLHashMap::iterator posm;
174 for( pos = order.begin(); pos != order.end() ; ++pos ) // Merge every l10n related string in the same order as export
176 posm = aXMLStrHM->find( *pos );
177 aLangHM = posm->second;
178 #if OSL_DEBUG_LEVEL > 2
179 printf("*********************DUMPING HASHMAP***************************************");
180 Dump(aXMLStrHM);
181 printf("DBG: sHelpFile = %s\n",sHelpFile.getStr() );
182 #endif
184 pResData.sGId = posm->first;
185 pResData.sFilename = sHelpFile;
187 ProcessHelp( aLangHM , sLanguage, &pResData , pMergeDataFile );
190 file->Write(sPath);
191 return true;
194 /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
195 void HelpParser::ProcessHelp( LangHashMap* aLangHM , const OString& sCur , ResData *pResData , MergeDataFile* pMergeDataFile ){
197 XMLElement* pXMLElement = NULL;
198 MergeEntrys *pEntrys = NULL;
200 OString sLId;
202 pEntrys = NULL;
204 if( !sCur.equalsIgnoreAsciiCase("en-US") ){
205 pXMLElement = (*aLangHM)[ "en-US" ];
206 if( pXMLElement == NULL )
208 printf("Error: Can't find en-US entry\n");
210 if( pXMLElement != NULL )
212 sLId = pXMLElement->GetOldref();
213 pResData->sId = sLId;
215 OString sNewText;
216 OString sNewdata;
217 OString sSourceText(
218 pXMLElement->ToOString().
219 replaceAll(
220 OString("\n"),
221 OString()).
222 replaceAll(
223 OString("\t"),
224 OString()));
225 // re-add spaces to the beginning of translated string,
226 // important for indentation of Basic code examples
227 sal_Int32 nPreSpaces = 0;
228 sal_Int32 nLen = sSourceText.getLength();
229 while ( (nPreSpaces < nLen) && (*(sSourceText.getStr()+nPreSpaces) == ' ') )
230 nPreSpaces++;
231 if( sCur == "qtz" )
233 sNewText = MergeEntrys::GetQTZText(*pResData, sSourceText);
234 sNewdata = sNewText;
236 else if( pMergeDataFile )
238 pEntrys = pMergeDataFile->GetMergeEntrys( pResData );
239 if( pEntrys != NULL)
241 pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur, true );
242 if (helper::isWellFormedXML(XMLUtil::QuotHTML(sNewText)))
244 sNewdata = sSourceText.copy(0,nPreSpaces) + sNewText;
248 if (!sNewdata.isEmpty())
250 if( pXMLElement != NULL )
252 XMLData *data = new XMLData( sNewdata , NULL , true ); // Add new one
253 pXMLElement->RemoveAndDeleteAllChildren();
254 pXMLElement->AddChild( data );
255 aLangHM->erase( sCur );
258 else if( pResData )
260 SAL_WARN(
261 "l10ntools",
262 "Can't find GID=" << pResData->sGId.getStr() << " LID="
263 << pResData->sId.getStr() << " TYP=" << pResData->sResTyp.getStr() << "\n");
265 pXMLElement->ChangeLanguageTag(sCur);
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */