All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xins.server.CallResultOutputter Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
/*
 * $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.
 *
 * 

The 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. */ private static final char[] ERRORCODE_IS = " errorcode=\"".toCharArray(); /** * The output just before a parameter name. Never null. */ private static final char[] PARAM_PREFACE = "null. */ private static final char[] PARAM_SUFFIX = "".toCharArray(); /** * The final output for each output conversion. Never null. */ private static final char[] DOCUMENT_SUFFIX = "".toCharArray(); /** * An XMLEncoder for the UTF-8 encoding. Initialized by the * class initialized and then never null. */ 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 new CallResultOutputter 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 be null. * * @param result * the call result to convert to XML, cannot be null. * * @throws IllegalArgumentException * if out == 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 the element 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); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy