Bundletool dev
authorzapata <zapata>
Fri, 4 Apr 2003 02:04:37 +0000 (02:04 +0000)
committerzapata <zapata>
Fri, 4 Apr 2003 02:04:37 +0000 (02:04 +0000)
source/mir/util/PropertiesManipulator.java
source/tool/BundleTool.java

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