package turgen.dtb;

import java.io.File;

/** Vyjimka je vyhozena, jestlize soubor nema spravny format.
    Zprava obsahuje popis chyby.
 */


public class DOS2BinaryException extends Exception {
    /** Zprava*/
    private final String message;
    private final int offset;
    private final String filename;
    
    public DOS2BinaryException(String filename,String message,int offset) {
        this.message=message;
        this.offset=offset;
        File f = new File(filename);
        this.filename=f.getName();
    }
    /** Pozadavek na vraceni zpravy
     *@return Zprava
     */
    public String getMessage() {return getMessageString();}
    
    public String toString() {
        return getClass().getName()+" "+getMessageString();
    }
    
    private String getMessageString() {
        StringBuilder sb = new StringBuilder();
        sb.append(filename);
        sb.append(": ");
        sb.append(message);
        if (!message.endsWith(".")) sb.append('.');
        sb.append(" Offset: ");
        sb.append(offset);
        sb.append(" [0x");
        sb.append(Integer.toHexString(offset).toUpperCase());
        sb.append(']');
        return sb.toString();
    }
    
    public String getFormattedMessage() {
        StringBuilder sb = new StringBuilder();
        sb.append("<HTML>");
        sb.append(getClass().getName());
        sb.append(": ");
        sb.append(filename);
        sb.append("<BR>");
        sb.append(message);
        if (!message.endsWith(".")) sb.append('.');
        sb.append("<BR>");
        sb.append(" Offset: ");
        sb.append(offset);
        sb.append(" [0x");
        sb.append(Integer.toHexString(offset).toUpperCase());
        sb.append(']');
        sb.append("</HTML>");
        return sb.toString();
    }
}