View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor;
2   
3   import java.lang.reflect.Field;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.Map;
7   import java.util.Set;
8   
9   /**
10   * General descriptor is responsible for all items which are not by default handled by the item translator.
11   * Those are usualy user-defined items thus the user can either create its own descriptor or just take this description
12   * which contains only the mapping obtained directly from the ITCMessage.
13   *
14   * @author Ondrej
15   */
16  public class GeneralDescriptor extends ItemDescriptor {
17  	
18  	private Map<String, Object> attributes = new HashMap<String, Object>();
19  
20  	GeneralDescriptor(Object configMsg) {
21  		for (Field field : configMsg.getClass().getDeclaredFields()) {
22  			field.setAccessible(true);
23  			try {
24  				attributes.put(field.getName(), field.get(configMsg));
25  			} catch (IllegalArgumentException e) {
26  			} catch (IllegalAccessException e) {
27  			} finally {
28  				field.setAccessible(false);
29  			}
30  		}		
31  	}
32  
33  	public boolean contains(String key) {
34  		return attributes.containsKey(key);
35  	}
36  	
37  	public Object get(String key) {
38  		return attributes.get(key);
39  	}
40  
41  	@Override
42  	public String toString() {
43  		Set<String> keys = attributes.keySet();
44  		Iterator<String> iterator = keys.iterator();
45  		String result = "", actual;
46  		while (iterator.hasNext()) {
47  			actual = iterator.next();
48  			result += actual + ": " + attributes.get(actual) + "\n";
49  		}
50  		return result;
51  	}
52  }
53