dataunformatfilter.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 172 行

JAVA
172
字号
package cn.js.fan.util;import java.util.Stack;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;class DataUnformatFilter extends XMLFilterBase{                    public DataUnformatFilter()    {    }        public DataUnformatFilter(XMLReader xmlreader)    {        super(xmlreader);    }                    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) {                        int end = start + length;            while (end-- > start) {                if (!isXMLWhitespace(ch[end]))                    break;            }                        if (end < start) {                saveWhitespace(ch, start, length);            } else {                state = SEEN_DATA;                emitWhitespace();            }        }                if (state == SEEN_DATA) {            super.characters(ch, start, length);        }    }         public void ignorableWhitespace (char ch[], int start, int length)    throws SAXException    {        emitWhitespace();            }        public void processingInstruction (String target, String data)    throws SAXException    {        emitWhitespace();        super.processingInstruction(target, data);    }                    protected void saveWhitespace (char[] ch, int start, int length) {        whitespace.append(ch, start, length);    }        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);        }    }        protected void clearWhitespace () {        whitespace.setLength(0);    }        private boolean isXMLWhitespace (char c)    {        return c == ' ' || c == '\t' || c == '\r' || c == '\n';    }                private static final Object SEEN_NOTHING = new Object();    private static final Object SEEN_ELEMENT = new Object();    private static final Object SEEN_DATA = new Object();                private Object state = SEEN_NOTHING;    private Stack stateStack = new Stack();    private StringBuffer whitespace = new StringBuffer();}

⌨️ 快捷键说明

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