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

📄 tokenrepository.java

📁 WAP协议栈的JAVA实现
💻 JAVA
字号:
/**
 * 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.util.wbxml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * @author suvarna
 **/
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilderFactory;


public class TokenRepository {
    private static TokenRepository instance;
    private byte wbxmlEncoding;
    private byte charEncoding;
    private String publicID;
    private Document tokenDoc;
    private Hashtable tags;
    private Hashtable attributeNames;
    private Hashtable attributeNameTokens; // for decoder
    private Hashtable attributeValues;
    private Hashtable attributePrefixes;

    private TokenRepository() {
        initialize();
    }

    public static TokenRepository getInstance() {
        if (instance == null) {
            instance = new TokenRepository();
        }
        return instance;
    }

    public void initialize() {
        try {
            tokenDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                                             .parse("./TokenRepository.xml");
            initializeHeaderInfo();
            initializeTags();
            initializeAttributeNames();
            initializeAttributeNameTokens();
            initializeAttributeValues();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }

    private void initializeHeaderInfo() {
        Element codePage = (Element) tokenDoc.getElementsByTagName("codepage")
                                             .item(0);
        publicID = codePage.getAttribute("publicID");

        String wbxmlEncodingStr = codePage.getAttribute("wbxmlVersion");
        int dotPosition = wbxmlEncodingStr.indexOf('.');
        byte majorVersion = Byte.parseByte(wbxmlEncodingStr.substring(0,
                    dotPosition));
        majorVersion = (byte) (majorVersion - 1);

        byte minorVersion = Byte.parseByte(wbxmlEncodingStr.substring(wbxmlEncodingStr.indexOf(
                        '.') + 1));
        wbxmlEncoding = (byte) ((majorVersion << 4) + minorVersion);

        String charEncodingStr = codePage.getAttribute("default_encoding");
        charEncoding = (byte) IANACharSet.getMIBEnum(charEncodingStr.toUpperCase());
    }

    private void initializeTags() {
        tags = new Hashtable();

        NodeList tagNodes = tokenDoc.getElementsByTagName("tag");
        Element aTag;

        for (int i = 0; i < tagNodes.getLength(); i++) {
            aTag = (Element) tagNodes.item(i);
            tags.put(aTag.getAttribute("name"), aTag.getAttribute("token-value"));
        }
    }

    private void initializeAttributeNames() {
        attributeNames = new Hashtable();
        attributePrefixes = new Hashtable();

        NodeList attributeNameNodes = tokenDoc.getElementsByTagName(
                "attribute-start");
        Element anAttributeNameNode;

        for (int i = 0; i < attributeNameNodes.getLength(); i++) {
            anAttributeNameNode = (Element) attributeNameNodes.item(i);

            String name = anAttributeNameNode.getAttribute("name");
            String prefix = anAttributeNameNode.getAttribute("prefix");
            String key = name + prefix;
            attributeNames.put(key,
                anAttributeNameNode.getAttribute("token-value"));

            if (attributePrefixes.containsKey(name)) {
                ((Vector) attributePrefixes.get(name)).addElement(prefix);
            } else {
                Vector prefixes = new Vector();
                prefixes.addElement(prefix);
                attributePrefixes.put(name, prefixes);
            }
        }
    }

    private void initializeAttributeNameTokens() {
        attributeNameTokens = new Hashtable();

        NodeList attributeNameNodes = tokenDoc.getElementsByTagName(
                "attribute-start");
        Element anAttributeNameNode;

        for (int i = 0; i < attributeNameNodes.getLength(); i++) {
            anAttributeNameNode = (Element) attributeNameNodes.item(i);

            String token = String.valueOf(getByteValue(
                        anAttributeNameNode.getAttribute("token-value")));
            String[] nameAndPrefixes = new String[2];
            String name = anAttributeNameNode.getAttribute("name");
            String prefix = anAttributeNameNode.getAttribute("prefix");
            nameAndPrefixes[0] = name;
            nameAndPrefixes[1] = prefix;
            attributeNameTokens.put(token, nameAndPrefixes);
        }
    }

    private void initializeAttributeValues() {
        attributeValues = new Hashtable();

        NodeList attributeValueNodes = tokenDoc.getElementsByTagName(
                "attribute-value");
        Element anAttributeValueNode;

        for (int i = 0; i < attributeValueNodes.getLength(); i++) {
            anAttributeValueNode = (Element) attributeValueNodes.item(i);
            attributeValues.put(anAttributeValueNode.getAttribute("name"),
                anAttributeValueNode.getAttribute("token-value"));
        }
    }

    public byte getTagToken(String tagName) {
        System.out.println(tags.get(tagName.toLowerCase()));

        return getByteValue(tags.get(tagName.toLowerCase()).toString());
    }

    private byte getByteValue(String value) {
        try {
            //byte tokenValue = Byte.parseByte(value, 16);
            byte tokenValue = Integer.valueOf(value, 16).byteValue();

            return tokenValue;
        } catch (Exception exp) {
            System.out.println("token not found!!!, returning literal");

            return GlobalTokens.LITERAL;
        }
    }

    public String getPublicIdentifier() {
        Element codePage = (Element) tokenDoc.getElementsByTagName("codepage")
                                             .item(0);

        return codePage.getAttribute("publicID");
    }

    public String getTagName(byte tokenValue) {
        Iterator iterator = tags.entrySet().iterator();

        return getKeyFromValue(iterator, tokenValue);
    }

    public String[] getAttributeNameAndPrefix(byte tokenValue) {
        String token = String.valueOf(tokenValue);

        return (String[]) attributeNameTokens.get(token);
    }

    public byte getAttributeNameToken(String attributeName, String prefix) {
        String token = (String) attributeNames.get((attributeName + prefix).toLowerCase());

        if (token == null) {
            return GlobalTokens.LITERAL;
        }

        return getByteValue(token);
    }

    public Vector getAttributePrefixes(String attributeName) {
        return (Vector) attributePrefixes.get(attributeName);
    }

    public String getAttributeValue(byte tokenValue) {
        Iterator iterator = attributeValues.entrySet().iterator();

        return getKeyFromValue(iterator, tokenValue);
    }

    public byte getAttributeValueToken(String attributeValue) {
        String hexValue = attributeValues.get(attributeValue.toLowerCase())
                                         .toString();

        return getByteValue(hexValue);
    }

    private String getKeyFromValue(Iterator iterator, byte tokenValue) {
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();

            if (getByteValue(entry.getValue().toString()) == tokenValue) {
                return entry.getKey().toString();
            }
        }

        return null;
    }

    /**
    * Returns the attributeValues.
    * @return Hashtable
    */
    public Hashtable getAttributeValues() {
        return attributeValues;
    }

    /**
     * Sets the attributeValues.
     * @param attributeValues The attributeValues to set
     */
    public void setAttributeValues(Hashtable attributeValues) {
        this.attributeValues = attributeValues;
    }

    /**
     * Returns the publicID.
     * @return String
     */
    public String getPublicID() {
        return publicID;
    }

    /**
     * Returns the wbxmlEncoding.
     * @return byte
     */
    public byte getWbxmlEncoding() {
        return wbxmlEncoding;
    }

    /**
     * Returns the charEncoding.
     * @return byte
     */
    public byte getCharEncoding() {
        return charEncoding;
    }
}

⌨️ 快捷键说明

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