下面列出了org.w3c.dom.traversal.NodeFilter#SHOW_ELEMENT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Given a {@link Node}, return the appropriate constant for whatToShow.
*
* @param node the node
* @return the whatToShow constant for the type of specified node
*/
static int getFlagForNode(final Node node) {
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
return NodeFilter.SHOW_ELEMENT;
case Node.ATTRIBUTE_NODE:
return NodeFilter.SHOW_ATTRIBUTE;
case Node.TEXT_NODE:
return NodeFilter.SHOW_TEXT;
case Node.CDATA_SECTION_NODE:
return NodeFilter.SHOW_CDATA_SECTION;
case Node.ENTITY_REFERENCE_NODE:
return NodeFilter.SHOW_ENTITY_REFERENCE;
case Node.ENTITY_NODE:
return NodeFilter.SHOW_ENTITY;
case Node.PROCESSING_INSTRUCTION_NODE:
return NodeFilter.SHOW_PROCESSING_INSTRUCTION;
case Node.COMMENT_NODE:
return NodeFilter.SHOW_COMMENT;
case Node.DOCUMENT_NODE:
return NodeFilter.SHOW_DOCUMENT;
case Node.DOCUMENT_TYPE_NODE:
return NodeFilter.SHOW_DOCUMENT_TYPE;
case Node.DOCUMENT_FRAGMENT_NODE:
return NodeFilter.SHOW_DOCUMENT_FRAGMENT;
case Node.NOTATION_NODE:
return NodeFilter.SHOW_NOTATION;
default:
return 0;
}
}
/**
* Given a {@link Node}, return the appropriate constant for whatToShow.
*
* @param node the node
* @return the whatToShow constant for the type of specified node
*/
static int getFlagForNode(final Node node) {
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
return NodeFilter.SHOW_ELEMENT;
case Node.ATTRIBUTE_NODE:
return NodeFilter.SHOW_ATTRIBUTE;
case Node.TEXT_NODE:
return NodeFilter.SHOW_TEXT;
case Node.CDATA_SECTION_NODE:
return NodeFilter.SHOW_CDATA_SECTION;
case Node.ENTITY_REFERENCE_NODE:
return NodeFilter.SHOW_ENTITY_REFERENCE;
case Node.ENTITY_NODE:
return NodeFilter.SHOW_ENTITY;
case Node.PROCESSING_INSTRUCTION_NODE:
return NodeFilter.SHOW_PROCESSING_INSTRUCTION;
case Node.COMMENT_NODE:
return NodeFilter.SHOW_COMMENT;
case Node.DOCUMENT_NODE:
return NodeFilter.SHOW_DOCUMENT;
case Node.DOCUMENT_TYPE_NODE:
return NodeFilter.SHOW_DOCUMENT_TYPE;
case Node.DOCUMENT_FRAGMENT_NODE:
return NodeFilter.SHOW_DOCUMENT_FRAGMENT;
case Node.NOTATION_NODE:
return NodeFilter.SHOW_NOTATION;
default:
return 0;
}
}
/**
* 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;
}
@Override
public int getWhatToShow() {
return NodeFilter.SHOW_ELEMENT;
}
/**
* This is the same as {@link CSSFactory#assignDOM(Document, String, URL, MediaSpec, boolean)}
* with the possibility of specifying a custom network processor for obtaining data from URL
* resources.
*
* @param doc
* DOM tree
* @param encoding
* The default encoding used for the referenced style sheets
* @param network
* Custom network processor
* @param base
* Base URL against which all files are searched
* @param media
* Current media specification used for evaluating the media queries
* @param useInheritance
* Whether inheritance will be used to determine values
* @param matchCond
* The match condition to match the against.
* @return Map between DOM element nodes and data structure containing CSS
* information
*/
public static final StyleMap assignDOM(Document doc, String encoding, NetworkProcessor network,
URL base, MediaSpec media, boolean useInheritance, final MatchCondition matchCond) {
SourceData pair = new SourceData(base, network, media);
Traversal<StyleSheet> traversal = new CSSAssignTraversal(doc, encoding,
pair, NodeFilter.SHOW_ELEMENT);
StyleSheet style = (StyleSheet) getRuleFactory().createStyleSheet()
.unlock();
traversal.listTraversal(style);
Analyzer analyzer = new Analyzer(style);
if (matchCond != null) {
analyzer.registerMatchCondition(matchCond);
}
return analyzer.evaluateDOM(doc, media, useInheritance);
}
/**
* Loads all the style sheets used from the specified DOM tree.
* The following style specifications are evaluated:
* <ul>
* <li>The style sheets included using the <code>link</code> and <code>style</code> tags.
* <li>Inline styles specified using the <code>style</code> element attribute.
* <li><strong>Proprietary extension:</strong> Default styles defined using the <code>XDefaultStyle</code>
* element attribute. These styles behave the same way as the inline styles but they have the lowest priority
* (the values are used only when not redefined by any other way)
* </ul>
*
* @param doc
* DOM tree
* @param encoding
* The default encoding used for the referenced style sheets
* @param base
* Base URL against which all files are searched
* @param media
* Selected media for style sheet
* @return the rules of all the style sheets used in the document including the inline styles
*/
public static final StyleSheet getUsedStyles(Document doc, String encoding, URL base, MediaSpec media, NetworkProcessor network)
{
SourceData pair = new SourceData(base, network, media);
Traversal<StyleSheet> traversal = new CSSAssignTraversal(doc, encoding,
pair, NodeFilter.SHOW_ELEMENT);
StyleSheet style = (StyleSheet) getRuleFactory().createStyleSheet().unlock();
traversal.listTraversal(style);
return style;
}