2012-12-11 15 views
8

Dati i seguenti esempio xsd snippet:JAXB xjc: come generare codice per le stringhe che restituisce vuoto se il valore è nullo?

< xs:attribute name="SEGMENT" default="" use="optional" type="xs:string"/ > 

quando xjc genera la classe che contiene l'attributo SEGMENT di fagioli, il seguente getter è generato automaticamente:

public String getSEGMENT() { 
    if (segment == null) { 
     return ""; 
    } else { 
     return segment; 
    } 
} 

mia domanda è come si fa a farlo fare lo stesso per gli oggetti xs:element? In altre parole, dati i seguenti xsd snippet:

< xs:element name="NAME" default="" type="xs:string"/ > 

voglio sapere se posso ottenere xjc per generare il seguente:

public String getNAME() { 
    if (name == null) { 
     return ""; 
    } else { 
     return name; 
    } 
} 

Come si può fare?

risposta

2

JAXB non genera lo stesso codice per un elemento con valore di default, come fa per un attributo con valore di default, perché il XML schema differentiates between element and attribute defaults:

Default values of both attributes and elements are declared using the default attribute, although this attribute has a slightly different consequence in each case. When an attribute is declared with a default value, the value of the attribute is whatever value appears as the attribute's value in an instance document; if the attribute does not appear in the instance document, the schema processor provides the attribute with a value equal to that of the default attribute. Note that default values for attributes only make sense if the attributes themselves are optional, and so it is an error to specify both a default value and anything other than a value of optional for use.

The schema processor treats defaulted elements slightly differently. When an element is declared with a default value, the value of the element is whatever value appears as the element's content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.

Si può sempre contare sul valore predefinito per un attributo mancante (da qui il getter speciale) ma c'è un problema con un valore di elemento mancante.

Tuttavia, quando si cancella un'istanza, l'Unmarshaller sa come gestire il valore predefinito. Vedi qui per maggiori dettagli:

XJC non sarà aggiungere il codice getter o inizializzare i campi con il valore di default, quindi se avete bisogno del "controllo di sicurezza nullo" si possibile aggiungere manualmente da soli dopo che il codice è generato da XJC o cercare di utilizzare alcuni plugin per farlo automaticamente:

+0

Un inizializzatore di campo sarebbe sicuramente appropriato. La domanda ora è Come ottengo xjc per aggiungere l'inizializzatore String vuoto a ogni stringa in ogni classe? Forse in un file di binding separato? –

+0

@java luva: ho aggiunto altri dettagli alla mia risposta e ho trovato anche alcuni plugin che sembrano interessanti. Vedi se aiuta. – Bogdan

+0

Grazie per le informazioni aggiuntive. Sto avendo un problema però. I collegamenti che hai inviato implicano la creazione di un compito di esperto o di formica. Non uso Maven. E il problema durante l'impostazione dell'attività della formica è che sto ricevendo un errore di collegamento che indica che diverse classi vengono caricate con lo stesso nome. Il problema, credo, è che java 1.6 ha incorporato xjc e aggiungendo jaxb-xjc-2.1.9.jar al mio classpath xdep taskdef, sono in conflitto. La mia domanda è: come si può ottenere lo script ant per ignorare 1.6 (il mio JAVA_HOME è impostato su quello) e usare il mio classpath taskdef, o fare riferimento al jar 1.6 xjc invece nel taskdef? –

Problemi correlati