2012-12-11 12 views
8

Biorąc pod poniższym przykładzie xsd snippet:JAXB xjc: Jak wygenerować kod dla ciągów, który zwraca pusty, jeśli wartość jest zerowa?

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

gdy XJC generuje klasę zawierającą atrybut fasoli SEGMENT następujące getter jest generowane automatycznie:

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

Moje pytanie brzmi: jak masz go zrób to samo dla obiektów xs:element? Innymi słowy, biorąc pod uwagę następujące urywek xsd:

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

Chcę wiedzieć, czy mogę dostać XJC generować następujące:

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

Jak można to zrobić?

Odpowiedz

2

JAXB nie generuje ten sam kod dla elementu z domyślnej wartości jak dla atrybutu o wartości domyślnej, ponieważ 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.

Zawsze można liczyć na wartość domyślną dla brakującego atrybutu (stąd specjalny getter), ale jest haczyk z brakującą wartością elementu.

Mimo to, gdy odrzucasz instancję, unmarshaller wie, jak postępować z wartością domyślną. Zobacz tutaj szczegóły:

XJC nie będzie dodać kod getter lub zainicjować pola z wartością domyślną, więc jeśli potrzebujesz „bezpieczną kontrolę null” ci można też dodać go samodzielnie ręcznie po kod jest generowany przez XJC lub spróbować użyć jakiś plugin to zrobić automatycznie:

+0

initializer pole pewno byłoby właściwe. Pytanie brzmi teraz: Jak uzyskać xjc, aby dodać pusty inicjator String do każdego ciągu w każdej klasie? Być może w osobnym pliku powiązań? –

+0

@java luva: Dodałem trochę więcej szczegółów do mojej odpowiedzi, a także znalazłem kilka wtyczek, które wyglądają interesująco. Sprawdź, czy to pomaga. – Bogdan

+0

Dzięki za dodatkowe informacje. Mam jednak jeden problem. Linki, które wysłałeś, wymagają skonfigurowania zadania maven lub mrówki. Nie używam maven. Problemem podczas ustawiania zadania ant jest błąd Linkage stwierdzający, że różne klasy są ładowane z tą samą nazwą. Problem, jak sądzę, polega na tym, że java 1.6 ma wbudowany xjc, a dodanie jaxb-xjc-2.1.9.jar do mojej ścieżki klasy xjc taskdef powoduje konflikt. Moje pytanie brzmi: w jaki sposób można uzyskać skrypt ant do zignorowania 1.6 (mój JAVA_HOME jest ustawiony na to) i użyć mojej ścieżki klasy taskdef, lub odwołać się do słoika 1.6 xjc zamiast w taskdef? –

Powiązane problemy