|
|
|
|
|
|
|
这两个java类的作用是什么?
|
2007年06月02日 03:31
|
|
|
|
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.AttributesImpl; import org.xml.sax.helpers.XMLFilterImpl; /*
派生 XML 过滤器的基类。
此模块(包括源代码和文档)位于公共域中,对该模块不提供担保。 有关更多信息,请参阅http://www.saxproject.org。。 此类设计为位于 XMLReader 和客户端应用程序的事件处理程序之间。 默认情况下,除了将请求传递给阅读器和将事件传递给未修改的处理程序外, ]不执行其他操作。 但是,子类可以重写特定方法在它们传递时修改事件流或配置请求。
*/ class XMLFilterBase extends XMLFilterImpl {
public XMLFilterBase() { }
public XMLFilterBase(XMLReader parent) { super(parent); } /* startElement void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException接收元素开始的通知。 解析器在 XML 文档中的每个元素的开始调用此方法;对于每个 startElement 事件都将有相应的 endElement 事件(即使该元素为空时)。所有元素的内容都将在相应的 endElement 事件之前顺序地报告。
此事件允许每个元素最多有以下三个名称组件:
名称空间 URI; 本地名称;和 限定(前缀)名。 可以提供它们中的部分或全部,具体如何取决于 http://xml.org/sax/features/namespaces 和 http://xml.org/sax/features/namespace-prefixes 属性的值:
当名称空间属性为 true(默认)时,名称空间 URI 和本地名称是必需项,当名称空间属性为 false 时,则为可选项(如果指定一个值,两个都必须指定); 当名称空间前缀属性为 true 时,限定名是必需项,当名称空间前缀属性为 false(默认值)时,则为可选项。 注意,所提供的属性列表仅包括具有显式值(指定的或默认的)的属性:将忽略 #IMPLIED 属性。仅在 http://xml.org/sax/features/namespace-prefixes 属性为 true (默认情况下为 false,并且对 true 值的支持是可选项)时属性列表才包括用于名称空间声明(xmlns* 属性)的属性。
与 characters() 一样,属性值可以具有需要不止一个 char 值的字符。
参数: uri - 名称空间 URI,如果元素没有名称空间 URI,或者未执行名称空间处理,则为空字符串 localName - 本地名称(不带前缀),如果未执行名称空间处理,则为空字符串 qName - 限定名(带有前缀),如果限定名不可用,则为空字符串 atts - 连接到元素上的属性。如果没有属性,则它将是空 Attributes 对象。在 startElement 返回后,此对象的值是未定义的 抛出: SAXException - 任何 SAX 异常,可能包装另外的异常 另请参见: endElement(java.lang.String, java.lang.String, java.lang.String), Attributes, AttributesImpl
*/ //接收元素开始的通知 public void startElement (String uri, String localName) throws SAXException { startElement(uri, localName, "", EMPTY_ATTS); }
public void startElement (String localName) throws SAXException { startElement("", localName, "", EMPTY_ATTS); }
//接收元素结束的通知 public void endElement (String uri, String localName) throws SAXException { endElement(uri, localName, ""); }
public void endElement (String localName) throws SAXException { endElement("", localName, ""); }
public void emptyElement (String uri, String localName, String qName, Attributes atts) throws SAXException { startElement(uri, localName, qName, atts); endElement(uri, localName, qName); }
public void emptyElement (String uri, String localName) throws SAXException { emptyElement(uri, localName, "", EMPTY_ATTS); }
public void emptyElement (String localName) throws SAXException { emptyElement("", localName, "", EMPTY_ATTS); }
public void dataElement (String uri, String localName, String qName, Attributes atts, String content) throws SAXException { startElement(uri, localName, qName, atts); characters(content); endElement(uri, localName, qName); }
public void dataElement (String uri, String localName, String content) throws SAXException { dataElement(uri, localName, "", EMPTY_ATTS, content); }
public void dataElement (String localName, String content) throws SAXException { dataElement("", localName, "", EMPTY_ATTS, content); }
public void characters (String data) throws SAXException { char ch[] = data.toCharArray(); characters(ch, 0, ch.length); }
//关于 XML 属性列表的接口 protected static final Attributes EMPTY_ATTS = new AttributesImpl(); }
|
|
|
|
|
|
re:这两个java类的作用是什么?
|
2007年06月02日 03:31
|
|
|
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(); } 我自己看了j2se和 jdom的帮助文件,可由于水平有限,没弄明白,请高手指点一二 [该贴被linxuewei于2007年06月02日 03:40修改过]
|
|
|
|