Java XML Element Root getRootFaceletElement(Document document)

Here you can find the source of getRootFaceletElement(Document document)

Description

get Root Facelet Element

License

Open Source License

Parameter

Parameter Description
document a parameter

Declaration

public static Element getRootFaceletElement(Document document) 

Method Source Code

//package com.java2s;
/******************************************************************************* 
 * Copyright (c) 2007 Red Hat, Inc./* w  ww. ja v a 2 s . c  o  m*/
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.util.HashSet;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * facelet elements, if there are these elements on a page then other
     * elements are deleted
     */
    static public HashSet<String> componentElements = new HashSet<String>();

    /**
     * 
     * @param document
     * @return
     */
    public static Element getRootFaceletElement(Document document) {
        Element root = document.getDocumentElement();
        Element component = findComponentElement(root);
        return component != null ? component : root;
    }

    /**
     * 
     * @param root
     * @return
     */
    public static Element findComponentElement(Element root) {
        if (root == null) {
            return null;
        }
        NodeList children = root.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                Element trimmedElement = findComponentElement((Element) child);
                if (trimmedElement != null)
                    return trimmedElement;
            }
        }
        if (componentElements.contains(root.getLocalName())) {
            return root;
        }
        return null;
    }
}

Related

  1. getRootElement(String elementName, String namespace, String prefix)
  2. getRootElement(String location)
  3. getRootElementFromString(String payload)
  4. getRootElementName(Class clazz)
  5. getRootElementPosition(String buf, Element root)
  6. getRootFromPom(File pomFile)
  7. getRootFromString(String str)
  8. getRootNode(Document doc, String nodeName)
  9. getRootNode(Document document)