// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.util.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.*;

public class XmlBinFilCtrlExt {
    public static final int FIELD_BYTE = 1;
    public static final int FIELD_SHORT = 2;
    public static final int FIELD_INT = 3;
    public static final int FIELD_LONG = 4;
    public static final int FIELD_FLOAT = 5;
    public static final int FIELD_DOUBLE = 6;
    public static int parseType(String s) {
        if(s.equals("byte")) return FIELD_BYTE;
        if(s.equals("short")) return FIELD_SHORT;
        if(s.equals("int")) return FIELD_INT;
        if(s.equals("long")) return FIELD_LONG;
        if(s.equals("float")) return FIELD_FLOAT;
        if(s.equals("double")) return FIELD_DOUBLE;
        return -1;
    }
    public static FieldDescr[] readConfig(String xmlctrl) throws SAXException, IOException, ParserConfigurationException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File(xmlctrl));
        NodeList elements = doc.getElementsByTagName("field");
        FieldDescr[] res = new FieldDescr[elements.getLength()];
        int[] types = new int[elements.getLength()];
        for (int i = 0; i < elements.getLength(); i++) {
            res[i] = new FieldDescr();
            res[i].setName(elements.item(i).getAttributes().getNamedItem("name").getNodeValue());
            res[i].setType(parseType(elements.item(i).getAttributes().getNamedItem("type").getNodeValue()));
        }
        return res;
    }
    public static void xput(Map data, String name, Object o) {
        ArrayList al = (ArrayList)data.get(name);
        if(al == null) {
            al = new ArrayList();
            data.put(name, al);
        }
        al.add(o);
    }
    public static Map readData(int n, DataInputStream dis, FieldDescr[] fields) throws IOException {
        Map res = new HashMap();
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < fields.length; i++) {
                String name = fields[i].getName();
                switch (fields[i].getType()) {
                case FIELD_BYTE:
                    xput(res, name, new Byte(dis.readByte()));
                    break;
                case FIELD_SHORT:
                    xput(res, name, new Short(dis.readShort()));
                    break;
                case FIELD_INT:
                    xput(res, name, new Integer(dis.readInt()));
                    break;
                case FIELD_LONG:
                    xput(res, name, new Long(dis.readLong()));
                    break;
                case FIELD_FLOAT:
                    xput(res, name, new Float(dis.readFloat()));
                    break;
                case FIELD_DOUBLE:
                    xput(res, name, new Double(dis.readDouble()));
                    break;
                }
            }
        }
        return res;
    }
    private static Object xget(Map data, String name, int ix) {
        return ((ArrayList)data.get(name)).get(ix);
    }
    public static void writeData(Map data, int n, DataOutputStream dos, FieldDescr[] fields) throws IOException {
        for(int j = 0; j < n; j++) {
            for (int i = 0; i < fields.length; i++) {
                String name = fields[i].getName();
                switch (fields[i].getType()) {
                case FIELD_BYTE:
                    dos.writeByte(((Byte) xget(data, name, j)).byteValue());
                    break;
                case FIELD_SHORT:
                    dos.writeShort(((Short) xget(data, name, j)).shortValue());
                    break;
                case FIELD_INT:
                    dos.writeInt(((Integer) xget(data, name, j)).intValue());
                    break;
                case FIELD_LONG:
                    dos.writeLong(((Long) xget(data, name, j)).longValue());
                    break;
                case FIELD_FLOAT:
                    dos.writeFloat(((Float) xget(data, name, j)).floatValue());
                    break;
                case FIELD_DOUBLE:
                    dos.writeDouble(((Double) xget(data, name, j)).doubleValue());
                    break;
                }
            }
        }
    }
    public static void main(String[] args) throws Exception {
        FieldDescr[] f = readConfig("C:\\ctrl.xml");
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("C:\\bin.dat"));
        ArrayList a1 = new ArrayList();
        a1.add(new Integer(123));
        a1.add(new Integer(321));
        ArrayList b1 = new ArrayList();
        b1.add(new Double(123.456));
        b1.add(new Double(654.321));
        Map hm1 = new HashMap();
        hm1.put("a", a1);
        hm1.put("b", b1);
        System.out.println(hm1);
        writeData(hm1, 2, dos, f);
        dos.close();
        DataInputStream dis = new DataInputStream(new FileInputStream("C:\\bin.dat"));
        Map hm2 = readData(2, dis, f);
        System.out.println(hm2);
        dis.close();
    }
}

class FieldDescr {
    private String name;
    private int type;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
}
