package org.springframework.data.domain.jaxb;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
public abstract class SpringDataJaxb {
public static final String NAMESPACE = "http://www.springframework.org/schema/data/jaxb";
private SpringDataJaxb() {}
@XmlRootElement(name = "page-request", namespace = NAMESPACE)
@XmlAccessorType(XmlAccessType.FIELD)
public static class PageRequestDto {
@XmlAttribute int page, size;
@XmlElement(name = "order", namespace = NAMESPACE) List<OrderDto> orders = new ArrayList<>();
}
@XmlRootElement(name = "sort", namespace = NAMESPACE)
@XmlAccessorType(XmlAccessType.FIELD)
public static class SortDto {
@XmlElement(name = "order", namespace = SpringDataJaxb.NAMESPACE) List<OrderDto> orders = new ArrayList<>();
}
@XmlRootElement(name = "order", namespace = NAMESPACE)
@XmlAccessorType(XmlAccessType.FIELD)
public static class OrderDto {
@Nullable @XmlAttribute String property;
@Nullable @XmlAttribute Direction direction;
}
@XmlRootElement(name = "page", namespace = NAMESPACE)
@XmlAccessorType(XmlAccessType.FIELD)
public static class PageDto extends RepresentationModel {
@Nullable @XmlAnyElement @XmlElementWrapper(name = "content") List<Object> content;
}
public static <T, S> List<T> unmarshal(Collection<S> source, XmlAdapter<S, T> adapter) {
Assert.notNull(adapter, "Adapter must not be null!");
if (source == null || source.isEmpty()) {
return Collections.emptyList();
}
List<T> result = new ArrayList<>(source.size());
for (S element : source) {
try {
result.add(adapter.unmarshal(element));
} catch (Exception o_O) {
throw new RuntimeException(o_O);
}
}
return result;
}
public static <T, S> List<S> marshal(@Nullable Iterable<T> source, XmlAdapter<S, T> adapter) {
Assert.notNull(adapter, "Adapter must not be null!");
if (source == null) {
return Collections.emptyList();
}
List<S> result = new ArrayList<>();
for (T element : source) {
try {
result.add(adapter.marshal(element));
} catch (Exception o_O) {
throw new RuntimeException(o_O);
}
}
return result;
}
}