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