Renames project from pz2utils4jsf to mkjsf
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / data / CommandError.java
index 016f68e..64080e3 100644 (file)
@@ -4,23 +4,31 @@ import static com.indexdata.pz2utils4jsf.utils.Utils.nl;
 \r
 import java.util.ArrayList;\r
 import java.util.List;\r
+import java.util.regex.Pattern;\r
 \r
-import com.indexdata.pz2utils4jsf.errors.ApplicationError;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorInterface;\r
 import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
 import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode;\r
 import com.indexdata.utils.XmlUtils;\r
 \r
 /**\r
- * Captures errors encountered during the execution of a command. \r
- * Is parsed by Pazpar2ResponseParser, piggybacked in a (seemingly)\r
- * regular command respond.\r
+ * Holds an error encountered during the execution of a command.\r
+ * \r
+ * An error can be received by a command thread as an exception message \r
+ * or as an error XML. In both cases the error (string or xml) will be embedded\r
+ * in a new 'applicationerror' element which in turn will be embedded in a\r
+ * command XML (i.e. a 'search' or a 'show' response XML)  \r
+ * \r
+ * The command response XML is subsequently parsed by Pazpar2ResponseParser, \r
+ * which will then create the CommandError object.\r
  * \r
  * @author Niels Erik\r
  *\r
  */\r
-public class CommandError extends Pazpar2ResponseData implements ApplicationError {\r
+public class CommandError extends Pazpar2ResponseData implements ErrorInterface {\r
 \r
   private static final long serialVersionUID = 8878776025779714122L;\r
+  private static Pattern xmlDeclaration = Pattern.compile("<\\?xml.*\\?>");\r
   private ErrorCode applicationErrorCode;\r
   private ErrorHelper errorHelper = null;\r
   \r
@@ -56,6 +64,7 @@ public class CommandError extends Pazpar2ResponseData implements ApplicationErro
   \r
   /**\r
    * Creates an XML string error message, embedded in an XML string document named by the command\r
+   * This is the XML that Pazpar2ResponseParser will turn into a CommandError object. \r
    * @param commandName\r
    * @param exceptionName\r
    * @param errorMessage\r
@@ -73,19 +82,35 @@ public class CommandError extends Pazpar2ResponseData implements ApplicationErro
     return errorXml.toString(); \r
   }\r
   \r
+  /**\r
+   * Embeds a Pazpar2 (or Pazpar2 client) error response document as a child element of\r
+   * a command response document (like 'search' or 'show').\r
+   * This is the XML that Pazpar2ResponseParser will turn into a CommandError object.\r
+   * \r
+   * \r
+   * @param commandName The name of the command during which's execution the error was encountered\r
+   * @param exceptionName The (possibly loosely defined) name of the exception that was thrown\r
+   * @param pazpar2ErrorXml The error document as created by Pazpar2, or the Service Proxy or \r
+   *                        by the Pazpar2 client itself. \r
+   * @return\r
+   */\r
   public static String insertPazpar2ErrorXml (String commandName, String exceptionName, String pazpar2ErrorXml) {\r
     StringBuilder errorXml = new StringBuilder("");\r
     errorXml.append("<" + commandName + ">"+nl);\r
     errorXml.append(" <applicationerror>"+nl);\r
     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
     errorXml.append("  <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);    \r
-    errorXml.append(pazpar2ErrorXml+nl);    \r
+    errorXml.append(xmlDeclaration.matcher(pazpar2ErrorXml).replaceAll("")+nl);    \r
     errorXml.append(" </applicationerror>"+nl);\r
     errorXml.append("</" + commandName + ">"+nl);\r
     return errorXml.toString(); \r
     \r
   }\r
-    \r
+   \r
+  /**\r
+   * Sets the object that should be used to analyze the error\r
+   *  \r
+   */\r
   public void setErrorHelper (ErrorHelper errorHelper) {\r
     this.errorHelper = errorHelper; \r
   }\r