public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
return type.equals(StyledText.class);
}
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
StyledText st = (StyledText) source;
if (st.getStyle() != null) {
writer.addAttribute("style", st.getStyle());
}
writer.setValue(st.getText());
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
String style = reader.getAttribute("style");
String text = reader.getValue();
return new StyledText(text, style);
}
});
// this makes it so that if the type is not specified in the xml, we
// assume a string.
xstream.registerConverter(new StringConverter() {
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
return super.canConvert(type) || type.equals(Object.class);
}
}, 10);
// this compacts the representation of a hint map, which is
// otherwise excessive
xstream.registerConverter(new Converter() {
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
HintMap map = (HintMap) source;
for (Iterator<Entry<String, Float>> iterator = map.entrySet().iterator(); iterator.hasNext();) {
Entry<String, Float> entry = iterator.next();
writer.addAttribute(entry.getKey().toString(), entry.getValue().toString());
}
}
@SuppressWarnings("unchecked")
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
HintMap properties = new HintMap();
for (Iterator iterator = reader.getAttributeNames(); iterator.hasNext();) {
String n = (String) iterator.next();
String v = reader.getAttribute(n);
Float f = Float.parseFloat(v);
properties.put(n, f);
}
return properties;
}
@SuppressWarnings("unchecked")
public boolean canConvert(Class type) {
return type == HintMap.class;
}
});
// allow labels/stereotypes to appear in attributes.
xstream.registerConverter(new ReflectionConverter(xstream.getMapper(), xstream.getReflectionProvider()) {
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
return type.equals(Glyph.class) || type.equals(Arrow.class);
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
String attLabel = reader.getAttribute("label");
String attStereo = reader.getAttribute("stereotype");
Object out = super.unmarshal(reader, context);
if (out instanceof Glyph) {
if (((Glyph) out).getLabel() == null) {
((Glyph) out).setLabel(new StyledText(attLabel));
}
if (((Glyph) out).getStereotype() == null) {
((Glyph) out).setStereotype(new StyledText(attStereo));
}
} else if (out instanceof Arrow) {
if (((Arrow) out).getLabel() == null) {
((Arrow) out).setLabel(new StyledText(attLabel));
}
}
return out;
}