fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / l10ntools / source / uimerge.cxx
blob8a0450d306e45ffadca1081338a7ba514441ba5a
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 (reinterpret_cast<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 g=\"" << aI->second->sGID.getStr() << "\" i=\""
117 << aI->second->sLID.getStr() << "\">"
118 << helper::QuotHTML(sOut).getStr() << "</e>\n";
121 aDestination << "</t>";
122 aDestination.close();
124 return true;
129 bool Merge(
130 const OString &rPOFile,
131 const OString &rSourceFile,
132 const OString &rDestinationDir,
133 const OString &rLanguage )
136 bool bDestinationIsDir(false);
138 const OUString aDestDir(OStringToOUString(rDestinationDir, RTL_TEXTENCODING_UTF8));
139 OUString aDestDirUrl;
140 if (osl::FileBase::E_None == osl::FileBase::getFileURLFromSystemPath(aDestDir, aDestDirUrl))
142 osl::DirectoryItem aTmp;
143 if (osl::DirectoryItem::E_None == osl::DirectoryItem::get(aDestDirUrl, aTmp))
145 osl::FileStatus aDestinationStatus(osl_FileStatus_Mask_Type);
146 if (osl::DirectoryItem::E_None == aTmp.getFileStatus(aDestinationStatus))
147 bDestinationIsDir = aDestinationStatus.isDirectory();
151 if (!bDestinationIsDir)
153 fprintf(stderr, "%s must be a directory\n", rDestinationDir.getStr());
154 return false;
158 MergeDataFile aMergeDataFile( rPOFile, rSourceFile, false );
159 std::vector<OString> aLanguages;
160 if( rLanguage.equalsIgnoreAsciiCase("ALL") )
161 aLanguages = aMergeDataFile.GetLanguages();
162 else
163 aLanguages.push_back(rLanguage);
165 const MergeDataHashMap& rMap = aMergeDataFile.getMap();
166 const OString aDestinationDir(rDestinationDir + "/");
168 bool bResult = true;
169 for(size_t n = 0; n < aLanguages.size(); ++n)
171 OString sCur = aLanguages[ n ];
172 if (sCur.isEmpty() || sCur.equalsIgnoreAsciiCase("en-US"))
173 continue;
174 const OString aDestinationFile(aDestinationDir + sCur + ".ui");
175 if (!lcl_MergeLang(rMap, sCur, aDestinationFile))
176 bResult = false;
179 return bResult;
182 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
184 int nRetValue = 0;
186 common::HandledArgs aArgs;
187 if ( !common::handleArguments(argc, argv, aArgs) )
189 common::writeUsage("uiex","*.ui");
190 return 1;
193 sInputFileName = aArgs.m_sInputFile;
194 sOutputFile = aArgs.m_sOutputFile;
196 if (!aArgs.m_bMergeMode)
198 nRetValue = extractTranslations();
200 else
202 Merge(aArgs.m_sMergeSrc, sInputFileName, sOutputFile, aArgs.m_sLanguage);
205 return nRetValue;
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */