001: /*
002: * $Id: CallResultOutputter.java,v 1.51 2007/09/18 08:45:07 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.server;
008:
009: import java.io.IOException;
010: import java.io.UnsupportedEncodingException;
011: import java.io.Writer;
012: import java.util.Iterator;
013:
014: import org.xins.common.MandatoryArgumentChecker;
015: import org.xins.common.collections.PropertyReader;
016: import org.xins.common.xml.Element;
017: import org.xins.common.xml.ElementSerializer;
018: import org.xins.logdoc.ExceptionUtils;
019:
020: import org.znerd.xmlenc.XMLEncoder;
021: import org.znerd.xmlenc.XMLOutputter;
022:
023: /**
024: * Converter that can be used by calling conventions to generate responses
025: * which are compatible with the XINS standard calling convention.
026: *
027: * <p>The result output is always in the UTF-8 encoding.
028: *
029: * @version $Revision: 1.51 $ $Date: 2007/09/18 08:45:07 $
030: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
031: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
032: *
033: * @since XINS 1.5.0
034: */
035: public final class CallResultOutputter {
036:
037: /**
038: * The first output for each output conversion. Never <code>null</code>.
039: */
040: private static final char[] DOCUMENT_PREFACE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><result"
041: .toCharArray();
042:
043: /**
044: * The output for the new-style calling convention in case success is
045: * false, just before the name of the error code.
046: * Never <code>null</code>.
047: */
048: private static final char[] ERRORCODE_IS = " errorcode=\""
049: .toCharArray();
050:
051: /**
052: * The output just before a parameter name. Never <code>null</code>.
053: */
054: private static final char[] PARAM_PREFACE = "<param name=\""
055: .toCharArray();
056:
057: /**
058: * The output right after a parameter value. Never <code>null</code>.
059: */
060: private static final char[] PARAM_SUFFIX = "</param>".toCharArray();
061:
062: /**
063: * The final output for each output conversion. Never <code>null</code>.
064: */
065: private static final char[] DOCUMENT_SUFFIX = "</result>"
066: .toCharArray();
067:
068: /**
069: * An <code>XMLEncoder</code> for the UTF-8 encoding. Initialized by the
070: * class initialized and then never <code>null</code>.
071: */
072: private static final XMLEncoder XML_ENCODER;
073:
074: static {
075: try {
076: XML_ENCODER = XMLEncoder.getEncoder("UTF-8");
077: } catch (UnsupportedEncodingException exception) {
078: Error error = new Error();
079: ExceptionUtils.setCause(error, exception);
080: throw error;
081: }
082: }
083:
084: /**
085: * Constructs a new <code>CallResultOutputter</code> object.
086: */
087: private CallResultOutputter() {
088: // empty
089: }
090:
091: /**
092: * Generates XML for the specified call result. The XML is sent to the
093: * specified output stream.
094: *
095: * @param out
096: * the output stream to send the XML to, cannot be <code>null</code>.
097: *
098: * @param result
099: * the call result to convert to XML, cannot be <code>null</code>.
100: *
101: * @throws IllegalArgumentException
102: * if <code>out == null
103: * || result == null</code>.
104: *
105: * @throws IOException
106: * if there was an I/O error while writing to the output stream.
107: */
108: public static void output(Writer out, FunctionResult result)
109: throws IllegalArgumentException, IOException {
110:
111: // Check preconditions
112: MandatoryArgumentChecker.check("out", out, "result", result);
113:
114: // Output the declaration
115: out.write(DOCUMENT_PREFACE);
116:
117: // Output the start of the <result> element
118: String code = result.getErrorCode();
119: if (code == null) {
120: out.write('>');
121: } else {
122: out.write(ERRORCODE_IS);
123: out.write(code);
124: out.write('"');
125: out.write('>');
126: }
127:
128: // Write the output parameters, if any
129: PropertyReader params = result.getParameters();
130: if (params != null) {
131: Iterator names = params.getNames();
132: while (names.hasNext()) {
133: String n = (String) names.next();
134: if (n != null && n.length() > 0) {
135: String v = params.get(n);
136: if (v != null && v.length() > 0) {
137: out.write(PARAM_PREFACE);
138: XML_ENCODER.text(out, n, true);
139: out.write('"');
140: out.write('>');
141: XML_ENCODER.text(out, v, true);
142: out.write(PARAM_SUFFIX);
143: }
144: }
145: }
146: }
147:
148: // Write the data element, if any
149: Element dataElement = result.getDataElement();
150: if (dataElement != null) {
151: ElementSerializer serializer = new ElementSerializer();
152: XMLOutputter xmlout = new XMLOutputter(out, "UTF-8");
153: serializer.output(xmlout, dataElement);
154: }
155:
156: // End the root element <result>
157: out.write(DOCUMENT_SUFFIX);
158: }
159: }
|