/*
 * 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 universalName;
    Configuration config;
    
    /**
     *
     */
    public ResourceConfiguration(InputStream in) throws Exception {
        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(true);
        config = builder.build(in);
        Configuration rtiConfig = config.getChild("rti");
	universalName = "<{"+rtiConfig.getAttribute("namespace")+"}" + rtiConfig.getAttribute("name") + "/>";
        log.debug("Universal Name: " + universalName);

        // TODO: Read properties and set this.properties
    }
    
    /**
     * Create a resource from scratch
     */
    public ResourceConfiguration(String universalName, Map properties) {
        if (properties == null) {
            this.properties = new HashMap();
        } else {
            this.properties = properties;
        }
        this.universalName = universalName;
    }
    
    /**
     * Get universal name of resource type
     */
    public String getUniversalName() {
        return universalName;
    }
    
    /**
     * @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;
    }
}

@@ -1104,28 +1104,51 @@
  String 
-universalNam
+name;%0A    protected String namespac
 e;%0A    C
@@ -1448,29 +1448,15 @@
 );%0A%09
-universalN
+n
 ame = 
-%22%3C%7B%22+
 rtiC
@@ -1483,21 +1483,24 @@
 name
-space%22)+%22%7D%22 +
+%22);%0A%09namespace =
  rti
@@ -1528,17 +1528,15 @@
 name
-%22) + %22/%3E%22
+space%22)
 ;%0A  
@@ -1572,17 +1572,20 @@
 me: %22 + 
-u
+getU
 niversal
@@ -1588,16 +1588,18 @@
 rsalName
+()
 );%0A%0A    
@@ -1718,82 +1718,235 @@
    *
-/%0A    public ResourceConfiguration(String universalName, Map properties) %7B
+ @param name Resource Type Name%0A     * @param name Resource Type Namespace%0A     */%0A    public ResourceConfiguration(String name, String namespace, Map properties) %7B%0A        this.name = name;%0A        this.namespace = namespace;%0A
 %0A   
@@ -2094,52 +2094,8 @@
   %7D%0A
-        this.universalName = universalName;%0A
     
@@ -2203,35 +2203,282 @@
 ) %7B%0A
-        return universalNam
+%09return %22%3C%7B%22 + namespace + %22%7D%22 + name + %22/%3E%22;%0A    %7D%0A    %0A    /**%0A     * Get resource type name%0A     */%0A    public String getName() %7B%0A        return name;%0A    %7D%0A    %0A    /**%0A     * Get resource type namespace%0A     */%0A    public String getNamespace() %7B%0A        return namespac
 e;%0A 

use name and namespace instead universal name directly