From: zapata Date: Fri, 4 Apr 2003 02:04:37 +0000 (+0000) Subject: Bundletool dev X-Git-Tag: BEFORE_MERGE_1_1~191 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=86a7f9a337462ad82b23c77e91621929b2a741e3;p=mir.git Bundletool dev --- diff --git a/source/mir/util/PropertiesManipulator.java b/source/mir/util/PropertiesManipulator.java index 72c67a47..31bd19e3 100755 --- a/source/mir/util/PropertiesManipulator.java +++ b/source/mir/util/PropertiesManipulator.java @@ -44,6 +44,10 @@ public class PropertiesManipulator { return (String) values.get(aKey); } + public boolean containsKey(String aKey) { + return values.containsKey(aKey); + } + public static class Comment { private String comment; @@ -114,7 +118,7 @@ public class PropertiesManipulator { } } - private static String encode(String aValue) { + private static String encode(String aValue, boolean aUseUnicodeEscapes) { try { StringBuffer result = new StringBuffer(); boolean leadingspace=true; @@ -122,7 +126,7 @@ public class PropertiesManipulator { for (int i = 0; i0x7e) { + if (aUseUnicodeEscapes && (c<0x20 || c>0x7e)) { String code=Integer.toHexString(c); result.append("\\u"); for (int j=0; j<4-code.length(); j++) @@ -168,10 +172,15 @@ public class PropertiesManipulator { private final static String SEPARATOR = "[\t\n\r ]*[:=]?[\t\n\r ]*"; private final static String VALUE = "(([\\\\].)|([^\\\\]))*"; + public static PropertiesManipulator readProperties(InputStream anInputStream) throws PropertiesManipulatorExc, PropertiesManipulatorFailure { + return readProperties(anInputStream, "ISO-8859-1"); + } + + public static PropertiesManipulator readProperties(InputStream anInputStream, String anEncoding) throws PropertiesManipulatorExc, PropertiesManipulatorFailure { try { PropertiesManipulator result = new PropertiesManipulator(); - LineNumberReader reader = new LineNumberReader(new InputStreamReader(anInputStream, "ISO-8859-1")); + LineNumberReader reader = new LineNumberReader(new InputStreamReader(anInputStream, anEncoding)); String line = reader.readLine(); @@ -221,8 +230,12 @@ public class PropertiesManipulator { } public static void writeProperties(PropertiesManipulator aProperties, OutputStream anOutputStream) throws PropertiesManipulatorExc, PropertiesManipulatorFailure { + writeProperties(aProperties, anOutputStream, "ISO-8859-1", true); + } + + public static void writeProperties(PropertiesManipulator aProperties, OutputStream anOutputStream, String anEncoding, boolean aUseUnicodeEscapes) throws PropertiesManipulatorExc, PropertiesManipulatorFailure { try { - PrintWriter p = new PrintWriter(new OutputStreamWriter(anOutputStream, "ISO-8859-1")); + PrintWriter p = new PrintWriter(new OutputStreamWriter(anOutputStream, anEncoding)); try { Iterator i = aProperties.getEntries(); @@ -237,10 +250,10 @@ public class PropertiesManipulator { p.println(((Comment) entry).getComment()); } else if (entry instanceof Entry) { - String key = encode( ( (Entry) entry).getKey()); + String key = encode( ( (Entry) entry).getKey(), aUseUnicodeEscapes); String value = ""; if ( ( (Entry) entry).getValue() != null) - value = encode( ( (Entry) entry).getValue()); + value = encode( ( (Entry) entry).getValue(), aUseUnicodeEscapes); String line = key + " = " + value; diff --git a/source/tool/BundleTool.java b/source/tool/BundleTool.java index 1b9602c1..1a5edaa3 100755 --- a/source/tool/BundleTool.java +++ b/source/tool/BundleTool.java @@ -7,83 +7,227 @@ import org.apache.commons.collections.*; import mir.util.*; public class BundleTool { - public static void main(String[] anArguments) { - if (anArguments.length!=3) { - System.out.println("Usage:"); + public static void compare(String aMaster, String aSlave) { + PropertiesManipulator master; + PropertiesManipulator slave; + PropertiesManipulator result; - System.out.println(" BundleTool "); - System.out.println(""); - System.out.println("Description:"); - System.out.println(" Reorders keys/values from a slave bundle according to a master bundle."); + try { + master = PropertiesManipulator.readProperties(new FileInputStream(new File(aMaster))); + } + catch (Throwable t) { + System.out.println("Unable to read master properties: " + t.getMessage()); + return; + } + try { + slave = PropertiesManipulator.readProperties(new FileInputStream(new File(aSlave))); + } + catch (FileNotFoundException t) { + slave = new PropertiesManipulator(); + } + catch (Throwable t) { + System.out.println("Unable to read slave properties: " + t.getMessage()); return; } - PropertiesManipulator master; - PropertiesManipulator slave; - PropertiesManipulator result; + int missing=0; - try { - master = PropertiesManipulator.readProperties(new FileInputStream(new File(anArguments[0] + "_" + anArguments[1] + ".properties"))); - } - catch (Throwable t) { - System.out.println("Unable to read master properties: " + t.getMessage()); - return; + Iterator i = master.getEntries(); + while (i.hasNext()) { + Object e = i.next(); + + if (e instanceof PropertiesManipulator.Entry) { + String key = ( (PropertiesManipulator.Entry) e).getKey(); + + if (!slave.containsKey(key) || slave.get(key) == null || slave.get(key).length()==0 ) { + if (missing==0) { + System.out.println(aSlave+" is missing:"); + } + System.out.println(" " + key); + missing++; + } } + } + + if (missing>0) + System.out.println("total missing: " +missing); + + missing=0; + i = slave.getEntries(); + while (i.hasNext()) { + Object e = i.next(); - try { - slave = PropertiesManipulator.readProperties(new FileInputStream(new File(anArguments[0] + "_" + anArguments[2] + ".properties"))); + if (e instanceof PropertiesManipulator.Entry) { + String key = ( (PropertiesManipulator.Entry) e).getKey(); + + if (!master.containsKey(key)) { + if (missing==0) { + System.out.println(aSlave+" has extra:"); + } + System.out.println(" " + key); + missing++; + } } - catch (FileNotFoundException t) { - slave = new PropertiesManipulator(); + } + if (missing>0) + System.out.println("total extra: " +missing); + } + + public static void align(String aMaster, String aSlave) { + PropertiesManipulator master; + PropertiesManipulator slave; + PropertiesManipulator result; + + try { + master = PropertiesManipulator.readProperties(new FileInputStream(new File(aMaster))); + } + catch (Throwable t) { + System.out.println("Unable to read master properties: " + t.getMessage()); + return; + } + + try { + slave = PropertiesManipulator.readProperties(new FileInputStream(new File(aSlave))); + } + catch (FileNotFoundException t) { + slave = new PropertiesManipulator(); + } + catch (Throwable t) { + System.out.println("Unable to read slave properties: " + t.getMessage()); + return; + } + + result = new PropertiesManipulator(); + + Iterator i = slave.getEntries(); + while (i.hasNext()) { + Object e = i.next(); + + if (e instanceof PropertiesManipulator.EmptyLine) { + result.addEmptyLine(); } - catch (Throwable t) { - System.out.println("Unable to read slave properties: " + t.getMessage()); - return; + else if (e instanceof PropertiesManipulator.Comment) { + result.addComment( ( (PropertiesManipulator.Comment) e).getComment()); } - result = new PropertiesManipulator(); - Iterator i = slave.getEntries(); - while (i.hasNext()) { - Object e = i.next(); + if (! (e instanceof PropertiesManipulator.Comment)) + break; + } - if (e instanceof PropertiesManipulator.EmptyLine) { - result.addEmptyLine(); - } - else if (e instanceof PropertiesManipulator.Comment) { - result.addComment(((PropertiesManipulator.Comment) e).getComment()); - } + boolean insideHeader = true; + i = master.getEntries(); + while (i.hasNext()) { + Object e = i.next(); - if (!(e instanceof PropertiesManipulator.Comment)) - break; + if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) { + result.addEmptyLine(); + } + else if (!insideHeader && e instanceof PropertiesManipulator.Comment) { + result.addComment( ( (PropertiesManipulator.Comment) e).getComment()); + } + else if (e instanceof PropertiesManipulator.Entry) { + String key = ( (PropertiesManipulator.Entry) e).getKey(); + String value = slave.get(key); + result.addEntry(key, value); } - boolean insideHeader=true; - i = master.getEntries(); - while (i.hasNext()) { - Object e = i.next(); + insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment); + } - if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) { - result.addEmptyLine(); - } - else if (!insideHeader && e instanceof PropertiesManipulator.Comment) { - result.addComment(((PropertiesManipulator.Comment) e).getComment()); - } - else if (e instanceof PropertiesManipulator.Entry) { - String key = ((PropertiesManipulator.Entry) e).getKey(); - String value = slave.get(key); - result.addEntry(key,value); + try { + PropertiesManipulator.writeProperties(result, new FileOutputStream(new File(aSlave))); + } + catch (Throwable t) { + System.out.println("Unable to write slave properties: " + t.getMessage()); + return; + } + } + + public static void encode(String aBundle, String anEncoding, String anOutputFile) { + PropertiesManipulator bundle; + + try { + bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aBundle))); + + PropertiesManipulator.writeProperties(bundle, new FileOutputStream(anOutputFile), anEncoding, false); + } + catch (Throwable t) { + System.out.println("Unable to read master properties: " + t.getMessage()); + return; + } + } + + public static void decode(String aBundle, String anEncoding, String aSourceFile) { + PropertiesManipulator bundle; + + try { + bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aSourceFile)), anEncoding); + + PropertiesManipulator.writeProperties(bundle, new FileOutputStream(aBundle)); + } + catch (Throwable t) { + System.out.println("Unable to read master properties: " + t.getMessage()); + return; + } + } + + public static void main(String[] anArguments) { + String command = "help"; + + if (anArguments.length >= 1) { + command = anArguments[0]; + + if (command.equals("compare")) { + if (anArguments.length==3) { + compare(anArguments[1], anArguments[2]); + + return; } + } + else if (command.equals("align")) { + if (anArguments.length==3) { + align(anArguments[1], anArguments[2]); - insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment); + return; + } } - try { - PropertiesManipulator.writeProperties(result, new FileOutputStream(new File(anArguments[0] + "_" + anArguments[2] + ".properties"))); + else if (command.equals("encode")) { + if (anArguments.length==4) { + encode(anArguments[1], anArguments[2], anArguments[3]); + + return; + } } - catch (Throwable t) { - System.out.println("Unable to write slave properties: " + t.getMessage()); - return; + else if (command.equals("decode")) { + if (anArguments.length==4) { + decode(anArguments[1], anArguments[2], anArguments[3]); + + return; + } } + } + + + + System.out.println("Usage:"); + + System.out.println(" BundleTool align "); + System.out.println(""); + System.out.println(" Reorders keys/values in a slave bundle according to a master bundle."); + System.out.println(""); + System.out.println(" BundleTool compare "); + System.out.println(""); + System.out.println(" Compares availability of bundle keys."); + System.out.println(""); + System.out.println(" BundleTool encode "); + System.out.println(""); + System.out.println(" Encodes the keys/values with a custom encoding."); + System.out.println(""); + System.out.println(" BundleTool decode "); + System.out.println(""); + System.out.println(" Decodes the keys/values with a custom encoding."); } + } \ No newline at end of file