
package org.torquebox.rack.metadata;

import java.io.IOException;
import java.util.Map;
import java.util.List;

import org.jboss.vfs.VirtualFile;
import org.yaml.snakeyaml.Yaml;


public class TorqueBoxYamlParser {
    
    public TorqueBoxYamlParser() {
    }

    public TorqueBoxYamlParser(RackApplicationMetaData metaData) {
        this.metaData = metaData;
    }

    public TorqueBoxYamlParser(RackApplicationMetaData metaData, VirtualFile root) {
        this.metaData = metaData;
        getMetaData().setRackRoot( root );
    }

    public RackApplicationMetaData parse(VirtualFile file) throws IOException {
        Yaml yaml = new Yaml();
        Map<String, Object> config = (Map<String, Object>) yaml.load(file.openStream());
        return config==null ? null : parse(config);
    }

    public RackApplicationMetaData parse(Map<String,Object> config) throws IOException {
        parseApplication( (Map<String,String>) config.get( "application" ) );
        parseWeb( (Map<String,Object>) config.get( "web" ) );
        parseEnvironment( (Map<String,String>) config.get( "environment" ) );
        return getMetaData();
    }

    public RackApplicationMetaData parseApplication(Map<String,String> application) throws IOException {
        if (application != null) {
            getMetaData().setRackRoot( getOneOf( application, "RACK_ROOT", "RAILS_ROOT", "root" ) );
            getMetaData().setRackEnv( getOneOf( application, "RACK_ENV", "RAILS_ENV", "env" ) );
            getMetaData().setRackUpScriptLocation( application.get( "rackup" ) );
        }
        return getMetaData();
    }

    public RackApplicationMetaData parseWeb(Map<String,Object> web) {
        if (web != null) {
            getMetaData().setContextPath( (String) web.get( "context" ) );
            getMetaData().setStaticPathPrefix( (String) web.get( "static" ) );
            parseHosts( web.get( "host" ) );
        }
        return getMetaData();
    }

    public RackApplicationMetaData parseEnvironment(Map<String,String> environment) {
        if (environment != null) {
            getMetaData().setEnvironmentVariables( environment );
        }
        return getMetaData();
    }

    public RackApplicationMetaData getMetaData() {
        if (this.metaData == null) {
            this.metaData = new WriteOnceRackApplicationMetaData();
        }
        return this.metaData;
    }

    protected RackApplicationMetaData parseHosts(Object hosts) {
        if (hosts instanceof List) {
            List<String> list = (List<String>) hosts;
            for (String each: list) {
                getMetaData().addHost(each);
            }
        } else {
            getMetaData().addHost( (String) hosts );
        }
        return getMetaData();
    }

    protected String getOneOf(Map<String,String> map, String... keys) {
        for (String each: keys) {
            for (String key: map.keySet()) {
                if (each.equalsIgnoreCase(key)) {
                    return map.get(key);
                }
            }
        }
        return null;
    }

    private RackApplicationMetaData metaData;
    
}
@@ -60,16 +60,44 @@
 eption;%0A
+import java.io.InputStream;%0A
 import j
@@ -633,32 +633,94 @@
 s IOException %7B%0A
+        InputStream in = file.openStream();%0A        try %7B%0A    
         Yaml yam
@@ -743,16 +743,20 @@
         
+    
 Map%3CStri
@@ -832,24 +832,28 @@
 ));%0A        
+    
 return confi
@@ -884,16 +884,70 @@
 onfig);%0A
+        %7D finally %7B%0A            in.close();%0A        %7D%0A
     %7D%0A%0A 

Clean up after myself