helpmerge.cxx 7.85 KB
Newer Older
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
Michael Meeks's avatar
Michael Meeks committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
19

20
#include <sal/config.h>
21

22
#include <fstream>
23
#include <functional>
24
#include <memory>
25

26
#include <osl/file.hxx>
27
#include <sal/log.hxx>
28

29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <helpmerge.hxx>
32
#include <algorithm>
33 34
#include <sys/types.h>
#include <sys/stat.h>
35 36
#include <iostream>
#include <vector>
37
#include <rtl/strbuf.hxx>
38
#ifdef _WIN32
39 40 41
#if !defined WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
42 43
#include <windows.h>
#undef CopyFile
44 45 46
#include <direct.h>
#endif

47 48 49 50
#include <export.hxx>
#include <common.hxx>
#include <helper.hxx>
#include <po.hxx>
51

52
#if OSL_DEBUG_LEVEL > 2
53 54
void HelpParser::Dump(XMLHashMap* rElem_in)
{
55
    for (auto const& pos : *rElem_in)
56
    {
57
        Dump(pos.second,pos.first);
58 59
    }
}
60

61
void HelpParser::Dump(LangHashMap* rElem_in,const OString & sKey_in)
62
{
63
    OString x;
64
    OString y;
65
    fprintf(stdout,"+------------%s-----------+\n",sKey_in.getStr() );
66
    for (auto const& posn : *rElem_in)
67
    {
68 69
        x=posn.first;
        y=posn.second->ToOString();
70
        fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
71 72 73
    }
    fprintf(stdout,"+--------------------------+\n");
}
74
#endif
75

76
HelpParser::HelpParser( const OString &rHelpFile )
77
        : sHelpFile( rHelpFile )
78
          {};
79 80

/*****************************************************************************/
81
bool HelpParser::CreatePO(
82
/*****************************************************************************/
83
    const OString &rPOFile_in, const OString &sHelpFile,
84
    XMLFile* pXmlFile, const OString &rGsi1){
85
    SimpleXMLParser aParser;
86
    //TODO: explicit BOM handling?
87

88
    if (!aParser.Execute( sHelpFile, pXmlFile ))
89
    {
Stephan Bergmann's avatar
Stephan Bergmann committed
90 91 92
        printf(
            "%s: %s\n",
            sHelpFile.getStr(),
93
            aParser.GetError().m_sMessage.getStr());
94 95
        exit(-1);
    }
96 97
    pXmlFile->Extract();
    if( !pXmlFile->CheckExportStatus() ){
98 99
        return true;
    }
100

101 102 103 104
    PoOfstream aPoOutput( rPOFile_in, PoOfstream::APP );

    if (!aPoOutput.isOpen()) {
        fprintf(stdout,"Can't open file %s\n",rPOFile_in.getStr());
105 106 107
        return false;
    }

108
    XMLHashMap* aXMLStrHM = pXmlFile->GetStrings();
109

110
    std::vector<OString> order = pXmlFile->getOrder();
111

112
    for (auto const& pos : order)
113
    {
114
        auto posm = aXMLStrHM->find(pos);
115
        LangHashMap* pElem = posm->second;
116

117
        XMLElement* pXMLElement = (*pElem)[ "en-US" ];
118

119
        if( pXMLElement != nullptr )
120 121
        {
            OString data(
122
                pXMLElement->ToOString().
123 124 125 126 127
                    replaceAll("\n",OString()).
                    replaceAll("\t",OString()).trim());

            common::writePoEntry(
                "Helpex", aPoOutput, sHelpFile, rGsi1,
128
                posm->first, OString(), OString(), data);
129

130
            pXMLElement=nullptr;
131 132
        }
        else
133
        {
Tor Lillqvist's avatar
Tor Lillqvist committed
134 135
            // If this is something totally unexpected, wouldn't an assert() be in order?
            // On the other hand, if this is expected, why the printf?
136
            fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreatePO, File %s\n", sHelpFile.getStr());
137
        }
138
    }
139
    aPoOutput.close();
140

Stephan Bergmann's avatar
Stephan Bergmann committed
141
    return true;
142
}
143

144
bool HelpParser::Merge( const OString &rDestinationFile,
145
    const OString& rLanguage , MergeDataFile* pMergeDataFile )
146
{
147 148
    SimpleXMLParser aParser;

149
    //TODO: explicit BOM handling?
150

151 152
    std::unique_ptr<XMLFile> xmlfile(new XMLFile( OString('0') ));
    if (!aParser.Execute( sHelpFile, xmlfile.get()))
153 154 155 156
    {
        SAL_WARN("l10ntools", "could not parse " << sHelpFile);
        return false;
    }
157
    MergeSingleFile( xmlfile.get() , pMergeDataFile , rLanguage , rDestinationFile );
158
    return true;
159 160
}

161
void HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile* pMergeDataFile , const OString& sLanguage ,
162
                                  OString const & sPath )
163 164 165
{
    file->Extract();

166
    XMLHashMap* aXMLStrHM = file->GetStrings();
167 168
    static ResData s_ResData("","");
    s_ResData.sResTyp   = "help";
169

170
    std::vector<OString> order = file->getOrder();
171

172
    for (auto const& pos : order) // Merge every l10n related string in the same order as export
173
    {
174
        auto posm = aXMLStrHM->find(pos);
175
        LangHashMap*  aLangHM = posm->second;
176
#if OSL_DEBUG_LEVEL > 2
177 178
        printf("*********************DUMPING HASHMAP***************************************");
        Dump(aXMLStrHM);
179
        printf("DBG: sHelpFile = %s\n",sHelpFile.getStr() );
180
#endif
181

182 183
        s_ResData.sGId      =  posm->first;
        s_ResData.sFilename  =  sHelpFile;
184

185
        ProcessHelp( aLangHM , sLanguage, &s_ResData , pMergeDataFile );
186 187
     }

188
    file->Write(sPath);
189 190
}

Andrea Gelmini's avatar
Andrea Gelmini committed
191
/* ProcessHelp method: search for en-US entry and replace it with the current language*/
192
void HelpParser::ProcessHelp( LangHashMap* aLangHM , const OString& sCur , ResData *pResData , MergeDataFile* pMergeDataFile ){
193

194 195
    XMLElement*   pXMLElement = nullptr;
    MergeEntrys   *pEntrys    = nullptr;
196

197
    if( !sCur.equalsIgnoreAsciiCase("en-US") ){
198
        pXMLElement = (*aLangHM)[ "en-US" ];
199
        if( pXMLElement == nullptr )
200 201
        {
            printf("Error: Can't find en-US entry\n");
202
        }
203
        if( pXMLElement != nullptr )
204
        {
205
            OString sNewText;
206 207 208
            OString sNewdata;
            OString sSourceText(
            pXMLElement->ToOString().
209
                replaceAll(
210
                    "\n",
211
                    OString()).
212
                replaceAll(
213
                    "\t",
214
                    OString()));
215 216 217 218
            // re-add spaces to the beginning of translated string,
            // important for indentation of Basic code examples
            sal_Int32 nPreSpaces = 0;
            sal_Int32 nLen = sSourceText.getLength();
219
            while ( (nPreSpaces < nLen) && (sSourceText[nPreSpaces] == ' ') )
220 221
                nPreSpaces++;
            if( sCur == "qtz" )
222
            {
223 224
                sNewText = MergeEntrys::GetQTZText(*pResData, sSourceText);
                sNewdata = sNewText;
225 226 227 228
            }
            else if( pMergeDataFile )
            {
                pEntrys = pMergeDataFile->GetMergeEntrys( pResData );
229
                if( pEntrys != nullptr)
230
                {
Noel Grandin's avatar
Noel Grandin committed
231
                    pEntrys->GetText( sNewText, sCur, true );
232
                    if (helper::isWellFormedXML(XMLUtil::QuotHTML(sNewText)))
233
                    {
234
                        sNewdata = sSourceText.copy(0,nPreSpaces) + sNewText;
235
                    }
236
                }
237
            }
238 239
            if (!sNewdata.isEmpty())
            {
240
                if( pXMLElement != nullptr )
241
                {
242
                    XMLData *data = new XMLData( sNewdata , nullptr ); // Add new one
243 244 245 246 247
                    pXMLElement->RemoveAndDeleteAllChildren();
                    pXMLElement->AddChild( data );
                    aLangHM->erase( sCur );
                }
            }
248
            else
249
            {
250 251
                SAL_WARN(
                    "l10ntools",
252
                    "Can't find GID=" << pResData->sGId << " TYP=" << pResData->sResTyp);
253
            }
254
            pXMLElement->ChangeLanguageTag(sCur);
255 256 257 258
        }

    }
}
259

260
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */