1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
public class Root
{
private String name;
private int junk;
private String surname;
@XmlJavaTypeAdapter(MapAdapter.class)
private Map<String, Boolean> lights;
public Root()
{
name = "";
junk = 0;
lights = new HashMap<String, Boolean>();
surname = "";
}
public void setName(String newName) { name = newName; }
public String getName() { return name; }
public void setJunk(int newJunk) { junk = newJunk; }
public int getJunk() { return junk; }
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void turnLightOn(String lightName) { lights.put(lightName, true); }
public void turnLightOff(String lightName) { lights.put(lightName, false); }
}
class MapAdapter extends XmlAdapter<MapElements[], Map<String, Boolean>>
{
public MapElements[] marshal(Map<String, Boolean> arg0) throws Exception
{
MapElements[] mapElements = new MapElements[arg0.size()];
int i = 0;
for (Map.Entry<String, Boolean> entry : arg0.entrySet())
mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());
return mapElements;
}
public Map<String, Boolean> unmarshal(MapElements[] arg0) throws Exception
{
Map<String,Boolean> r = new HashMap<String,Boolean>();
for(MapElements mapelement : arg0)
r.put(mapelement.key, mapelement.value);
return r;
}
}
class MapElements
{
@XmlElement public String key;
@XmlElement public Boolean value;
private MapElements() {} //Required by JAXB
public MapElements(String key, Boolean value)
{
this.key = key;
this.value = value;
}
} |