Author: markt Date: Fri Nov 9 20:29:29 2018 New Revision: 1846265 URL: http://svn.apache.org/viewvc?rev=1846265&view=rev Log: First pass at the import code
Added: tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Import.java (with props) tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Utils.java (with props) Modified: tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Export.java Modified: tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Export.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Export.java?rev=1846265&r1=1846264&r2=1846265&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Export.java (original) +++ tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Export.java Fri Nov 9 20:29:29 2018 @@ -17,18 +17,14 @@ 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.HashMap; import java.util.Map; import java.util.Properties; -import java.util.regex.Pattern; /** * Generates a single properties file per language for import into a translation @@ -37,8 +33,6 @@ import java.util.regex.Pattern; public class Export { private static final Map<String,Properties> translations = new HashMap<>(); - private static final Pattern ADD_CONTINUATION = Pattern.compile("\\n", Pattern.MULTILINE); - private static final Pattern ESCAPE_LEADING_SPACE = Pattern.compile("^(\\s)", Pattern.MULTILINE); public static void main(String... args) { for (String dir : Constants.SEARCH_DIRS) { @@ -82,23 +76,10 @@ public class Export { } // Determine language - String language = name.substring(Constants.L10N_PREFIX.length(), name.length() - Constants.L10N_SUFFIX.length()); - if (language.length() == 0) { - // Default - } else if (language.length() == 3) { - language = language.substring(1); - } + String language = Utils.getLanguage(name); String keyPrefix = getKeyPrefix(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(); - } + Properties props = Utils.load(f); // Create a Map for the language if one does not exist. Properties translation = translations.get(language); @@ -141,22 +122,12 @@ public class Export { try (FileOutputStream fos = new FileOutputStream(out); Writer w = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) { for (Object key : translation.keySet()) { - w.write(key + "=" + formatValue(translation.getProperty((String) key)) + "\n"); + w.write(key + "=" + Utils.formatValue(translation.getProperty((String) key)) + "\n"); } } catch (IOException ioe) { ioe.printStackTrace(); } } } - - - private 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"); - return result; - } } Added: tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Import.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Import.java?rev=1846265&view=auto ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Import.java (added) +++ tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Import.java Fri Nov 9 20:29:29 2018 @@ -0,0 +1,136 @@ +/* +* 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("."); + + 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()); + + // Skip the original + if (language.length() == 0) { + return; + } + + 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); + } + } + } +} Propchange: tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Import.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Utils.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Utils.java?rev=1846265&view=auto ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Utils.java (added) +++ tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Utils.java Fri Nov 9 20:29:29 2018 @@ -0,0 +1,70 @@ +/* +* 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.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +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 Utils() { + // Utility class. Hide default constructor. + } + + + static String getLanguage(String name) { + String language = name.substring(Constants.L10N_PREFIX.length(), name.length() - Constants.L10N_SUFFIX.length()); + if (language.length() == 0) { + // Default + } else if (language.length() == 3) { + language = language.substring(1); + } + return language; + } + + + 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"); + return result; + } +} Propchange: tomcat/trunk/java/org/apache/tomcat/buildutil/translate/Utils.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org