SdkPojo unmarshall()

in core/protocols/aws-xml-protocol/src/main/java/software/amazon/awssdk/protocols/xml/internal/unmarshall/XmlProtocolUnmarshaller.java [81:138]


    SdkPojo unmarshall(XmlUnmarshallerContext context, SdkPojo sdkPojo, XmlElement root) {
        for (SdkField<?> field : sdkPojo.sdkFields()) {
            XmlUnmarshaller<Object> unmarshaller = REGISTRY.getUnmarshaller(field.location(), field.marshallingType());

            if (field.location() != MarshallLocation.PAYLOAD) {
                Object unmarshalled = unmarshaller.unmarshall(context, null, (SdkField<Object>) field);
                field.set(sdkPojo, unmarshalled);
                continue;
            }

            if (isExplicitPayloadMember(field)) {
                InputStream content = context.response().content().orElse(null);
                if (field.marshallingType() == MarshallingType.SDK_BYTES) {
                    SdkBytes value = content == null ? SdkBytes.fromByteArrayUnsafe(new byte[0])
                                                     : SdkBytes.fromInputStream(content);
                    field.set(sdkPojo, value);
                    continue;
                }
                if (field.marshallingType() == MarshallingType.STRING) {
                    // TODO: If we ever break protected APIs, just parse this as a string and remove XML-wrapping
                    // compatibility for S3.
                    if (content == null) {
                        field.set(sdkPojo, "");
                    } else {
                        setExplicitStringPayload(unmarshaller, context, sdkPojo, root, field);
                    }
                    continue;
                }
                if (root != null && !isAttribute(field)) {
                    Object unmarshalled = unmarshaller.unmarshall(context, singletonList(root), (SdkField<Object>) field);
                    field.set(sdkPojo, unmarshalled);
                    continue;
                }
            }

            if (root == null) {
                continue;
            }

            if (isAttribute(field)) {
                root.getOptionalAttributeByName(field.unmarshallLocationName())
                    .ifPresent(e -> field.set(sdkPojo, e));
                continue;
            }

            List<XmlElement> element = root.getElementsByName(field.unmarshallLocationName());
            if (!CollectionUtils.isNullOrEmpty(element)) {
                Object unmarshalled = unmarshaller.unmarshall(context, element, (SdkField<Object>) field);
                field.set(sdkPojo, unmarshalled);
            }
        }

        if (!(sdkPojo instanceof Buildable)) {
            throw new RuntimeException("The sdkPojo passed to the unmarshaller is not buildable (must implement "
                                       + "Buildable)");
        }
        return (SdkPojo) ((Buildable) sdkPojo).build();
    }