com.sfs.whichdoctor.xml.writer.helper.WorkshopXmlHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.xml.writer.helper.WorkshopXmlHelper.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.helper;

import com.sfs.Formatter;
import com.sfs.whichdoctor.beans.WorkshopBean;
import com.sfs.whichdoctor.xml.writer.XmlWriter;

import java.util.Collection;

import org.apache.commons.lang.StringUtils;

/**
 * The Class WorkshopXmlHelper.
 */
public class WorkshopXmlHelper {

    /**
     * Instantiates a new workshop xml helper.
     */
    protected WorkshopXmlHelper() {
        throw new UnsupportedOperationException();
    }

    /**
     * Output the workshop collection as an XML string.
     *
     * @param workshops the workshops
     *
     * @return the xml string
     */
    public static String getWorkshopsXml(final Collection<WorkshopBean> workshops) {

        final XmlWriter xmlwriter = new XmlWriter();

        xmlwriter.writeEntity("workshops");

        for (WorkshopBean workshop : workshops) {

            xmlwriter.writeEntity("workshop").writeAttribute("GUID", workshop.getGUID())
                    .writeAttribute("workshopId", workshop.getId());

            xmlwriter.writeEntity("type").writeText(workshop.getType()).endEntity();
            xmlwriter.writeEntity("date").writeText(Formatter.convertDate(workshop.getWorkshopDate())).endEntity();

            if (StringUtils.isNotBlank(workshop.getMemo())) {
                StringBuffer message = new StringBuffer();
                message.append("<p>");
                message.append(StringUtils.replace(workshop.getMemo(), "\n", "</p><p>"));
                message.append("</p>");

                xmlwriter.writeEntity("message").writeXml(message.toString()).endEntity().endEntity();
            }
            xmlwriter.endEntity();
        }
        xmlwriter.endEntity();

        return xmlwriter.getXml();
    }
}