LCOV - code coverage report
Current view: top level - libreoffice/l10ntools/source - uimerge.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 102 0.0 %
Date: 2012-12-27 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             : 
      10             : #include <sal/main.h>
      11             : 
      12             : #include <rtl/strbuf.hxx>
      13             : 
      14             : #include <libexslt/exslt.h>
      15             : #include <libxslt/transform.h>
      16             : #include <libxslt/xslt.h>
      17             : #include <libxslt/xsltutils.h>
      18             : 
      19             : #include <stdio.h>
      20             : 
      21             : #include "common.hxx"
      22             : #include "export.hxx"
      23             : #include "xrmmerge.hxx"
      24             : #include "tokens.h"
      25             : #include <iostream>
      26             : #include <fstream>
      27             : #include <vector>
      28             : 
      29           0 : rtl::OString sPrj;
      30           0 : rtl::OString sPrjRoot;
      31           0 : rtl::OString sInputFileName;
      32           0 : rtl::OString sOutputFile;
      33             : 
      34           0 : int extractTranslations()
      35             : {
      36           0 :     FILE *pOutFile = fopen(sOutputFile.getStr(), "w");
      37           0 :     if (!pOutFile)
      38             :     {
      39           0 :         fprintf(stderr, "cannot open %s\n", sOutputFile.getStr());
      40           0 :         return 1;
      41             :     }
      42             : 
      43           0 :     exsltRegisterAll();
      44             : 
      45           0 :     rtl::OString sActFileName = common::pathnameToken(sInputFileName.getStr(), sPrjRoot.getStr());
      46             : 
      47           0 :     rtl::OString sStyleSheet = rtl::OString(getenv("SRC_ROOT"))  + rtl::OString("/solenv/bin/uilangfilter.xslt");
      48             : 
      49           0 :     xsltStylesheetPtr stylesheet = xsltParseStylesheetFile ((const xmlChar *)sStyleSheet.getStr());
      50             : 
      51           0 :     xmlDocPtr doc = xmlParseFile(sInputFileName.getStr());
      52             : 
      53           0 :     xmlDocPtr res = xsltApplyStylesheet(stylesheet, doc, NULL);
      54             : 
      55           0 :     for( xmlNodePtr nodeLevel1 = res->children; nodeLevel1 != NULL; nodeLevel1 = nodeLevel1->next)
      56             :     {
      57           0 :         for( xmlNodePtr nodeLevel2 = nodeLevel1->children; nodeLevel2 != NULL; nodeLevel2 = nodeLevel2->next)
      58             :         {
      59           0 :             if (nodeLevel2->type == XML_ELEMENT_NODE)
      60             :             {
      61           0 :                 fprintf(pOutFile, "%s\t%s\t0\t",sPrj.getStr(), sActFileName.getStr());
      62           0 :                 for(xmlAttrPtr attribute = nodeLevel2->properties; attribute != NULL; attribute = attribute->next)
      63             :                 {
      64           0 :                     xmlChar *content = xmlNodeListGetString(res, attribute->children, 1);
      65           0 :                     fprintf(pOutFile, "%s\t", content);
      66           0 :                     xmlFree(content);
      67             :                 }
      68           0 :                 fprintf(pOutFile, "\t\t0\ten-US\t%s\t\t\t\t\n", xmlNodeGetContent(nodeLevel2));
      69             :             }
      70             :         }
      71             :     }
      72             : 
      73           0 :     xmlFreeDoc(res);
      74             : 
      75           0 :     xmlFreeDoc(doc);
      76             : 
      77           0 :     xsltFreeStylesheet(stylesheet);
      78             : 
      79           0 :     fclose(pOutFile);
      80             : 
      81           0 :     return 0;
      82             : }
      83             : 
      84             : namespace
      85             : {
      86           0 :     rtl::OString QuotHTML(const rtl::OString &rString)
      87             :     {
      88           0 :         rtl::OStringBuffer sReturn;
      89           0 :         for (sal_Int32 i = 0; i < rString.getLength(); ++i) {
      90           0 :             switch (rString[i]) {
      91             :             case '\\':
      92           0 :                 if (i < rString.getLength()) {
      93           0 :                     switch (rString[i + 1]) {
      94             :                     case '"':
      95             :                     case '<':
      96             :                     case '>':
      97             :                     case '\\':
      98           0 :                         ++i;
      99           0 :                         break;
     100             :                     }
     101             :                 }
     102             :                 // fall through
     103             :             default:
     104           0 :                 sReturn.append(rString[i]);
     105           0 :                 break;
     106             : 
     107             :             case '<':
     108           0 :                 sReturn.append("&lt;");
     109           0 :                 break;
     110             : 
     111             :             case '>':
     112           0 :                 sReturn.append("&gt;");
     113           0 :                 break;
     114             : 
     115             :             case '"':
     116           0 :                 sReturn.append("&quot;");
     117           0 :                 break;
     118             : 
     119             :             case '&':
     120           0 :                 if (rString.matchL(RTL_CONSTASCII_STRINGPARAM("&amp;"), i))
     121           0 :                     sReturn.append('&');
     122             :                 else
     123           0 :                     sReturn.append(RTL_CONSTASCII_STRINGPARAM("&amp;"));
     124           0 :                 break;
     125             :             }
     126             :         }
     127           0 :         return sReturn.makeStringAndClear();
     128             :     }
     129             : }
     130             : 
     131           0 : bool Merge(
     132             :     const rtl::OString &rSDFFile,
     133             :     const rtl::OString &rSourceFile,
     134             :     const rtl::OString &rDestinationFile)
     135             : {
     136           0 :     Export::InitLanguages( true );
     137             :     std::ofstream aDestination(
     138           0 :         rDestinationFile.getStr(), std::ios_base::out | std::ios_base::trunc);
     139           0 :     if (!aDestination.is_open()) {
     140           0 :         return false;
     141             :     }
     142             : 
     143           0 :     aDestination << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
     144           0 :     aDestination << "<t>\n";
     145             : 
     146           0 :     MergeDataFile aMergeDataFile( rSDFFile, rSourceFile, sal_False );
     147           0 :     rtl::OString sTmp( Export::sLanguages );
     148           0 :     if( sTmp.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("ALL")) )
     149           0 :         Export::SetLanguages( aMergeDataFile.GetLanguages() );
     150             : 
     151           0 :     std::vector<rtl::OString> aLanguages = Export::GetLanguages();
     152             : 
     153           0 :     const MergeDataHashMap& rMap = aMergeDataFile.getMap();
     154             : 
     155           0 :     for(size_t n = 0; n < aLanguages.size(); ++n)
     156             :     {
     157           0 :         rtl::OString sCur = aLanguages[ n ];
     158           0 :         if (sCur.isEmpty() || sCur.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")))
     159           0 :             continue;
     160           0 :         for (MergeDataHashMap::const_iterator aI = rMap.begin(), aEnd = rMap.end(); aI != aEnd; ++aI)
     161             :         {
     162           0 :             if (aI->second->sGID.isEmpty())
     163           0 :                 continue;
     164             : 
     165           0 :             PFormEntrys* pFoo = aI->second->GetPFormEntries();
     166           0 :             rtl::OString sOut;
     167           0 :             pFoo->GetText( sOut, STRING_TYP_TEXT, sCur);
     168             : 
     169           0 :             if (sOut.isEmpty())
     170           0 :                 continue;
     171             : 
     172           0 :             aDestination << " <e "
     173           0 :                 << "g=\"" << aI->second->sGID.getStr() << "\" "
     174           0 :                 << "i=\"" << aI->second->sLID.getStr() << "\">"
     175           0 :                 << QuotHTML(sOut).getStr() << "</e>\n";
     176           0 :         }
     177           0 :     }
     178             : 
     179           0 :     aDestination << "</t>";
     180           0 :     aDestination.close();
     181           0 :     return sal_True;
     182             : }
     183             : 
     184           0 : SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
     185             : {
     186           0 :     int nRetValue = 0;
     187             : 
     188           0 :     HandledArgs aArgs;
     189           0 :     if ( !Export::handleArguments(argc, argv, aArgs) )
     190             :     {
     191           0 :         Export::writeUsage("uiex","ui");
     192           0 :         return 1;
     193             :     }
     194             : 
     195           0 :     sPrj = aArgs.m_sPrj;
     196           0 :     sPrjRoot = aArgs.m_sPrjRoot;
     197           0 :     sInputFileName = aArgs.m_sInputFile;
     198           0 :     sOutputFile = aArgs.m_sOutputFile;
     199             : 
     200           0 :     if (!aArgs.m_bMergeMode)
     201             :     {
     202           0 :         if (Export::sLanguages != "en-US")
     203             :         {
     204           0 :             fprintf(stderr, "only en-US can exist in source .ui files\n");
     205           0 :             nRetValue = 1;
     206             :         }
     207             :         else
     208           0 :             nRetValue = extractTranslations();
     209             :     }
     210             :     else
     211             :     {
     212           0 :         Merge(aArgs.m_sMergeSrc, sInputFileName, sOutputFile);
     213             :     }
     214             : 
     215           0 :     return nRetValue;
     216           0 : }
     217             : 
     218             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10