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 <cstddef>
23 : #include <cstdlib>
24 : #include <iostream>
25 : #include <string>
26 : #include <vector>
27 : #include <algorithm>
28 :
29 : #include "osl/file.h"
30 : #include "osl/file.hxx"
31 : #include "osl/thread.h"
32 : #include "rtl/string.h"
33 : #include "rtl/string.hxx"
34 : #include "rtl/textcvt.h"
35 : #include "rtl/strbuf.hxx"
36 : #include "rtl/ustring.h"
37 : #include "rtl/ustring.hxx"
38 : #include "sal/macros.h"
39 : #include "sal/main.h"
40 : #include "sal/types.h"
41 :
42 : #include "po.hxx"
43 :
44 : using namespace std;
45 :
46 : namespace {
47 :
48 : struct AsciiString {
49 : char const * string;
50 : sal_Int32 length;
51 : };
52 :
53 0 : bool matchList(
54 : const OUString& rUrl, const AsciiString* pList, size_t nLength)
55 : {
56 0 : for (size_t i = 0; i != nLength; ++i) {
57 0 : if (rUrl.endsWithAsciiL(pList[i].string, pList[i].length)) {
58 0 : return true;
59 : }
60 : }
61 0 : return false;
62 : }
63 :
64 0 : bool passesNegativeList(const OUString& rUrl) {
65 : static const AsciiString list[] = {
66 : { RTL_CONSTASCII_STRINGPARAM("/dictionaries.xcu") },
67 : { RTL_CONSTASCII_STRINGPARAM(
68 : "/dictionaries/da_DK/help/da/help.tree") },
69 : { RTL_CONSTASCII_STRINGPARAM(
70 : "/dictionaries/da_DK/help/da/"
71 : "org.openoffice.da.hunspell.dictionaries/page1.xhp") },
72 : { RTL_CONSTASCII_STRINGPARAM(
73 : "/dictionaries/da_DK/help/da/"
74 : "org.openoffice.da.hunspell.dictionaries/page2.xhp") },
75 : { RTL_CONSTASCII_STRINGPARAM(
76 : "/dictionaries/hu_HU/help/hu/help.tree") },
77 : { RTL_CONSTASCII_STRINGPARAM(
78 : "/dictionaries/hu_HU/help/hu/"
79 : "org.openoffice.hu.hunspell.dictionaries/page1.xhp") },
80 : { RTL_CONSTASCII_STRINGPARAM(
81 : "/officecfg/registry/data/org/openoffice/Office/"
82 : "Accelerators.xcu") },
83 : { RTL_CONSTASCII_STRINGPARAM(
84 : "/officecfg/registry/data/org/openoffice/Office/SFX.xcu") }
85 : };
86 0 : return !matchList(rUrl, list, SAL_N_ELEMENTS(list));
87 : }
88 :
89 0 : bool passesPositiveList(const OUString& rUrl) {
90 : static const AsciiString list[] = {
91 : { RTL_CONSTASCII_STRINGPARAM(
92 : "/chart2/source/controller/dialogs/res_DataLabel_tmpl.hrc") },
93 : { RTL_CONSTASCII_STRINGPARAM(
94 : "/chart2/source/controller/dialogs/res_ErrorBar_tmpl.hrc") },
95 : { RTL_CONSTASCII_STRINGPARAM(
96 : "/chart2/source/controller/dialogs/res_LegendPosition_tmpl.hrc") },
97 : { RTL_CONSTASCII_STRINGPARAM(
98 : "/chart2/source/controller/dialogs/"
99 : "res_SecondaryAxisCheckBoxes_tmpl.hrc") },
100 : { RTL_CONSTASCII_STRINGPARAM(
101 : "/chart2/source/controller/dialogs/res_Statistic_tmpl.hrc") },
102 : { RTL_CONSTASCII_STRINGPARAM(
103 : "/chart2/source/controller/dialogs/res_Titlesx_tmpl.hrc") },
104 : { RTL_CONSTASCII_STRINGPARAM(
105 : "/chart2/source/controller/dialogs/res_Trendline_tmpl.hrc") },
106 : { RTL_CONSTASCII_STRINGPARAM(
107 : "/chart2/source/controller/menu/MenuItems_tmpl.hrc") },
108 : { RTL_CONSTASCII_STRINGPARAM(
109 : "/dbaccess/source/ui/dlg/AutoControls_tmpl.hrc") },
110 : { RTL_CONSTASCII_STRINGPARAM(
111 : "/dbaccess/source/ui/inc/toolbox_tmpl.hrc") },
112 : { RTL_CONSTASCII_STRINGPARAM("/description.xml") },
113 : { RTL_CONSTASCII_STRINGPARAM("/android/sdremote/res/values/strings.xml") },
114 : { RTL_CONSTASCII_STRINGPARAM("/offmgr/inc/offmenu_tmpl.hrc") },
115 : { RTL_CONSTASCII_STRINGPARAM(
116 : "/offmgr/source/offapp/intro/intro_tmpl.hrc") },
117 : { RTL_CONSTASCII_STRINGPARAM("/svx/inc/globlmn_tmpl.hrc") },
118 : { RTL_CONSTASCII_STRINGPARAM("/svx/source/intro/intro_tmpl.hrc") },
119 : { RTL_CONSTASCII_STRINGPARAM(
120 : "/svx/source/unodialogs/textconversiondlgs/"
121 : "chinese_direction_tmpl.hrc") },
122 : { RTL_CONSTASCII_STRINGPARAM("/sw/source/ui/inc/swacc_tmpl.hrc") },
123 : { RTL_CONSTASCII_STRINGPARAM("/sw/source/ui/inc/swmn_tmpl.hrc") },
124 : { RTL_CONSTASCII_STRINGPARAM("/sw/source/ui/inc/toolbox_tmpl.hrc") }
125 : };
126 0 : return matchList(rUrl, list, SAL_N_ELEMENTS(list));
127 : }
128 :
129 0 : void handleCommand(
130 : const OString& rInPath, const OString& rOutPath,
131 : const OString& rExecutable)
132 : {
133 0 : OStringBuffer buf(OString(getenv("SOLARVER")));
134 0 : buf.append('/');
135 0 : buf.append(OString(getenv("INPATH_FOR_BUILD")));
136 0 : buf.append("/bin/");
137 0 : buf.append(rExecutable);
138 0 : buf.append(" -i ");
139 0 : buf.append(rInPath);
140 0 : buf.append(" -o ");
141 0 : buf.append(rOutPath);
142 :
143 0 : const OString cmd = buf.makeStringAndClear();
144 0 : if (system(cmd.getStr()) != 0)
145 : {
146 0 : cerr << "Error: Failed to execute " << cmd.getStr() << '\n';
147 0 : throw false; //TODO
148 0 : }
149 0 : }
150 :
151 0 : void InitPoFile(
152 : const OString& rProject, const OString& rInPath,
153 : const OString& rPotDir, const OString& rOutPath )
154 : {
155 : //Create directory for po file
156 : {
157 : OUString outDir =
158 : OStringToOUString(
159 0 : rPotDir.copy(0,rPotDir.lastIndexOf('/')), RTL_TEXTENCODING_UTF8);
160 0 : OUString outDirUrl;
161 0 : if (osl::FileBase::getFileURLFromSystemPath(outDir, outDirUrl)
162 : != osl::FileBase::E_None)
163 : {
164 0 : cerr << "Error: Cannot convert pathname to URL in " << __FILE__ << ", in line " << __LINE__ << "\n"
165 0 : << " outDir: " << OUStringToOString(outDir, RTL_TEXTENCODING_ASCII_US).getStr() << "\n";
166 0 : throw false; //TODO
167 : }
168 0 : osl::Directory::createPath(outDirUrl);
169 : }
170 :
171 : //Add header to the po file
172 0 : PoOfstream aPoOutPut;
173 0 : aPoOutPut.open(rOutPath.getStr());
174 0 : if (!aPoOutPut.isOpen())
175 : {
176 : cerr
177 0 : << "Error: Cannot open po file "
178 0 : << rOutPath.getStr() << "\n";
179 0 : throw false; //TODO
180 : }
181 :
182 0 : const sal_Int32 nProjectInd = rInPath.indexOf(rProject);
183 : const OString relativPath =
184 0 : rInPath.copy(nProjectInd, rInPath.lastIndexOf('/')- nProjectInd);
185 :
186 0 : PoHeader aTmp(relativPath);
187 0 : aPoOutPut.writeHeader(aTmp);
188 0 : aPoOutPut.close();
189 0 : }
190 :
191 0 : bool handleFile(
192 : const OString& rProject, const OUString& rUrl,
193 : const OString& rPotDir, bool bInitPoFile )
194 : {
195 0 : struct Command {
196 : char const * extension;
197 : sal_Int32 extensionLength;
198 : OString executable;
199 : bool positive;
200 : };
201 : static Command const commands[] = {
202 0 : { RTL_CONSTASCII_STRINGPARAM(".src"), "transex3", false },
203 0 : { RTL_CONSTASCII_STRINGPARAM(".hrc"), "transex3", true },
204 0 : { RTL_CONSTASCII_STRINGPARAM(".ulf"), "ulfex", false },
205 0 : { RTL_CONSTASCII_STRINGPARAM(".xcu"), "cfgex", false },
206 0 : { RTL_CONSTASCII_STRINGPARAM(".xrm"), "xrmex", false },
207 0 : { RTL_CONSTASCII_STRINGPARAM("description.xml"), "xrmex", true },
208 0 : { RTL_CONSTASCII_STRINGPARAM("strings.xml"), "stringex", true },
209 0 : { RTL_CONSTASCII_STRINGPARAM(".xhp"), "helpex", false },
210 0 : { RTL_CONSTASCII_STRINGPARAM(".properties"), "propex", false },
211 0 : { RTL_CONSTASCII_STRINGPARAM(".ui"), "uiex", false },
212 0 : { RTL_CONSTASCII_STRINGPARAM(".tree"), "treex", false } };
213 0 : for (size_t i = 0; i != SAL_N_ELEMENTS(commands); ++i)
214 : {
215 0 : if (rUrl.endsWithAsciiL(
216 0 : commands[i].extension, commands[i].extensionLength) &&
217 0 : (commands[i].executable != "propex" || rUrl.indexOf("en_US") != -1))
218 : {
219 0 : if (commands[i].positive ? passesPositiveList(rUrl) : passesNegativeList(rUrl))
220 : {
221 : //Get input file path
222 0 : OString sInPath;
223 : {
224 0 : OUString sInPathTmp;
225 0 : if (osl::FileBase::getSystemPathFromFileURL(rUrl, sInPathTmp) !=
226 : osl::FileBase::E_None)
227 : {
228 0 : cerr << "osl::FileBase::getSystemPathFromFileURL(" << rUrl << ") failed\n";
229 0 : throw false; //TODO
230 : }
231 0 : sInPath = OUStringToOString( sInPathTmp, RTL_TEXTENCODING_UTF8 );
232 : }
233 0 : OString sOutPath = rPotDir.concat(".pot");
234 :
235 0 : if ( bInitPoFile )
236 : {
237 0 : InitPoFile(rProject, sInPath, rPotDir, sOutPath);
238 : }
239 0 : handleCommand(sInPath, sOutPath, commands[i].executable);
240 0 : return true;
241 : }
242 0 : break;
243 : }
244 : }
245 0 : return false;
246 : }
247 :
248 0 : void handleFilesOfDir(
249 : std::vector<OUString>& aFiles, const OString& rProject,
250 : const OString& rPotDir )
251 : {
252 : ///Handle files in lexical order
253 0 : std::sort(aFiles.begin(), aFiles.end());
254 :
255 : typedef std::vector<OUString>::const_iterator citer_t;
256 :
257 0 : bool bFirstLocFile = true; ///< First file in directory which needs localization
258 :
259 0 : for( citer_t aIt = aFiles.begin(); aIt != aFiles.end(); ++aIt )
260 : {
261 0 : if (handleFile( rProject, *aIt, rPotDir, bFirstLocFile))
262 : {
263 0 : bFirstLocFile = false;
264 : }
265 : }
266 :
267 0 : if( !bFirstLocFile )
268 : {
269 : //Delete pot file if it contain only the header
270 0 : OString sPotFile = rPotDir.concat(".pot");
271 0 : PoIfstream aPOStream( sPotFile );
272 0 : PoEntry aPO;
273 0 : aPOStream.readEntry( aPO );
274 0 : bool bDel = aPOStream.eof();
275 0 : aPOStream.close();
276 0 : if( bDel )
277 : {
278 0 : if ( system(OString("rm " + sPotFile).getStr()) != 0 )
279 : {
280 : cerr
281 0 : << "Error: Cannot remove entryless pot file: "
282 0 : << sPotFile.getStr() << "\n";
283 0 : throw false; //TODO
284 : }
285 0 : }
286 : }
287 0 : }
288 :
289 0 : bool includeProject(const OString& rProject) {
290 : static OString projects[] = {
291 : "accessibility",
292 : "android",
293 : "avmedia",
294 : "basctl",
295 : "basic",
296 : "chart2",
297 : "connectivity",
298 : "cui",
299 : "dbaccess",
300 : "desktop",
301 : "dictionaries",
302 : "editeng",
303 : "extensions",
304 : "extras",
305 : "filter",
306 : "forms",
307 : "formula",
308 : "fpicker",
309 : "framework",
310 : "helpcontent2",
311 : "instsetoo_native",
312 : "librelogo",
313 : "mysqlc",
314 : "nlpsolver",
315 : "officecfg",
316 : "padmin",
317 : "readlicense_oo",
318 : "reportbuilder",
319 : "reportdesign",
320 : "sc",
321 : "scaddins",
322 : "sccomp",
323 : "scp2",
324 : "sd",
325 : "sdext",
326 : "setup_native",
327 : "sfx2",
328 : "shell",
329 : "starmath",
330 : "svl",
331 : "svtools",
332 : "svx",
333 : "sw",
334 : "swext",
335 : "sysui",
336 : "tubes",
337 : "uui",
338 : "vcl",
339 : "wizards",
340 0 : "xmlsecurity" };
341 0 : for (size_t i = 0; i != SAL_N_ELEMENTS(projects); ++i) {
342 0 : if (rProject == projects[i]) {
343 0 : return true;
344 : }
345 : }
346 0 : return false;
347 : }
348 :
349 : /// Handle one directory in the hierarchy.
350 : ///
351 : /// Ignores symlinks and instead explicitly descends into clone/* or src/*,
352 : /// as the Cygwin symlinks are not supported by osl::Directory on Windows.
353 : ///
354 : /// @param rUrl the absolute file URL of this directory
355 : ///
356 : /// @param nLevel 0 if this is either the root directory that contains the
357 : /// projects or one of the clone/* or src/* directories that contain the
358 : /// additional projects; -1 if this is the clone directory; 1 if this
359 : /// is a project directory; 2 if this is a directory inside a project
360 : ///
361 : /// @param rProject the name of the project (empty and ignored if nLevel <= 0)
362 : /// @param rPotDir the path of pot directory
363 0 : void handleDirectory(
364 : const OUString& rUrl, int nLevel,
365 : const OString& rProject, const OString& rPotDir)
366 : {
367 0 : osl::Directory dir(rUrl);
368 0 : if (dir.open() != osl::FileBase::E_None) {
369 : cerr
370 0 : << "Error: Cannot open directory: " << rUrl << '\n';
371 0 : throw false; //TODO
372 : }
373 0 : std::vector<OUString> aFileNames;
374 : for (;;) {
375 0 : osl::DirectoryItem item;
376 0 : osl::FileBase::RC e = dir.getNextItem(item);
377 0 : if (e == osl::FileBase::E_NOENT) {
378 0 : break;
379 : }
380 0 : if (e != osl::FileBase::E_None) {
381 0 : cerr << "Error: Cannot read directory\n";
382 0 : throw false; //TODO
383 : }
384 : osl::FileStatus stat(
385 : osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName
386 0 : | osl_FileStatus_Mask_FileURL);
387 0 : if (item.getFileStatus(stat) != osl::FileBase::E_None) {
388 0 : cerr << "Error: Cannot get file status\n";
389 0 : throw false; //TODO
390 : }
391 : const OString sDirName =
392 0 : OUStringToOString(stat.getFileName(),RTL_TEXTENCODING_UTF8);
393 0 : switch (nLevel) {
394 : case -1: // the clone or src directory
395 0 : if (stat.getFileType() == osl::FileStatus::Directory) {
396 : handleDirectory(
397 0 : stat.getFileURL(), 0, OString(), rPotDir);
398 : }
399 0 : break;
400 : case 0: // a root directory
401 0 : if (stat.getFileType() == osl::FileStatus::Directory) {
402 0 : if (includeProject(sDirName)) {
403 : handleDirectory(
404 0 : stat.getFileURL(), 1, sDirName, rPotDir.concat("/").concat(sDirName));
405 0 : } else if ( sDirName == "clone" ||
406 0 : sDirName == "src" )
407 : {
408 0 : handleDirectory( stat.getFileURL(), -1, OString(), rPotDir);
409 : }
410 : }
411 0 : break;
412 : default:
413 0 : if (stat.getFileType() == osl::FileStatus::Directory)
414 : {
415 : handleDirectory(
416 0 : stat.getFileURL(), 2, rProject, rPotDir.concat("/").concat(sDirName));
417 : }
418 : else
419 : {
420 0 : aFileNames.push_back(stat.getFileURL());
421 : }
422 0 : break;
423 : }
424 0 : }
425 :
426 0 : if( !aFileNames.empty() )
427 : {
428 0 : handleFilesOfDir( aFileNames, rProject, rPotDir );
429 : }
430 :
431 0 : if (dir.close() != osl::FileBase::E_None) {
432 0 : cerr << "Error: Cannot close directory\n";
433 0 : throw false; //TODO
434 : }
435 :
436 : //Remove empty pot directory
437 : OUString sPoPath =
438 : OStringToOUString(
439 0 : rPotDir.copy(0,rPotDir.lastIndexOf('/')), RTL_TEXTENCODING_UTF8);
440 0 : OUString sPoUrl;
441 0 : if (osl::FileBase::getFileURLFromSystemPath(sPoPath, sPoUrl)
442 : != osl::FileBase::E_None)
443 : {
444 0 : cerr << "Error: Cannot convert pathname to URL in " << __FILE__ << ", in line " << __LINE__ << "\n"
445 0 : << OUStringToOString(sPoPath, RTL_TEXTENCODING_UTF8).getStr() << "\n";
446 0 : throw false; //TODO
447 : }
448 0 : osl::Directory::remove(sPoUrl);
449 0 : }
450 :
451 0 : void handleProjects(char * sSourceRoot, char const * sDestRoot)
452 : {
453 0 : OUString root16;
454 0 : if (!rtl_convertStringToUString(
455 : &root16.pData, sSourceRoot, rtl_str_getLength(sSourceRoot),
456 0 : osl_getThreadTextEncoding(),
457 : (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
458 : | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
459 0 : | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
460 : {
461 0 : cerr << "Error: Cannot convert pathname to UTF-16\n";
462 0 : throw false; //TODO
463 : }
464 0 : OUString rootUrl;
465 0 : if (osl::FileBase::getFileURLFromSystemPath(root16, rootUrl)
466 : != osl::FileBase::E_None)
467 : {
468 0 : cerr << "Error: Cannot convert pathname to URL in " << __FILE__ << ", in line " << __LINE__ << "\n"
469 0 : << " root16: " << OUStringToOString(root16, RTL_TEXTENCODING_ASCII_US).getStr() << "\n";
470 0 : throw false; //TODO
471 : }
472 0 : handleDirectory(rootUrl, 0, OString(), OString(sDestRoot));
473 0 : }
474 : }
475 :
476 0 : SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) {
477 0 : if (argc != 3) {
478 : cerr
479 : << ("localize (c)2001 by Sun Microsystems\n\n"
480 : "As part of the L10N framework, localize extracts en-US\n"
481 : "strings for translation out of the toplevel modules defined\n"
482 : "in projects array in l10ntools/source/localize.cxx.\n\n"
483 0 : "Syntax: localize <source-root> <outfile>\n");
484 0 : exit(EXIT_FAILURE);
485 : }
486 : try {
487 0 : handleProjects(argv[1],argv[2]);
488 0 : } catch (bool) { //TODO
489 0 : return EXIT_FAILURE;
490 : }
491 0 : return EXIT_SUCCESS;
492 0 : }
493 :
494 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|