⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wapcodepage.java

📁 WAP Stack implementation jwap.sourceforge.net
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/** * JWAP - A Java Implementation of the WAP Protocols * Copyright (C) 2001-2004 Niko Bender * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package net.sourceforge.jwap.wsp.header;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Enumeration;import net.sourceforge.jwap.util.Logger;import net.sourceforge.jwap.util.TransTable;import net.sourceforge.jwap.wsp.WSPDecoder;/** * This class implements the WAP default codepage for Header encoding/decoding * * @author Michel Marti */public class WAPCodePage extends CodePage {      private static final String TT_CTENC = "content-encoding";    private static final String TT_WKPARMS = "wk-params-1.3";    private static final String TT_CHARSETS = "charsets";    private static final String TT_LNG = "languages";    private static final String TT_CCONTROL = "cache-control";    private static final String TT_CTYPES = "content-types-1.3";        private static final Logger log = Logger.getLogger(WAPCodePage.class);    private static SimpleDateFormat fmt = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");    private TransTable transTable;    /**     * Construct a new WAP Code Page. Encoding is done according to WAP encoding     * version 1.1     * @throws IOException if the code-page translation table cannot be found     */    protected WAPCodePage() throws IOException {        this(1, 1);    }    /**     * Construct a new WAP Code Page     * @param major WAP major version     * @param minor WAP minor version     * @throws IOException if the code-page translation table cannot be found     */    protected WAPCodePage(int major, int minor) throws IOException {        super(1, true, "default");        StringBuffer rn = new StringBuffer("wsp-headers-").append(major)                                                          .append(".").append(minor);        transTable = TransTable.getTable(rn.toString());    }    /**     * Returns an instance of the WAP Codepage. Encoding is done according     * to encoding version 1.1     */    public static WAPCodePage getInstance() {        try {            return new WAPCodePage(1,1);        } catch (IOException e) {            throw new RuntimeException(e.getMessage());        }    }    /**     * Returns an instance of the WAP Codepage. Encoding is done according     * to the specified encoding version.     * @param major the major encoding version     * @param minor the minor encoding version     * @throws IllegalArgumentException if no encoding exists for the     *   specified major/minor version     */    public static WAPCodePage getInstance(int major, int minor)        throws IllegalArgumentException {        try {            return new WAPCodePage(major, minor);        } catch (IOException e) {            throw new IllegalArgumentException(major + "." + minor +                ": No encoding available");        }    }    public byte[] encode(String key, String value) throws HeaderParseException {        String lowKey = key.toLowerCase().trim();        Object o = transTable.str2code(lowKey);        if (log.isDebugEnabled()) {            log.debug("encode: '" + key + "' -> " + o);        }        // Not a Well-Known value?         if (o == null) {            if (log.isDebugEnabled()) {                log.debug(key +                    ": Not a well known value, using text-string encoding");            }            return Encoding.encodeHeader(key, Encoding.textString(value));        }        ByteArrayOutputStream out = new ByteArrayOutputStream();        short wk = ((Integer) o).shortValue();        try {            if ("content-base".equals(lowKey)) {                throw new HeaderParseException(key +                    ": Deprecated Header field");            }            try {                // Invoke handler using reflection..                encodeHeader(lowKey, out, wk, value);            } catch (NoSuchMethodException nsme) {                log.warn(key +                    ": No handler for this header, using text-string encoding");                out.write(Encoding.encodeHeader(wk, Encoding.textString(value)));            }        } catch (HeaderParseException hpe) {            throw hpe;        } catch (IOException ex) {            throw new HeaderParseException("I/O Error while encoding.", ex);        } catch (Exception uex) {            log.warn("Unexpected exception while encoding "+key+"="+value, uex);            throw new HeaderParseException("Unexpected exception while encoding.",                uex);        }        return out.toByteArray();    }    public byte[] encode(String key, Date value) {        Object o = transTable.str2code(key.trim());        long secs = (value == null) ? 0 : (value.getTime() / 1000);        byte[] hv = Encoding.longInteger(secs);        if (o == null) {            return Encoding.encodeHeader(key, hv);        } else {            return Encoding.encodeHeader(((Integer) o).shortValue(), hv);        }    }    public byte[] encode(String key, long value) throws HeaderParseException {        Object o = transTable.str2code(key.trim());        short wk = ((o == null) ? (-1) : ((Integer) o).shortValue());        if (value < 0) {            throw new HeaderParseException(value +                ": negative integer values not accepted");        }        byte[] hv = Encoding.integerValue(value);        if (o == null) {            return Encoding.encodeHeader(key, hv);        } else {            return Encoding.encodeHeader(wk, hv);        }    }    public void encodeAccept(OutputStream hdrs, short wk, String value)        throws IOException {        for (Enumeration e = HeaderToken.tokenize(value); e.hasMoreElements();) {            HeaderToken ht = (HeaderToken) e.nextElement();            String token = ht.getToken();            // Lookup content-type            Integer code = TransTable.getTable(TT_CTYPES).str2code(token);            if (code == null) {                hdrs.write(Encoding.encodeHeader(wk, Encoding.textString(token)));            } else {                hdrs.write(Encoding.encodeHeader(wk,                        Encoding.shortInteger(code.shortValue())));            }        }    }    public void encodeAcceptCharset(OutputStream hdrs, short wk, String value)        throws IOException {        for (Enumeration e = HeaderToken.tokenize(value); e.hasMoreElements();) {            HeaderToken ht = (HeaderToken) e.nextElement();            String token = ht.getToken();            if ("*".equals(token)) {                hdrs.write(new byte[] { (byte) wk, (byte) 0xff });                continue;            }            Integer wkl = TransTable.getTable(TT_CHARSETS).str2code(token);            String qf = ht.getParameter("q");            if (qf == null) {                if (wkl != null) {                    hdrs.write(Encoding.encodeHeader(wk,                            Encoding.integerValue(wkl.longValue())));                } else {                    hdrs.write(Encoding.encodeHeader(wk,                            Encoding.extensionMedia(token)));                }            } else {                float q = Float.parseFloat(qf);                byte[] qb = Encoding.qualityFactor(q);                byte[] lng = null;                if (wkl != null) {                    lng = Encoding.shortInteger(wkl.shortValue());                } else {                    lng = Encoding.textString(value);                }                byte[] dt = new byte[qb.length + lng.length + 1];                dt[0] = (byte) (qb.length + lng.length);                System.arraycopy(lng, 0, dt, 1, lng.length);                System.arraycopy(qb, 0, dt, lng.length + 1, qb.length);                hdrs.write(Encoding.encodeHeader(wk, dt));            }        }    }    public void encodeAcceptEncoding(OutputStream hdrs, short wk, String value)        throws IOException {        for (Enumeration e = HeaderToken.tokenize(value); e.hasMoreElements();) {            HeaderToken ht = (HeaderToken) e.nextElement();            String token = ht.getToken();            byte[] encoding = null;            byte[] params = null;            Integer code = TransTable.getTable(TT_CTENC).str2code(token);            if (code != null) {                encoding = new byte[] { (byte) (code.intValue() & 0xff) };            } else {                encoding = Encoding.textString(token);            }            // Q-Factor?            String qFactor = ht.getParameter("q");            if (qFactor != null) {                float qf = Float.parseFloat(qFactor);                params = Encoding.qualityFactor(qf);            }            hdrs.write(Encoding.shortInteger(wk));            if ((params != null) || "*".equals(token)) {                hdrs.write(Encoding.valueLength(encoding.length +                        ((params == null) ? 0 : params.length)));            }            hdrs.write(encoding);            if (params != null) {                hdrs.write(params);            }        }    }    public void encodeAcceptLanguage(OutputStream hdrs, short wk, String value)        throws IOException {        for (Enumeration e = HeaderToken.tokenize(value); e.hasMoreElements();) {            HeaderToken ht = (HeaderToken) e.nextElement();            String token = ht.getToken();            if ("*".equals(token)) {                hdrs.write(new byte[] { (byte) wk, (byte) 0xff });                continue;            }            Integer wkl = TransTable.getTable(TT_LNG).str2code(token);            String qf = ht.getParameter("q");            if (qf == null) {                if (wkl != null) {                    hdrs.write(Encoding.encodeHeader(wk,                            Encoding.shortInteger(wkl.shortValue())));                } else {

⌨️ 快捷键说明

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