XmlWriter.java

/*
 * Copyright © 2024-2025 The CTAN Team and individual authors
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */

package org.ctan.site.resources.catalogue.api;

import java.io.StringWriter;

import org.apache.commons.lang3.StringUtils;

/**
 * The class <code>XmlWriter</code> contains the specialised writer for XML
 * files. It collects the data in memory.
 */
public class XmlWriter extends StringWriter {

    /**
     * The method <code>out</code> provides means to write some arguments to the
     * writer.
     *
     * @param args the arguments to append
     * @return this
     */
    public XmlWriter out(String... args) {

        for (var arg : args) {
            if (arg == null) {
                return this;
            }
        }
        for (var arg : args) {
            this.append(arg);
        }
        return this;
    }

    /**
     * The method <code>outFullTag</code> provides means to generate a full tag.
     *
     * @param tag the tag
     * @param value the value
     * @return this
     */
    public XmlWriter outFullTag(String tag, String value) {

        if (StringUtils.isNotEmpty(value)) {
            out("  <", tag, ">", value, "</", tag, ">\n");
        }

        return this;
    }

    /**
     * The method <code>outIf</code> provides means to write a parameter if a
     * condition is met and the value is not empty.
     *
     * @param cond the condition to check
     * @param attr the attribute
     * @param value the value
     * @return this
     */
    public XmlWriter outIf(boolean cond, String attr, String value) {

        if (cond && StringUtils.isNotEmpty(value)) {
            out(" ", attr, "=\"", value, "\"");
        }

        return this;
    }

    /**
     * The method <code>outIf</code> provides means to write a parameter if a
     * condition is met and the value is not empty.
     *
     * @param attr the attribute
     * @param value the value
     * @return this
     */
    public XmlWriter outIf(String attr, String value) {

        if (StringUtils.isNotEmpty(value)) {
            out(" ", attr, "=\"", value, "\"");
        }

        return this;
    }

    /**
     * The method <code>outIf</code> provides means to write a parameter if a
     * condition is met and the value is not empty.
     *
     * @param tag the tag
     * @param attr the attribute
     * @param value the value
     * @return this
     */
    public XmlWriter outIf(String tag, String attr, String value) {

        if (StringUtils.isNotEmpty(value)) {
            out("<", tag, " ", attr, "=\"", value, "\" />");
        }

        return this;
    }

    /**
     * The method <code>outName</code> provides means to write out a parameter.
     *
     * @param cond check for a condition
     * @param tag the tag
     * @param value the value
     * @return this
     */
    public XmlWriter outName(boolean cond, String tag, String value) {

        if (!cond && StringUtils.isNotEmpty(value)) {
            out(" ", tag, "=\"", value, "\"");
        }

        return this;
    }

    /**
     * The method <code>out</code> provides means to write some arguments to the
     * writer followed by a newline character.
     *
     * @param args the arguments to append
     * @return this
     */
    public XmlWriter outNl(String... args) {

        for (var arg : args) {
            if (arg == null) {
                return this;
            }
        }
        for (var arg : args) {
            this.append(arg);
        }
        this.append('\n');
        return this;
    }

    /**
     * The method <code>outTag</code> provides means to add a tag with an
     * attribute value.
     *
     * @param tag the name of the tag
     * @param attribute the name of the attribute
     * @param value the value of the attribute
     * @return this
     */
    public XmlWriter outTagWithAttribute(String tag, String attribute,
            String value) {

        if (StringUtils.isNotEmpty(value)) {
            out("  <", tag, " ", attribute, "=\"", value, "\" />\n");
        }

        return this;
    }
}