/*
 * $Id$
 * 
 * Copyright (c) OSGi Alliance (2002, 2006, 2007). All Rights Reserved.
 * 
 * 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 org.osgi.impl.bundle.obr.resource;

import java.lang.reflect.Array;
import java.util.*;

import org.osgi.service.obr.Capability;
import org.xmlpull.v1.XmlPullParser;



public class CapabilityImpl implements Capability {
	String				name;
	Map	properties	= new TreeMap();

	public CapabilityImpl(String name) {
		this.name = name;
	}

	public CapabilityImpl(XmlPullParser parser) throws Exception {
		parser.require(XmlPullParser.START_TAG, null, "capability");
		name = parser.getAttributeValue(null,"name");
		while ( parser.nextTag() == XmlPullParser.START_TAG ) {
			if ( parser.getName().equals("p")) {
				String name = parser.getAttributeValue(null,"n");
				String value = parser.getAttributeValue(null,"v");
				String type = parser.getAttributeValue(null,"t");
				Object v = value;

				if ( "nummeric".equals(type))
					v = new Long(value);
				else if ( "version".equals(type))
					v = new VersionRange(value);
				addProperty(name,v);
			}
			parser.next();
			parser.require(XmlPullParser.END_TAG, null, "p" );
		}
		parser.require(XmlPullParser.END_TAG, null, "capability" );
	}


	public void addProperty(String key, Object value) {
		List values = (List) properties.get(key);
		if (values == null) {
			values = new ArrayList();
			properties.put(key, values);
		}
		values.add(value);
	}

	public Tag toXML() {
		return toXML(this);
	}
	
	public static Tag toXML(Capability capability) {
		Tag tag = new Tag("capability");
		tag.addAttribute("name", capability.getName());
		Map properties = capability.getProperties();
		for ( Iterator k= properties.keySet().iterator(); k.hasNext(); ) {
			String key = (String) k.next();
			List values = (List) properties.get(key);
			for ( Iterator v = values.iterator(); v.hasNext(); ) {
				Object value = v.next();
				Tag p = new Tag("p");
				tag.addContent(p);
				p.addAttribute("n", key);
				if ( value != null ) {
					p.addAttribute("v", valueString(value));
					String type = null;
					if (value instanceof Number )
						type = "number";
					else if (value.getClass() == VersionRange.class)
						type = "version";
					else if ( value.getClass().isArray() ) {
						type = "set";
					}
					
					if (type != null)
						p.addAttribute("t", type);
				}
				else
					System.out.println("Missing value " + key);
			}
		}
		return tag;
	}


	private static String valueString(Object value) {
		if ( value.getClass().isArray() ) {
			StringBuffer buf = new StringBuffer();
			for ( int i = 0; i < Array.getLength(value); i++) {
				if ( i > 0 ) {
					buf.append( "," );
				}
				buf.append( Array.get(value, i).toString() );
			}
			return buf.toString();
		}
		else {
			return value.toString();
		}
	}

	public String getName() {
		return name;
	}


	public Map getProperties() {
		return properties;
	}

}

@@ -894,16 +894,38 @@
 me;%0A%09Map
+%3CString, List%3CObject%3E%3E
 %09propert
@@ -941,16 +941,38 @@
  TreeMap
+%3CString, List%3CObject%3E%3E
 ();%0A%0A%09pu
@@ -1849,24 +1849,32 @@
 ue) %7B%0A%09%09List
+%3CObject%3E
  values = (L
@@ -1862,39 +1862,32 @@
 Object%3E values =
- (List)
  properties.get(
@@ -1941,16 +1941,24 @@
 rrayList
+%3CObject%3E
 ();%0A%09%09%09p
@@ -2204,118 +2204,175 @@
 ;%0A%09%09
-Map properties = capability.getProperties();%0A%09%09for ( Iterator k= properties.keySet().iterator(); k.hasNex
+@SuppressWarnings(%22unchecked%22)%0A%09%09Map%3CString, List%3CObject%3E%3E properties = capability.getProperties();%0A%09%09for (Map.Entry%3CString, List%3CObject%3E%3E prop : properties.entrySe
 t()
-; 
 ) %7B%0A
@@ -2391,23 +2391,19 @@
 y = 
-(String) k.next
+prop.getKey
 ();%0A
@@ -2409,16 +2409,24 @@
 %0A%09%09%09List
+%3CObject%3E
  values 
@@ -2431,98 +2431,33 @@
 s = 
-(List) properties.get(key);%0A%09%09%09for ( Iterator v = values.iterator(); v.hasNext(); ) %7B%0A%09%09%09%09
+prop.getValue();%0A%09%09%09for (
 Obje
@@ -2469,19 +2469,19 @@
 lue 
-= v.next();
+: values) %7B
 %0A%09%09%09
@@ -3440,16 +3440,38 @@
 blic Map
+%3CString, List%3CObject%3E%3E
  getProp

fix generics warnings