package contrib; import org.jdom.Element; import org.jdom.Namespace; import org.jdom.Attribute; import org.xml.sax.SAXException; import javax.swing.tree.DefaultMutableTreeNode; import java.util.List; import java.util.Iterator; /** * @author Phill Perryman * * Extends the Element Class from JDOM to provide functionality * which is not appropriate for the core library. * * The class provides several convenience methods for handling * attribute data as well as methods for manipulating the elemment itself. */ public class Elementx extends Element implements Comparable { // attribute for convenience methods Attribute a = null; // descriptor private static ElementDescriptor descriptor = null; private static ElementDescriptor oldDescriptor = null; /** * Constructor for ExtendedElement. */ public Elementx() { super(); } /** * Constructor for ExtendedElement. * @param arg0 * @param arg1 */ public Elementx(String arg0, Namespace arg1) { super(arg0, arg1); } /** * Constructor for ExtendedElement. * @param arg0 */ public Elementx(String arg0) { super(arg0); } /** * Constructor for ExtendedElement. * @param arg0 * @param arg1 */ public Elementx(String arg0, String arg1) { super(arg0, arg1); } /** * Constructor for ExtendedElement. * @param arg0 * @param arg1 * @param arg2 */ public Elementx(String arg0, String arg1, String arg2) { super(arg0, arg1, arg2); } /** * Allows the element to be put into a sorted map etc. * * By using the toString method the descriptor can * be used to control the sorting by attribute * */ public int compareTo(Object comp) { Elementx e = (Elementx) comp; return this.toString().compareTo(e.toString()); } /** * Allows the toString descriptor to be overidden. * * Setting the parameter to null will revert to the super * toString method. Note the descriptor is static and so * only one can be in force for all Elements. * * If the descriptions should be different for diferent * elements then the element descriptor should test the * names of the passed element and respond accordingly. * * @param the new descriptor * @return the old one or null if none. */ public static ElementDescriptor setDescriptor(ElementDescriptor newDescriptor) { oldDescriptor = descriptor; descriptor = newDescriptor; return oldDescriptor; } /** * Allows subclasses to perform validation. * * This method is called by the SAXHandlerx when * the element has finished parsing. It allows for context * sensitive validation as opposed to the syntactical one * the parser carries out. * * It can also be used where information about elements * can usefully be collected while the document is being * parsed. * * The subclass would overide this method to provide the * functionality. */ public void validate() throws SAXException { } // convenience methods /** * Converts the attribute value to an int * * @param name of attribute */ public int getInt(String attributeName) { a = getAttribute(attributeName); if (a != null) { return Integer.parseInt(a.getValue()); } else { return 0; } } /** * Converts the attribute value to an int * * @param name of attribute * @param default value if attribute does not exist * @return the int value of the attribute or the default value * if the attribute does not exist */ public int getInt(String attributeName, int defaultValue) { a = getAttribute(attributeName); if (a != null) { return Integer.parseInt(a.getValue()); } else { return defaultValue; } } /** * Stores the int value into an attribute * * @param name of attribute * @param value of attribute */ public void setInt(String attributeName, int value) { this.setAttribute(attributeName, value + ""); } /** * Stores the int value into an attribute unless the value * is the default in which case the attribute is removed. * * This can save space in a stored document where a matching * pair of setInt(name, value, default) and getInt(name, default) * methods are used with the same name and default values. * * This method is only useful when the application knows the default * values of attributes or can obtain them. Typically only used * with documents local to an application. * * @param name of attribute * @param value of attribute * @param default value of the attribute */ public void setInt(String attributeName, int value, int defaultValue) { if (value == defaultValue) { a = getAttribute(attributeName); if (a != null) { this.removeAttribute(a); } } else { this.setAttribute(attributeName, value + ""); } } /** * Gets a boolean value from an attribute. * * Returns true if the attribute has a value of "true" * otherwise it returns false * * @param attribute name * @return value of attrubute */ public boolean getBoolean(String attributeName) { a = getAttribute(attributeName); if (a != null) { if (a.getValue().equals("true")) { return true; } else { return false; } } else { return false; } } /** * Gets a boolean value from an attribute. * * Returns true if the attribute has a value of "true" * otherwise it returns the default value * * @param attribute name * @param default value * @return value of attrubute or default if missing */ public boolean getBoolean(String attributeName, boolean defaultValue) { a = getAttribute(attributeName); if (a != null) { if (a.getValue().equals("true")) { return true; } else { return false; } } else { return defaultValue; } } /** * Sets the attribute value to a text representation of the * boolean value. The values are explicitly set to "true" or * "false" so that internationalisation is not an issue rather * than use the default toString(). * * @param attribute name * @param attribute value */ public void setBoolean(String attributeName, boolean value) { if (value) { setAttribute(attributeName, "true"); } else { setAttribute(attributeName, "false"); }; } /** * Stores the boolean value into an attribute unless the value * is the default in which case the attribute is removed. * * This can save space in a stored document where a matching * pair of setBoolean(name, value, default) and getBoolean(name, default) * methods are used with the same name and default values. * * This method is only useful when the application knows the default * values of attributes or can obtain them. Typically only used * with documents local to an application. * * @param name of attribute * @param value of attribute * @param default value of the attribute */ public void setBoolean( String attributeName, boolean value, boolean defaultValue) { if (value == defaultValue) { a = getAttribute(attributeName); if (a != null) { this.removeAttribute(a); } } else { this.setBoolean(attributeName, value); } } /** * Returns a DefaultMutableTreeNode for use in a JTree which * represents the element * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getTreeNode() { DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(this); addChildren(treeNode, this); return treeNode; } /** * Internal function to support get tree node */ private static void addChildren( DefaultMutableTreeNode tn, Elementx element) { DefaultMutableTreeNode node; List children = element.getChildren(); Iterator it = children.iterator(); while (it.hasNext()) { Elementx child = (Elementx) it.next(); node = new DefaultMutableTreeNode(child); tn.add(node); addChildren(node, child); }; } /** * This method checks to see if an ElementDescriptor has * been set. If it has it uses that to provide the * String representation otherwise it uses Elements toString * functionality. */ public String toString() { if (descriptor == null) { return super.toString(); } else { return descriptor.getDescription(this); } } }