1 package com.thoughtworks.xstream.converters.extended; 2 3 import com.thoughtworks.xstream.converters.Converter; 4 import com.thoughtworks.xstream.converters.MarshallingContext; 5 import com.thoughtworks.xstream.converters.UnmarshallingContext; 6 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 7 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 8 9 import javax.swing.plaf.FontUIResource ; 10 import java.awt.*; 11 import java.util.Map ; 12 13 public class FontConverter implements Converter { 14 15 public boolean canConvert(Class type) { 16 return type.getName().equals("java.awt.Font") || type.getName().equals("javax.swing.plaf.FontUIResource"); 19 } 20 21 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 22 Font font = (Font) source; 23 Map attributes = font.getAttributes(); 24 writer.startNode("attributes"); context.convertAnother(attributes); 26 writer.endNode(); } 28 29 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 30 reader.moveDown(); Map attributes = (Map ) context.convertAnother(null, Map .class); 32 reader.moveUp(); Font font = Font.getFont(attributes); 34 if (context.getRequiredType() == FontUIResource .class) { 35 return new FontUIResource (font); 36 } else { 37 return font; 38 } 39 } 40 } 41 | Popular Tags |