Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #include <fstream>
23 : #include <functional>
24 :
25 : #include <osl/file.hxx>
26 : #include <sal/log.hxx>
27 :
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
43 :
44 : #include "common.hxx"
45 : #include "helper.hxx"
46 :
47 : #if OSL_DEBUG_LEVEL > 2
48 : void HelpParser::Dump(XMLHashMap* rElem_in)
49 : {
50 : for(XMLHashMap::iterator pos = rElem_in->begin();pos != rElem_in->end(); ++pos)
51 : {
52 : Dump(pos->second,pos->first);
53 : }
54 : }
55 :
56 : void HelpParser::Dump(LangHashMap* rElem_in,const rtl::OString & sKey_in)
57 : {
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)
62 : {
63 : x=posn->first;
64 : y=posn->second->ToOString();
65 : fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
66 : }
67 : fprintf(stdout,"+--------------------------+\n");
68 : }
69 : #endif
70 :
71 0 : HelpParser::HelpParser( const rtl::OString &rHelpFile )
72 0 : : sHelpFile( rHelpFile )
73 0 : {};
74 :
75 : /*****************************************************************************/
76 0 : 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 0 : SimpleXMLParser aParser;
81 : rtl::OUString sXmlFile(
82 0 : rtl::OStringToOUString(sHelpFile, RTL_TEXTENCODING_ASCII_US));
83 : //TODO: explicit BOM handling?
84 :
85 0 : std::auto_ptr <XMLFile> file ( aParser.Execute( sXmlFile, pXmlFile ) );
86 :
87 0 : if(file.get() == NULL)
88 : {
89 : printf(
90 : "%s: %s\n",
91 : sHelpFile.getStr(),
92 : (rtl::OUStringToOString(
93 0 : aParser.GetError().sMessage, RTL_TEXTENCODING_ASCII_US).
94 0 : getStr()));
95 0 : exit(-1);
96 : }
97 0 : file->Extract();
98 0 : if( !file->CheckExportStatus() ){
99 0 : return true;
100 : }
101 : std::ofstream aSDFStream(
102 0 : rSDFFile_in.getStr(), std::ios_base::out | std::ios_base::trunc);
103 :
104 0 : if (!aSDFStream.is_open()) {
105 0 : fprintf(stdout,"Can't open file %s\n",rSDFFile_in.getStr());
106 0 : return false;
107 : }
108 :
109 : rtl::OString sActFileName(
110 0 : common::pathnameToken(sHelpFile.getStr(), rRoot_in.getStr()));
111 :
112 0 : XMLHashMap* aXMLStrHM = file->GetStrings();
113 : LangHashMap* pElem;
114 0 : XMLElement* pXMLElement = NULL;
115 :
116 0 : OUStringBuffer sBuffer;
117 0 : const OUString sOUPrj( rPrj_in.getStr() , rPrj_in.getLength() , RTL_TEXTENCODING_ASCII_US );
118 0 : const OUString sOUActFileName(sActFileName.getStr() , sActFileName.getLength() , RTL_TEXTENCODING_ASCII_US );
119 0 : const OUString sOUGsi1( rGsi1.getStr() , rGsi1.getLength() , RTL_TEXTENCODING_ASCII_US );
120 :
121 0 : Export::InitLanguages( false );
122 0 : std::vector<rtl::OString> aLanguages = Export::GetLanguages();
123 :
124 0 : std::vector<rtl::OString> order = file->getOrder();
125 0 : std::vector<rtl::OString>::iterator pos;
126 0 : XMLHashMap::iterator posm;
127 :
128 0 : for( pos = order.begin(); pos != order.end() ; ++pos )
129 : {
130 0 : posm = aXMLStrHM->find( *pos );
131 0 : pElem = posm->second;
132 0 : rtl::OString sCur;
133 :
134 0 : for( unsigned int n = 0; n < aLanguages.size(); n++ )
135 : {
136 0 : sCur = aLanguages[ n ];
137 0 : pXMLElement = (*pElem)[ sCur ];
138 :
139 0 : if( pXMLElement != NULL )
140 : {
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 0 : rtl::OUString()).trim());
149 0 : sBuffer.append( sOUPrj );
150 0 : sBuffer.append('\t');
151 0 : if ( !rRoot_in.isEmpty())
152 0 : sBuffer.append( sOUActFileName );
153 0 : sBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t0\t"));
154 0 : sBuffer.append( sOUGsi1 ); //"help";
155 0 : sBuffer.append('\t');
156 0 : rtl::OString sID = posm->first; // ID
157 0 : sBuffer.append( rtl::OStringToOUString( sID, RTL_TEXTENCODING_UTF8 ) );
158 0 : sBuffer.append('\t');
159 0 : rtl::OString sOldRef = pXMLElement->GetOldref(); // oldref
160 0 : sBuffer.append( rtl::OStringToOUString(sOldRef, RTL_TEXTENCODING_UTF8 ) );
161 0 : sBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t\t\t0\t"));
162 0 : sBuffer.append( rtl::OStringToOUString( sCur, RTL_TEXTENCODING_UTF8 ) );
163 0 : sBuffer.append('\t');
164 0 : sBuffer.append( data );
165 0 : sBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t\t\t\t"));
166 0 : rtl::OString sOut(rtl::OUStringToOString(sBuffer.makeStringAndClear().getStr() , RTL_TEXTENCODING_UTF8));
167 0 : if( !data.isEmpty() )
168 0 : aSDFStream << sOut.getStr() << '\n';
169 0 : pXMLElement=NULL;
170 : }
171 : else
172 : {
173 0 : fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreateSDF, Language %s, File %s\n", sCur.getStr(), sHelpFile.getStr());
174 : }
175 : }
176 0 : }
177 0 : aSDFStream.close();
178 :
179 0 : return sal_True;
180 : }
181 :
182 0 : bool HelpParser::Merge( const rtl::OString &rSDFFile, const rtl::OString &rDestinationFile ,
183 : const rtl::OString& rLanguage , MergeDataFile& aMergeDataFile )
184 : {
185 :
186 : (void) rSDFFile;
187 0 : bool hasNoError = true;
188 :
189 0 : SimpleXMLParser aParser;
190 :
191 : rtl::OUString sXmlFile(
192 0 : rtl::OStringToOUString(sHelpFile, RTL_TEXTENCODING_ASCII_US));
193 : //TODO: explicit BOM handling?
194 :
195 0 : XMLFile* xmlfile = ( aParser.Execute( sXmlFile, new XMLFile( rtl::OUString('0') ) ) );
196 0 : hasNoError = MergeSingleFile( xmlfile , aMergeDataFile , rLanguage , rDestinationFile );
197 0 : delete xmlfile;
198 0 : return hasNoError;
199 : }
200 :
201 0 : bool HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile& aMergeDataFile , const rtl::OString& sLanguage ,
202 : rtl::OString const & sPath )
203 : {
204 0 : file->Extract();
205 :
206 0 : XMLHashMap* aXMLStrHM = file->GetStrings();
207 : LangHashMap* aLangHM;
208 0 : static ResData pResData( "","","");
209 0 : pResData.sResTyp = "help";
210 :
211 0 : for(XMLHashMap::iterator pos=aXMLStrHM->begin();pos!=aXMLStrHM->end();++pos) // Merge every l10n related string
212 : {
213 :
214 0 : 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
220 :
221 0 : pResData.sGId = pos->first;
222 0 : pResData.sFilename = sHelpFile;
223 :
224 0 : ProcessHelp( aLangHM , sLanguage, &pResData , aMergeDataFile );
225 : }
226 :
227 0 : file->Write(sPath);
228 0 : return true;
229 : }
230 :
231 : /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
232 0 : void HelpParser::ProcessHelp( LangHashMap* aLangHM , const rtl::OString& sCur , ResData *pResData , MergeDataFile& aMergeDataFile ){
233 :
234 0 : XMLElement* pXMLElement = NULL;
235 0 : PFormEntrys *pEntrys = NULL;
236 0 : XMLData *data = NULL;
237 :
238 0 : rtl::OString sLId;
239 0 : rtl::OString sGId;
240 :
241 0 : pEntrys = NULL;
242 :
243 0 : if( !sCur.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")) ){
244 0 : pXMLElement = (*aLangHM)[ "en-US" ];
245 0 : if( pXMLElement == NULL )
246 : {
247 0 : printf("Error: Can't find en-US entry\n");
248 : }
249 0 : if( pXMLElement != NULL )
250 : {
251 0 : sLId = pXMLElement->GetOldref();
252 0 : pResData->sId = sLId;
253 :
254 0 : pEntrys = aMergeDataFile.GetPFormEntrys( pResData );
255 0 : if( pEntrys != NULL)
256 : {
257 0 : 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 0 : rtl::OUString()));
266 : // re-add spaces to the beginning of translated string,
267 : // important for indentation of Basic code examples
268 0 : sal_Int32 nPreSpaces = 0;
269 0 : sal_Int32 nLen = sSourceText.getLength();
270 0 : while ( (nPreSpaces < nLen) && (*(sSourceText.getStr()+nPreSpaces) == ' ') )
271 0 : nPreSpaces++;
272 0 : pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur , true );
273 0 : OUString sNewdata;
274 0 : if (helper::isWellFormedXML(helper::QuotHTML(sNewText)))
275 : {
276 : sNewdata = sSourceText.copy(0,nPreSpaces) +
277 0 : rtl::OStringToOUString(sNewText, RTL_TEXTENCODING_UTF8);
278 : }
279 : else
280 : {
281 0 : sNewdata = sSourceText;
282 : }
283 0 : if (!sNewdata.isEmpty())
284 : {
285 0 : if( pXMLElement != NULL )
286 : {
287 0 : data = new XMLData( sNewdata , NULL , true ); // Add new one
288 0 : pXMLElement->RemoveAndDeleteAllChildren();
289 0 : pXMLElement->AddChild( data );
290 0 : aLangHM->erase( sCur );
291 : }
292 0 : }
293 : }
294 0 : else if( pResData == NULL )
295 : {
296 : fprintf(stdout,"Can't find GID=%s LID=%s TYP=%s\n",
297 : pResData->sGId.getStr(), pResData->sId.getStr(),
298 0 : pResData->sResTyp.getStr());
299 : }
300 : pXMLElement->ChangeLanguageTag(
301 0 : rtl::OStringToOUString(sCur, RTL_TEXTENCODING_ASCII_US));
302 : }
303 :
304 0 : }
305 0 : }
306 :
307 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|