 /**   Resource files.
 *  All resource are store into resource files.   Resource files are created during build time, and packed all resources of same type
 *  into one file.  All resource files have a common header:
 *  <nbr of resource><offset of resource 1><offset of resource 2><offset of resource 3><offset of resource 4>....   
 *  Each header elts is 4 byte longs (int), so the header size is ((nbr of resource)+1)*4 
 *  
 *  Resource ID.
 *  Each resource is identified by an ID (and no longer by a string), which is the position of the resource in the file,
 *  and then the position of the resource in the array.   All Ids are static final member of the TextRscId class, which is generated 
 *  during the build.   For ex the RscId class will look like that:   
 *  
 *  public final class RscId
 *  {
 *      public final short Tx_rbLabel = 0 ;
 *      public final short Tx_lbLabel = 1 ;
 *      public final short Tx_mainViewTitle = 2 ;
 *      public final short Tx_welcomeMessage = 3 ;
 *      ......
 *  }
 *  
 *  it could be generate by the build from such resource files:
 *  text.english.resource
 *  rbLabel="ok"
 *  lbLabel="Quit"
 *  mainViewTitle="this is the main view"
 *  welcomeMessage="Hello world"
 *  
 *  Each resource file name is coded as the following: <type>.<name>.resource   The build shall automatically process any .resource files.
 */

import java.io.IOException;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import org.apache.tools.ant.BuildException;

public class TxtToBinary extends ResourceToBinary
{
    public TxtToBinary()
    {
        super();
        CLASS_NAME = "public final class TextRscId";
        PREFIX = "TX_";
    }

    public void execute() throws BuildException
    {
        TxtToBinary t2b = new TxtToBinary();
        t2b.setFileName(fileName);
        t2b.setJavaFileName(javaFileName);
        t2b.setTodir(toDir) ;
        
        try
        {
            t2b.loadText();
            t2b.writeToBinary();
            t2b.testLoadBinary();
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }

    }

    protected void writeBinary(DataOutputStream dos) throws IOException
    {
        // write number of resources
        dos.writeInt(values.size());
        int offset = 0;

        for(int i = 0; i < values.size(); i++)
        {
            dos.writeInt(offset);
            String value = (String) this.values.get(i);
            if(fileName.indexOf("chinese") != -1)
            offset += value.getBytes("utf8").length;
            else
            offset += value.getBytes().length;
        }
        for(int i = 0; i < values.size(); i++)
        {
            String value = (String) this.values.get(i);
            if(fileName.indexOf("chinese") != -1)
            	dos.write(value.getBytes("utf8"));
            else
            dos.write(value.getBytes());
        }
    }

    private void testLoadBinary() throws IOException
    {
        DataInputStream dis = new DataInputStream(new FileInputStream(this.fileName + ".bin"));

        int totalSize = dis.available();
        int rsCount = dis.readInt();
        
        // get each resource data offset
        int[] offset = new int[rsCount];
        for (int i = 0; i < rsCount; i++)
        {
            offset[i] = dis.readInt();
        }
        int dataLength = totalSize - (rsCount + 1) * 4;
        byte textData[] = new byte[dataLength];
        dis.readFully(textData);
        
        DataInputStream offsetDis = new DataInputStream(new ByteArrayInputStream(textData));

        int stringSize = 0;
        for (int i = 0; i < rsCount; i++)
        {
            if(i < rsCount -1)
                stringSize = offset[i+1] - offset[i];
            else
                stringSize = dataLength - offset[rsCount-1];
            
            byte[] data = new byte[stringSize];
            offsetDis.read(data);
            String value = new String(data);
//            System.out.println("value: " + value);
        }
    }
}
