/**
   * 格式化xml
   *
   * @param xml
   * @return
   */
  public static String formatXML(String xml) {

    SAXReader reader = null;
    XMLWriter writer = null;
    StringWriter stringWriter = null;
    try {
      reader = new SAXReader();
      org.dom4j.Document document = reader.read(new StringReader(xml));
      if (document != null) {
        stringWriter = new StringWriter();
        OutputFormat format = new OutputFormat("    ", false);
        format.setSuppressDeclaration(true);
        writer = new XMLWriter(stringWriter, format);
        writer.write(document);
        writer.flush();
      }
      return stringWriter.getBuffer().toString();
    } catch (Exception e) {
      LOG.error("格式化xml失败", e);
    } finally {
      try {
        writer.close();
        stringWriter.close();
      } catch (Exception e) {
        // ignore
      }
    }
    return xml;
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static String executeXml(
      RepositoryFile file,
      HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse,
      IPentahoSession userSession)
      throws Exception {
    try {
      HttpSessionParameterProvider sessionParameters =
          new HttpSessionParameterProvider(userSession);
      HttpRequestParameterProvider requestParameters =
          new HttpRequestParameterProvider(httpServletRequest);
      Map parameterProviders = new HashMap();
      parameterProviders.put("request", requestParameters); // $NON-NLS-1$
      parameterProviders.put("session", sessionParameters); // $NON-NLS-1$
      List messages = new ArrayList();
      IParameterProvider requestParams = new HttpRequestParameterProvider(httpServletRequest);
      httpServletResponse.setContentType("text/xml"); // $NON-NLS-1$
      httpServletResponse.setCharacterEncoding(LocaleHelper.getSystemEncoding());
      boolean forcePrompt =
          "true"
              .equalsIgnoreCase(
                  requestParams.getStringParameter(
                      "prompt", "false")); // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$

      OutputStream contentStream = new ByteArrayOutputStream();
      SimpleOutputHandler outputHandler = new SimpleOutputHandler(contentStream, false);
      IRuntimeContext runtime = null;
      try {
        runtime =
            executeInternal(
                file,
                requestParams,
                httpServletRequest,
                outputHandler,
                parameterProviders,
                userSession,
                forcePrompt,
                messages);
        Document responseDoc =
            SoapHelper.createSoapResponseDocument(runtime, outputHandler, contentStream, messages);
        OutputFormat format = OutputFormat.createCompactFormat();
        format.setSuppressDeclaration(true);
        format.setEncoding("utf-8"); // $NON-NLS-1$
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(outputStream, format);
        writer.write(responseDoc);
        writer.flush();
        return outputStream.toString("utf-8"); // $NON-NLS-1$
      } finally {
        if (runtime != null) {
          runtime.dispose();
        }
      }
    } catch (Exception e) {
      logger.warn(
          Messages.getInstance().getString("XactionUtil.XML_OUTPUT_NOT_SUPPORTED")); // $NON-NLS-1$
      throw e;
    }
  }
  public XMLNotificationWriter(
      Writer out,
      String encoding,
      String boundary,
      Thread workingThread,
      boolean compact,
      boolean supressDecl) {
    this.out = out;
    this.boundary = boundary;
    this.workingThread = workingThread;

    OutputFormat format =
        compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint();
    format.setSuppressDeclaration(supressDecl);
    format.setEncoding(encoding);
    format.setExpandEmptyElements(!compact);
    xmlWriter = new XMLWriter(out, format);
    xStream = new XStream();
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static String doParameter(
      final RepositoryFile file,
      IParameterProvider parameterProvider,
      final IPentahoSession userSession)
      throws IOException {
    ActionSequenceJCRHelper helper = new ActionSequenceJCRHelper();
    final IActionSequence actionSequence =
        helper.getActionSequence(
            file.getPath(), PentahoSystem.loggingLevel, RepositoryFilePermission.READ);
    final Document document = DocumentHelper.createDocument();
    try {
      final Element parametersElement = document.addElement("parameters");

      // noinspection unchecked
      final Map<String, IActionParameter> params =
          actionSequence.getInputDefinitionsForParameterProvider(IParameterProvider.SCOPE_REQUEST);
      for (final Map.Entry<String, IActionParameter> entry : params.entrySet()) {
        final String paramName = entry.getKey();
        final IActionParameter paramDef = entry.getValue();
        final String value = paramDef.getStringValue();
        final Class type;
        // yes, the actual type-code uses equals-ignore-case and thus allows the user
        // to specify type information in a random case. sTrInG is equal to STRING is equal to the
        // value
        // defined as constant (string)
        if (IActionParameter.TYPE_LIST.equalsIgnoreCase(paramDef.getType())) {
          type = String[].class;
        } else {
          type = String.class;
        }
        final String label = paramDef.getSelectionDisplayName();

        final String[] values;
        if (StringUtils.isEmpty(value)) {
          values = new String[0];
        } else {
          values = new String[] {value};
        }

        createParameterElement(
            parametersElement, paramName, type, label, "user", "parameters", values);
      }

      createParameterElement(
          parametersElement,
          "path",
          String.class,
          null,
          "system",
          "system",
          new String[] {file.getPath()});
      createParameterElement(
          parametersElement,
          "prompt",
          String.class,
          null,
          "system",
          "system",
          new String[] {"yes", "no"});
      createParameterElement(
          parametersElement,
          "instance-id",
          String.class,
          null,
          "system",
          "system",
          new String[] {parameterProvider.getStringParameter("instance-id", null)});
      // no close, as far as I know tomcat does not like it that much ..
      OutputFormat format = OutputFormat.createCompactFormat();
      format.setSuppressDeclaration(true);
      format.setEncoding("utf-8"); // $NON-NLS-1$
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      XMLWriter writer = new XMLWriter(outputStream, format);
      writer.write(document);
      writer.flush();
      return outputStream.toString("utf-8");
    } catch (Exception e) {
      logger.warn(Messages.getInstance().getString("HttpWebService.ERROR_0003_UNEXPECTED"), e);
      return null;
    }
  }