content: /*
   * Copyright (c) 2024, Alibaba Cloud;
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.aliyun.dataworks.migrationx.domain.dataworks.datago;
  
  import org.apache.velocity.Template;
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.app.VelocityEngine;
  
  import java.io.StringWriter;
  import java.util.Map;
  
  /**
   * @author qiwei.hqw
   * @version 1.0.0
   * @description
   * @createTime 2020-04-29
   */
  public class VelocityUtil {
      /**
       * render
       *
       * @param templatePath
       * @param params
       * @return
       */
      public static String render(String templatePath, Map<String, Object> params) throws Exception {
          VelocityEngine engine = new VelocityEngine();
          engine.setProperty(
              "file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
          Template template = engine.getTemplate(templatePath, "utf-8");
          StringWriter stringWriter = new StringWriter();
          template.merge(convertVelocityContext(params), stringWriter);
          return stringWriter.getBuffer().toString();
      }
  
      private static VelocityContext convertVelocityContext(Map<String, Object> map) {
          VelocityContext context = new VelocityContext();
          if (map == null) {
              return context;
          }
          for (Map.Entry<String, Object> entry : map.entrySet()) {
              context.put(entry.getKey(), entry.getValue());
          }
          return context;
      }
  }
  