Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package net.sourceforge.jsdialect.util;
  
  
  import java.util.*;
  
  import static net.sourceforge.jsdialect.util.JsDialectUtil.empty;

Some utility methods for managing documents and Javascript.
 
 public class DomUtils {
 
     private DomUtils() {
     }

    
Get the unique element with the provided id.

Returns:
null if element is not found.
Throws:
java.lang.IllegalStateException if more than one element are found.
 
     public static Element getElementById(Document documentString elementNameString id) {
         DOMSelector selector = new DOMSelector("//" + elementName + "[@id=\"" + id + "\"]");
         List<Nodenodes = selector.select(document.getChildren());
         if (empty(nodes)) {
             return null;
         } else if (nodes.size() > 1) {
             throw new IllegalStateException("More than one element with id -" + id + "- found");
         } else {
             return (Elementnodes.get(0);
         }
     }

    
Get the first element with the provided id. This method has worse performance than getElementById(document, elementName, id) method.

Returns:
null if element is not found.
 
     public static Element getFirstElementById(NestableNode parentString id) {
         if (id == null) {
             return null;
         }
         for (Element child : parent.getElementChildren()) {
             if (id.equals(child.getAttributeValue("id"))) {
                 return child;
             } else if (child.hasChildren()) {
                 Element result = getFirstElementById(childid);
                 if (result != null) {
                     return result;
                 }
             }
         }
         return null;
     }

    
Get the list of elements with the provided name.
 
     public static List<ElementgetElementsByTagName(NestableNode elementString tagName) {
         DOMSelector selector = new DOMSelector("//" + tagName);
         return (List<Element>) (Listselector.select(element.getChildren());
     }

    
Given an element, return all descendants with provided tag names, in order of appearance.
 
     public static List<ElementgetElementsByTagNames(NestableNode parentString ... tagNames) {
         List<Elementresult = new ArrayList<Element>();
         List<Elementchildren = parent.getElementChildren();
         if (!children.isEmpty()) {
             List<StringtagNamesList = Arrays.asList(tagNames);
             for (Element child : children) {
                 if (JsDialectUtil.startsWith(child.getOriginalName(), tagNamesList)) {
                     result.add(child);
                 }
                 result.addAll(getElementsByTagNames(childtagNames));
             }
         }
         return result;
     }
 
     // FIXME: unit test this
     
Get id from provided element, or in absence create it.

Parameters:
document Full document
element Element to get or create id
idPrefix In case of creating id, prefix for id
Returns:
element id (existent or created)
 
     public static String getOrCreateId(Document documentElement elementString idPrefix) {
         if (element.hasAttribute("id")) {
             return element.getAttributeValue("id");
         } else {
             String newId = idPrefix;
             int id = 0;
             // Search for an unique id
             do {
                 id++;
                 newId = idPrefix + id;
            } while (DomUtils.getFirstElementById(documentnewId) != null);
            element.setAttribute("id"newId);
            return newId;
        }
    }


    
Get all CSS classes from a Node.
    // FIXME: unit test this
    public static Set<StringgetClassNames(Element element) {
        Set<StringclassNames = new HashSet<String>();
        if (element.hasAttribute("class")) {
            StringTokenizer st = new StringTokenizer(element.getAttributeValue("class"), " ");
            while (st.hasMoreTokens()) {
                classNames.add(st.nextToken());
            }
        }
        return classNames;
    }

    
Add an attribute value to an element. If attribute value exists, add new value separated by an space.
    // FIXME: unit test this
    public static void addAttributeValue(Element elementString attributeNameString newValue) {
        String attributeValue = newValue;
        if (element.hasAttribute(attributeName)) {
            attributeValue += " " + element.getAttributeValue(attributeName);
        }
        element.setAttribute(attributeNameattributeValue);
    }
    public static void removeElement(Element element) {
        element.clearChildren();
        element.getParent().removeChild(element);
    }
    public static Element getDocumentHead(Document document) {
        return document.getFirstElementChild().getFirstElementChild();
    }
New to GrepCode? Check out our FAQ X