anyType 태그는 SDO API에서 기타 다른 복합 유형과 비슷하게 처리됩니다.
anyType과 기타 복합 유형 간의 유일한 차이점은 인스턴스 데이터 및 직렬화/직렬화 해제(비즈니스 오브젝트에만 해당되는 내부 개념), 그리고 데이터와 필드 간의 맵핑 방향이 유효한지 여부를 판별하는 데만 있습니다. 복합 유형에는 고객, 주소 등의 단일 유형만 포함됩니다. 그러나 anyType에는 유형에 관계 없이 모든 DataObject가 포함됩니다. maxOccurs > 1인 경우 목록의 각 DataObject 유형이 다를 수 있습니다.
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://AnyType"> <xsd:complexType name="AnyType"> <xsd:sequence> <xsd:element name="person" type="xsd:anyType"/> </xsd:sequence> </xsd:complexType> </xsd:schema> <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://Customer"> <xsd:complexType name="Customer"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://Employee" targetNamespace="http://Employee"> <xsd:complexType name="Employee"> <xsd:sequence> <xsd:element name="id" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> DataObject anyType = ... DataObject customer = ... DataObject employee = ... // Set the person to a Customer anyType.set("person", customer); // The instance data will be a Customer // Displays "Customer" System.out.println(anyType.getDataObject("person").getName()); // Set the person to an Employee anyType.set("person", employee); // The instance data will be an Employee // Displays "Employee" System.out.println(anyType.getDataObject("person").getName());
anySimpleType과 유사하게 anyType은 직렬화 중에 xsi:type을 사용하여 직렬화 해제 시 의도한 DataObject 유형이 유지보수되도록 합니다. 따라서 유형을 "Customer"로 설정하는 경우 XML은 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8"?> <p:AnyType xsi:type="p:AnyType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:customer="http://Customer" xmlns:p="http://AnyType"> <person xsi:type="customer:Customer"> <name>foo</name> </person> </p:AnyType>
유형을 "Employee"로 설정하는 경우는 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8"?> <p:AnyType xsi:type="p:AnyType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:employee="http://Employee" xmlns:p="http://AnyType"> <person xsi:type="employee:Employee"> <id>foo</id> </person> </p:AnyType>
AnyType을 사용하면 랩퍼 DataObject를 통해 단순 유형 값을 설정할 수도 있습니다. 이러한 랩퍼 DataObject에는 단순 유형 값을 보유한 "value"(요소)라는 단일 특성이 있습니다. get<Type>/set<Type> API 사용 시 이들 단순 유형 및 랩퍼 DataObject를 자동으로 랩핑하거나 랩핑 해제하도록 SDO API가 대체되었습니다. 비유형 캐스팅 get/set API는 이러한 랩핑을 수행하지 않습니다.
DataObject anyType = ... // Calling a set<Type> API on an anyType Property causes automatic // creation of a wrapper DataObject anyType.setString("person", "foo"); // The regular get/set APIs are not overridden, so they will return // the wrapper DataObject DataObject wrapped = anyType.get("person"); // The wrapped DataObject will have the "value" Property // Displays "foo" System.out.println(wrapped.getString("value")); // The get<Type> API will automatically unwrap the DataObject // Displays "foo" System.out.println(anyType.getString("person"));
랩퍼 DataObject를 직렬화하는 경우 xsi:type 필드의 XSD 유형에 대한 Java™ 인스턴스 클래스의 anySimpleType 맵핑과 유사하게 직렬화됩니다. 따라서 이 설정은 다음과 같이 직렬화됩니다.
<?xml version="1.0" encoding="UTF-8"?> <p:AnyType xsi:type="p:AnyType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" http://www.w3.org/2001/XMLSchema" xmlns:p="http://AnyType"> <person xsi:type="xsd:string">foo</person> </p:AnyType>
xsi:type이 주어지지 않거나 잘못된 xsi:type이 주어진 경우에는 예외가 처리됩니다. 자동 랩핑 이외에도 BOFactory createDataTypeWrapper(유형, 오브젝트)를 통해 set() API에서 사용하도록 랩퍼를 수동으로 작성할 수 있습니다. 여기서, 유형은 랩핑할 데이터의 SDO 단순 유형이고 오브젝트는 랩핑할 데이터입니다.
Type stringType = boType.getType("http://www.w3.org/2001/XMLSchema", "string"); DataObject stringType = boFactory.createByMessage(stringType, "foo");
DataObject가 랩퍼 유형인지 판별하기 위해 BOType isDataTypeWrapper(유형)를 호출할 수 있습니다.
DataObject stringType = ... boolean isWrapper = boType.isDataTypeWrapper(stringType.getType());
기타 복합 유형의 경우 한 필드에서 다른 필드로 데이터를 이동시키려면 데이터 유형이 동일해야 합니다. AnyType에는 모든 복합 유형이 포함될 수 있으나, 이 때문에 필드의 인스턴스 데이터에 따라 맵핑하지 않고도 바로 이동할 수 있는지 여부가 결정됩니다.
특성 유형의 URI 및 이름을 사용하여 특성 유형이 anyType인지 판별할 수 있습니다. URI는 "commonj.sdo"이고 이름은 "DataObject"입니다. 모든 데이터를 anyType에 삽입할 수 있습니다. 이로 인해 다음과 같은 맵핑 규칙이 적용됩니다.