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