hyperlinkrecord.java

来自「EXCEL read and write」· Java 代码 · 共 529 行 · 第 1/2 页

JAVA
529
字号
        int idx = address.indexOf('\u0000');        return idx == -1 ? address : address.substring(0, idx);    }    /**     * Hypelink address. Depending on the hyperlink type it can be URL, e-mail, patrh to a file, etc.     *     * @param address  the address of this hyperlink     */    public void setAddress(String address)    {        this.address = address + '\u0000';    }    /**     * Link options. Must be a combination of HLINK_* constants.     */    public int getLinkOptions(){        return link_opts;    }    /**     * Label options     */    public int getLabelOptions(){        return label_opts;    }    /**     * Options for a file link     */    public int getFileOptions(){        return file_opts;    }    public byte[] getTail(){        return tail;    }    /**     * @param in the RecordInputstream to read the record from     */    public HyperlinkRecord(RecordInputStream in)    {        try {            rwFirst = in.readShort();            rwLast = in.readUShort();            colFirst = in.readShort();            colLast = in.readShort();            // 16-byte GUID            guid = new byte[16];            in.read(guid);            label_opts = in.readInt();            link_opts = in.readInt();            if ((link_opts & HLINK_LABEL) != 0){                int label_len = in.readInt();                label = in.readUnicodeLEString(label_len);            }            if ((link_opts & HLINK_URL) != 0){                moniker = new byte[16];                in.read(moniker);                if(Arrays.equals(URL_MONIKER, moniker)){                    int len = in.readInt();                    address = in.readUnicodeLEString(len/2);                    tail = in.readRemainder();                } else if (Arrays.equals(FILE_MONIKER, moniker)){                    file_opts = in.readShort();                    int len = in.readInt();                    byte[] path_bytes = new byte[len];                    in.read(path_bytes);                    address = new String(path_bytes);                    tail = in.readRemainder();                }            } else if((link_opts & HLINK_PLACE) != 0){                int len = in.readInt();                address = in.readUnicodeLEString(len);            }        } catch (IOException e){            throw new RuntimeException(e);        }    }    public short getSid()    {        return HyperlinkRecord.sid;    }    public int serialize(int offset, byte[] data)    {        int pos = offset;        LittleEndian.putShort(data, pos, sid); pos += 2;        LittleEndian.putShort(data, pos, ( short )(getRecordSize()-4)); pos += 2;        LittleEndian.putUShort(data, pos, rwFirst); pos += 2;        LittleEndian.putUShort(data, pos, rwLast); pos += 2;        LittleEndian.putShort(data, pos, colFirst); pos += 2;        LittleEndian.putShort(data, pos, colLast); pos += 2;        System.arraycopy(guid, 0, data, pos, guid.length); pos += guid.length;        LittleEndian.putInt(data, pos, label_opts); pos += 4;        LittleEndian.putInt(data, pos, link_opts); pos += 4;        if ((link_opts & HLINK_LABEL) != 0){            LittleEndian.putInt(data, pos, label.length()); pos += 4;            StringUtil.putUnicodeLE(label, data, pos);  pos += label.length()*2;        }        if ((link_opts & HLINK_URL) != 0){            System.arraycopy(moniker, 0, data, pos, moniker.length); pos += moniker.length;            if(Arrays.equals(URL_MONIKER, moniker)){                LittleEndian.putInt(data, pos, address.length()*2 + tail.length); pos += 4;                StringUtil.putUnicodeLE(address, data, pos);  pos += address.length()*2;                if(tail.length > 0){                    System.arraycopy(tail, 0, data, pos, tail.length); pos += tail.length;                }            } else if (Arrays.equals(FILE_MONIKER, moniker)){                LittleEndian.putShort(data, pos, file_opts); pos += 2;                LittleEndian.putInt(data, pos, address.length()); pos += 4;                byte[] bytes = address.getBytes();                System.arraycopy(bytes, 0, data, pos, bytes.length); pos += bytes.length;                if(tail.length > 0){                    System.arraycopy(tail, 0, data, pos, tail.length); pos += tail.length;                }            }        } else if((link_opts & HLINK_PLACE) != 0){            LittleEndian.putInt(data, pos, address.length()); pos += 4;            StringUtil.putUnicodeLE(address, data, pos);  pos += address.length()*2;        }    	return getRecordSize();    }    public int getRecordSize()    {        int size = 4;        size += 2 + 2 + 2 + 2;  //rwFirst, rwLast, colFirst, colLast        size += guid.length;        size += 4;  //label_opts        size += 4;  //link_opts        if ((link_opts & HLINK_LABEL) != 0){            size += 4;  //link length            size += label.length()*2;        }        if ((link_opts & HLINK_URL) != 0){            size += moniker.length;  //moniker length            if(Arrays.equals(URL_MONIKER, moniker)){                size += 4;  //address length                size += address.length()*2;                size += tail.length;            } else if (Arrays.equals(FILE_MONIKER, moniker)){                size += 2;  //file_opts                size += 4;  //address length                size += address.length();                size += tail.length;            }        } else if((link_opts & HLINK_PLACE) != 0){            size += 4;  //address length            size += address.length()*2;        }        return size;    }    public String toString()    {        StringBuffer buffer = new StringBuffer();        buffer.append("[HYPERLINK RECORD]\n");        buffer.append("    .rwFirst            = ").append(Integer.toHexString(getFirstRow())).append("\n");        buffer.append("    .rwLast         = ").append(Integer.toHexString(getLastRow())).append("\n");        buffer.append("    .colFirst            = ").append(Integer.toHexString(getFirstColumn())).append("\n");        buffer.append("    .colLast         = ").append(Integer.toHexString(getLastColumn())).append("\n");        buffer.append("    .guid        = ").append(HexDump.toHex(guid)).append("\n");        buffer.append("    .label_opts          = ").append(label_opts).append("\n");        buffer.append("    .label          = ").append(getLabel()).append("\n");        if((link_opts & HLINK_URL) != 0){            buffer.append("    .moniker          = ").append(HexDump.toHex(moniker)).append("\n");        }        buffer.append("    .address            = ").append(getAddress()).append("\n");        buffer.append("[/HYPERLINK RECORD]\n");        return buffer.toString();    }    /**     * Initialize a new url link     */    public void newUrlLink(){        rwFirst = 0;        rwLast = 0;        colFirst = 0;        colLast = 0;        guid = STD_MONIKER;        label_opts = 0x2;        link_opts = HLINK_URL | HLINK_ABS | HLINK_LABEL;        label = "" + '\u0000';        moniker = URL_MONIKER;        address = "" + '\u0000';        tail = URL_TAIL;    }    /**     * Initialize a new file link     */    public void newFileLink(){        rwFirst = 0;        rwLast = 0;        colFirst = 0;        colLast = 0;        guid = STD_MONIKER;        label_opts = 0x2;        link_opts = HLINK_URL | HLINK_LABEL;        file_opts = 0;        label = "" + '\u0000';        moniker = FILE_MONIKER;        address = "" + '\0';        tail = FILE_TAIL;    }    /**     * Initialize a new document link     */    public void newDocumentLink(){        rwFirst = 0;        rwLast = 0;        colFirst = 0;        colLast = 0;        guid = STD_MONIKER;        label_opts = 0x2;        link_opts = HLINK_LABEL | HLINK_PLACE;        label = "" + '\u0000';        moniker = FILE_MONIKER;        address = "" + '\0';        tail = new byte[]{};    }    public Object clone() {        HyperlinkRecord rec = new HyperlinkRecord();        rec.rwFirst = rwFirst;        rec.rwLast = rwLast;        rec.colFirst = colFirst;        rec.colLast = colLast;        rec.guid = guid;        rec.label_opts = label_opts;        rec.link_opts = link_opts;        rec.file_opts = file_opts;        rec.label = label;        rec.address = address;        rec.moniker = moniker;        rec.tail = tail;        return rec;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?