Results 1 to 5 of 5
  1. #1
    Mortein79 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default gettext binary MO file creating with Java

    I tried creating a utility to parse gettext po file and generate binary mo file. The parser is simple (my co. not use fuzzy, plural, etc. things, just msgid/msgstr), but the generator is not work.

    Here is the description of the mo file, here is the original generator source (it's C), and found a php script also.

    My code:

    Java Code:
    public void writeFile(String filename, Map<String, String> polines) throws FileNotFoundException, IOException {
    
      DataOutputStream os = new DataOutputStream(new FileOutputStream(filename));
      HashMap<String, String> bvc = new HashMap<String, String>();
      TreeMap<String, String> hash = new TreeMap(bvc);
      hash.putAll(polines);
    
    
      StringBuilder ids = new StringBuilder();
      StringBuilder strings = new StringBuilder();
      ArrayList<ArrayList> offsets = new ArrayList<ArrayList>();
      ArrayList<Integer> key_offsets = new ArrayList<Integer>();
      ArrayList<Integer> value_offsets = new ArrayList<Integer>();
      ArrayList<Integer> temp_offsets = new ArrayList<Integer>();
    
      for (Map.Entry<String, String> entry : hash.entrySet()) {
        String id = entry.getKey();
        String str = entry.getValue();
    
        ArrayList<Integer> offsetsItems = new ArrayList<Integer>();
        offsetsItems.add(ids.length());
        offsetsItems.add(id.length());
        offsetsItems.add(strings.length());
        offsetsItems.add(str.length());
        offsets.add((ArrayList) offsetsItems.clone());
    
        ids.append(id).append('\0');
        strings.append(str).append('\0');
      }
      Integer key_start = 7 * 4 + hash.size() * 4 * 4;
      Integer value_start = key_start + ids.length();
    
      Iterator e = offsets.iterator();
      while (e.hasNext()) {
        ArrayList<Integer> offEl = (ArrayList<Integer>) e.next();
        key_offsets.add(offEl.get(1));
        key_offsets.add(offEl.get(0) + key_start);
        value_offsets.add(offEl.get(3));
        value_offsets.add(offEl.get(2) + value_start);
      }
    
      temp_offsets.addAll(key_offsets);
      temp_offsets.addAll(value_offsets);
    
    
      os.writeInt(0xde120495);
    
      os.writeByte(0x00);
      os.writeInt(hash.size());
      os.writeInt((7 * 4));
      os.writeInt((7 * 4 + hash.size() * 8));
      os.writeInt(0x00000000);
      os.writeInt(key_start);
    
      Iterator offi = temp_offsets.iterator();
      while (offi.hasNext()) {
        Integer off = (Integer) offi.next();
        os.writeInt(off);
      }
      os.write(ids.toString().getBytes("utf-8"));
      os.write(strings.toString().getBytes("utf-8"));
    
      os.close();
    }
    The line os.writeInt(key_start); seems like ok, the differences from the original tool generated mo file starting after write the hash table.

    There is a good generated file binary dump (with the original msgfmt utility): the good And this is the wrong, generated by the code above: the bad.

    So, what's wrong? (aside from my scary english..)
    Last edited by Mortein79; 02-08-2012 at 10:24 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: gettext binary MO file creating with Java

    How are you trying to debug the code?
    Take a very small input file and follow all the calculations done by the program, byte by byte, as it creates the output file.
    If you don't have an interactive debugger then use printlns statements to show what the code is doing.
    If you understand what the code is supposed to do, seeing what it is doing should help you change the code so it does what you want.

    If you are having trouble with statements that don't do what you want, post the statements, the input to them and the desired output from them and your questions.

  3. #3
    Mortein79 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: gettext binary MO file creating with Java

    Thanks to the reply. My problem is: don't understand the specification of the mo file exatly, specially the hash table part. My implementation is buggy, and when step-by-step debugging (I use Netbeans 7.1), I don't know the numbers in the variables is true or wrong. My problem is not theoretical, it's concrete technical. :)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: gettext binary MO file creating with Java

    You need to understand the algorithm for what the code is supposed to do.
    When you can do the steps manually you should be able to write code to do the steps.

    If you are having trouble writing the steps, then post your specific questions about how to do a specific step.

  5. #5
    Mortein79 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: gettext binary MO file creating with Java

    Ok, thanks! :)

Similar Threads

  1. Replies: 1
    Last Post: 11-27-2011, 04:25 AM
  2. Creating a file is Holding up java.
    By rizowski in forum New To Java
    Replies: 0
    Last Post: 04-20-2011, 05:47 PM
  3. Reading Binary File using java
    By pnbalaji in forum New To Java
    Replies: 16
    Last Post: 06-11-2010, 02:50 PM
  4. Creating Folder in JAVA and Copying File
    By fnoman in forum New To Java
    Replies: 8
    Last Post: 10-18-2008, 07:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •