com.sfs.whichdoctor.xml.writer.XmlWriter.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.xml.writer.XmlWriter.java

Source

/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.whichdoctor.xml.writer;

import java.util.Stack;

import org.apache.commons.lang.StringUtils;

/**
 * Makes writing XML much much easier.
 *
 * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
 * @version 0.1 Modified by David Harrison for use in WhichDoctor
 */
public class XmlWriter {

    /** The string buffer. */
    private StringBuffer sb;

    /** The stack. */
    private Stack<String> stack;

    /** The attrs. */
    private StringBuffer attrs;

    /** The empty. */
    private boolean empty;

    /** The closed. */
    private boolean closed;

    /**
     * Create an XmlWriter.
     */
    public XmlWriter() {
        this.sb = new StringBuffer();
        this.closed = true;
        this.stack = new Stack<String>();
    }

    /**
     * Begin to output an entity.
     *
     * @param name of entity.
     *
     * @return XmlWriter
     */
    public final XmlWriter writeEntity(final String name) {
        closeOpeningTag();
        this.closed = false;
        this.sb.append("<");
        this.sb.append(name);
        stack.add(name);
        this.empty = true;
        return this;
    }

    /**
     * Close off the opening tag.
     */
    private void closeOpeningTag() {
        if (!this.closed) {
            writeAttributes();
            this.closed = true;
            this.sb.append(">");
        }
    }

    /**
     * Write out all current attributes.
     */
    private void writeAttributes() {
        if (this.attrs != null) {
            this.sb.append(this.attrs.toString());
            this.attrs.setLength(0);
            this.empty = false;
        }
    }

    /**
     * Write an attribute out for the current entity. Any xml characters in the
     * value are escaped.
     *
     * @param attr the attr
     * @param value the value
     *
     * @return XmlWriter
     */
    public final XmlWriter writeAttribute(final String attr, final String value) {

        if (this.attrs == null) {
            this.attrs = new StringBuffer();
        }
        this.attrs.append(" ");
        this.attrs.append(attr);
        this.attrs.append("=\"");
        this.attrs.append(escapeXml(value));
        this.attrs.append("\"");

        return this;
    }

    /**
     * Write an attribute out for the current entity.
     *
     * @param attr the attr
     * @param value the value
     *
     * @return XmlWriter
     */
    public final XmlWriter writeAttribute(final String attr, final int value) {
        return writeAttribute(attr, String.valueOf(value));
    }

    /**
     * End the current entity.
     *
     * @return XmlWriter
     */
    public final XmlWriter endEntity() {
        if (!this.stack.empty()) {
            closeOpenEntity();
        }
        return this;
    }

    /**
     * Gets the xml string.
     * This closes the writer and can only be called once.
     *
     * @return the xml
     */
    public final String getXml() {
        if (!this.stack.empty()) {
            closeOpenEntity();
        }
        return this.sb.toString();
    }

    /**
     * Output body text. Any xml characters are escaped.
     *
     * @param text the text
     *
     * @return XmlWriter
     */
    public final XmlWriter writeText(final String text) {
        closeOpeningTag();
        this.empty = false;
        this.sb.append(escapeXml(text));
        return this;
    }

    /**
     * Output body text. Any xml characters are escaped.
     *
     * @param value the integer value
     *
     * @return XmlWriter
     */
    public final XmlWriter writeText(final int value) {
        return writeText(String.valueOf(value));
    }

    /**
     * Output body text. Any xml characters are escaped.
     *
     * @param value the double value
     *
     * @return XmlWriter
     */
    public final XmlWriter writeText(final double value) {
        return writeText(String.valueOf(value));
    }

    /**
     * Out an XML element.
     *
     * @param xml the xml
     *
     * @return XmlWriter
     */
    public final XmlWriter writeXml(final String xml) {
        closeOpeningTag();
        this.empty = false;
        this.sb.append(xml);
        return this;
    }

    /**
     * Static functions lifted from generationjava helper classes to make the
     * jar smaller.
     *
     * @param str the str
     *
     * @return String
     */
    public static String escapeXml(final String str) {

        String returnString = "";

        if (StringUtils.isNotBlank(str)) {
            returnString = StringUtils.replace(str, "& ", "&amp; ");
            returnString = StringUtils.replace(returnString, "<", "&lt;");
            returnString = StringUtils.replace(returnString, ">", "&gt;");
            returnString = StringUtils.replace(returnString, "\"", "&quot;");
            returnString = StringUtils.replace(returnString, "'", "&apos;");
        }
        return returnString;
    }

    /**
     * Close open entity.
     */
    private void closeOpenEntity() {
        // The stack is not empty, close the XML
        String name = (String) this.stack.pop();
        if (name != null) {
            if (this.empty) {
                writeAttributes();
                this.sb.append("/>");
                this.closed = true;
            } else {
                this.sb.append("</");
                this.sb.append(name);
                this.sb.append(">");
            }
            this.empty = false;
        }
    }
}