<commit_before>/*
 * $Id$
 */
package org.xins.client;

import java.io.StringReader;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import org.xins.common.MandatoryArgumentChecker;

import org.xins.common.collections.PropertyReader;
import org.xins.common.collections.ProtectedPropertyReader;

import org.xins.common.service.TargetDescriptor;

import org.xins.common.text.FastStringBuffer;
import org.xins.common.text.ParseException;

/**
 * Call result parser. XML is parsed to produce a
 * {@link XINSServiceCaller.Result} object.
 *
 * @version $Revision$ $Date$
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 */
public class ResultParser extends Object {

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

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

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

   /**
    * Constructs a new <code>ResultParser</code>.
    */
   public ResultParser() {
      _xmlBuilder = new SAXBuilder();
   }


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

   /**
    * Parser that takes an XML document and converts it to a JDOM Document.
    */
   private final SAXBuilder _xmlBuilder;


   //-------------------------------------------------------------------------
   // Methods
   //-------------------------------------------------------------------------

   /**
    * Parses the given XML string to create a
    * <code>XINSServiceCaller.Result</code> object with the specified
    * <code>TargetDescriptor</code>.
    *
    * @param target
    *    the {@link TargetDescriptor} that was used to get the XML, cannot be
    *    <code>null</code>.
    *
    * @param xml
    *    the XML to be parsed, not <code>null</code>.
    *
    * @return
    *    the parsed result of the call, not <code>null</code>.
    *
    * @throws IllegalArgumentException
    *    if <code>target == null || xml == null</code>
    *
    * @throws ParseException
    *    if the specified string is not valid XML or if it is not a valid XINS
    *    API function call result.
    */
   public XINSServiceCaller.Result parse(TargetDescriptor target,
                                         String           xml)
   throws IllegalArgumentException, ParseException {

      // Check preconditions
      MandatoryArgumentChecker.check("target", target, "xml", xml);

      StringReader reader = new StringReader(xml);
      Document document;
      try {
         document = _xmlBuilder.build(reader);
      } catch (Throwable exception) {
         String detail = exception.getMessage();
         FastStringBuffer buffer = new FastStringBuffer(250);
         buffer.append("Unable to convert the specified character string to XML");
         if (detail != null && detail.length() > 0) {
            buffer.append(": ");
            buffer.append(detail);
         } else {
            buffer.append('.');
         }
         String message = buffer.toString();
         Log.log_2005(exception, detail);
         throw new ParseException(message);
      } finally {
         reader.close();
      }

      return parse(target, document);
   }

   /**
    * Parses the given XML <code>Document</code> to create a
    * <code>XINSServiceCaller.Result</code> object with the specified
    * <code>TargetDescriptor</code>.
    *
    * @param target
    *    the {@link TargetDescriptor} that was used to get the XML, cannot be
    *    <code>null</code>.
    *
    * @param document
    *    the document to be parsed, not <code>null</code>.
    *
    * @return
    *    the parsed result of the call, not <code>null</code>.
    *
    * @throws NullPointerException
    *    if <code>target == null
    *          || document == null
    *          || document.</code>{@link Document#getRootElement() getRootElement()}<code> == null</code>
    *
    * @throws ParseException
    *    if the specified XML document is not a valid XINS API function call
    *    result.
    */
   private XINSServiceCaller.Result parse(TargetDescriptor target,
                                          Document         document)
   throws NullPointerException, ParseException {

      Element element = document.getRootElement();

      // Check that the root element is <result/>
      if ("result".equals(element.getName()) == false) {
         String message = "The returned XML is invalid. The type of the root element is \"" + element.getName() + "\" instead of \"result\".";
         Log.log_2006(element.getName());
         throw new ParseException(message);
      }

      String         code        = parseResultCode(element);
      PropertyReader parameters  = parseParameters(element);
      Element        dataElement = element.getChild("data");

      return new XINSServiceCaller.Result(target, code, parameters, dataElement);
   }

   /**
    * Parses the result code in the specified result element.
    *
    * @param element
    *    the <code>&lt;result/&gt;</code> element, not <code>null</code>.
    *<commit_msg>Expecting CallRequest and duration in parse() methods.<commit_after>/*
 * $Id$
 */
package org.xins.client;

import java.io.StringReader;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import org.xins.common.MandatoryArgumentChecker;

import org.xins.common.collections.PropertyReader;
import org.xins.common.collections.ProtectedPropertyReader;

import org.xins.common.service.TargetDescriptor;

import org.xins.common.text.FastStringBuffer;
import org.xins.common.text.ParseException;

/**
 * Call result parser. XML is parsed to produce a
 * {@link XINSServiceCaller.Result} object.
 *
 * @version $Revision$ $Date$
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 */
public class ResultParser extends Object {

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

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

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

   /**
    * Constructs a new <code>ResultParser</code>.
    */
   public ResultParser() {
      _xmlBuilder = new SAXBuilder();
   }


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

   /**
    * Parser that takes an XML document and converts it to a JDOM Document.
    */
   private final SAXBuilder _xmlBuilder;


   //-------------------------------------------------------------------------
   // Methods
   //-------------------------------------------------------------------------

   /**
    * Parses the given XML string to create a
    * <code>XINSServiceCaller.Result</code> object with the specified
    * <code>TargetDescriptor</code>.
    *
    * @param request
    *    the original {@link CallRequest} that was used to perform the call,
    *    cannot be <code>null</code>.
    *
    * @param target
    *    the {@link TargetDescriptor} that was used to get the XML, cannot be
    *    <code>null</code>.
    *
    * @param duration
    *    the call duration, should be &gt;= 0.
    *
    * @param xml
    *    the XML to be parsed, not <code>null</code>.
    *
    * @return
    *    the parsed result of the call, not <code>null</code>.
    *
    * @throws IllegalArgumentException
    *    if <code>request == null
    *          || target  == null
    *          || xml     == null
    *          || duration &lt; 0</code>
    *
    * @throws ParseException
    *    if the specified string is not valid XML or if it is not a valid XINS
    *    API function call result.
    */
   public XINSServiceCaller.Result parse(CallRequest      request,
                                         TargetDescriptor target,
                                         long             duration,
                                         String           xml)
   throws IllegalArgumentException, ParseException {

      // Check preconditions
      MandatoryArgumentChecker.check("request", request,
                                     "target",  target,
                                     "xml",     xml);
      if (duration < 0) {
         throw new IllegalArgumentException("duration (" + duration + ") < 0");
      }

      // Convert the String to a JDOM Document object
      StringReader reader = new StringReader(xml);
      Document document;
      try {
         document = _xmlBuilder.build(reader);
      } catch (Throwable exception) {
         String detail = exception.getMessage();
         FastStringBuffer buffer = new FastStringBuffer(250);
         buffer.append("Unable to convert the specified character string to XML");
         if (detail != null && detail.length() > 0) {
            buffer.append(": ");
            buffer.append(detail);
         } else {
            buffer.append('.');
         }
         String message = buffer.toString();
         Log.log_2005(exception, detail);
         throw new ParseException(message);
      } finally {
         reader.close();
      }

      return parse(request, target, duration, document);
   }

   /**
    * Parses the given XML <code>Document</code> to create a
    * <code>XINSServiceCaller.Result</code> object with the specified
    * <code>TargetDescriptor</code>.
    *
    * @param target
    *    the {@link TargetDescriptor} that was used to get the XML, cannot be
    *    <code>null</code>.
    *
    * @param request
    *    the original {@link CallRequest} that was used to perform the call,
    *    cannot be <code>null</code>.
    *
    * @param duration
    *    the call duration, should be &gt;= 0.
    *
    * @param document
    *    the document to be parsed, not <code>null</code>.
    *
    * @return
    *    the parsed result of the call, not <code>null</code>.
    *
    * @throws NullPointerException
    *    if <code>target == null
    *          || document == null
    *          || document.</code>{@link Document#getRootElement() getRootElement()}<code> == null</code>
    *
    * @throws IllegalArgumentException
    *    if <code>request == null || duration &lt; 0</code>.
    *
    * @throws ParseException
    *    if the specified XML document is not a valid XINS API function call
    *    result.
    */
   private XINSServiceCaller.Result parse(CallRequest      request,
                                          TargetDescriptor target,
                                          long             duration,
                                          Document         document)
   throws NullPointerException, ParseException {

      Element element = document.getRootElement();

      // Check that the root element is <result/>
      if ("result".equals(element.getName()) == false) {
         String message = "The returned XML is invalid. The type of the root element is \"" + element.getName() + "\" instead of \"result\".";
         Log.log_2006(element.getName());
         throw new ParseException(message);
      }

      String         code        = parseResultCode(element);
      PropertyReader parameters  = parseParameters(element);
      Element        dataElement = element.getChild("data");

      return new XINSServiceCaller.Result(request, target, duration, code, parameters, dataElement);
   }

   /**
    * Parses the result code in the specified result element.
    *
    * @param element
    *    the <code>&lt;result/&gt;</code> element, not <code>null</code>.
    *
{'fraction_non_alphanumeric': 0.21785481770833334, 'fraction_numerical': 0.00244140625, 'mean_word_length': 1.4916869424168695, 'pattern_counts': {'":': 2, '<': 91, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4937885', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2950, 'n_words': 851}