public static void closeQuietly(InputSource inputSource) {
    if (inputSource == null) {
      return; // nothing to do
    }

    closeQuietly(inputSource.getCharacterStream());
    closeQuietly(inputSource.getByteStream());
  }
  public static void writeToFile(String destLoc, String fileName, String contents)
      throws IOException {

    Writer fileWriter = null;
    try {
      fileWriter = getFileWriter(destLoc, fileName);
      fileWriter.write(contents);
    } finally {
      closeQuietly(fileWriter);
    }
  }
  public static void generateClientConfigXml(
      ClientConfigList clientCfgList, String destLoc, String configFileName)
      throws CodeGenFailedException {

    Writer fileWriter = null;
    try {
      fileWriter = CodeGenUtil.getFileWriter(destLoc, configFileName);

      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      javaXmlBinder.generateClientConfigXml(clientCfgList, fileWriter);

    } catch (Exception ex) {
      throw new CodeGenFailedException("Failed to generate Client Config xml file", ex);
    } finally {
      CodeGenUtil.closeQuietly(fileWriter);
    }
  }
  public static ServiceConfig parseServiceConfig(InputStream input) throws Exception {

    ServiceConfig serviceConfig = null;

    try {
      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      serviceConfig = (ServiceConfig) javaXmlBinder.unmarshal(input, ServiceConfig.class);
    } catch (Exception ex) {
      String errMsg = "Failed to parse service config xml : " + input;
      throw new Exception(errMsg, ex);
    } finally {
      CodeGenUtil.closeQuietly(
          input); // TODO: do not close here, let the calling method close its own inputstream.
    }

    return serviceConfig;
  }
  public static ClientConfigList parseClientConfig(InputStream input) throws Exception {

    ClientConfigList clientCfgList = null;

    try {
      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      clientCfgList = (ClientConfigList) javaXmlBinder.unmarshal(input, ClientConfigList.class);
    } catch (Exception ex) {
      String errMsg = "Failed to parse client config xml : " + input;
      throw new Exception(errMsg, ex);
    } finally {
      CodeGenUtil.closeQuietly(
          input); // TODO: do not close here, let the calling method close its own inputstream.
    }

    return clientCfgList;
  }
  public static void generateSecurityPolicyXml(
      ServiceSecurityConfig securityPolicyConfig, String destLoc, String configFileName)
      throws CodeGenFailedException {

    Writer fileWriter = null;
    try {
      fileWriter = CodeGenUtil.getFileWriter(destLoc, configFileName);

      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      javaXmlBinder.generateSecurityPolicyXml(securityPolicyConfig, fileWriter);

    } catch (Exception ex) {
      throw new CodeGenFailedException("Failed to generate Security Policy Config xml file", ex);
    } finally {
      CodeGenUtil.closeQuietly(fileWriter);
    }
  }