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

📄 xmlfilternull.java

📁 java实现的可配置的工作流引擎,采用jsp+javabean实现
💻 JAVA
字号:
package com.hongsoft.agile.xmlParser;


import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;


/**
 * 参考了www.jdon.org
 * Filter 用来移除文件中的格式化数据
 */
public class XmlFilterNull extends XMLFilterRefact
{

    ////////////////////////////////////////////////////////////////////
    // Constructors.
    ////////////////////////////////////////////////////////////////////

    /**
     * Create a new filter.
     */
    public XmlFilterNull()
    {
    }

    public XmlFilterNull(XMLReader xmlreader)
    {
        super(xmlreader);
    }

    ////////////////////////////////////////////////////////////////////
    // Public methods.
    ////////////////////////////////////////////////////////////////////

    /**
     * 对filter进行reset
     */
    public void reset ()
    {
        state = SEEN_NOTHING;
        stateStack = new Stack();
        whitespace = new StringBuffer();
    }


    public void startDocument ()
    throws SAXException
    {
        reset();
        super.startDocument();
    }


    public void startElement (String uri, String localName,
                              String qName, Attributes atts)
    throws SAXException
    {
        clearWhitespace();
        stateStack.push(SEEN_ELEMENT);
        state = SEEN_NOTHING;
        super.startElement(uri, localName, qName, atts);
    }


    public void endElement (String uri, String localName, String qName)
    throws SAXException
    {
        if (state == SEEN_ELEMENT) {
            clearWhitespace();
        } else {
            emitWhitespace();
        }
        state = stateStack.pop();
        super.endElement(uri, localName, qName);
    }


    public void characters (char ch[], int start, int length)
    throws SAXException
    {
        if (state != SEEN_DATA) {

            /* Look for non-whitespace. */
            int end = start + length;
            while (end-- > start) {
                if (!isXMLWhitespace(ch[end]))
                    break;
            }

            /*
             * If all the characters are whitespace, save them for later.
             * If we've got some data, emit any saved whitespace and update
             * our state to show we've seen data.
             */
            if (end < start) {
                saveWhitespace(ch, start, length);
            } else {
                state = SEEN_DATA;
                emitWhitespace();
            }
        }

        /* Pass on everything inside a data field. */
        if (state == SEEN_DATA) {
            super.characters(ch, start, length);
        }
    }

     /**
      * Filter an ignorable whitespace event.
      *
      * @param ch The array of characters to write.
      * @param start The starting position in the array.
      * @param length The number of characters to write.
      * @exception com.jivesoftware.sax.SAXException If a filter
      *            further down the chain raises an exception.
      * @see com.jivesoftware.sax.ContentHandler#ignorableWhitespace
      */
    public void ignorableWhitespace (char ch[], int start, int length)
    throws SAXException
    {
        emitWhitespace();
        // ignore
    }

    /**
     * Filter a processing instruction event.
     *
     * @param target The PI target.
     * @param data The PI data.
     * @exception com.jivesoftware.sax.SAXException If a filter
     *            further down the chain raises an exception.
     * @see com.jivesoftware.sax.ContentHandler#processingInstruction
     */
    public void processingInstruction (String target, String data)
    throws SAXException
    {
        emitWhitespace();
        super.processingInstruction(target, data);
    }

    ////////////////////////////////////////////////////////////////////
    // Internal methods.
    ////////////////////////////////////////////////////////////////////

    /**
     * Saves trailing whitespace.
     */
    protected void saveWhitespace (char[] ch, int start, int length) {
        whitespace.append(ch, start, length);
    }

    /**
     * Passes saved whitespace down the filter chain.
     */
    protected void emitWhitespace ()
    throws SAXException
    {
        char[] data = new char[whitespace.length()];
        if (whitespace.length() > 0) {
            whitespace.getChars(0, data.length, data, 0);
            whitespace.setLength(0);
            super.characters(data, 0, data.length);
        }
    }

    /**
     * Discards saved whitespace.
     */
    protected void clearWhitespace () {
        whitespace.setLength(0);
    }

    /**
     * Returns <var>true</var> if character is XML whitespace.
     */
    private boolean isXMLWhitespace (char c)
    {
        return c == ' ' || c == '\t' || c == '\r' || c == '\n';
    }

    ////////////////////////////////////////////////////////////////////
    // Constants.
    ////////////////////////////////////////////////////////////////////

    private static final Object SEEN_NOTHING = new Object();
    private static final Object SEEN_ELEMENT = new Object();
    private static final Object SEEN_DATA = new Object();


    ////////////////////////////////////////////////////////////////////
    // Internal state.
    ////////////////////////////////////////////////////////////////////

    private Object state = SEEN_NOTHING;
    private Stack stateStack = new Stack();

    private StringBuffer whitespace = new StringBuffer();
}

⌨️ 快捷键说明

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