下面列出了怎么用org.w3c.dom.traversal.NodeFilter的API类实例代码及写法,或者点击链接到github查看源代码。
/** Internal function.
* Return the parent Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getParentNode(Node node) {
if (node == null || node == fRoot) return null;
Node newNode = node.getParentNode();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
//if (accept == NodeFilter.SKIP_NODE) // and REJECT too.
{
return getParentNode(newNode);
}
}
/** Internal function.
* Return the parent Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getParentNode(Node node) {
if (node == null || node == fRoot) return null;
Node newNode = node.getParentNode();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
//if (accept == NodeFilter.SKIP_NODE) // and REJECT too.
{
return getParentNode(newNode);
}
}
/** Internal function.
* Return the parent Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getParentNode(Node node) {
if (node == null || node == fRoot) return null;
Node newNode = node.getParentNode();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
//if (accept == NodeFilter.SKIP_NODE) // and REJECT too.
{
return getParentNode(newNode);
}
}
/**
* Create and return a NodeIterator. The NodeIterator is
* added to a list of NodeIterators so that it can be
* removed to free up the DOM Nodes it references.
*
* @param root The root of the iterator.
* @param whatToShow The whatToShow mask.
* @param filter The NodeFilter installed. Null means no filter.
* @param entityReferenceExpansion true to expand the contents of
* EntityReference nodes
* @since WD-DOM-Level-2-19990923
*/
public NodeIterator createNodeIterator(Node root,
int whatToShow,
NodeFilter filter,
boolean entityReferenceExpansion)
{
if (root == null) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
NodeIterator iterator = new NodeIteratorImpl(this,
root,
whatToShow,
filter,
entityReferenceExpansion);
if (iterators == null) {
iterators = new ArrayList<>();
}
iterators.add(iterator);
return iterator;
}
/**
* Create and return a NodeIterator. The NodeIterator is
* added to a list of NodeIterators so that it can be
* removed to free up the DOM Nodes it references.
*
* @param root The root of the iterator.
* @param whatToShow The whatToShow mask.
* @param filter The NodeFilter installed. Null means no filter.
* @param entityReferenceExpansion true to expand the contents of
* EntityReference nodes
* @since WD-DOM-Level-2-19990923
*/
public NodeIterator createNodeIterator(Node root,
int whatToShow,
NodeFilter filter,
boolean entityReferenceExpansion)
{
if (root == null) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
NodeIterator iterator = new NodeIteratorImpl(this,
root,
whatToShow,
filter,
entityReferenceExpansion);
if (iterators == null) {
iterators = new ArrayList<>();
}
iterators.add(iterator);
return iterator;
}
/** Internal function.
* Return the parent Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getParentNode(Node node) {
if (node == null || node == fRoot) return null;
Node newNode = node.getParentNode();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
//if (accept == NodeFilter.SKIP_NODE) // and REJECT too.
{
return getParentNode(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);
}
/**
* Serializes an ProcessingInstruction Node.
*
* @param node The ProcessingInstruction Node to serialize
*/
protected void serializePI(ProcessingInstruction node)
throws SAXException {
ProcessingInstruction pi = node;
String name = pi.getNodeName();
// well-formed=true
if ((fFeatures & WELLFORMED) != 0) {
isPIWellFormed(node);
}
// apply the LSSerializer filter
if (!applyFilter(node, NodeFilter.SHOW_PROCESSING_INSTRUCTION)) {
return;
}
// String data = pi.getData();
if (name.equals("xslt-next-is-raw")) {
fNextIsRaw = true;
} else {
this.fSerializer.processingInstruction(name, pi.getData());
}
}
/**
* Serializes a Comment Node.
*
* @param node The Comment Node to serialize
*/
protected void serializeComment(Comment node) throws SAXException {
// comments=true
if ((fFeatures & COMMENTS) != 0) {
String data = node.getData();
// well-formed=true
if ((fFeatures & WELLFORMED) != 0) {
isCommentWellFormed(data);
}
if (fLexicalHandler != null) {
// apply the LSSerializer filter after the operations requested by the
// DOMConfiguration parameters have been applied
if (!applyFilter(node, NodeFilter.SHOW_COMMENT)) {
return;
}
fLexicalHandler.comment(data.toCharArray(), 0, data.length());
}
}
}
/** Internal function.
* Return the parent Node, from the input node
* after applying filter, whatToshow.
* The current node is not consulted or set.
*/
Node getParentNode(Node node) {
if (node == null || node == fRoot) return null;
Node newNode = node.getParentNode();
if (newNode == null) return null;
int accept = acceptNode(newNode);
if (accept == NodeFilter.FILTER_ACCEPT)
return newNode;
else
//if (accept == NodeFilter.SKIP_NODE) // and REJECT too.
{
return getParentNode(newNode);
}
}
/** Public constructor */
public NodeIteratorImpl( DocumentImpl document,
Node root,
int whatToShow,
NodeFilter nodeFilter,
boolean entityReferenceExpansion) {
fDocument = document;
fRoot = root;
fCurrentNode = null;
fWhatToShow = whatToShow;
fNodeFilter = nodeFilter;
fEntityReferenceExpansion = entityReferenceExpansion;
}
/** The node is accepted if it passes the whatToShow and the filter. */
boolean acceptNode(Node node) {
if (fNodeFilter == null) {
return ( fWhatToShow & (1 << node.getNodeType()-1)) != 0 ;
} else {
return ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 )
&& fNodeFilter.acceptNode(node) == NodeFilter.FILTER_ACCEPT;
}
}
/** The node is accepted if it passes the whatToShow and the filter. */
boolean acceptNode(Node node) {
if (fNodeFilter == null) {
return ( fWhatToShow & (1 << node.getNodeType()-1)) != 0 ;
} else {
return ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 )
&& fNodeFilter.acceptNode(node) == NodeFilter.FILTER_ACCEPT;
}
}
/** 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;
}
}
}
/**
* Returns whether the node is visible by the TreeWalker.
*/
private boolean isNodeVisible(final Node n) {
if (acceptNode(n) == NodeFilter.FILTER_ACCEPT) {
if (filter_ == null || filter_.acceptNode(n) == NodeFilter.FILTER_ACCEPT) {
return expandEntityReferences_ || n.getParentNode() == null
|| n.getParentNode().getNodeType() != Node.ENTITY_REFERENCE_NODE;
}
}
return false;
}
/** 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);
}
}
/**
* 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 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.
* 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;
}
}
}
/**
* Creates map of declarations assigned to each element of a DOM tree
*
* @param doc
* DOM document
* @param media
* Media type to be used for declarations
* @param inherit
* Inheritance (cascade propagation of values)
* @return Map of elements as keys and their declarations
*/
protected DeclarationMap assingDeclarationsToDOM(Document doc, MediaSpec media, final boolean inherit) {
// classify the rules
classifyAllSheets(media);
// resulting map
DeclarationMap declarations = new DeclarationMap();
// if the holder is empty skip evaluation
if(rules!=null && !rules.isEmpty()) {
Traversal<DeclarationMap> traversal = new Traversal<DeclarationMap>(
doc, (Object) rules, NodeFilter.SHOW_ELEMENT) {
protected void processNode(DeclarationMap result,
Node current, Object source) {
assignDeclarationsToElement(result, walker, (Element) current,
(Holder) source);
}
};
// list traversal will be enough
if (!inherit)
traversal.listTraversal(declarations);
// we will do level traversal to economize blind returning
// in tree
else
traversal.levelTraversal(declarations);
}
return declarations;
}
/** 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.
* 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.
* 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);
}
}
/** Public constructor */
public NodeIteratorImpl( DocumentImpl document,
Node root,
int whatToShow,
NodeFilter nodeFilter,
boolean entityReferenceExpansion) {
fDocument = document;
fRoot = root;
fCurrentNode = null;
fWhatToShow = whatToShow;
fNodeFilter = nodeFilter;
fEntityReferenceExpansion = entityReferenceExpansion;
}
/** 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);
}
}
/**
* {@inheritDoc}
*/
public short accept(Element element) {
if (DomUtils.nodeNameEquals(element, "beans")) {
element.appendChild(element.getOwnerDocument().createTextNode("\n ")); //TODO make indentation configurable
element.appendChild(element.getOwnerDocument().importNode(beanDefinition, true));
}
return NodeFilter.FILTER_ACCEPT;
}
/** Public constructor */
public TreeWalkerImpl(Node root,
int whatToShow,
NodeFilter nodeFilter,
boolean entityReferenceExpansion) {
fCurrentNode = root;
fRoot = root;
fWhatToShow = whatToShow;
fNodeFilter = nodeFilter;
fEntityReferenceExpansion = entityReferenceExpansion;
}
/**
* Transfers the properties document to a map.
*
* @param propertiesDocument to be transfered to a map.
* @return transfered map.
*/
public static HashMap<String, String> docToMap(final Document propertiesDocument, final boolean allowEmptyEntries) {
final HashMap<String, String> reponseMap = new HashMap<>();
final DocumentTraversal traversal = (DocumentTraversal) propertiesDocument;
final NodeIterator iterator =
traversal.createNodeIterator(propertiesDocument.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
for (Node node = iterator.nextNode(); node != null; node = iterator.nextNode()) {
final String name = ((Element) node).getLocalName();
final StringBuilder content = new StringBuilder();
final NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
final Node child = children.item(i);
if (child.getNodeType() == Node.TEXT_NODE) {
content.append(child.getTextContent());
}
}
if (allowEmptyEntries) {
reponseMap.put(name, content.toString());
} else {
if (!content.toString().trim().isEmpty()) {
reponseMap.put(name, content.toString());
}
}
}
return reponseMap;
}
/** Public constructor */
public TreeWalkerImpl(Node root,
int whatToShow,
NodeFilter nodeFilter,
boolean entityReferenceExpansion) {
fCurrentNode = root;
fRoot = root;
fWhatToShow = whatToShow;
fNodeFilter = nodeFilter;
fEntityReferenceExpansion = entityReferenceExpansion;
}