chg: merged parser code modified in antville app back into hopkit

This commit is contained in:
Tobi Schäfer 2020-04-03 14:11:49 +02:00
parent 0bf39c8f4b
commit 657f66c0ce
2 changed files with 441 additions and 399 deletions

View file

@ -1,34 +1,26 @@
//
// Jala Project [http://opensvn.csie.org/traccgi/jala]
//
// Copyright 2004 ORF Online und Teletext GmbH
// Copyright 2004 ORF Online und Teletext GmbH.
//
// Licensed under the Apache License, Version 2.0 (the ``License'');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an ``AS IS'' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Revision$
// $LastChangedBy$
// $LastChangedDate$
// $HeadURL$
//
/**
* @fileoverview
* @fileoverview
* A parser script that converts GNU Gettext .po files into plain JavaScript objects
* for use with jala.I18n. To run it either start the build script of HopKit
* or call it directly using the JavaScript shell of Rhino:
* <code>java -cp rhino.jar org.mozilla.javascript.tools.shell.Main PoParser.js <input> <output> [namespace]</code>
*
*
* The accepted arguments are:
* <ul>
* <li><code>input</code>: Either a single .po file or a directory containing multiple files</li>
@ -49,24 +41,24 @@
* @constructor
*/
var PoParser = function(namespace) {
/**
* An array containing the parsed messages
* @type Array
*/
this.messages = [];
/**
* An array containing the parsed messages
* @type Array
*/
this.messages = [];
/**
* The locale key string (eg. "de_AT") of the .po file
* @type String
*/
this.localeKey = null;
/**
* The locale key string (eg. "de_AT") of the .po file
* @type String
*/
this.localeKey = null;
/**
* The namespace (optional) where to store the generated messages
* @type String
*/
this.namespace = namespace;
return this;
/**
* The namespace (optional) where to store the generated messages
* @type String
*/
this.namespace = namespace;
return this;
};
/**
@ -108,7 +100,7 @@ PoParser.REGEX_MSGSTR = /^\s*msgstr(?:\[(\d)\])?\s+\"(.*)\"\s*$/;
PoParser.REGEX_DATA = /^\s*msg/;
PoParser.isData = function(str) {
return PoParser.REGEX_DATA.test(str);
return PoParser.REGEX_DATA.test(str);
};
/**
@ -118,21 +110,21 @@ PoParser.isData = function(str) {
* @type java.lang.String
*/
PoParser.readFile = function(file) {
var inStream = new java.io.InputStreamReader(new java.io.FileInputStream(file), "UTF-8");
var buffer = new java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, 2048);
var read = 0;
var r = 0;
while ((r = inStream.read(buffer, read, buffer.length - read)) > -1) {
read += r;
if (read == buffer.length) {
// grow input buffer
var newBuffer = new java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, buffer.length * 2);
java.lang.System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
buffer = newBuffer;
}
}
inStream.close();
return new java.lang.String(buffer, 0, read);
var inStream = new java.io.InputStreamReader(new java.io.FileInputStream(file), "UTF-8");
var buffer = new java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, 2048);
var read = 0;
var r = 0;
while ((r = inStream.read(buffer, read, buffer.length - read)) > -1) {
read += r;
if (read == buffer.length) {
// grow input buffer
var newBuffer = new java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, buffer.length * 2);
java.lang.System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
buffer = newBuffer;
}
}
inStream.close();
return new java.lang.String(buffer, 0, read);
}
/**
@ -141,63 +133,63 @@ PoParser.readFile = function(file) {
* @param {java.io.File} file The .po file to parse
*/
PoParser.prototype.parse = function(file) {
// parse the locale key out of the file name
var fileName = file.getName();
if (!(this.localeKey = fileName.substring(0, fileName.indexOf(".")))) {
throw "Invalid PO file name: " + fileName;
}
// parse the locale key out of the file name
var fileName = file.getName();
if (!(this.localeKey = fileName.substring(0, fileName.indexOf(".")))) {
throw "Invalid PO file name: " + fileName;
}
// read the PO file content and parse it into messages
var content = PoParser.readFile(file);
var start = new Date();
var lines = content.split(PoParser.REGEX_LINES);
var idx = -1;
var line = null;
var m, value, nr;
var msg;
// read the PO file content and parse it into messages
var content = PoParser.readFile(file);
var start = new Date();
var lines = content.split(PoParser.REGEX_LINES);
var idx = -1;
var line = null;
var m, value, nr;
var msg;
var hasMoreLines = function() {
return idx < lines.length - 1;
};
var hasMoreLines = function() {
return idx < lines.length - 1;
};
var nextLine = function() {
return (line = lines[idx += 1]) != null;
};
var nextLine = function() {
return (line = lines[idx += 1]) != null;
};
var getContinuation = function(str) {
var nLine;
while ((nLine = lines[idx + 1]) != null) {
if ((m = nLine.match(PoParser.REGEX_MSG_CONT)) != null) {
str += m[1];
nextLine();
} else {
break;
}
var getContinuation = function(str) {
var nLine;
while ((nLine = lines[idx + 1]) != null) {
if ((m = nLine.match(PoParser.REGEX_MSG_CONT)) != null) {
str += m[1];
nextLine();
} else {
break;
}
return str;
}
}
return str;
}
while (nextLine()) {
if ((m = line.match(PoParser.REGEX_MSGID)) != null) {
value = getContinuation(m[1]);
if (value) {
msg = this.messages[this.messages.length] = new Message(value);
}
} else if ((m = line.match(PoParser.REGEX_MSGID_PLURAL)) != null) {
value = getContinuation(m[1]);
if (value && msg != null) {
msg.pluralKey = value;
}
} else if ((m = line.match(PoParser.REGEX_MSGSTR)) != null) {
nr = m[1];
value = getContinuation(m[2]);
if (value && msg != null) {
nr = parseInt(nr, 10);
msg.translations[nr || 0] = value;
}
while (nextLine()) {
if ((m = line.match(PoParser.REGEX_MSGID)) != null) {
value = getContinuation(m[1]);
if (value) {
msg = this.messages[this.messages.length] = new Message(value);
}
}
return;
} else if ((m = line.match(PoParser.REGEX_MSGID_PLURAL)) != null) {
value = getContinuation(m[1]);
if (value && msg != null) {
msg.pluralKey = value;
}
} else if ((m = line.match(PoParser.REGEX_MSGSTR)) != null) {
nr = m[1];
value = getContinuation(m[2]);
if (value && msg != null) {
nr = parseInt(nr, 10);
msg.translations[nr || 0] = value;
}
}
}
return;
};
/**
@ -207,44 +199,44 @@ PoParser.prototype.parse = function(file) {
* file should be saved
*/
PoParser.prototype.writeToFile = function(output) {
var buf = new java.lang.StringBuffer();
// write header
buf.append('/**\n');
buf.append(' * Instantiate the messages namespace if it\'s not already existing\n');
buf.append(' */\n');
var buf = new java.lang.StringBuffer();
// write header
buf.append('/**\n');
buf.append(' * Instantiate the messages namespace if it\'s not already existing\n');
buf.append(' */\n');
var objPath = "";
if (this.namespace) {
objPath += this.namespace;
buf.append('if (!global.' + objPath + ') {\n');
buf.append(' global.' + objPath + ' = {};\n');
buf.append('}\n');
objPath += ".";
}
objPath += "messages";
buf.append('if (!global.' + objPath + ') {\n');
buf.append(' global.' + objPath + ' = {};\n');
buf.append('}\n\n');
if (this.namespace) {
objPath += this.namespace;
buf.append('if (!global.' + objPath + ') {\n');
buf.append(' global.' + objPath + ' = {};\n');
buf.append('}\n');
objPath += ".";
}
objPath += "messages";
buf.append('if (!global.' + objPath + ') {\n');
buf.append(' global.' + objPath + ' = {};\n');
buf.append('}\n\n');
buf.append('/**\n');
buf.append(' * Messages for locale "' + this.localeKey + '"\n');
buf.append(' */\n');
objPath += "." + this.localeKey;
buf.append('global.' + objPath + ' = {\n');
// write messages
for (var i=0;i<this.messages.length; i++) {
this.messages[i].write(buf);
}
// write footer
buf.append('};\n');
buf.append('/**\n');
buf.append(' * Messages for locale "' + this.localeKey + '"\n');
buf.append(' */\n');
var fname = objPath + "." + this.localeKey + ".js";
objPath += "['" + this.localeKey + "']";
buf.append('global.' + objPath + ' = {\n');
// write messages
for (var i=0;i<this.messages.length; i++) {
this.messages[i].write(buf);
}
// write footer
buf.append('};\n');
// write the message catalog into the outFile
var file = new java.io.File(output, objPath + ".js");
var writer = new java.io.FileWriter(file);
writer.write(buf.toString());
// writer.write(buf.toString().getBytes("UTF-8"));
writer.close();
print("generated messages file " + file.getAbsolutePath());
return;
// write the message catalog into the outFile
var file = new java.io.File(output, fname);
var writer = new java.io.FileWriter(file);
writer.write(new java.lang.String(buf.toString().getBytes("UTF-8")));
writer.close();
print("generated messages file " + file.getAbsolutePath());
return;
};
/**
@ -255,10 +247,10 @@ PoParser.prototype.writeToFile = function(output) {
* @constructor
*/
var Message = function(singularKey) {
this.singularKey = singularKey;
this.pluralKey = null;
this.translations = [];
return this;
this.singularKey = singularKey;
this.pluralKey = null;
this.translations = [];
return this;
}
/**
@ -267,37 +259,37 @@ var Message = function(singularKey) {
* string to
*/
Message.prototype.write = function(buf) {
var writeLine = function(key, value) {
buf.append(' "');
buf.append(key);
buf.append('": "');
if (value !== null && value !== undefined) {
buf.append(value);
}
buf.append('",\n');
};
var writeLine = function(key, value) {
buf.append(' "');
buf.append(key);
buf.append('": "');
if (value !== null && value !== undefined) {
buf.append(value);
}
buf.append('",\n');
};
if (this.singularKey != null) {
writeLine(this.singularKey, this.translations[0]);
if (this.pluralKey != null) {
writeLine(this.pluralKey, this.translations[1]);
}
}
return;
if (this.singularKey != null) {
writeLine(this.singularKey, this.translations[0]);
if (this.pluralKey != null) {
writeLine(this.pluralKey, this.translations[1]);
}
}
return;
}
/**
* Main script body
*/
if (arguments.length < 2) {
print("Usage:");
print("PoParser.js <input> <output> [namespace]");
print("<input>: Either a single .po file or a directory containing .po files");
print("<output>: The directory where the generated messages files should be stored");
print("[namespace]: An optional global namespace where the messages should be");
print(" stored (eg. a namespace like 'jala' will lead to messages");
print(" stored in global.jala.messages by their locale.");
quit();
print("Usage:");
print("PoParser.js <input> <output> [namespace]");
print("<input>: Either a single .po file or a directory containing .po files");
print("<output>: The directory where the generated messages files should be stored");
print("[namespace]: An optional global namespace where the messages should be");
print(" stored (eg. a namespace like 'jala' will lead to messages");
print(" stored in global.jala.messages by their locale.");
quit();
}
var input = new java.io.File(arguments[0]);
@ -306,31 +298,31 @@ var namespace = arguments[2];
// check if the output destination is a directory
if (output.isFile()) {
print("Invalid arguments: the output destination must be a directory.");
quit();
print("Invalid arguments: the output destination must be a directory.");
quit();
}
if (namespace && namespace.indexOf(".") != -1) {
print("Invalid arguments: Please don't specify complex object paths, as this");
print("would corrupt the messages file.");
quit();
print("Invalid arguments: Please don't specify complex object paths, as this");
print("would corrupt the messages file.");
quit();
}
// parse the PO file(s) and create the message catalog files
var parser;
if (input.isDirectory()) {
var files = input.listFiles();
var file;
for (var i=0;i<files.length;i++) {
file = files[i];
if (file.getName().endsWith(".po")) {
parser = new PoParser(namespace);
parser.parse(file);
parser.writeToFile(output);
}
}
var files = input.listFiles();
var file;
for (var i=0;i<files.length;i++) {
file = files[i];
if (file.getName().endsWith(".po")) {
parser = new PoParser(namespace);
parser.parse(file);
parser.writeToFile(output);
}
}
} else {
parser = new PoParser(namespace);
parser.parse(input);
parser.writeToFile(output);
parser = new PoParser(namespace);
parser.parse(input);
parser.writeToFile(output);
}