下面列出了org.w3c.dom.traversal.NodeFilter#FILTER_SKIP 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Internal function. The node whatToShow and the filter are combined into
* one result.
*/
private short acceptNode(Node node) {
/***
* 7.1.2.4. Filters and whatToShow flags
*
* Iterator and TreeWalker apply whatToShow flags before applying
* Filters. If a node is rejected by the active whatToShow flags, a
* Filter will not be called to evaluate that node. When a node is
* rejected by the active whatToShow flags, children of that node will
* still be considered, and Filters may be called to evaluate them.
***/
if ((whatToShow & (1 << node.getNodeType() - 1)) != 0)
return NodeFilter.FILTER_ACCEPT;
else
return NodeFilter.FILTER_SKIP;
}
/**
* Internal function. Return the last child Node, from the input node after
* applying filter, whatToshow. The current node is not consulted or set.
*/
private Node getLastChild(Node node) {
if (node == null)
return null;
Node newNode = node.getLastChild();
if (newNode == null)
return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else if (accept == NodeFilter.FILTER_SKIP && newNode.hasChildNodes())
return getLastChild(newNode);
// if (accept == NodeFilter.REJECT_NODE)
return getPreviousSibling(newNode);
}
/**
* Internal function. Return the first child Node, from the input node after
* applying filter, whatToshow. The current node is not consulted or set.
*/
private Node getFirstChild(Node node) {
if (node == null)
return null;
Node newNode = node.getFirstChild();
if (newNode == null)
return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else if (accept == NodeFilter.FILTER_SKIP && newNode.hasChildNodes())
return getFirstChild(newNode);
// if (accept == NodeFilter.REJECT_NODE)
return getNextSibling(newNode);
}
/** Internal function.
* Return the previousSibling Node, from the input node
* after applying filter, whatToshow.
* NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.
* The current node is not consulted or set.
*/
Node getPreviousSibling(Node node, Node root) {
if (node == null || node == root) return null;
Node newNode = node.getPreviousSibling();
if (newNode == null) {
newNode = node.getParentNode();
if (newNode == null || newNode == root) return null;
int parentAccept = acceptNode(newNode);
if (parentAccept==NodeFilter.FILTER_SKIP) {
return getPreviousSibling(newNode, root);
}
return null;
}
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP) {
Node fChild = getLastChild(newNode);
if (fChild == null) {
return getPreviousSibling(newNode, root);
}
return fChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getPreviousSibling(newNode, root);
}
}
/**
* Test whether a specified node is visible in the logical view of a
* TreeWalker, based solely on the whatToShow constant.
*
* @param n The node to check to see if it should be shown or not
* @return a constant to determine whether the node is accepted, rejected,
* or skipped.
*/
private short acceptNode(final Node n) {
final int flag = getFlagForNode(n);
if ((whatToShow_ & flag) != 0) {
return NodeFilter.FILTER_ACCEPT;
}
// Skip, don't reject.
return NodeFilter.FILTER_SKIP;
}
/** Internal function.
* The node whatToShow and the filter are combined into one result. */
short acceptNode(Node node) {
/***
7.1.2.4. Filters and whatToShow flags
Iterator and TreeWalker apply whatToShow flags before applying Filters. If a node is rejected by the
active whatToShow flags, a Filter will not be called to evaluate that node. When a node is rejected by
the active whatToShow flags, children of that node will still be considered, and Filters may be called to
evaluate them.
***/
if (fNodeFilter == null) {
if ( ( fWhatToShow & (1 << node.getNodeType()-1)) != 0) {
return NodeFilter.FILTER_ACCEPT;
} else {
return NodeFilter.FILTER_SKIP;
}
} else {
if ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) {
return fNodeFilter.acceptNode(node);
} else {
// What to show has failed. See above excerpt from spec.
// Equivalent to FILTER_SKIP.
return NodeFilter.FILTER_SKIP;
}
}
}
/** Internal function.
* Return the first child Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getFirstChild(Node node) {
if (node == null) return null;
if ( !fEntityReferenceExpansion
&& node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
return null;
Node newNode = node.getFirstChild();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP
&& newNode.hasChildNodes())
{
Node fChild = getFirstChild(newNode);
if (fChild == null) {
return getNextSibling(newNode, node);
}
return fChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getNextSibling(newNode, node);
}
}
/** Internal function.
* The node whatToShow and the filter are combined into one result. */
short acceptNode(Node node) {
/***
7.1.2.4. Filters and whatToShow flags
Iterator and TreeWalker apply whatToShow flags before applying Filters. If a node is rejected by the
active whatToShow flags, a Filter will not be called to evaluate that node. When a node is rejected by
the active whatToShow flags, children of that node will still be considered, and Filters may be called to
evaluate them.
***/
if (fNodeFilter == null) {
if ( ( fWhatToShow & (1 << node.getNodeType()-1)) != 0) {
return NodeFilter.FILTER_ACCEPT;
} else {
return NodeFilter.FILTER_SKIP;
}
} else {
if ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) {
return fNodeFilter.acceptNode(node);
} else {
// What to show has failed. See above excerpt from spec.
// Equivalent to FILTER_SKIP.
return NodeFilter.FILTER_SKIP;
}
}
}
/**
* Prints attribute.
* NOTE: xml:space attribute modifies output format
*
* @param name
* @param value
* @param isSpecified
* @exception IOException
*/
private void printAttribute (String name, String value, boolean isSpecified, Attr attr) throws IOException{
if (isSpecified || (features & DOMSerializerImpl.DISCARDDEFAULT) == 0) {
if (fDOMFilter !=null &&
(fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ATTRIBUTE)!= 0) {
short code = fDOMFilter.acceptNode(attr);
switch (code) {
case NodeFilter.FILTER_REJECT:
case NodeFilter.FILTER_SKIP: {
return;
}
default: {
// fall through
}
}
}
_printer.printSpace();
_printer.printText( name );
_printer.printText( "=\"" );
printEscaped( value );
_printer.printText( '"' );
}
// If the attribute xml:space exists, determine whether
// to preserve spaces in this and child nodes based on
// its value.
if (name.equals( "xml:space" )) {
if (value.equals( "preserve" ))
fPreserveSpace = true;
else
fPreserveSpace = _format.getPreserveSpace();
}
}
/** Internal function.
* Return the last child Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getLastChild(Node node) {
if (node == null) return null;
if ( !fEntityReferenceExpansion
&& node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
return null;
Node newNode = node.getLastChild();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP
&& newNode.hasChildNodes())
{
Node lChild = getLastChild(newNode);
if (lChild == null) {
return getPreviousSibling(newNode, node);
}
return lChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getPreviousSibling(newNode, node);
}
}
/** Internal function.
* Return the last child Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getLastChild(Node node) {
if (node == null) return null;
if ( !fEntityReferenceExpansion
&& node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
return null;
Node newNode = node.getLastChild();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP
&& newNode.hasChildNodes())
{
Node lChild = getLastChild(newNode);
if (lChild == null) {
return getPreviousSibling(newNode, node);
}
return lChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getPreviousSibling(newNode, node);
}
}
/** Internal function.
* Return the first child Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getFirstChild(Node node) {
if (node == null) return null;
if ( !fEntityReferenceExpansion
&& node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
return null;
Node newNode = node.getFirstChild();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP
&& newNode.hasChildNodes())
{
Node fChild = getFirstChild(newNode);
if (fChild == null) {
return getNextSibling(newNode, node);
}
return fChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getNextSibling(newNode, node);
}
}
/**
* Prints attribute.
* NOTE: xml:space attribute modifies output format
*
* @param name
* @param value
* @param isSpecified
* @exception IOException
*/
private void printAttribute (String name, String value, boolean isSpecified, Attr attr) throws IOException{
if (isSpecified || (features & DOMSerializerImpl.DISCARDDEFAULT) == 0) {
if (fDOMFilter !=null &&
(fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ATTRIBUTE)!= 0) {
short code = fDOMFilter.acceptNode(attr);
switch (code) {
case NodeFilter.FILTER_REJECT:
case NodeFilter.FILTER_SKIP: {
return;
}
default: {
// fall through
}
}
}
_printer.printSpace();
_printer.printText( name );
_printer.printText( "=\"" );
printEscaped( value );
_printer.printText( '"' );
}
// If the attribute xml:space exists, determine whether
// to preserve spaces in this and child nodes based on
// its value.
if (name.equals( "xml:space" )) {
if (value.equals( "preserve" ))
fPreserveSpace = true;
else
fPreserveSpace = _format.getPreserveSpace();
}
}
/** Internal function.
* Return the nextSibling Node, from the input node
* after applying filter, whatToshow.
* NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.
* The current node is not consulted or set.
*/
Node getNextSibling(Node node, Node root) {
if (node == null || node == root) return null;
Node newNode = node.getNextSibling();
if (newNode == null) {
newNode = node.getParentNode();
if (newNode == null || newNode == root) return null;
int parentAccept = acceptNode(newNode);
if (parentAccept==NodeFilter.FILTER_SKIP) {
return getNextSibling(newNode, root);
}
return null;
}
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP) {
Node fChild = getFirstChild(newNode);
if (fChild == null) {
return getNextSibling(newNode, root);
}
return fChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getNextSibling(newNode, root);
}
}
/** Internal function.
* Return the previousSibling Node, from the input node
* after applying filter, whatToshow.
* NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.
* The current node is not consulted or set.
*/
Node getPreviousSibling(Node node, Node root) {
if (node == null || node == root) return null;
Node newNode = node.getPreviousSibling();
if (newNode == null) {
newNode = node.getParentNode();
if (newNode == null || newNode == root) return null;
int parentAccept = acceptNode(newNode);
if (parentAccept==NodeFilter.FILTER_SKIP) {
return getPreviousSibling(newNode, root);
}
return null;
}
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP) {
Node fChild = getLastChild(newNode);
if (fChild == null) {
return getPreviousSibling(newNode, root);
}
return fChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getPreviousSibling(newNode, root);
}
}
/** Internal function.
* The node whatToShow and the filter are combined into one result. */
short acceptNode(Node node) {
/***
7.1.2.4. Filters and whatToShow flags
Iterator and TreeWalker apply whatToShow flags before applying Filters. If a node is rejected by the
active whatToShow flags, a Filter will not be called to evaluate that node. When a node is rejected by
the active whatToShow flags, children of that node will still be considered, and Filters may be called to
evaluate them.
***/
if (fNodeFilter == null) {
if ( ( fWhatToShow & (1 << node.getNodeType()-1)) != 0) {
return NodeFilter.FILTER_ACCEPT;
} else {
return NodeFilter.FILTER_SKIP;
}
} else {
if ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) {
return fNodeFilter.acceptNode(node);
} else {
// What to show has failed. See above excerpt from spec.
// Equivalent to FILTER_SKIP.
return NodeFilter.FILTER_SKIP;
}
}
}
/**
* Prints attribute.
* NOTE: xml:space attribute modifies output format
*
* @param name
* @param value
* @param isSpecified
* @exception IOException
*/
private void printAttribute (String name, String value, boolean isSpecified, Attr attr) throws IOException{
if (isSpecified || (features & DOMSerializerImpl.DISCARDDEFAULT) == 0) {
if (fDOMFilter !=null &&
(fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ATTRIBUTE)!= 0) {
short code = fDOMFilter.acceptNode(attr);
switch (code) {
case NodeFilter.FILTER_REJECT:
case NodeFilter.FILTER_SKIP: {
return;
}
default: {
// fall through
}
}
}
_printer.printSpace();
_printer.printText( name );
_printer.printText( "=\"" );
printEscaped( value );
_printer.printText( '"' );
}
// If the attribute xml:space exists, determine whether
// to preserve spaces in this and child nodes based on
// its value.
if (name.equals( "xml:space" )) {
if (value.equals( "preserve" ))
fPreserveSpace = true;
else
fPreserveSpace = _format.getPreserveSpace();
}
}
/** Internal function.
* Return the last child Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getLastChild(Node node) {
if (node == null) return null;
if ( !fEntityReferenceExpansion
&& node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
return null;
Node newNode = node.getLastChild();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP
&& newNode.hasChildNodes())
{
Node lChild = getLastChild(newNode);
if (lChild == null) {
return getPreviousSibling(newNode, node);
}
return lChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getPreviousSibling(newNode, node);
}
}
/** Internal function.
* Return the nextSibling Node, from the input node
* after applying filter, whatToshow.
* NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.
* The current node is not consulted or set.
*/
Node getNextSibling(Node node, Node root) {
if (node == null || node == root) return null;
Node newNode = node.getNextSibling();
if (newNode == null) {
newNode = node.getParentNode();
if (newNode == null || newNode == root) return null;
int parentAccept = acceptNode(newNode);
if (parentAccept==NodeFilter.FILTER_SKIP) {
return getNextSibling(newNode, root);
}
return null;
}
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP) {
Node fChild = getFirstChild(newNode);
if (fChild == null) {
return getNextSibling(newNode, root);
}
return fChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getNextSibling(newNode, root);
}
}
/** Internal function.
* Return the previousSibling Node, from the input node
* after applying filter, whatToshow.
* NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.
* The current node is not consulted or set.
*/
Node getPreviousSibling(Node node, Node root) {
if (node == null || node == root) return null;
Node newNode = node.getPreviousSibling();
if (newNode == null) {
newNode = node.getParentNode();
if (newNode == null || newNode == root) return null;
int parentAccept = acceptNode(newNode);
if (parentAccept==NodeFilter.FILTER_SKIP) {
return getPreviousSibling(newNode, root);
}
return null;
}
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
if (accept == NodeFilter.FILTER_SKIP) {
Node fChild = getLastChild(newNode);
if (fChild == null) {
return getPreviousSibling(newNode, root);
}
return fChild;
}
else
//if (accept == NodeFilter.REJECT_NODE)
{
return getPreviousSibling(newNode, root);
}
}