類型的不自然映射到XML表示需要寫一個XmlAdapter
實施,Font
和Color
是這樣的類型。下面的代碼提供了一個如何在您的案例中編寫適配器的例子。
我將適配器類作爲嵌套類放入FontStyle
類中,但您可以根據需要將它們創建爲外部類。
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.awt.*;
@XmlRootElement
@XmlType(propOrder = {"font", "fontColor", "backgroundColor"}) // to keep ordering consistent with "example.xml"
public class FontStyle {
private Font font;
private Color fontColor = Color.BLACK;
private Color backgroundColor = Color.WHITE;
public FontStyle() {
}
public FontStyle(Font font, Color fontColor, Color backgroundColor) {
this.font = font;
this.fontColor = fontColor;
this.backgroundColor = backgroundColor;
}
@XmlJavaTypeAdapter(FontAdapter.class)
public Font getFont() {
return font;
}
@XmlJavaTypeAdapter(ColorAdapter.class)
public Color getFontColor() {
return fontColor;
}
@XmlJavaTypeAdapter(ColorAdapter.class)
public Color getBackgroundColor() {
return backgroundColor;
}
public void setFont(Font font) {
this.font = font;
}
public void setFontColor(Color fontColor) {
this.fontColor = fontColor;
}
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}
private static class ColorAdapter extends XmlAdapter<ColorAdapter.ColorValueType, Color> {
@Override
public Color unmarshal(ColorValueType v) throws Exception {
return new Color(v.red, v.green, v.blue);
}
@Override
public ColorValueType marshal(Color v) throws Exception {
return new ColorValueType(v.getRed(), v.getRed(), v.getBlue());
}
@XmlAccessorType(XmlAccessType.FIELD)
public static class ColorValueType {
private int red;
private int green;
private int blue;
public ColorValueType() {
}
public ColorValueType(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
}
}
private static class FontAdapter extends XmlAdapter<FontAdapter.FontValueType, Font> {
@Override
public Font unmarshal(FontValueType v) throws Exception {
return new Font(v.family, v.style, v.size);
}
@Override
public FontValueType marshal(Font v) throws Exception {
return new FontValueType(v.getFamily(), v.getStyle(), v.getSize());
}
@XmlAccessorType(XmlAccessType.FIELD)
public static class FontValueType {
private String family;
private int style;
private int size;
public FontValueType() {
}
public FontValueType(String family, int style, int size) {
this.family = family;
this.style = style;
this.size = size;
}
}
}
}
的代碼來解讀的example.xml和測試結果可能是這樣的:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class App {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(FontStyle.class);
// unmarshall "example.xml"
File exampleFile = new File("example.xml");
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
FontStyle fontStyle = (FontStyle) jaxbUnmarshaller.unmarshal(exampleFile);
// marshall back to XML and print the result
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); // removes xml declaration line for consistency with "example.xml" file
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(fontStyle, System.out);
}
}
只是一個建議:你可能是與字體風格的配置,不依賴更好到awt類的技術細節。例如,顏色可以通過諸如「白色」之類的名稱以及樣式來更好地指定。根據「normal」數值的整體定義,可以使用比例尺(小,小,正常,大,大,...)更好地指定尺寸。 – laune