/*
 * Copyright 2007 Wyona
 *
 *  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.wyona.org/licenses/APACHE-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.wyona.yanel.core;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;

import org.apache.log4j.Category;

/**
 * Abstraction of a resource configuration.
 */
public class ResourceConfiguration {

    private Category log = Category.getInstance(ResourceConfiguration.class);

    protected Map properties;
    protected String name;
    protected String namespace;
    Configuration config;
    
    /**
     *
     */
    public ResourceConfiguration(InputStream in) throws Exception {
        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(true);
        config = builder.build(in);
        Configuration rtiConfig = config.getChild("rti");
	name = rtiConfig.getAttribute("name");
	namespace = rtiConfig.getAttribute("namespace");
        log.debug("Universal Name: " + getUniversalName());

        // TODO: Read properties and set this.properties
    }
    
    /**
     * Create a resource from scratch
     * @param name Resource Type Name
     * @param name Resource Type Namespace
     */
    public ResourceConfiguration(String name, String namespace, Map properties) {
        this.name = name;
        this.namespace = namespace;

        if (properties == null) {
            this.properties = new HashMap();
        } else {
            this.properties = properties;
        }
    }
    
    /**
     * Get universal name of resource type
     */
    public String getUniversalName() {
	return "<{" + namespace + "}" + name + "/>";
    }
    
    /**
     * Get resource type name
     */
    public String getName() {
        return name;
    }
    
    /**
     * Get resource type namespace
     */
    public String getNamespace() {
        return namespace;
    }
    
    /**
     * @param key
     * @return value for this key or null if no value exists for this key.
     */
    public String getProperty(String key) throws Exception {
        //return (String)properties.get(key);
        Configuration[] props = config.getChildren("property");
        for (int i = 0; i < props.length; i++) {
            if (props[i].getAttribute("name") != null && props[i].getAttribute("name").equals(key)) return props[i].getAttribute("value");
        }
        return null;
    }
    
    /**
     * Check if property exists
     */
    public boolean containsKey(String key) throws Exception {
        //return properties.containsKey(key);
        Configuration[] props = config.getChildren("property");
        for (int i = 0; i < props.length; i++) {
            if (props[i].getAttribute("name") != null && props[i].getAttribute("name").equals(key)) return true;
        }
        return false;
    }
}

@@ -2699,24 +2699,58 @@
 s.get(key);%0A
+        if (config != null) %7B%0A    
         Conf
@@ -2801,32 +2801,36 @@
 erty%22);%0A        
+    
 for (int i = 0; 
@@ -2846,32 +2846,36 @@
 .length; i++) %7B%0A
+    
             if (
@@ -2993,24 +2993,38 @@
 e(%22value%22);%0A
+            %7D%0A
         %7D%0A  
@@ -3205,24 +3205,58 @@
 nsKey(key);%0A
+        if (config != null) %7B%0A    
         Conf
@@ -3307,32 +3307,36 @@
 erty%22);%0A        
+    
 for (int i = 0; 
@@ -3352,32 +3352,36 @@
 .length; i++) %7B%0A
+    
             if (
@@ -3477,16 +3477,30 @@
 n true;%0A
+            %7D%0A
         

check if config is null