update credits
[LibreOffice.git] / l10ntools / source / uimerge.cxx
blob91a0283f0b4e0ec9b62b9dfa36fedf7be52e777f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <sal/main.h>
12 #include <osl/file.hxx>
14 #include <rtl/strbuf.hxx>
16 #include <libexslt/exslt.h>
17 #include <libxslt/transform.h>
18 #include <libxslt/xslt.h>
19 #include <libxslt/xsltutils.h>
21 #include <stdio.h>
23 #include "common.hxx"
24 #include "helper.hxx"
25 #include "export.hxx"
26 #include "tokens.h"
27 #include "po.hxx"
28 #include <iostream>
29 #include <fstream>
30 #include <vector>
32 OString sInputFileName;
33 OString sOutputFile;
35 int extractTranslations()
37 PoOfstream aPOStream( sOutputFile, PoOfstream::APP);
38 if (!aPOStream.isOpen())
40 fprintf(stderr, "cannot open %s\n", sOutputFile.getStr());
41 return 1;
44 exsltRegisterAll();
46 OString sStyleSheet = OString(getenv("SRC_ROOT")) + OString("/solenv/bin/uilangfilter.xslt");
48 xsltStylesheetPtr stylesheet = xsltParseStylesheetFile ((const xmlChar *)sStyleSheet.getStr());
50 xmlDocPtr doc = xmlParseFile(sInputFileName.getStr());
52 xmlDocPtr res = xsltApplyStylesheet(stylesheet, doc, NULL);
54 for( xmlNodePtr nodeLevel1 = res->children; nodeLevel1 != NULL; nodeLevel1 = nodeLevel1->next)
56 for( xmlNodePtr nodeLevel2 = nodeLevel1->children; nodeLevel2 != NULL; nodeLevel2 = nodeLevel2->next)
58 if (nodeLevel2->type == XML_ELEMENT_NODE)
60 std::vector<OString> vIDs;
61 for(xmlAttrPtr attribute = nodeLevel2->properties; attribute != NULL; attribute = attribute->next)
63 xmlChar *content = xmlNodeListGetString(res, attribute->children, 1);
64 vIDs.push_back(helper::xmlStrToOString(content));
65 xmlFree(content);
67 OString sText = helper::UnQuotHTML(helper::xmlStrToOString(xmlNodeGetContent(nodeLevel2)));
68 common::writePoEntry(
69 "Uiex", aPOStream, sInputFileName, vIDs[0],
70 (vIDs.size()>=2) ? vIDs[1] : OString(),
71 (vIDs.size()>=3) ? vIDs[2] : OString(),
72 OString(), sText);
77 xmlFreeDoc(res);
79 xmlFreeDoc(doc);
81 xsltFreeStylesheet(stylesheet);
83 aPOStream.close();
85 return 0;
88 namespace
90 bool lcl_MergeLang(
91 const MergeDataHashMap &rMap,
92 const OString &rLanguage,
93 const OString &rDestinationFile)
95 std::ofstream aDestination(
96 rDestinationFile.getStr(), std::ios_base::out | std::ios_base::trunc);
97 if (!aDestination.is_open()) {
98 return false;
101 aDestination << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
102 aDestination << "<t>\n";
104 for (MergeDataHashMap::const_iterator aI = rMap.begin(), aEnd = rMap.end(); aI != aEnd; ++aI)
106 if (aI->second->sGID.isEmpty())
107 continue;
109 MergeEntrys* pEntrys = aI->second->GetMergeEntries();
110 OString sOut;
111 pEntrys->GetText( sOut, STRING_TYP_TEXT, rLanguage );
113 if (sOut.isEmpty())
114 continue;
116 aDestination << " <e "
117 << "g=\"" << aI->second->sGID.getStr() << "\" "
118 << "i=\"" << aI->second->sLID.getStr() << "\">"
119 << helper::QuotHTML(sOut).getStr() << "</e>\n";
122 aDestination << "</t>";
123 aDestination.close();
125 return true;
130 bool Merge(
131 const OString &rPOFile,
132 const OString &rSourceFile,
133 const OString &rDestinationDir,
134 const OString &rLanguage )
137 bool bDestinationIsDir(false);
139 const OUString aDestDir(OStringToOUString(rDestinationDir, RTL_TEXTENCODING_UTF8));
140 OUString aDestDirUrl;
141 if (osl::FileBase::E_None == osl::FileBase::getFileURLFromSystemPath(aDestDir, aDestDirUrl))
143 osl::DirectoryItem aTmp;
144 if (osl::DirectoryItem::E_None == osl::DirectoryItem::get(aDestDirUrl, aTmp))
146 osl::FileStatus aDestinationStatus(osl_FileStatus_Mask_Type);
147 if (osl::DirectoryItem::E_None == aTmp.getFileStatus(aDestinationStatus))
148 bDestinationIsDir = aDestinationStatus.isDirectory();
152 if (!bDestinationIsDir)
154 fprintf(stderr, "%s must be a directory\n", rDestinationDir.getStr());
155 return false;
159 MergeDataFile aMergeDataFile( rPOFile, rSourceFile, sal_False );
160 std::vector<OString> aLanguages;
161 if( rLanguage.equalsIgnoreAsciiCase("ALL") )
162 aLanguages = aMergeDataFile.GetLanguages();
163 else
164 aLanguages.push_back(rLanguage);
166 const MergeDataHashMap& rMap = aMergeDataFile.getMap();
167 const OString aDestinationDir(rDestinationDir + "/");
169 bool bResult = true;
170 for(size_t n = 0; n < aLanguages.size(); ++n)
172 OString sCur = aLanguages[ n ];
173 if (sCur.isEmpty() || sCur.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")))
174 continue;
175 const OString aDestinationFile(aDestinationDir + sCur + ".ui");
176 if (!lcl_MergeLang(rMap, sCur, aDestinationFile))
177 bResult = false;
180 return bResult;
183 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
185 int nRetValue = 0;
187 common::HandledArgs aArgs;
188 if ( !common::handleArguments(argc, argv, aArgs) )
190 common::writeUsage("uiex","*.ui");
191 return 1;
194 sInputFileName = aArgs.m_sInputFile;
195 sOutputFile = aArgs.m_sOutputFile;
197 if (!aArgs.m_bMergeMode)
199 nRetValue = extractTranslations();
201 else
203 Merge(aArgs.m_sMergeSrc, sInputFileName, sOutputFile, aArgs.m_sLanguage);
206 return nRetValue;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */