2013-05-14 2 views
1

Color를 XML로 마샬링/비 정렬하려고합니다. JAXB 프로젝트에는 XmlJavaTypeAdapter https://jaxb.java.net/guide/XML_layout_and_in_memory_data_layout.html을 통해이 정확한 작업을 수행하는 예제 코드가 있습니다.JAXB XmlJavaTypeAdapter unmarshal 메서드가 호출되지 않습니다.

마샬링 잘 작동하고 출력 내가 기대하는 것입니다 : 비 정렬 화 방법에 이의를 XML에서 이동하려고 할 때

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<beanWithColor> 
    <foreground>#ff0000ff</foreground> 
    <name>CleverName</name> 
</beanWithColor> 

는하지만, 호출되지 않습니다. 왜 누군가는 통찰력을 제공 할 수 있습니까? 그것은 비 정렬 화 전경색가 null 내가 호출되고 있지 않습니다 비 정렬 화 내 디버거를 확인한 후 :

BeanWithColor{foreground=null, name='CleverName'} 

SCCE :

@XmlRootElement 
public class BeanWithColor { 
private String name; 
private Color foreground; 

public BeanWithColor() { 

} 

public BeanWithColor(Color foreground, String name) { 
    this.foreground = foreground; 
    this.name = name; 
} 

@XmlJavaTypeAdapter(ColorAdapter.class) 
public Color getForeground() { 
    return this.foreground; 
} 

public void setForeground(Color foreground) { 
    this.foreground = foreground; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

@Override 
public String toString() { 
    final StringBuilder sb = new StringBuilder("BeanWithColor{"); 
    sb.append("foreground=").append(foreground); 
    sb.append(", name='").append(name).append('\''); 
    sb.append('}'); 
    return sb.toString(); 
} 

public static void main(String[] args) { 
    BeanWithColor bean = new BeanWithColor(Color.blue, "CleverName"); 

    try { 
     StringWriter writer = new StringWriter(1000); 
     JAXBContext context = JAXBContext.newInstance(BeanWithColor.class); 
     final Marshaller marshaller = context.createMarshaller(); 
     marshaller.marshal(bean, writer); 
     System.out.println("Marshaled XML: " + writer); 

     final Unmarshaller unmarshaller = context.createUnmarshaller(); 
     BeanWithColor beanWithColor = (BeanWithColor) unmarshaller.unmarshal(new StringReader(writer.toString())); 
     System.out.println("beanWithColor = " + beanWithColor); 
    } catch (JAXBException e) { 
     e.printStackTrace(); 
    } 

} 

static class ColorAdapter extends XmlAdapter<String, Color> { 
    public Color unmarshal(String s) { 
     return Color.decode(s); 
    } 

    public String marshal(Color color) { 
     return '#' + Integer.toHexString(color.getRGB()); 
    } 
} 
} 

답변

2

내가 XmlAdapterunmarshal에 대한하지만 것을 호출되고 있다고 의심 Color.decode 메서드가 실패합니다 (코드를 디버깅 할 때 발생하는 현상입니다). 다음에

Color.decode("#ff0000ff"); 

결과 :

Exception in thread "main" java.lang.NumberFormatException: For input string: "ff0000ff" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
    at java.lang.Integer.parseInt(Integer.java:461) 
    at java.lang.Integer.valueOf(Integer.java:528) 
    at java.lang.Integer.decode(Integer.java:958) 
    at java.awt.Color.decode(Color.java:707) 

당신은 모든 실패에 후크를 얻기 위해 UnmarshallerValidationEventHandler을 설정할 수 있습니다. 기본적으로 JAXB impl은 그 문제를보고하지 않습니다.

수정

ColorAdapter.marshal 방법은 정확한 값을 리턴하도록 수정 될 필요가있다.

String rgb = Integer.toHexString(color.getRGB()); 
return "#" + rgb.substring(2, rgb.length()); 
+1

유효성 검사 처리기를 넣으라고 말한 후에 NumberFormatException을 확인하고 방금 같은 해결책으로 마무리했습니다. 0xff0000ff는 MAX_INT 이상이고 Color.decode()는 Integer.decode()를 호출합니다. – Michael

관련 문제