import javax.swing.border.LineBorder;
import java.awt.Color;
import java.beans.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class LineBorderXMLSerializationTest {

    public static class Bean {
        public final static LineBorder DEFAULT = new LineBorder(Color.RED, 2, true);
        private LineBorder value;
        public Bean() {this.value = DEFAULT;}
        public LineBorder getValue() {return value;}
        public void setValue(LineBorder value) {this.value = value;}
    }

    public static void main(String[] args) throws IOException {
        LineBorder originalBorder = new LineBorder(null, 0, false);

        Bean beanToEncode = new Bean();
        beanToEncode.setValue(originalBorder);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try (XMLEncoder encoder = new XMLEncoder(outputStream)) {
            encoder.setPersistenceDelegate(
                LineBorder.class,
                new PersistenceDelegate() {
                    @Override
                    protected Expression instantiate(Object oldInstance, Encoder out) {
                        LineBorder lb = (LineBorder) oldInstance;
                        return new Expression(
                                lb,
                                lb.getClass(),
                                "new",
                                new Object[]{lb.getLineColor(),
                                        lb.getThickness(),
                                        lb.getRoundedCorners()}
                        );
                    }
                }
            );
            encoder.writeObject(beanToEncode);
        }

        byte[] xmlData = outputStream.toByteArray();


        System.out.println(new String(xmlData, StandardCharsets.UTF_8));

        Bean deserializedBean;
        ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlData);
        try (XMLDecoder decoder = new XMLDecoder(inputStream)) {
            deserializedBean = (Bean) decoder.readObject();
        }

        LineBorder deserializedBorder = deserializedBean.getValue();
        Color originalColor = originalBorder.getLineColor();
        Color deserializedColor = deserializedBorder.getLineColor();
        if (!Objects.equals(originalColor, deserializedColor)) {
            throw new RuntimeException("Color mismatch: original=" + originalColor + ", deserialized=" + deserializedColor);
        } else {
            System.out.println("\nColors match.");
        }
    }
} 