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

📄 xmlcontenthandler.java

📁 WAP协议栈的JAVA实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * 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.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;

import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Stack;
import java.util.Vector;


/**
 * @author Suvarna
 */
public class XMLContentHandler extends DefaultHandler implements LexicalHandler {
    private Stack parentTrace;
    private Vector tokens;
    private String encoding;
    private byte parentBitMask = (byte) 0x40; // The bit representation 10000000
    private byte attributeBitMask = (byte) 0x80; /**
     * Constructor for XMLContentHandler.
     */
    public XMLContentHandler(String encoding) {
        this.encoding = encoding.toUpperCase();
        parentTrace = new Stack();
        tokens = new Vector();
    }

    /**
     * @see org.xml.sax.ContentHandler#characters(char[], int, int)
     */
    public void characters(char[] ch, int start, int length)
        throws SAXException {
        String inlineString = String.valueOf(ch, start, length).trim();

        if (inlineString.length() > 0) {
            checkForParentTag();
            writeInlineString(inlineString);
        }
    }

    /**
     * @see org.xml.sax.ContentHandler#endDocument()
     */
    public void endDocument() throws SAXException {
        System.out.println("endDocument");

        try {
            System.setOut(new java.io.PrintStream(
                    new FileOutputStream("d:\\suvarna\\bytecodes.txt")));
        } catch (Exception exp) {
        }

        for (int i = 0; i < tokens.size(); i++) {
            Token token = (Token) tokens.elementAt(i);

            if (token.getName().equals("STR_INLINE")) {
                System.out.println((char) token.getValue() + "  " +
                    token.getValue());
            } else {
                System.out.println(Integer.toHexString(token.getValue()) + " " +
                    token.getName() + token.getValue());
            }
        }

        /*for (int i = 0; i < tokens.size(); i++) {
                Token token = (Token) tokens.elementAt(i);
                System.out.print(Integer.toHexString(token.getValue()) + " ");
        }*/
    }

    /**
     * @see org.xml.sax.ContentHandler#endElement(String, String, String)
     */
    public void endElement(String namespaceURI, String localName, String qName)
        throws SAXException {
        // pop the element from the stack if any
        if (!parentTrace.isEmpty()) {
            parentTrace.pop();
        } else {
            tokens.addElement(new TagToken("END element" + localName,
                    GlobalTokens.END));
        }
    }

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

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

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

    /**
     * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
     */
    public void ignorableWhitespace(char[] ch, int start, int length)
        throws SAXException {
        //System.out.println("ignorableWhitespace");
    }

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

    /**
     * @see org.xml.sax.ContentHandler#processingInstruction(String, String)
     */
    public void processingInstruction(String target, String data)
        throws SAXException {
        System.out.println("target= " + target + " data " + data);
    }

    /**
     * @see org.xml.sax.EntityResolver#resolveEntity(String, String)
     */
    public InputSource resolveEntity(String publicId, String systemId)
        throws SAXException {
        System.out.println("resolveEntity");

        try {
            return super.resolveEntity(publicId, systemId);
        } catch (Exception e) {
            if( e instanceof SAXException ) {
                throw (SAXException) e;
            } else {
                throw new SAXException(e);
            }
        }
    }

    /**
     * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator)
     */
    public void setDocumentLocator(Locator locator) {
        System.out.println("setDocumentLocator" + locator.getPublicId() +
            locator.getSystemId());
        super.setDocumentLocator(locator);
    }

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

    /**
     * @see org.xml.sax.ContentHandler#startDocument()
     * as per bnf of wbxml...
     * ?? from where should the value for wbxml is taken??? application property??
     */
    public void startDocument() throws SAXException {
        System.out.println("startDocument");

        //	Token version = new Token("version", TokenRepository.getInstance().getWbxmlEncoding());
        //	tokens.addElement(version);
    }

    /**
     * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
     */
    public void startElement(String namespaceURI, String localName,
        String qName, Attributes atts) throws SAXException {
        checkForParentTag();

        boolean hasAttributes = atts.getLength() > 0;
        createTagToken(localName, hasAttributes);

        if (hasAttributes) {
            createAttributeTags(localName, atts);
        }
    }

    private void checkForParentTag() {
        // check if this element has a parent..
        if (!parentTrace.isEmpty()) {
            TagToken parentToken = (TagToken) parentTrace.pop();

            if (parentToken != null) { // apply the bit mask for parent tag.

                byte tokenValue = (byte) (parentToken.getValue() |
                    parentBitMask);
                parentToken.setValue(tokenValue);
            }
        }
    }

    private void createTagToken(String elementName, boolean hasAttributes) {
        // create token for current element
        byte tokenValue = TokenRepository.getInstance().getTagToken(elementName);
        TagToken currentTagToken = new TagToken(elementName, tokenValue);

        // store on tokenArray
        tokens.addElement(currentTagToken);

        // push the current token on stack
        parentTrace.push(currentTagToken);

⌨️ 快捷键说明

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