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

Generated by: LCOV version 1.10