com.sfs.whichdoctor.beans.IsbPayloadBean.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.beans.IsbPayloadBean.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.beans;

import com.sfs.DataFilter;

import com.sfs.whichdoctor.dao.WhichDoctorDaoException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * The Class IsbPayloadBean.
 *
 * @author David Harrison 19th March 2009
 */
public class IsbPayloadBean extends WhichDoctorBean {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The Constant HEADER_COUNT. */
    private static final int DEFAULT_HEADER_COUNT = 3;

    /** The isb message id. */
    private int isbMessageId;

    /** The xml payload. */
    private String xmlPayload;

    /** The header count. */
    private int headerCount = DEFAULT_HEADER_COUNT;

    /**
     * Sets the isb message id.
     *
     * @param isbMessageIdVal the new isb message id
     */
    public final void setIsbMessageId(final int isbMessageIdVal) {
        this.isbMessageId = isbMessageIdVal;
    }

    /**
     * Gets the isb message id.
     *
     * @return the isb message id
     */
    public final int getIsbMessageId() {
        return this.isbMessageId;
    }

    /**
     * Sets the xml payload.
     *
     * @param xmlPayloadVal the new xml payload
     */
    public final void setXmlPayload(final String xmlPayloadVal) {
        this.xmlPayload = xmlPayloadVal;
    }

    /**
     * Gets the xml payload.
     *
     * @return the xml payload
     */
    public final String getXmlPayload() {
        if (this.xmlPayload == null) {
            this.xmlPayload = "";
        }
        return this.xmlPayload;
    }

    /**
     * Gets the tabbed xml payload.
     *
     * @return the tabbed xml payload
     */
    private String getTabbedXmlPayload() {
        String htmlPayload = DataFilter.getHtml(getXmlPayload());

        return StringUtils.replace(htmlPayload, "  ", "   ");
    }

    /**
     * Gets the formatted xml payload.
     *
     * @return the formatted xml payload
     */
    public final String getFormattedXmlPayload() {
        return StringUtils.replace(getTabbedXmlPayload(), "\n", "<br/>");
    }

    /**
     * Gets the formatted xml payload header.
     *
     * @return the formatted xml payload header
     */
    public final String getFormattedXmlPayloadHeader() {
        final String htmlPayload = getTabbedXmlPayload();

        StringBuffer htmlHeader = new StringBuffer();

        StringTokenizer tokenizer = new StringTokenizer(htmlPayload, "\n");
        if (tokenizer.countTokens() < this.headerCount) {
            this.headerCount = tokenizer.countTokens();
        }
        for (int i = 0; i < this.headerCount; i++) {
            htmlHeader.append(tokenizer.nextToken());
            htmlHeader.append("<br/>");
        }
        return htmlHeader.toString();
    }

    /**
     * Gets the formatted xml payload body.
     *
     * @return the formatted xml payload body
     */
    public final String getFormattedXmlPayloadBody() {
        final String htmlPayload = getTabbedXmlPayload();

        StringBuffer htmlHeader = new StringBuffer();

        StringTokenizer tokenizer = new StringTokenizer(htmlPayload, "\n");

        int i = 0;
        while (tokenizer.hasMoreElements()) {
            final String line = tokenizer.nextToken();
            if (i >= this.headerCount) {
                htmlHeader.append(line);
                htmlHeader.append("<br/>");
            }
            i++;
        }
        return htmlHeader.toString();
    }

    /**
     * Gets the xml document.
     *
     * @return the xml document
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final Document getXmlDocument() throws WhichDoctorDaoException {
        Document document = null;

        if (StringUtils.isNotBlank(this.xmlPayload)) {
            SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
            Reader stringReader = new StringReader(this.xmlPayload);

            try {
                document = saxBuilder.build(stringReader);
            } catch (JDOMException jde) {
                throw new WhichDoctorDaoException("Error parsing ISB XML: " + jde.getMessage());
            } catch (IOException ioe) {
                throw new WhichDoctorDaoException(
                        "I/O error when trying to " + "parse ISB XML: " + ioe.getMessage());
            }
        }
        return document;
    }
}