Java tutorial
/******************************************************************************* * 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 java.io.IOException; import java.io.Reader; import java.io.StringReader; import org.apache.commons.lang.StringUtils; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; /** * The Class IsbMessageBean. * * @author David Harrison 24th March 2008 */ public class IsbMessageBean extends WhichDoctorBean { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The isb payload. */ private IsbPayloadBean isbPayload; /** The source. */ private String source; /** The target. */ private String target; /** The action. */ private String action; /** The identifier. */ private String identifier; /** The is inbound. */ private boolean isInbound; /** The processed. */ private boolean processed; /** * Sets the isb payload. * * @param isbPayloadRef the new isb payload */ public final void setIsbPayload(final IsbPayloadBean isbPayloadRef) { this.isbPayload = isbPayloadRef; } /** * Gets the isb payload. * * @return the isb payload */ public final IsbPayloadBean getIsbPayload() { if (this.isbPayload == null) { this.isbPayload = new IsbPayloadBean(); } return this.isbPayload; } /** * Try parsing the XML document and setting the relevant ISB message * attributes e.g. Source, Target, Identifier, Action, XmlPayload * * @param xmlPayloadVal the xml payload * * @throws JDOMException the JDOM exception */ public final void parseXmlPayload(final String xmlPayloadVal) throws JDOMException { // Ensure there are no "& " characters String xmlPayload = StringUtils.replace(xmlPayloadVal, ">", "|gt;"); xmlPayload = StringUtils.replace(xmlPayload, "<", "|lt;"); xmlPayload = StringUtils.replace(xmlPayload, "&", "|amp;"); // Replace the & characters left xmlPayload = StringUtils.replace(xmlPayload, "&", "&"); // Bring the special characters back xmlPayload = StringUtils.replace(xmlPayload, "|gt;", ">"); xmlPayload = StringUtils.replace(xmlPayload, "|lt;", "<"); xmlPayload = StringUtils.replace(xmlPayload, "|amp;", "&"); if (xmlPayload.startsWith("<?xml ")) { xmlPayload = StringUtils.replace(xmlPayload, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""); } // Parse the ISB XML document to get the identifier final SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); final Reader stringReader = new StringReader(xmlPayload); Document isbDocument = null; try { isbDocument = saxBuilder.build(stringReader); } catch (JDOMException e) { // Error parsing the XML document throw new JDOMException("Error parsing XML into IsbMessageBean: " + e.getMessage() + "\n" + xmlPayload); } catch (IOException e) { // Error parsing the XML document throw new JDOMException("Error reading xmlPayload string: " + e.getMessage()); } if (isbDocument != null) { // Get the ISB source for this change try { this.setSource(isbDocument.getRootElement().getAttribute("source").getValue()); } catch (NullPointerException npe) { throw new JDOMException("The source element does not exist"); } // Get the ISB target of this change try { this.setTarget(isbDocument.getRootElement().getAttribute("target").getValue()); } catch (NullPointerException npe) { throw new JDOMException("The target element does not exist"); } Element isbIdentityElement = isbDocument.getRootElement().getChild("identity"); if (isbIdentityElement != null) { // Get the ISB identifier of this change try { this.setIdentifier(isbIdentityElement.getAttribute("id").getValue()); } catch (NullPointerException npe) { throw new JDOMException("The id element does not exist"); } // Get the ISB action of this change try { this.setAction(isbIdentityElement.getAttribute("action").getValue()); } catch (NullPointerException npe) { throw new JDOMException("The action element does not exist"); } } XMLOutputter outputter = new XMLOutputter(); outputter.setFormat(Format.getPrettyFormat()); if (isbPayload == null) { isbPayload = new IsbPayloadBean(); } isbPayload.setXmlPayload(outputter.outputString(isbDocument)); } else { throw new JDOMException("The parsed ISB XML document was null"); } } /** * Sets the identifier. * * @param identifierVal the new identifier */ public final void setIdentifier(final String identifierVal) { this.identifier = identifierVal; } /** * Gets the identifier. * * @return the identifier */ public final String getIdentifier() { if (this.identifier == null) { this.identifier = ""; } return this.identifier; } /** * Sets the action. * * @param actionVal the new action */ public final void setAction(final String actionVal) { this.action = actionVal; } /** * Gets the action. * * @return the action */ public final String getAction() { if (this.action == null) { this.action = ""; } return this.action; } /** * Sets the source. * * @param sourceVal the new source */ public final void setSource(final String sourceVal) { this.source = sourceVal; } /** * Gets the source. * * @return the source */ public final String getSource() { if (this.source == null) { this.source = ""; } return this.source; } /** * Sets the target. * * @param targetVal the new target */ public final void setTarget(final String targetVal) { this.target = targetVal; } /** * Gets the target. * * @return the target */ public final String getTarget() { if (this.target == null) { this.target = ""; } return this.target; } /** * Sets the checks if is inbound. * * @param isInboundVal the new checks if is inbound */ public final void setIsInbound(final boolean isInboundVal) { this.isInbound = isInboundVal; } /** * Gets the checks if is inbound. * * @return the checks if is inbound */ public final boolean getIsInbound() { return this.isInbound; } /** * Sets the processed. * * @param processedVal the new processed */ public final void setProcessed(final boolean processedVal) { this.processed = processedVal; } /** * Gets the processed. * * @return the processed */ public final boolean getProcessed() { return this.processed; } }