/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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 org.wso2.carbon.uuf.internal.deployment.parser;

import org.wso2.carbon.uuf.api.reference.FileReference;
import org.wso2.carbon.uuf.exception.MalformedConfigurationException;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

/**
 * Parser for YAML configuration files in an UUF apps, themes, and components.
 *
 * @since 1.0.0
 */
public class YamlFileParser {

    /**
     * Parses the given YAML configuration file and de-serialize the content into given bean type.
     *
     * @param configFile reference to the YAML configuration file
     * @param type       class of the bean to be used when de-serializing
     * @param <T>        type of the bean class to be used when de-serializing
     * @return returns the populated bean instance
     * @throws MalformedConfigurationException if cannot read or parse the content of the specified YAML file
     */
    public static <T> T parse(FileReference configFile, Class<T> type) throws MalformedConfigurationException {
        T loadedBean;
        try {
            loadedBean = new Yaml(new CustomClassLoaderConstructor()).loadAs(configFile.getContent(), type);
        } catch (Exception e) {
            throw new MalformedConfigurationException(
                    "Cannot parse the configuration file '" + configFile.getAbsolutePath() + "'.", e);
        }
        if (loadedBean == null) {
            // Either configuration file is empty or has oly comments.
            throw new MalformedConfigurationException(
                    "Cannot parse the configuration file '" + configFile.getAbsolutePath() + "' as it is empty.");
        }
        return loadedBean;
    }

    private static class CustomClassLoaderConstructor extends Constructor {

        private final ClassLoader classLoader;

        public CustomClassLoaderConstructor() {
            super(Object.class);
            this.classLoader = CustomClassLoaderConstructor.class.getClassLoader();
        }

        @Override
        protected Class<?> getClassForName(String name) throws ClassNotFoundException {
            return Class.forName(name, true, classLoader);
        }
    }
}

@@ -778,16 +778,138 @@
 erence;%0A
+import org.wso2.carbon.uuf.exception.ConfigurationException;%0Aimport org.wso2.carbon.uuf.exception.FileOperationException;%0A
 import o
@@ -1319,22 +1319,20 @@
  @param 
-config
+yaml
 File ref
@@ -1584,33 +1584,24 @@
   * @throws 
-Malformed
 Configuratio
@@ -1726,22 +1726,20 @@
 ference 
-config
+yaml
 File, Cl
@@ -1758,25 +1758,16 @@
  throws 
-Malformed
 Configur
@@ -1900,36 +1900,225 @@
 dAs(
-configFile.getContent(), typ
+yamlFile.getContent(), type);%0A        %7D catch (FileOperationException e) %7B%0A            throw new ConfigurationException(%0A                    %22Cannot read the configuration file '%22 + yamlFile.getAbsolutePath() + %22'.%22, 
 e);%0A
@@ -2258,38 +2258,36 @@
 ation file '%22 + 
-config
+yaml
 File.getAbsolute
@@ -2407,16 +2407,17 @@
 or has o
+n
 ly comme
@@ -2496,37 +2496,36 @@
         %22Cannot 
-parse
+load
  the configurati
@@ -2537,22 +2537,20 @@
 le '%22 + 
-config
+yaml
 File.get

improve error handling in YamlFileParser class