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

📄 xmlcontenthandler.java

📁 WAP协议栈的JAVA实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        // apply bit mask for tag with attributes
        if (hasAttributes) {
            byte currentTokenValue = (byte) (currentTagToken.getValue() |
                attributeBitMask);
            currentTagToken.setValue(currentTokenValue);
        }
    }

    private void createAttributeTags(String localName, Attributes atts) {
        // process attributes name and prefix..
        for (int i = 0; i < atts.getLength(); i++) {
            System.out.println("&&" + atts.getURI(i));

            if (atts.getURI(i).length() <= 0) {
                String attributeName = atts.getLocalName(i);
                String attributeValue = atts.getValue(i);
                String prefix = getAttributePrefix(attributeName, attributeValue);
                byte attributeTokenValue = TokenRepository.getInstance()
                                                          .getAttributeNameToken(attributeName,
                        prefix);
                AttributeNameToken attrNameToken = new AttributeNameToken(attributeName,
                        attributeTokenValue);

                // store on tokens vector
                tokens.addElement(attrNameToken);

                // do the same for remaining attribute value ....
                String remainingAttrValue = attributeValue.substring(prefix.length())
                                                          .trim();

                if (remainingAttrValue.length() > 0) {
                    processAttributeValue(remainingAttrValue);
                }
            }
        }

        tokens.addElement(new TagToken("END attribute" + localName,
                GlobalTokens.END));
    }

    private String getAttributePrefix(String attributeName,
        String attributeValue) {
        Vector prefixes = TokenRepository.getInstance().getAttributePrefixes(attributeName);

        if (prefixes == null) {
            return "";
        }

        Iterator iterator = prefixes.iterator();
        int maxMatchSize = 0;
        String maxMatchedPrefix = "";

        while (iterator.hasNext()) {
            String prefix = (String) iterator.next();

            if (attributeValue.startsWith(prefix) &&
                    (maxMatchSize < prefix.length())) {
                maxMatchedPrefix = prefix;
                maxMatchSize = prefix.length();
            }
        }

        return maxMatchedPrefix;
    }

    private void processAttributeValue(String attributeValue) {
        String maxMatchedValue = getMatchedPattern(attributeValue);

        if (maxMatchedValue.length() == 0) {
            writeInlineString(attributeValue);

            return;
        }

        int matchStartIdx = (maxMatchedValue.length() > 0)
            ? attributeValue.indexOf(maxMatchedValue) : 0;
        int matchEndIdx = matchStartIdx + maxMatchedValue.length();

        if (matchStartIdx != 0) {
            String startOfAttrValue = attributeValue.substring(0, matchStartIdx);
            writeInlineString(startOfAttrValue);
        }

        byte tokenValue = TokenRepository.getInstance().getAttributeValueToken(maxMatchedValue);
        AttributeValueToken token = new AttributeValueToken("attvalue",
                tokenValue);
        tokens.addElement(token);

        if (matchEndIdx != 0) {
            String endOfAttrValue = attributeValue.substring(matchEndIdx);
            writeInlineString(endOfAttrValue);
        }
    }

    private String getMatchedPattern(String attributeValue) {
        String maxMatchedValue = "";
        int maxMatchSize = 0;
        Enumeration enum = TokenRepository.getInstance().getAttributeValues()
                                          .keys();

        while (enum.hasMoreElements()) {
            String predefinedValue = (String) enum.nextElement();
            boolean exists = (attributeValue.indexOf(predefinedValue) != -1)
                ? true : false;

            if (exists && (maxMatchSize < predefinedValue.length())) {
                maxMatchedValue = predefinedValue;
                maxMatchSize = predefinedValue.length();
            }
        }

        return maxMatchedValue.trim();
    }

    private void writeInlineString(String inlineString) {
        writeInlineStringToken(GlobalTokens.STR_ISTR_I);

        for (int j = 0; j < inlineString.length(); j++)
            writeInlineStringToken((byte) inlineString.charAt(j));

        writeInlineStringToken((byte) 00);
    }

    private void writeInlineStringToken(byte value) {
        Token stringToken = new Token("STR_INLINE", value);
        tokens.addElement(stringToken);
    }

    /**
     * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
     */
    public void startPrefixMapping(String prefix, String uri)
        throws SAXException {
        System.out.println("startPrefixMapping");
        super.startPrefixMapping(prefix, uri);
    }

    /**
     * @see org.xml.sax.DTDHandler#unparsedEntityDecl(String, String, String, String)
     */
    public void unparsedEntityDecl(String name, String publicId,
        String systemId, String notationName) throws SAXException {
        System.out.println("unparsedEntityDecl");
        super.unparsedEntityDecl(name, publicId, systemId, notationName);
    }

    /**
     * @see org.xml.sax.ErrorHandler#warning(SAXParseException)
     */
    public void warning(SAXParseException exception) throws SAXException {
        System.out.println("warning");
        exception.printStackTrace();
        super.warning(exception);
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
     */
    public void comment(char[] ch, int start, int length)
        throws SAXException {
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#endCDATA()
     */
    public void endCDATA() throws SAXException {
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#endDTD()
     */
    public void endDTD() throws SAXException {
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#endEntity(String)
     */
    public void endEntity(String name) throws SAXException {
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#startCDATA()
     */
    public void startCDATA() throws SAXException {
        System.out.println("START CDATA");
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#startDTD(String, String, String)
     */
    public void startDTD(String name, String publicId, String systemId)
        throws SAXException {
        System.out.println("START DTD");

        int publicidValue = PublicIdentifiers.getInstance()
                                             .getPublicIdentifierValue(publicId);
        byte[] multipleBytes = getMultipleBytes(publicidValue);

        for (int i = 0; i < multipleBytes.length; i++) {
            if (multipleBytes[i] != 0) {
                Token publicid = new Token("publicid", multipleBytes[i]);
                tokens.addElement(publicid);
            }
        }

        Token charset = new Token("charset",
                (byte) IANACharSet.getMIBEnum(encoding)); // to do ...if charset is multiple byte..
        tokens.addElement(charset);

        Token strtbl = new Token("strtble", (byte) 00); // to do ...currently no strings are stored in string table..
        tokens.addElement(strtbl);
    }

    private byte[] getMultipleBytes(int intValue) {
        byte[] multipleBytes = new byte[4];
        multipleBytes[0] = (byte) (intValue >>> 24);
        multipleBytes[1] = (byte) (intValue >>> 16);
        multipleBytes[2] = (byte) (intValue >>> 8);
        multipleBytes[3] = (byte) intValue; // cast implies & 0xff

        return multipleBytes;
    }

    /**
     * WBXML-6. Conversion of all XML parsed entities into string or entity tokens (Mandatory feature)
     * @see org.xml.sax.ext.LexicalHandler#startEntity(String)
     */
    public void startEntity(String name) throws SAXException {
        System.out.println("START entity" + name);
    }

    /**
     * Returns the tokens.
     * @return iterator
     * */
    public Iterator getTokens() {
        return tokens.iterator();
    }
}

⌨️ 快捷键说明

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