Subversion Repositories DevTools

Rev

Rev 7047 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.erggroup.buildtool.utilities;

import java.util.ArrayList;

public class XmlBuilder {
        /**
         * A class to simply the process of creating nice XML
         * Designed to only write xml fragments
         */
        private static final  String mlf = System.getProperty("line.separator");
        
        String mTagName;
        boolean mComment = false;
        boolean mNoShrink = false;
        ArrayList<Attribute> mAttributes = new ArrayList<Attribute>();
        ArrayList<XmlBuilder> mElements = new ArrayList<XmlBuilder>();
        
        /**
         * Class to describe an an XML attribute
         */
        private class Attribute
        {
                private String aName;
                private String aValue;
                
                Attribute( String name, String value)
                {
                        aName = name;
                        aValue = value;
                }
                
                public String toString()
                {
                        String result;
                        result = " " + aName + "=\"" + aValue + "\"";
                        return result;
                }
        }
        
        /**     Create an XML element
         * 
         * @param name  - Tag name of the element
         */
        public XmlBuilder( String name)
        {
                mTagName = name;
        }
        
        /**     Add an attribute the the XML element
         * 
         * @param name - The name of the attribute
         * @param value - The value of the attribute
         * @return The XML element. This allows chaining of operations
         */
        public XmlBuilder addAttribute ( String name, String value)
        {
                mAttributes.add(new Attribute(name,value));
                return this;
        }
        
        /**     Add an attribute the the XML element
         * 
         * @param name - The name of the attribute
     * @param value - The value of the attribute
         * @return The XML element. This allows chaining of operations
         */
        public XmlBuilder addAttribute(String name, int value) {
                mAttributes.add(new Attribute(name,Integer.toString(value)));
                return this;
        }
        
        /**     Add an attribute the the XML element
         * 
         * @param name - The name of the attribute
     * @param value - The value of the attribute
         * @return The XML element. This allows chaining of operations
         */
        public XmlBuilder addAttribute(String name, long value) {
                mAttributes.add(new Attribute(name,Long.toString(value)));
                return this;
        }

        /**Add a child element to the XML element
         * 
         * @param element - The element to extend
         * @return The XML element. This allows chaining of operations
         */
        public XmlBuilder addElement(XmlBuilder element)
        {
                mElements.add(element);
                return this;
        }
        
        /**Create and add a new XmlBuild entry
         * 
         * @param name  - Name of the element to add
         * @return The element just added
         */
        public XmlBuilder addNewElement(String name) {
                
                XmlBuilder element = new XmlBuilder(name);
                addElement(element);
                return element;
        }
        
        /**Add a comment element
         * 
         */
        public XmlBuilder addComment(String comment)
        {
                addNewElement(comment).isComment();
                return this;
        }
        
        /** Create a property string
         * 
         * @param name          - Name of the property
         * @param value         - Value of the property
         */
        public XmlBuilder addProperty(String name, String value)
        {
                addNewElement("property")
                        .addAttribute("name", name)
                        .addAttribute("value", value);

                return this;
        }
        
        /** Create a property string
         * 
         * @param name          - Name of the property
         * @param value         - Integer Value of the property
         */
        public XmlBuilder addProperty(String name, int value)
        {
                addNewElement("property")
                        .addAttribute("name", name)
                        .addAttribute("value", value);
                
                return this;
        }

        /** Create a property string
         * 
         * @param name          - Name of the property
         * @param value         - Long Integer Value of the property
         */
        public XmlBuilder addProperty(String name, long value)
        {
                addNewElement("property")
                        .addAttribute("name", name)
                        .addAttribute("value", value);
                
                return this;
        }
        
        /** Create a property tag - will only appear if boolean value is true
         * @param name          - Name of the property
         * @param flag          - Create property if true
         */
        public XmlBuilder makePropertyTag(String name, boolean flag)
        {
                if (flag)
                {
                        addProperty(name, "");
                }
                return this;
        }
        
        /**Convert the XML to a string
         * 
         * @param indent        - Indent prefix
         * @return String version of the XML
         */
        public String toString(String indent)
        {
                String leadin = "<";
                String leadout = "/>";
                
                if (mComment)
                {
                        leadin = "<!-- ";
                        leadout = " -->";
                }
                
                String result = indent + leadin + mTagName;
                
                // Process each attribute
                for (Attribute e : mAttributes) {
                    result += e.toString();
                }
                
                // Process child elements
                if (mElements.isEmpty() && ! mNoShrink)
                {
                        result += leadout + mlf;        
                }
                else
                {
                        result += ">" + mlf;
                        // Process each child
                        for (XmlBuilder e : mElements) {
                            result += e.toString( "  " + indent);
                        }
                        
                        result += indent + "</"+ mTagName +">" + mlf;
                }
                
                return result;
        }
        
        /**Convert the XML to a string with no leading indent
         * 
         * @return String version of the XML
         */
        public String toString()
        {
                return toString("");
        }
        
        /** Convert the XML to a single line of text
         *  Remove end-of-line characters.
         *  Replace double quotes with single quotes
         *  Simplifies UTF processing
         */
        public String toOneLine()
        {
                return toString("").replace(mlf, "").replace("\"", "'");
        }

        /**Mark this XML element as a comment
         * Comments do not have attributes or child elements
         * 
         * Chaining with this method is not possible. Use addComment.
         * 
         * @return
         */
        public XmlBuilder isComment() {
                mComment = true;
                return this;
        }
        
        /**Mark this XML element as always expanded.
         * This will prevent the XML tag being shrink if it has no child elements
         * 
         * @return The current element
         */
        public XmlBuilder isExpanded() {
                mNoShrink = true;
                return this;
        }

}