Red Hat Training

A Red Hat training course is available for Red Hat Fuse

35.4. Complex Types에서 복합 유형 파생

35.4.1. 개요

XML 스키마를 사용하면 복잡한 요소를 사용하여 다른 복잡한 유형을 확장하거나 제한하여 새 복합 형식을 파생할 수 있습니다.Using XML Schema, you can derive new complex types by either extending or restricting other complex types using the complexContent element. 파생된 복잡한 유형을 나타내는 Java 클래스를 생성할 때 Apache CXF는 기본 유형의 클래스를 확장합니다. 이러한 방식으로 생성된 Java 코드는 XML 스키마에서 의도한 상속 계층을 유지합니다.

35.4.2. 스키마 구문

complexContent 요소 및 확장 요소 또는 제한 요소를 사용하여 다른 복잡한 형식에서 복잡한 형식을 파생시킵니다. complexContent 요소는 포함된 데이터 설명에 둘 이상의 필드가 포함되도록 지정합니다. 확장 요소 및 복합Content 요소의 자식인 제한 요소는 새 유형을 생성하도록 수정되는 기본 유형을 지정합니다. 기본 유형은 base 특성으로 지정됩니다.

35.4.3. 복잡한 유형 확장

복잡한 유형을 확장하려면 확장 요소를 사용하여 새 유형을 구성하는 추가 요소 및 특성을 정의합니다. 복잡한 유형 설명에서 허용되는 모든 요소는 새 유형의 정의의 일부로 허용될 수 있습니다. 예를 들어 새 형식에 익명 열거형을 추가하거나 choice 요소를 사용하여 새 필드 중 하나만 유효할 수 있습니다.For example, you can add an anonymous enumeration to the new type, or you can use the choice element to specify that only one of the new fields can be valid at a time.

예 35.15. “Extension으로 Complex 유형 파생” 두 가지 복잡한 유형, widgetOrderInfo 및 위젯 OrderBillInfo 를 정의하는 XML 스키마 조각을 보여줍니다. widgetOrderBillInfowidgetOrderInfo 를 확장하여 orderNumberamtDue 의 두 가지 새로운 요소를 포함합니다.

예 35.15. Extension으로 Complex 유형 파생

<complexType name="widgetOrderInfo">
  <sequence>
    <element name="amount" type="xsd:int"/>
    <element name="order_date" type="xsd:dateTime"/>
    <element name="type" type="xsd1:widgetSize"/>
    <element name="shippingAddress" type="xsd1:Address"/>
  </sequence>
  <attribute name="rush" type="xsd:boolean" use="optional" />
</complexType>
<complexType name="widgetOrderBillInfo">
  <complexContent>
    <extension base="xsd1:widgetOrderInfo">
      <sequence>
        <element name="amtDue" type="xsd:decimal"/>
        <element name="orderNumber" type="xsd:string"/>
      </sequence>
      <attribute name="paid" type="xsd:boolean"
                 default="false" />
    </extension>
  </complexContent>
</complexType>

35.4.4. 복잡한 유형 제한

복잡한 유형을 제한하려면 restriction 요소를 사용하여 기본 유형의 요소 또는 속성의 가능한 값을 제한합니다. 복잡한 유형을 제한하는 경우 기본 형식의 모든 요소와 특성을 나열해야 합니다. 각 요소에 대해 정의에 제한적인 속성을 추가할 수 있습니다. 예를 들어 요소에 maxOccurs 특성을 추가하여 발생할 수 있는 횟수를 제한할 수 있습니다.For example, you can add a maxOccurs attribute to an element to limit the number of times it can occur. 고정 특성을 사용하여 요소 중 하나 이상이 미리 설정된 값을 갖도록 할 수도 있습니다.

예 35.16. “제한으로 복잡한 유형 정의” 다른 복잡한 유형을 제한하여 복잡한 유형을 정의하는 예를 보여줍니다. 제한된 유형인 wallawallaAddress도시 요소, 상태 요소 및 zipCode 요소가 고정되기 때문에 워싱턴의 주소에만 사용할 수 있습니다.

예 35.16. 제한으로 복잡한 유형 정의

<complexType name="Address">
  <sequence>
    <element name="name" type="xsd:string"/>
    <element name="street" type="xsd:short" maxOccurs="3"/>
    <element name="city" type="xsd:string"/>
    <element name="state" type="xsd:string"/>
    <element name="zipCode" type="xsd:string"/>
  </sequence>
</complexType>
<complexType name="wallawallaAddress">
  <complexContent>
    <restriction base="xsd1:Address">
      <sequence>
        <element name="name" type="xsd:string"/>
        <element name="street" type="xsd:short"
                 maxOccurs="3"/>
        <element name="city" type="xsd:string"
                 fixed="WallaWalla"/>
        <element name="state" type="xsd:string"
                 fixed="WA" />
        <element name="zipCode" type="xsd:string"
                 fixed="99362" />
      </sequence>
    </restriction>
  </complexContent>
</complexType>

35.4.5. Java로의 매핑

모든 복잡한 형식과 마찬가지로 Apache CXF는 다른 복잡한 유형에서 파생된 복잡한 유형을 나타내는 클래스를 생성합니다. 파생 복합 형식에 대해 생성된 Java 클래스는 기본 복잡한 유형을 지원하기 위해 생성된 Java 클래스를 확장합니다. 기본 Java 클래스도 @XmlSeeAlso 주석을 포함하도록 수정되었습니다. 기본 클래스의 @XmlSee also 주석 은 기본 클래스를 확장하는 모든 클래스를 나열합니다.

확장 프로그램에서 새 복합 형식을 파생하면 생성된 클래스에 추가된 모든 요소 및 특성에 대한 멤버 변수가 포함됩니다.When the new complex type is derived by extension, the generated class will include member variables for all of the added elements and attributes. 새 멤버 변수는 다른 모든 요소와 동일한 매핑에 따라 생성됩니다.

제한으로 새 복합 형식을 파생하면 생성된 클래스에 새 멤버 변수가 없습니다.When the new complex type is derived by restriction, the generated class will have no new member variables. 생성된 클래스는 단순히 추가 기능을 제공하지 않는 쉘이 됩니다. XML 스키마에 정의된 제한이 적용되도록 전적으로 사용자에게 제공됩니다.It is entirely up to you to ensure that the restrictions defined in the XML Schema are enforced.

예를 들어 예 35.15. “Extension으로 Complex 유형 파생” 의 스키마는 두 개의 Java 클래스, 즉 widget Ord erInfo 및 위젯 BillOrderInfo가 생성됩니다. widgetOrderBillInfo 는 widget Ord erInfo에서 확장 가능하므로 widgetOrderInfo 를 확장합니다. 예 35.17. “WidgetOrderBillInfo” 위젯 OrderBillInfo에 대해 생성된 클래스를 표시합니다.

예 35.17. WidgetOrderBillInfo

@XmlType(name = "widgetOrderBillInfo", propOrder = {
    "amtDue",
    "orderNumber"
})
public class WidgetOrderBillInfo
    extends WidgetOrderInfo
{
    @XmlElement(required = true)
    protected BigDecimal amtDue;
    @XmlElement(required = true)
    protected String orderNumber;
    @XmlAttribute
    protected Boolean paid;

    public BigDecimal getAmtDue() {
        return amtDue;
    }

    public void setAmtDue(BigDecimal value) {
        this.amtDue = value;
    }

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String value) {
        this.orderNumber = value;
    }

    public boolean isPaid() {
        if (paid == null) {
            return false;
        } else {
            return paid;
        }
    }

    public void setPaid(Boolean value) {
        this.paid = value;
    }
}