init
This commit is contained in:
65
java/org/apache/tomcat/buildutil/translate/BackportBase.java
Normal file
65
java/org/apache/tomcat/buildutil/translate/BackportBase.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.apache.tomcat.buildutil.translate;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Base class providing common implementation for back-port utilities.
|
||||
*/
|
||||
public abstract class BackportBase {
|
||||
|
||||
protected final Map<String,Properties> sourceTranslations = new HashMap<>();
|
||||
protected final Map<String,Properties> targetTranslations = new HashMap<>();
|
||||
protected final File targetRoot;
|
||||
protected final Properties sourceEnglish;
|
||||
protected final Properties targetEnglish;
|
||||
protected final File storageDir;
|
||||
|
||||
protected BackportBase(String... args) throws IOException {
|
||||
if (args.length != 1) {
|
||||
throw new IllegalArgumentException("Missing back-port target");
|
||||
}
|
||||
targetRoot = new File(args[0]);
|
||||
|
||||
if (!targetRoot.isDirectory()) {
|
||||
throw new IllegalArgumentException("Back-port target not a directory");
|
||||
}
|
||||
|
||||
File sourceRoot = new File(".");
|
||||
for (String dir : Constants.SEARCH_DIRS) {
|
||||
File directory = new File(dir);
|
||||
Utils.processDirectory(sourceRoot, directory, sourceTranslations);
|
||||
}
|
||||
|
||||
for (String dir : Constants.SEARCH_DIRS) {
|
||||
File directory = new File(targetRoot, dir);
|
||||
Utils.processDirectory(targetRoot, directory, targetTranslations);
|
||||
}
|
||||
|
||||
sourceEnglish = sourceTranslations.get("");
|
||||
targetEnglish = targetTranslations.get("");
|
||||
|
||||
storageDir = new File(targetRoot, Constants.STORAGE_DIR);
|
||||
}
|
||||
|
||||
protected abstract void execute() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.apache.tomcat.buildutil.translate;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Generates a set of English property files to back-port updates to a previous
|
||||
* version. Where a key exists in the source and target versions the value is
|
||||
* copied from the source to the target, overwriting the value in the target.
|
||||
* The expectation is that the changes will be manually reviewed before
|
||||
* committing them.
|
||||
*/
|
||||
public class BackportEnglish extends BackportBase {
|
||||
|
||||
public static void main(String... args) throws IOException {
|
||||
BackportEnglish backport = new BackportEnglish(args);
|
||||
backport.execute();
|
||||
}
|
||||
|
||||
|
||||
protected BackportEnglish(String[] args) throws IOException {
|
||||
super(args);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void execute() throws IOException {
|
||||
for (Object key : sourceEnglish.keySet()) {
|
||||
if (targetEnglish.containsKey(key)) {
|
||||
targetEnglish.put(key, sourceEnglish.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
Utils.export("", targetEnglish, storageDir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.apache.tomcat.buildutil.translate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Generates a set of translated property files to back-port updates to a
|
||||
* previous version. If the source and target use the same value for the English
|
||||
* key then any translated value for that key is copied from the source to the
|
||||
* target.
|
||||
*/
|
||||
public class BackportTranslations extends BackportBase {
|
||||
|
||||
public static void main(String... args) throws IOException {
|
||||
BackportTranslations backport = new BackportTranslations(args);
|
||||
backport.execute();
|
||||
}
|
||||
|
||||
protected BackportTranslations(String[] args) throws IOException {
|
||||
super(args);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void execute() throws IOException {
|
||||
for (String langauge : targetTranslations.keySet()) {
|
||||
// Skip source
|
||||
if (langauge.length() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Properties sourceTranslated = sourceTranslations.get(langauge);
|
||||
Properties targetTranslated = targetTranslations.get(langauge);
|
||||
if (targetTranslated == null) {
|
||||
targetTranslated = new Properties();
|
||||
targetTranslations.put(langauge, targetTranslated);
|
||||
}
|
||||
|
||||
for (Object key : targetEnglish.keySet()) {
|
||||
if (sourceTranslated.containsKey(key) &&
|
||||
targetEnglish.get(key).equals(sourceEnglish.get(key))) {
|
||||
|
||||
targetTranslated.put(key, sourceTranslated.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
// Remove translated values for keys that have been removed
|
||||
Iterator<Map.Entry<Object,Object>> iter = targetTranslated.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry<Object,Object> entry = iter.next();
|
||||
if (!targetEnglish.containsKey(entry.getKey())) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
Utils.export(langauge, targetTranslated, storageDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
java/org/apache/tomcat/buildutil/translate/Constants.java
Normal file
29
java/org/apache/tomcat/buildutil/translate/Constants.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.apache.tomcat.buildutil.translate;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final String L10N_PREFIX = "LocalStrings";
|
||||
public static final String L10N_SUFFIX = ".properties";
|
||||
|
||||
public static final String[] SEARCH_DIRS = new String[] { "java", "webapps" };
|
||||
|
||||
public static final String STORAGE_DIR = ".settings/translations";
|
||||
|
||||
public static final String END_PACKAGE_MARKER = ".zzz.";
|
||||
}
|
||||
133
java/org/apache/tomcat/buildutil/translate/Import.java
Normal file
133
java/org/apache/tomcat/buildutil/translate/Import.java
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.apache.tomcat.buildutil.translate;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
public class Import {
|
||||
|
||||
public static void main(String... args) throws IOException {
|
||||
File root = new File(Constants.STORAGE_DIR);
|
||||
|
||||
for (File f : root.listFiles()) {
|
||||
// Not robust but good enough
|
||||
if (f.isFile() && f.getName().startsWith(Constants.L10N_PREFIX)) {
|
||||
processFile(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("null")
|
||||
private static void processFile(File f) throws IOException {
|
||||
String language = Utils.getLanguage(f.getName());
|
||||
|
||||
// Unlike the master branch, don't skip the original so we can import
|
||||
// updates to the English translations
|
||||
Properties props = Utils.load(f);
|
||||
Object[] objKeys = props.keySet().toArray();
|
||||
Arrays.sort(objKeys);
|
||||
|
||||
String currentPkg = null;
|
||||
Writer w = null;
|
||||
String currentGroup = "zzz";
|
||||
|
||||
for (Object objKey : objKeys) {
|
||||
String key = (String) objKey;
|
||||
CompositeKey cKey = new CompositeKey(key);
|
||||
|
||||
if (!cKey.pkg.equals(currentPkg)) {
|
||||
currentPkg = cKey.pkg;
|
||||
if (w != null) {
|
||||
w.close();
|
||||
}
|
||||
File outFile = new File(currentPkg.replace('.', File.separatorChar), Constants.L10N_PREFIX + language + Constants.L10N_SUFFIX);
|
||||
FileOutputStream fos = new FileOutputStream(outFile);
|
||||
w = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
|
||||
insertLicense(w);
|
||||
}
|
||||
|
||||
if (!currentGroup.equals(cKey.group)) {
|
||||
currentGroup = cKey.group;
|
||||
w.write(System.lineSeparator());
|
||||
}
|
||||
|
||||
w.write(cKey.key + "=" + Utils.formatValue(props.getProperty(key)));
|
||||
w.write(System.lineSeparator());
|
||||
}
|
||||
if (w != null) {
|
||||
w.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void insertLicense(Writer w) throws IOException {
|
||||
w.write("# Licensed to the Apache Software Foundation (ASF) under one or more");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# contributor license agreements. See the NOTICE file distributed with");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# this work for additional information regarding copyright ownership.");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# The ASF licenses this file to You under the Apache License, Version 2.0");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# (the \"License\"); you may not use this file except in compliance with");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# the License. You may obtain a copy of the License at");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("#");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# http://www.apache.org/licenses/LICENSE-2.0");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("#");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# Unless required by applicable law or agreed to in writing, software");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# distributed under the License is distributed on an \"AS IS\" BASIS,");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# See the License for the specific language governing permissions and");
|
||||
w.write(System.lineSeparator());
|
||||
w.write("# limitations under the License.");
|
||||
w.write(System.lineSeparator());
|
||||
}
|
||||
private static class CompositeKey {
|
||||
|
||||
public final String pkg;
|
||||
public final String key;
|
||||
public final String group;
|
||||
|
||||
public CompositeKey(String in) {
|
||||
int posPkg = in.indexOf(Constants.END_PACKAGE_MARKER);
|
||||
pkg = in.substring(0, posPkg);
|
||||
key = in.substring(posPkg + Constants.END_PACKAGE_MARKER.length());
|
||||
int posGroup = key.indexOf('.');
|
||||
if (posGroup == -1) {
|
||||
group = "";
|
||||
} else {
|
||||
group = key.substring(0, posGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
149
java/org/apache/tomcat/buildutil/translate/Utils.java
Normal file
149
java/org/apache/tomcat/buildutil/translate/Utils.java
Normal file
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.apache.tomcat.buildutil.translate;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Utils {
|
||||
|
||||
private static final Pattern ADD_CONTINUATION = Pattern.compile("\\n", Pattern.MULTILINE);
|
||||
private static final Pattern ESCAPE_LEADING_SPACE = Pattern.compile("^(\\s)", Pattern.MULTILINE);
|
||||
private static final Pattern FIX_SINGLE_QUOTE = Pattern.compile("(?<!')'(?!')", Pattern.MULTILINE);
|
||||
|
||||
private Utils() {
|
||||
// Utility class. Hide default constructor.
|
||||
}
|
||||
|
||||
|
||||
static String getLanguage(String name) {
|
||||
return name.substring(Constants.L10N_PREFIX.length(), name.length() - Constants.L10N_SUFFIX.length());
|
||||
}
|
||||
|
||||
|
||||
static Properties load(File f) {
|
||||
Properties props = new Properties();
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(f);
|
||||
Reader r = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
|
||||
props.load(r);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
|
||||
static String formatValue(String in) {
|
||||
String result = ADD_CONTINUATION.matcher(in).replaceAll("\\\\n\\\\\n");
|
||||
if (result.endsWith("\\\n")) {
|
||||
result = result.substring(0, result.length() - 2);
|
||||
}
|
||||
result = ESCAPE_LEADING_SPACE.matcher(result).replaceAll("\\\\$1");
|
||||
|
||||
if (result.contains("\n\\\t")) {
|
||||
result = result.replace("\n\\\t", "\n\\t");
|
||||
}
|
||||
|
||||
if (result.contains("[{0}]")) {
|
||||
result = FIX_SINGLE_QUOTE.matcher(result).replaceAll("''");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static void processDirectory(File root, File dir, Map<String,Properties> translations) throws IOException {
|
||||
File[] files = dir.listFiles();
|
||||
if (files == null) {
|
||||
throw new IllegalArgumentException("Not a directory [" + dir.getAbsolutePath() + "]");
|
||||
}
|
||||
for (File f : files) {
|
||||
if (f.isDirectory()) {
|
||||
processDirectory(root, f, translations);
|
||||
} else if (f.isFile()) {
|
||||
processFile(root, f, translations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void processFile(File root, File f, Map<String,Properties> translations) throws IOException {
|
||||
String name = f.getName();
|
||||
|
||||
// non-l10n files
|
||||
if (!name.startsWith(Constants.L10N_PREFIX)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine language
|
||||
String language = Utils.getLanguage(name);
|
||||
|
||||
String keyPrefix = getKeyPrefix(root, f);
|
||||
Properties props = Utils.load(f);
|
||||
|
||||
// Create a Map for the language if one does not exist.
|
||||
Properties translation = translations.get(language);
|
||||
if (translation == null) {
|
||||
translation = new Properties();
|
||||
translations.put(language, translation);
|
||||
}
|
||||
|
||||
// Add the properties from this file to the combined file, prefixing the
|
||||
// key with the package name to ensure uniqueness.
|
||||
for (Object obj : props.keySet()) {
|
||||
String key = (String) obj;
|
||||
String value = props.getProperty(key);
|
||||
|
||||
translation.put(keyPrefix + key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static String getKeyPrefix(File root, File f) throws IOException {
|
||||
String prefix = f.getParentFile().getCanonicalPath();
|
||||
prefix = prefix.substring(root.getCanonicalPath().length() + 1);
|
||||
prefix = prefix.replace(File.separatorChar, '.');
|
||||
prefix = prefix + Constants.END_PACKAGE_MARKER;
|
||||
return prefix;
|
||||
}
|
||||
|
||||
|
||||
static void export(String language, Properties translation, File storageDir) {
|
||||
File out = new File(storageDir, Constants.L10N_PREFIX + language + Constants.L10N_SUFFIX);
|
||||
try (FileOutputStream fos = new FileOutputStream(out);
|
||||
Writer w = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
|
||||
String[] keys = translation.keySet().toArray(new String[0]);
|
||||
Arrays.sort(keys);
|
||||
for (Object key : keys) {
|
||||
w.write(key + "=" + Utils.formatValue(translation.getProperty((String) key)) + "\n");
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user