LCOV - code coverage report
Current view: top level - l10ntools/source - helpmerge.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 92 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 7 0.0 %
Legend: Lines: hit not hit

          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             : #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             : #include <sys/types.h>
      34             : #include <sys/stat.h>
      35             : #include <iostream>
      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 "export.hxx"
      45             : #include "common.hxx"
      46             : #include "helper.hxx"
      47             : #include "po.hxx"
      48             : 
      49             : #if OSL_DEBUG_LEVEL > 2
      50             : void HelpParser::Dump(XMLHashMap* rElem_in)
      51             : {
      52             :     for(XMLHashMap::iterator pos = rElem_in->begin();pos != rElem_in->end(); ++pos)
      53             :     {
      54             :         Dump(pos->second,pos->first);
      55             :     }
      56             : }
      57             : 
      58             : void HelpParser::Dump(LangHashMap* rElem_in,const OString & sKey_in)
      59             : {
      60             :     OString x;
      61             :     OString y;
      62             :     fprintf(stdout,"+------------%s-----------+\n",sKey_in.getStr() );
      63             :     for(LangHashMap::iterator posn=rElem_in->begin();posn!=rElem_in->end();++posn)
      64             :     {
      65             :         x=posn->first;
      66             :         y=posn->second->ToOString();
      67             :         fprintf(stdout,"key=%s value=%s\n",x.getStr(),y.getStr());
      68             :     }
      69             :     fprintf(stdout,"+--------------------------+\n");
      70             : }
      71             : #endif
      72             : 
      73           0 : HelpParser::HelpParser( const OString &rHelpFile )
      74           0 :         : sHelpFile( rHelpFile )
      75           0 :           {};
      76             : 
      77             : /*****************************************************************************/
      78           0 : bool HelpParser::CreatePO(
      79             : /*****************************************************************************/
      80             :     const OString &rPOFile_in, const OString &sHelpFile,
      81             :     XMLFile *pXmlFile, const OString &rGsi1){
      82           0 :     SimpleXMLParser aParser;
      83             :     //TODO: explicit BOM handling?
      84             : 
      85           0 :     std::unique_ptr <XMLFile> file ( aParser.Execute( sHelpFile, pXmlFile ) );
      86             : 
      87           0 :     if(file.get() == NULL)
      88             :     {
      89             :         printf(
      90             :             "%s: %s\n",
      91             :             sHelpFile.getStr(),
      92           0 :             aParser.GetError().m_sMessage.getStr());
      93           0 :         exit(-1);
      94             :     }
      95           0 :     file->Extract();
      96           0 :     if( !file->CheckExportStatus() ){
      97           0 :         return true;
      98             :     }
      99             : 
     100           0 :     PoOfstream aPoOutput( rPOFile_in, PoOfstream::APP );
     101             : 
     102           0 :     if (!aPoOutput.isOpen()) {
     103           0 :         fprintf(stdout,"Can't open file %s\n",rPOFile_in.getStr());
     104           0 :         return false;
     105             :     }
     106             : 
     107           0 :     XMLHashMap* aXMLStrHM = file->GetStrings();
     108             : 
     109           0 :     std::vector<OString> order = file->getOrder();
     110           0 :     std::vector<OString>::iterator pos;
     111           0 :     XMLHashMap::iterator posm;
     112             : 
     113           0 :     for( pos = order.begin(); pos != order.end() ; ++pos )
     114             :     {
     115           0 :         posm = aXMLStrHM->find( *pos );
     116           0 :         LangHashMap* pElem = posm->second;
     117             : 
     118           0 :         XMLElement* pXMLElement = (*pElem)[ "en-US" ];
     119             : 
     120           0 :         if( pXMLElement != NULL )
     121             :         {
     122             :             OString data(
     123             :                 pXMLElement->ToOString().
     124             :                     replaceAll("\n",OString()).
     125           0 :                     replaceAll("\t",OString()).trim());
     126             : 
     127             :             common::writePoEntry(
     128             :                 "Helpex", aPoOutput, sHelpFile, rGsi1,
     129           0 :                 posm->first, pXMLElement->GetOldref(), OString(), data);
     130             : 
     131           0 :             pXMLElement=NULL;
     132             :         }
     133             :         else
     134             :         {
     135           0 :             fprintf(stdout,"\nDBG: NullPointer in HelpParser::CreatePO, File %s\n", sHelpFile.getStr());
     136             :         }
     137             :     }
     138           0 :     aPoOutput.close();
     139             : 
     140           0 :     return true;
     141             : }
     142             : 
     143           0 : bool HelpParser::Merge( const OString &rPOFile, const OString &rDestinationFile,
     144             :     const OString& rLanguage , MergeDataFile* pMergeDataFile )
     145             : {
     146             : 
     147             :     (void) rPOFile;
     148             : 
     149           0 :     SimpleXMLParser aParser;
     150             : 
     151             :     //TODO: explicit BOM handling?
     152             : 
     153           0 :     XMLFile* xmlfile = ( aParser.Execute( sHelpFile, new XMLFile( OString('0') ) ) );
     154           0 :     if (!xmlfile)
     155             :     {
     156             :         SAL_WARN("l10ntools", "could not parse " << sHelpFile);
     157           0 :         return false;
     158             :     }
     159           0 :     bool hasNoError = MergeSingleFile( xmlfile , pMergeDataFile , rLanguage , rDestinationFile );
     160           0 :     delete xmlfile;
     161           0 :     return hasNoError;
     162             : }
     163             : 
     164           0 : bool HelpParser::MergeSingleFile( XMLFile* file , MergeDataFile* pMergeDataFile , const OString& sLanguage ,
     165             :                                   OString const & sPath )
     166             : {
     167           0 :     file->Extract();
     168             : 
     169           0 :     XMLHashMap* aXMLStrHM = file->GetStrings();
     170           0 :     static  ResData pResData("","");
     171           0 :     pResData.sResTyp   = "help";
     172             : 
     173           0 :     std::vector<OString> order = file->getOrder();
     174           0 :     std::vector<OString>::iterator pos;
     175           0 :     XMLHashMap::iterator posm;
     176             : 
     177           0 :     for( pos = order.begin(); pos != order.end() ; ++pos ) // Merge every l10n related string in the same order as export
     178             :     {
     179           0 :         posm = aXMLStrHM->find( *pos );
     180           0 :         LangHashMap*  aLangHM = posm->second;
     181             : #if OSL_DEBUG_LEVEL > 2
     182             :         printf("*********************DUMPING HASHMAP***************************************");
     183             :         Dump(aXMLStrHM);
     184             :         printf("DBG: sHelpFile = %s\n",sHelpFile.getStr() );
     185             : #endif
     186             : 
     187           0 :         pResData.sGId      =  posm->first;
     188           0 :         pResData.sFilename  =  sHelpFile;
     189             : 
     190           0 :         ProcessHelp( aLangHM , sLanguage, &pResData , pMergeDataFile );
     191             :      }
     192             : 
     193           0 :     file->Write(sPath);
     194           0 :     return true;
     195             : }
     196             : 
     197             : /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
     198           0 : void HelpParser::ProcessHelp( LangHashMap* aLangHM , const OString& sCur , ResData *pResData , MergeDataFile* pMergeDataFile ){
     199             : 
     200           0 :     XMLElement*   pXMLElement = NULL;
     201           0 :     MergeEntrys   *pEntrys    = NULL;
     202             : 
     203           0 :     OString sLId;
     204             : 
     205           0 :     pEntrys = NULL;
     206             : 
     207           0 :     if( !sCur.equalsIgnoreAsciiCase("en-US") ){
     208           0 :         pXMLElement = (*aLangHM)[ "en-US" ];
     209           0 :         if( pXMLElement == NULL )
     210             :         {
     211           0 :             printf("Error: Can't find en-US entry\n");
     212             :         }
     213           0 :         if( pXMLElement != NULL )
     214             :         {
     215           0 :             sLId    = pXMLElement->GetOldref();
     216           0 :             pResData->sId     =  sLId;
     217             : 
     218           0 :             OString sNewText;
     219           0 :             OString sNewdata;
     220             :             OString sSourceText(
     221             :             pXMLElement->ToOString().
     222             :                 replaceAll(
     223             :                     OString("\n"),
     224             :                     OString()).
     225             :                 replaceAll(
     226             :                     OString("\t"),
     227           0 :                     OString()));
     228             :             // re-add spaces to the beginning of translated string,
     229             :             // important for indentation of Basic code examples
     230           0 :             sal_Int32 nPreSpaces = 0;
     231           0 :             sal_Int32 nLen = sSourceText.getLength();
     232           0 :             while ( (nPreSpaces < nLen) && (*(sSourceText.getStr()+nPreSpaces) == ' ') )
     233           0 :                 nPreSpaces++;
     234           0 :             if( sCur == "qtz" )
     235             :             {
     236           0 :                 sNewText = MergeEntrys::GetQTZText(*pResData, sSourceText);
     237           0 :                 sNewdata = sNewText;
     238             :             }
     239           0 :             else if( pMergeDataFile )
     240             :             {
     241           0 :                 pEntrys = pMergeDataFile->GetMergeEntrys( pResData );
     242           0 :                 if( pEntrys != NULL)
     243             :                 {
     244           0 :                     pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur, true );
     245           0 :                     if (helper::isWellFormedXML(XMLUtil::QuotHTML(sNewText)))
     246             :                     {
     247           0 :                         sNewdata = sSourceText.copy(0,nPreSpaces) + sNewText;
     248             :                     }
     249             :                 }
     250             :             }
     251           0 :             if (!sNewdata.isEmpty())
     252             :             {
     253           0 :                 if( pXMLElement != NULL )
     254             :                 {
     255           0 :                     XMLData *data = new XMLData( sNewdata , NULL , true ); // Add new one
     256           0 :                     pXMLElement->RemoveAndDeleteAllChildren();
     257           0 :                     pXMLElement->AddChild( data );
     258           0 :                     aLangHM->erase( sCur );
     259             :                 }
     260             :             }
     261             :             else
     262             :             {
     263             :                 SAL_WARN(
     264             :                     "l10ntools",
     265             :                     "Can't find GID=" << pResData->sGId.getStr() << " LID="
     266             :                         << pResData->sId.getStr() << " TYP=" << pResData->sResTyp.getStr());
     267             :             }
     268           0 :             pXMLElement->ChangeLanguageTag(sCur);
     269             :         }
     270             : 
     271           0 :     }
     272           0 : }
     273             : 
     274             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11