<commit_before>/*
 * $Id$
 *
 * Copyright 2004 Wanadoo Nederland B.V.
 * See the COPYRIGHT file for redistribution and use restrictions.
 */
package org.xins.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.collections.PropertyReader;
import org.xins.common.text.TextUtils;
import org.xins.common.xml.Element;
import org.xins.common.xml.ElementSerializer;

import org.znerd.xmlenc.XMLOutputter;

/**
 * Transformer that is able to externalize a <code>FunctionResult</code> object to
 * XML.
 *
 * @version $Revision$ $Date$
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 *
 * @since XINS 1.0.0
 */
final class CallResultOutputter extends Object {

   //-------------------------------------------------------------------------
   // Class fields
   //-------------------------------------------------------------------------

   //-------------------------------------------------------------------------
   // Class functions
   //-------------------------------------------------------------------------

   /**
    * 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 <code>null</code>.
    *
    * @param encoding
    *    the encoding format for the XML, cannot be <code>null</code>.
    *
    * @param result
    *    the call result to convert to XML, cannot be <code>null</code>.
    *
    * @param oldStyle
    *    flag that indicates if old-style output should be generated or not.
    *
    * @throws IllegalArgumentException
    *    if <code>out      == null
    *          || encoding == null
    *          || result   == null</code>.
    *
    * @throws IOException
    *    if there was an I/O error while writing to the output stream.
    */
   public static void output(PrintWriter    out,
                             String         encoding,
                             FunctionResult result,
                             boolean        oldStyle)
   throws IllegalArgumentException, IOException {

      // TODO: Support OutputStream instead of Writer

      // Check preconditions
      MandatoryArgumentChecker.check("out",      out,
                                     "encoding", encoding,
                                     "result",   result);

      // Create an XMLOutputter
      XMLOutputter xmlout = new XMLOutputter(out, encoding);

      // Output the declaration
      // XXX: Make it configurable whether the declaration is output or not?
      xmlout.declaration();

      // Write the result start tag
      xmlout.startTag("result");

      // Write the error code
      String code = result.getErrorCode();
      if (oldStyle) {
         xmlout.attribute("success", code == null ? "true" : "false");
         if (code != null) {
            xmlout.attribute("code", code);
         }
      }

      // TODO: Only print the 'errorcode' attribute if not oldStyle?
      if (code != null) {
         xmlout.attribute("errorcode", code);
      }

      // Write the output parameters
      PropertyReader params = result.getParameters();
      if (params != null) {
         Iterator names = params.getNames();
         while (names.hasNext()) {
            String name  = (String) names.next();
            if (! TextUtils.isEmpty(name)) {
               String value = params.get(name);

               if (! TextUtils.isEmpty(value)) {
                  xmlout.startTag("param");
                  xmlout.attribute("name", name);
                  xmlout.pcdata(value);
                  xmlout.endTag(); // param
               }
            }
         }
      }

      // Write the data element
      Element dataElement = result.getDataElement();
      if (dataElement != null) {
         ElementSerializer serializer = new ElementSerializer();
         serializer.output(xmlout, dataElement);
      }

      xmlout.endTag(); // result
   }


   //-------------------------------------------------------------------------
   // Constructors
   //-------------------------------------------------------------------------

   /**
    * Constructs a new <code>CallResultOutputter</code> object.
    */
   private CallResultOutputter() {
      // empty
   }


   //-------------------------------------------------------------------------
   // Fields
   //-------------------------------------------------------------------------

   //-------------------------------------------------------------------------
   // Methods
   //-------------------------------------------------------------------------
}
<commit_msg>Removed @since tag for this package-private class.<commit_after>/*
 * $Id$
 *
 * Copyright 2004 Wanadoo Nederland B.V.
 * See the COPYRIGHT file for redistribution and use restrictions.
 */
package org.xins.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.collections.PropertyReader;
import org.xins.common.text.TextUtils;
import org.xins.common.xml.Element;
import org.xins.common.xml.ElementSerializer;

import org.znerd.xmlenc.XMLOutputter;

/**
 * Transformer that is able to externalize a <code>FunctionResult</code> object to
 * XML.
 *
 * @version $Revision$ $Date$
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 */
final class CallResultOutputter extends Object {

   //-------------------------------------------------------------------------
   // Class fields
   //-------------------------------------------------------------------------

   //-------------------------------------------------------------------------
   // Class functions
   //-------------------------------------------------------------------------

   /**
    * 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 <code>null</code>.
    *
    * @param encoding
    *    the encoding format for the XML, cannot be <code>null</code>.
    *
    * @param result
    *    the call result to convert to XML, cannot be <code>null</code>.
    *
    * @param oldStyle
    *    flag that indicates if old-style output should be generated or not.
    *
    * @throws IllegalArgumentException
    *    if <code>out      == null
    *          || encoding == null
    *          || result   == null</code>.
    *
    * @throws IOException
    *    if there was an I/O error while writing to the output stream.
    */
   public static void output(PrintWriter    out,
                             String         encoding,
                             FunctionResult result,
                             boolean        oldStyle)
   throws IllegalArgumentException, IOException {

      // TODO: Support OutputStream instead of Writer

      // Check preconditions
      MandatoryArgumentChecker.check("out",      out,
                                     "encoding", encoding,
                                     "result",   result);

      // Create an XMLOutputter
      XMLOutputter xmlout = new XMLOutputter(out, encoding);

      // Output the declaration
      // XXX: Make it configurable whether the declaration is output or not?
      xmlout.declaration();

      // Write the result start tag
      xmlout.startTag("result");

      // Write the error code
      String code = result.getErrorCode();
      if (oldStyle) {
         xmlout.attribute("success", code == null ? "true" : "false");
         if (code != null) {
            xmlout.attribute("code", code);
         }
      }

      // TODO: Only print the 'errorcode' attribute if not oldStyle?
      if (code != null) {
         xmlout.attribute("errorcode", code);
      }

      // Write the output parameters
      PropertyReader params = result.getParameters();
      if (params != null) {
         Iterator names = params.getNames();
         while (names.hasNext()) {
            String name  = (String) names.next();
            if (! TextUtils.isEmpty(name)) {
               String value = params.get(name);

               if (! TextUtils.isEmpty(value)) {
                  xmlout.startTag("param");
                  xmlout.attribute("name", name);
                  xmlout.pcdata(value);
                  xmlout.endTag(); // param
               }
            }
         }
      }

      // Write the data element
      Element dataElement = result.getDataElement();
      if (dataElement != null) {
         ElementSerializer serializer = new ElementSerializer();
         serializer.output(xmlout, dataElement);
      }

      xmlout.endTag(); // result
   }


   //-------------------------------------------------------------------------
   // Constructors
   //-------------------------------------------------------------------------

   /**
    * Constructs a new <code>CallResultOutputter</code> object.
    */
   private CallResultOutputter() {
      // empty
   }


   //-------------------------------------------------------------------------
   // Fields
   //-------------------------------------------------------------------------

   //-------------------------------------------------------------------------
   // Methods
   //-------------------------------------------------------------------------
}

{'fraction_non_alphanumeric': 0.2471815404067011, 'fraction_numerical': 0.0011589927299546939, 'mean_word_length': 1.226078799249531, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '981806', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2173, 'n_words': 623}