org.jdom.contrib.helpers
Class JDOMHelper

java.lang.Object
  extended by org.jdom.contrib.helpers.JDOMHelper

public class JDOMHelper
extends java.lang.Object

This class contains static helper methods.

Author:
Alex Rosen

Constructor Summary
JDOMHelper()
           
 
Method Summary
static void sortElements(org.jdom.Element parent, java.util.Comparator c)
           Sorts the child elements, using the specified comparator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JDOMHelper

public JDOMHelper()
Method Detail

sortElements

public static void sortElements(org.jdom.Element parent,
                                java.util.Comparator c)

Sorts the child elements, using the specified comparator.

Parameters:
parent - The parent Element, whose child Elements should be sorted.
c - The Comparator to use for ordering the child Elements. It will only be given Element objects to compare.

This method overcomes two problems with the standard Collections.sort():

  • Collections.sort() doesn't bother to remove an item from its old location before placing it in its new location, which causes JDOM to complain that the item has been added twice.
  • This method will sort the child Elements without moving any other content, such as formatting text nodes (newlines, indents, etc.) Otherwise, all the formatting whitespace would move to the beginning or end of the content list. (Note that this means that the elements will now be in a different order with respect to any comments, which may cause a problem if the comments describe the elements.)



Copyright © 2007 Jason Hunter, Brett McLaughlin. All Rights Reserved.