// 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 XmlBinFilCtrl {
    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 Field[] 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");
        Field[] res = new Field[elements.getLength()];
        int[] types = new int[elements.getLength()];
        for (int i = 0; i < elements.getLength(); i++) {
            res[i] = new Field();
            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 Map readData(DataInputStream dis, Field[] fields) throws IOException {
        Map res = new HashMap();
        for(int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            switch(fields[i].getType()) {
            case FIELD_BYTE:
                res.put(name, new Byte(dis.readByte()));
                break;
            case FIELD_SHORT:
                res.put(name, new Short(dis.readShort()));
                break;
            case FIELD_INT:
                res.put(name, new Integer(dis.readInt()));
                break;
            case FIELD_LONG:
                res.put(name, new Long(dis.readLong()));
                break;
            case FIELD_FLOAT:
                res.put(name, new Float(dis.readFloat()));
                break;
            case FIELD_DOUBLE:
                res.put(name, new Double(dis.readDouble()));
                break;
            }
        }
        return res;
    }
    public static void writeData(Map data, DataOutputStream dos, Field[] fields) throws IOException {
        for(int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            switch(fields[i].getType()) {
            case FIELD_BYTE:
                dos.writeByte(((Byte)data.get(name)).byteValue());
                break;
            case FIELD_SHORT:
                dos.writeShort(((Short)data.get(name)).shortValue());
                break;
            case FIELD_INT:
                dos.writeInt(((Integer)data.get(name)).intValue());
                break;
            case FIELD_LONG:
                dos.writeLong(((Long)data.get(name)).longValue());
                break;
            case FIELD_FLOAT:
                dos.writeFloat(((Float)data.get(name)).floatValue());
                break;
            case FIELD_DOUBLE:
                dos.writeDouble(((Double)data.get(name)).doubleValue());
                break;
            }
        }
    }
    public static void main(String[] args) throws Exception {
        Field[] f = readConfig("C:\\ctrl.xml");
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("C:\\bin.dat"));
        Map hm1 = new HashMap();
        hm1.put("a", new Integer(123));
        hm1.put("b", new Double(123.456));
        writeData(hm1, dos, f);
        Map hm2 = new HashMap();
        hm2.put("a", new Integer(321));
        hm2.put("b", new Double(654.321));
        writeData(hm2, dos, f);
        dos.close();
        DataInputStream dis = new DataInputStream(new FileInputStream("C:\\bin.dat"));
        Map hm3 = readData(dis, f);
        System.out.println(hm3);
        Map hm4 = readData(dis, f);
        System.out.println(hm4);
        dis.close();
    }
}

class Field {
    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;
    }
}
