org.xins.server.CallResultOutputter Maven / Gradle / Ivy
/* * $Id: CallResultOutputter.java,v 1.58 2011/04/16 15:48:02 agoubard Exp $ * * See the COPYRIGHT file for redistribution and use restrictions. */ package org.xins.server; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Iterator; import java.util.Map; import org.xins.common.MandatoryArgumentChecker; import org.w3c.dom.Element; import org.xins.common.xml.ElementFormatter; import org.znerd.xmlenc.XMLEncoder; /** * Converter that can be used by calling conventions to generate responses * which are compatible with the XINS standard calling convention. * *
. */ private static final char[] ERRORCODE_IS = " errorcode=\"".toCharArray(); /** * The output just before a parameter name. NeverThe result output is always in the UTF-8 encoding. * * @version $Revision: 1.58 $ $Date: 2011/04/16 15:48:02 $ * @author Ernst de Haan * @author Anthony Goubard * * @since XINS 1.5.0 */ public final class CallResultOutputter { /** * The first output for each output conversion. Never
null
. */ private static final char[] DOCUMENT_PREFACE = "null null
. */ private static final char[] PARAM_PREFACE = "null. */ private static final char[] PARAM_SUFFIX = "".toCharArray(); /** * The final output for each output conversion. Nevernull
. */ private static final char[] DOCUMENT_SUFFIX = "".toCharArray(); /** * AnXMLEncoder
for the UTF-8 encoding. Initialized by the * class initialized and then nevernull
. */ private static final XMLEncoder XML_ENCODER; static { try { XML_ENCODER = XMLEncoder.getEncoder("UTF-8"); } catch (UnsupportedEncodingException exception) { Error error = new Error(exception); throw error; } } /** * Constructs a newCallResultOutputter
object. */ private CallResultOutputter() { // empty } /** * Generates XML for the specified call result. The XML is sent to the * specified output stream. * * @param out * the output stream to send the XML to, cannot benull
. * * @param result * the call result to convert to XML, cannot benull
. * * @throws IllegalArgumentException * ifout == null * || result == null
. * * @throws IOException * if there was an I/O error while writing to the output stream. */ public static void output(Writer out, FunctionResult result) throws IllegalArgumentException, IOException { // Check preconditions MandatoryArgumentChecker.check("out", out, "result", result); // Output the declaration out.write(DOCUMENT_PREFACE); // Output the start of theelement String code = result.getErrorCode(); if (code == null) { out.write('>'); } else { out.write(ERRORCODE_IS); out.write(code); out.write('"'); out.write('>'); } // Write the output parameters, if any Map params = result.getParameters(); for (String paramName : params.keySet()) { if (paramName != null && paramName.length() > 0) { String v = params.get(paramName); if (v != null && v.length() > 0) { out.write(PARAM_PREFACE); XML_ENCODER.text(out, paramName, true); out.write('"'); out.write('>'); XML_ENCODER.text(out, v, true); out.write(PARAM_SUFFIX); } } } // Write the data element, if any Element dataElement = result.getDataElement(); if (dataElement != null) { String dataXML = ElementFormatter.format(dataElement); out.write(dataXML); } // End the root element out.write(DOCUMENT_SUFFIX); } }