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

Generated by: LCOV version 1.10