CognosBIException.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2012
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. //Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  9. //Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  10. // sn_dg_sdk_exception_start_1
  11. import javax.xml.xpath.*;
  12. import org.apache.axis.AxisFault;
  13. import org.w3c.dom.Node;
  14. import org.w3c.dom.NodeList;
  15. public class CognosBIException
  16. {
  17. private AxisFault _exception = null;
  18. /**
  19. * Create a CognosBIException object.
  20. *
  21. * @param ex An AxisFault thrown by an IBM Cognos method call.
  22. */
  23. public CognosBIException(AxisFault ex)
  24. {
  25. _exception = ex;
  26. }
  27. /**
  28. * Return the exception message.
  29. *
  30. * @return The exception's message string.
  31. */
  32. public String getMessage()
  33. {
  34. return _exception.getMessage();
  35. }
  36. /**
  37. * Return the exception severity.
  38. *
  39. * @return The exception severity string.
  40. */
  41. public String getSeverity()
  42. {
  43. try
  44. {
  45. Node n =
  46. getSingleNode(
  47. "(//*[namespace-uri()=\"http://developer.cognos.com/schemas/bibus/3/\" and local-name()=\"severity\"])[1]");
  48. return new String(n.getFirstChild().getNodeValue());
  49. }
  50. catch (Exception ex)
  51. {
  52. return new String("");
  53. }
  54. }
  55. /**
  56. * Return the exception errorCode.
  57. *
  58. * @return The exception errorCode string.
  59. */
  60. public String getErrorCode()
  61. {
  62. try
  63. {
  64. Node n =
  65. getSingleNode(
  66. "(//*[namespace-uri()=\"http://developer.cognos.com/schemas/bibus/3/\" and local-name()=\"errorCode\"])[1]");
  67. return new String(n.getFirstChild().getNodeValue());
  68. }
  69. catch (Exception ex)
  70. {
  71. return new String("");
  72. }
  73. }
  74. /**
  75. * Return the exception's messageStrings.
  76. *
  77. * @return The exception messageString array of strings.
  78. */
  79. public String[] getDetails()
  80. {
  81. try
  82. {
  83. NodeList nodes =
  84. getNodeList(
  85. "//*[namespace-uri()=\"http://developer.cognos.com/schemas/bibus/3/\" and local-name()=\"messageString\"]");
  86. String retval[] = new String[nodes.getLength()];
  87. for (int idx = 0; idx < nodes.getLength(); idx++)
  88. {
  89. retval[idx] =
  90. new String(nodes.item(idx).getFirstChild().getNodeValue());
  91. }
  92. return retval;
  93. }
  94. catch (Exception ex)
  95. {
  96. return new String[] { "" };
  97. }
  98. }
  99. /**
  100. * Convert this CognosBIException into a string for printing.
  101. *
  102. * @return A string representation of the CognosBIException.
  103. */
  104. public String toString()
  105. {
  106. StringBuffer str = new StringBuffer();
  107. str.append("Message: ").append(getMessage()).append("\n");
  108. str.append("Severity: ").append(getSeverity()).append("\n");
  109. str.append("ErrorCode: ").append(getErrorCode()).append("\n");
  110. str.append("Details:\n");
  111. String details[] = getDetails();
  112. for (int i = 0; i < details.length; i++)
  113. {
  114. str.append("\t").append(details[i]).append("\n");
  115. }
  116. return str.toString();
  117. }
  118. /**
  119. * Return a Node from the exception based on supplied search path intended to return a single Node.
  120. *
  121. * @param searchString An XPath expression
  122. *
  123. * @return Node n.
  124. */
  125. public Node getSingleNode(String searchString) throws XPathFactoryConfigurationException, XPathExpressionException
  126. {
  127. XPath xpath = XPathFactory.newInstance().newXPath();
  128. XPathExpression xPathExpr = xpath.compile(searchString);
  129. Node n = (Node)xPathExpr.evaluate(_exception.getFaultDetails()[0].getParentNode(), XPathConstants.NODE);
  130. return n;
  131. }
  132. /**
  133. * Return a NodeList from the exception based on supplied search path intended to return one or more Nodes.
  134. *
  135. * @param searchString An XPath expression
  136. *
  137. * @return NodeList nl.
  138. */
  139. public NodeList getNodeList(String searchString) throws XPathFactoryConfigurationException, XPathExpressionException
  140. {
  141. XPath xpath = XPathFactory.newInstance().newXPath();
  142. XPathExpression xPathExpr = xpath.compile(searchString);
  143. NodeList nl = (NodeList)xPathExpr.evaluate(_exception.getFaultDetails()[0].getParentNode(), XPathConstants.NODESET);
  144. return nl;
  145. }
  146. /**
  147. * Convert a SoapException into a CognosBIException string.
  148. *
  149. * This is the same as creating a CognosBIException and calling
  150. * its ToString() method.
  151. *
  152. * @param ex The AxisFault to format.
  153. * @return A string representation.
  154. */
  155. static public String convertToString(AxisFault ex)
  156. {
  157. CognosBIException exception = new CognosBIException(ex);
  158. return exception.toString();
  159. }
  160. }
  161. // sn_dg_sdk_exception_end_1