org.w3c.dom.traversal.DocumentTraversal Java Examples

The following examples show how to use org.w3c.dom.traversal.DocumentTraversal. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ManagementBusInvocationPluginRest.java    From container with Apache License 2.0 6 votes vote down vote up
/**
 * Transfers the document to a map.
 *
 * @param doc to be transfered to a map.
 * @return transfered map.
 */
private Map<String, String> docToMap(final Document doc) {
    final Map<String, String> map = new HashMap<>();

    final DocumentTraversal traversal = (DocumentTraversal) doc;
    final NodeIterator iterator = traversal.createNodeIterator(doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);

    for (Node node = iterator.nextNode(); node != null; node = iterator.nextNode()) {
        final String name = ((Element) node).getTagName();
        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 (!content.toString().trim().isEmpty()) {
            map.put(name, content.toString());
        }
    }
    return map;
}
 
Example #2
Source File: AnalyzerTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@BeforeClass
public static void init() throws IOException, CSSException, SAXException {
	log.info("\n\n\n == AnalyzerTest test at {} == \n\n\n", new Date());

       DOMSource ds = new DOMSource(AnalyzerTest.class.getResourceAsStream("/simple/data.html"));
       doc = ds.parse();
       
	sheet = CSSFactory.parse(AnalyzerTest.class.getResource("/simple/data.css"), null);

	analyzer = new Analyzer(sheet);

	NodeList list = doc.getElementsByTagName("body");
	assertEquals("There is one <body> element", 1, list.getLength());

	//walker = new TidyTreeWalker(list.item(0), NodeFilter.SHOW_ELEMENT);
	DocumentTraversal traversal = (DocumentTraversal) doc;
	walker = traversal.createTreeWalker(list.item(0), NodeFilter.SHOW_ELEMENT, null, false);
	elements = new ElementMap(doc);
}
 
Example #3
Source File: MBUtils.java    From container with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: NodeTest.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Try to cast a Document into a DocumentTraversal
 * @param document
 * @return DocumentTraversal interface if the DOM implementation supports it
 */
private static DocumentTraversal getDocumentTraversal(Document document) {
    try {
        return (DocumentTraversal) document;
    } catch (ClassCastException e) {
        throw new IllegalArgumentException("DOM Traversal not supported by "
                                           + document.getImplementation().getClass().getName()
                                           + ". To use this class you will need to switch to a DOM implementation that supports Traversal.");
    }
}
 
Example #5
Source File: Traversal.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Traversal(Document doc, Object source, int whatToShow) {
    if (doc instanceof DocumentTraversal) {
        DocumentTraversal dt = (DocumentTraversal) doc;
        this.walker = dt.createTreeWalker(doc.getDocumentElement(), whatToShow, null, false);
    } else {
        this.walker = new GenericTreeWalker(doc.getDocumentElement(), whatToShow);
    }
    this.source = source;
}
 
Example #6
Source File: ElementMap.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ElementMap(Document doc) {
	elementIDs = new HashMap<String, Element>();
	elementNames = new HashMap<String, Element>();
       DocumentTraversal traversal = (DocumentTraversal) doc;
       TreeWalker w = traversal.createTreeWalker(doc, NodeFilter.SHOW_ELEMENT, null, false);
	//TreeWalker w = new TidyTreeWalker(doc, NodeFilter.SHOW_ELEMENT);
	Element current;
	while ((current = (Element) w.nextNode()) != null) {
		elementNames.put(current.getNodeName().toLowerCase(), current);
		String id = current.getAttribute("id");
		if(id!=null)
			elementIDs.put(id, current);
	}
}
 
Example #7
Source File: NodeTest.java    From xmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a NodeTest using the specified DocumentTraversal, starting at
 * the specified root node
 */
public NodeTest(DocumentTraversal documentTraversal, Node rootNode) {
    this.documentTraversal = documentTraversal;
    this.rootNode = rootNode;
}
 
Example #8
Source File: UtilsClustering.java    From apogen with Apache License 2.0 3 votes vote down vote up
private static LblTree getDomTree(String dom1) throws IOException {

		org.w3c.dom.Document doc1 = DomUtils.asDocument(dom1);

		LblTree domTree = null;

		DocumentTraversal traversal = (DocumentTraversal) doc1;
		TreeWalker walker = traversal.createTreeWalker(doc1.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
		domTree = createTree(walker);

		return domTree;
	}