Source: xxe/control/CommandResult.js

  1. // Must be the ordinals of Java enum CommandResult.Status
  2. /**
  3. * Command execution has succeeded.
  4. * @type {number}
  5. * @see CommandResult
  6. */
  7. export const COMMAND_RESULT_DONE = 0;
  8. /**
  9. * Command execution has been stopped because
  10. * this <em>remote command</em> is interactive.
  11. * @type {number}
  12. * @see CommandResult
  13. */
  14. export const COMMAND_RESULT_STOPPED = 1;
  15. /**
  16. * Command execution was canceled by user.
  17. * @type {number}
  18. * @see CommandResult
  19. */
  20. export const COMMAND_RESULT_CANCELED = 2;
  21. /**
  22. * Command execution has failed.
  23. * @type {number}
  24. * @see CommandResult
  25. */
  26. export const COMMAND_RESULT_FAILED = 3;
  27. /**
  28. * Result object returned by {@link Command#execute}.
  29. */
  30. export class CommandResult {
  31. /**
  32. * Constructor.
  33. *
  34. * @param status - status of the command execution
  35. * @param [value=null] - result of the command execution
  36. */
  37. constructor(status, value=null) {
  38. this._status = status;
  39. this._value = value;
  40. }
  41. /**
  42. * Get the <code>status</code> property of this command result:
  43. * the status of the command execution.
  44. *
  45. * @type {number}
  46. * @see COMMAND_RESULT_DONE
  47. * @see COMMAND_RESULT_STOPPED
  48. * @see COMMAND_RESULT_CANCELED
  49. * @see COMMAND_RESULT_FAILED
  50. */
  51. get status() {
  52. return this._status;
  53. }
  54. /**
  55. * Get the <code>value</code> property of this command result:
  56. * the result of the command execution.
  57. * May be <code>null</code>, which means: nothing useful.
  58. *
  59. * <p>How to interpret <code>value</code> depends on <code>status</code>:
  60. * <dl>
  61. * <dt><code>COMMAND_RESULT_DONE</code>
  62. * <dd>The result value, a string, or <code>null</code>.
  63. * <dt><code>COMMAND_RESULT_STOPPED</code> (<em>Remote command only.</em>)
  64. * <dd><code>null</code> or a string which normally, after parsing it,
  65. * should be displayed in a dialog box (e.g. a list of choices
  66. * letting the user specify what she/he wants).</dd>
  67. * <dt><code>COMMAND_RESULT_CANCELED</code>
  68. * <dd>Should be <code>null</code>.
  69. * <dt><code>COMMAND_RESULT_FAILED</code>
  70. * <dd>An error message or <code>null</code>.
  71. * </dl>
  72. *
  73. * @type {string}
  74. */
  75. get value() {
  76. return this._value;
  77. }
  78. /**
  79. * Returns a a string representing this result.
  80. */
  81. toString() {
  82. let s = "CommandResult.";
  83. switch (this._status) {
  84. case COMMAND_RESULT_DONE:
  85. s += "DONE";
  86. break;
  87. case COMMAND_RESULT_STOPPED:
  88. s += "STOPPED";
  89. break;
  90. case COMMAND_RESULT_CANCELED:
  91. s += "CANCELED";
  92. break;
  93. case COMMAND_RESULT_FAILED:
  94. s += "FAILED";
  95. break;
  96. default:
  97. s += "???";
  98. break;
  99. }
  100. if (this._value !== null) {
  101. s += '(' + this._value.toString() + ')';
  102. }
  103. return s;
  104. }
  105. /**
  106. * Convenience method: tests whether specified result is successful.
  107. *
  108. * @param {CommandResult} result - result to be tested;
  109. * may be <code>null</code> which is considered to be
  110. * a non-successful result.
  111. */
  112. static isDone(result) {
  113. return (result !== null && result._status === COMMAND_RESULT_DONE);
  114. }
  115. /**
  116. * Convenience method equivalent to returning
  117. * <code>new CommandResult(COMMAND_RESULT_FAILED,value)</code>.
  118. */
  119. static failed(value) {
  120. return new CommandResult(COMMAND_RESULT_FAILED, value);
  121. }
  122. static deserialize(data) {
  123. if (Array.isArray(data) && // null data OK.
  124. data.length === 2 &&
  125. Number.isInteger(data[0])) {
  126. switch (data[0]) {
  127. case COMMAND_RESULT_DONE:
  128. case COMMAND_RESULT_STOPPED:
  129. case COMMAND_RESULT_CANCELED:
  130. case COMMAND_RESULT_FAILED:
  131. data = new CommandResult(data[0], data[1]);
  132. break;
  133. }
  134. }
  135. return data;
  136. }
  137. static formatCommandResult(cmd, result) {
  138. return (result === null)?
  139. `command\n"${cmd}"\ncould not be executed` :
  140. `command\n"${cmd}"\nreturned\n${result}`;
  141. }
  142. }
  143. CommandResult.DONE = new CommandResult(COMMAND_RESULT_DONE);
  144. CommandResult.STOPPED = new CommandResult(COMMAND_RESULT_STOPPED);
  145. CommandResult.CANCELED = new CommandResult(COMMAND_RESULT_CANCELED);
  146. CommandResult.FAILED = new CommandResult(COMMAND_RESULT_FAILED);