Bump for 4.0-15
[LibreOffice.git] / l10ntools / source / helpmerge.cxx
blob317378544928b6259ab0d0da273f404fb62641dd
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 <fstream>
36 #include <vector>
37 #include <rtl/strbuf.hxx>
38 #ifdef WNT
39 #include <windows.h>
40 #undef CopyFile
41 #include <direct.h>
42 #endif
44 #include "common.hxx"
45 #include "helper.hxx"
47 #if OSL_DEBUG_LEVEL > 2
48 void HelpParser::Dump(XMLHashMap* rElem_in)
50 for(XMLHashMap::iterator pos = rElem_in->begin();pos != rElem_in->end(); ++pos)
52 Dump(pos->second,pos->first);
56 void HelpParser::Dump(LangHashMap* rElem_in,const rtl::OString & sKey_in)
58 rtl::OString x;
59 OString y;
60 fprintf(stdout,"+------------%s-----------+\n",sKey_in.getStr() );
61 for(LangHashMap::iterator posn=rElem_in->begin();posn!=rElem_in->end();++posn)
63 x=posn->first;
64 y=posn->second->ToOString();
65 fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
67 fprintf(stdout,"+--------------------------+\n");
69 #endif
71 HelpParser::HelpParser( const rtl::OString &rHelpFile )
72 : sHelpFile( rHelpFile )
73 {};
75 /*****************************************************************************/
76 bool HelpParser::CreateSDF(
77 /*****************************************************************************/
78 const rtl::OString &rSDFFile_in, const rtl::OString &rPrj_in,const rtl::OString &rRoot_in,
79 const rtl::OString &sHelpFile, XMLFile *pXmlFile, const rtl::OString &rGsi1){
80 SimpleXMLParser aParser;
81 rtl::OUString sXmlFile(
82 rtl::OStringToOUString(sHelpFile, RTL_TEXTENCODING_ASCII_US));
83 //TODO: explicit BOM handling?
85 std::auto_ptr <XMLFile> file ( aParser.Execute( sXmlFile, pXmlFile ) );
87 if(file.get() == NULL)
89 printf(
90 "%s: %s\n",
91 sHelpFile.getStr(),
92 (rtl::OUStringToOString(
93 aParser.GetError().sMessage, RTL_TEXTENCODING_ASCII_US).
94 getStr()));
95 exit(-1);
97 file->Extract();
98 if( !file->CheckExportStatus() ){
99 return true;
101 std::ofstream aSDFStream(
102 rSDFFile_in.getStr(), std::ios_base::out | std::ios_base::trunc);
104 if (!aSDFStream.is_open()) {
105 fprintf(stdout,"Can't open file %s\n",rSDFFile_in.getStr());
106 return false;
109 rtl::OString sActFileName(
110 common::pathnameToken(sHelpFile.getStr(), rRoot_in.getStr()));
112 XMLHashMap* aXMLStrHM = file->GetStrings();
113 LangHashMap* pElem;
114 XMLElement* pXMLElement = NULL;
116 OUStringBuffer sBuffer;
117 const OUString sOUPrj( rPrj_in.getStr() , rPrj_in.getLength() , RTL_TEXTENCODING_ASCII_US );
118 const OUString sOUActFileName(sActFileName.getStr() , sActFileName.getLength() , RTL_TEXTENCODING_ASCII_US );
119 const OUString sOUGsi1( rGsi1.getStr() , rGsi1.getLength() , RTL_TEXTENCODING_ASCII_US );
121 Export::InitLanguages( false );
122 std::vector<rtl::OString> aLanguages = Export::GetLanguages();
124 std::vector<rtl::OString> order = file->getOrder();
125 std::vector<rtl::OString>::iterator pos;
126 XMLHashMap::iterator posm;
128 for( pos = order.begin(); pos != order.end() ; ++pos )
130 posm = aXMLStrHM->find( *pos );
131 pElem = posm->second;
132 rtl::OString sCur;
134 for( unsigned int n = 0; n < aLanguages.size(); n++ )
136 sCur = aLanguages[ n ];
137 pXMLElement = (*pElem)[ sCur ];
139 if( pXMLElement != NULL )
141 OUString data(
142 pXMLElement->ToOUString().
143 replaceAll(
144 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n")),
145 rtl::OUString()).
146 replaceAll(
147 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t")),
148 rtl::OUString()).trim());
149 sBuffer.append( sOUPrj );
150 sBuffer.append('\t');
151 if ( !rRoot_in.isEmpty())
152 sBuffer.append( sOUActFileName );
153 sBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t0\t"));
154 sBuffer.append( sOUGsi1 ); //"help";
155 sBuffer.append('\t');
156 rtl::OString sID = posm->first; // ID
157 sBuffer.append( rtl::OStringToOUString( sID, RTL_TEXTENCODING_UTF8 ) );
158 sBuffer.append('\t');
159 rtl::OString sOldRef = pXMLElement->GetOldref(); // oldref
160 sBuffer.append( rtl::OStringToOUString(sOldRef, RTL_TEXTENCODING_UTF8 ) );
161 sBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t\t\t0\t"));
162 sBuffer.append( rtl::OStringToOUString( sCur, RTL_TEXTENCODING_UTF8 ) );
163 sBuffer.append('\t');
164 sBuffer.append( data );
165 sBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t\t\t\t"));
166 rtl::OString sOut(rtl::OUStringToOString(sBuffer.makeStringAndClear().getStr() , RTL_TEXTENCODING_UTF8));
167 if( !data.isEmpty() )
168 aSDFStream << sOut.getStr() << '\n';
169 pXMLElement=NULL;
171 else
173 fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreateSDF, Language %s, File %s\n", sCur.getStr(), sHelpFile.getStr());
177 aSDFStream.close();
179 return sal_True;
182 bool HelpParser::Merge( const rtl::OString &rSDFFile, const rtl::OString &rDestinationFile ,
183 const rtl::OString& rLanguage , MergeDataFile& aMergeDataFile )
186 (void) rSDFFile;
187 bool hasNoError = true;
189 SimpleXMLParser aParser;
191 rtl::OUString sXmlFile(
192 rtl::OStringToOUString(sHelpFile, RTL_TEXTENCODING_ASCII_US));
193 //TODO: explicit BOM handling?
195 XMLFile* xmlfile = ( aParser.Execute( sXmlFile, new XMLFile( rtl::OUString('0') ) ) );
196 hasNoError = MergeSingleFile( xmlfile , aMergeDataFile , rLanguage , rDestinationFile );
197 delete xmlfile;
198 return hasNoError;
201 bool HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile& aMergeDataFile , const rtl::OString& sLanguage ,
202 rtl::OString const & sPath )
204 file->Extract();
206 XMLHashMap* aXMLStrHM = file->GetStrings();
207 LangHashMap* aLangHM;
208 static ResData pResData( "","","");
209 pResData.sResTyp = "help";
211 for(XMLHashMap::iterator pos=aXMLStrHM->begin();pos!=aXMLStrHM->end();++pos) // Merge every l10n related string
214 aLangHM = pos->second;
215 #if OSL_DEBUG_LEVEL > 2
216 printf("*********************DUMPING HASHMAP***************************************");
217 Dump(aXMLStrHM);
218 printf("DBG: sHelpFile = %s\n",sHelpFile.getStr() );
219 #endif
221 pResData.sGId = pos->first;
222 pResData.sFilename = sHelpFile;
224 ProcessHelp( aLangHM , sLanguage, &pResData , aMergeDataFile );
227 file->Write(sPath);
228 return true;
231 /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
232 void HelpParser::ProcessHelp( LangHashMap* aLangHM , const rtl::OString& sCur , ResData *pResData , MergeDataFile& aMergeDataFile ){
234 XMLElement* pXMLElement = NULL;
235 PFormEntrys *pEntrys = NULL;
236 XMLData *data = NULL;
238 rtl::OString sLId;
239 rtl::OString sGId;
241 pEntrys = NULL;
243 if( !sCur.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")) ){
244 pXMLElement = (*aLangHM)[ "en-US" ];
245 if( pXMLElement == NULL )
247 printf("Error: Can't find en-US entry\n");
249 if( pXMLElement != NULL )
251 sLId = pXMLElement->GetOldref();
252 pResData->sId = sLId;
254 pEntrys = aMergeDataFile.GetPFormEntrys( pResData );
255 if( pEntrys != NULL)
257 rtl::OString sNewText;
258 rtl::OUString sSourceText(
259 pXMLElement->ToOUString().
260 replaceAll(
261 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n")),
262 rtl::OUString()).
263 replaceAll(
264 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t")),
265 rtl::OUString()));
266 // re-add spaces to the beginning of translated string,
267 // important for indentation of Basic code examples
268 sal_Int32 nPreSpaces = 0;
269 sal_Int32 nLen = sSourceText.getLength();
270 while ( (nPreSpaces < nLen) && (*(sSourceText.getStr()+nPreSpaces) == ' ') )
271 nPreSpaces++;
272 pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur , true );
273 OUString sNewdata;
274 if (helper::isWellFormedXML(helper::QuotHTML(sNewText)))
276 sNewdata = sSourceText.copy(0,nPreSpaces) +
277 rtl::OStringToOUString(sNewText, RTL_TEXTENCODING_UTF8);
279 else
281 sNewdata = sSourceText;
283 if (!sNewdata.isEmpty())
285 if( pXMLElement != NULL )
287 data = new XMLData( sNewdata , NULL , true ); // Add new one
288 pXMLElement->RemoveAndDeleteAllChildren();
289 pXMLElement->AddChild( data );
290 aLangHM->erase( sCur );
294 else if( pResData == NULL )
296 fprintf(stdout,"Can't find GID=%s LID=%s TYP=%s\n",
297 pResData->sGId.getStr(), pResData->sId.getStr(),
298 pResData->sResTyp.getStr());
300 pXMLElement->ChangeLanguageTag(
301 rtl::OStringToOUString(sCur, RTL_TEXTENCODING_ASCII_US));
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */