Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2021 Exilor Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ic4j.candid.jaxb.javax;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlEnumValue;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import org.ic4j.candid.parser.IDLType;
import org.ic4j.candid.parser.IDLValue;
import org.ic4j.candid.types.Label;
import org.ic4j.candid.types.Type;
import org.ic4j.types.Principal;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.ic4j.candid.CandidError;
import org.ic4j.candid.IDLUtils;
import org.ic4j.candid.ObjectDeserializer;
public final class JAXBDeserializer implements ObjectDeserializer {
Optional idlType = Optional.empty();
public static JAXBDeserializer create() {
JAXBDeserializer deserializer = new JAXBDeserializer();
return deserializer;
}
public void setIDLType(IDLType idlType)
{
this.idlType = Optional.ofNullable(idlType);
}
public Class> getDefaultResponseClass() {
return Object.class;
}
@Override
public T deserialize(IDLValue idlValue, Class clazz) {
if (idlValue == null)
return null;
// handle OPT
if (idlValue.getType() == Type.OPT) {
Optional optionalValue = (Optional) idlValue.getValue();
if (Optional.class.isAssignableFrom(clazz))
return (T) optionalValue;
// if innerType is primitive
if (idlValue.getIDLType().getInnerType().getType().isPrimitive())
return (T) optionalValue.orElse(null);
else {
T result = null;
if (optionalValue.isPresent())
result = this.getValue(optionalValue.get(), clazz);
return result;
}
}
// handle arrays
if (idlValue.getType() == Type.VEC) {
// if innerType is primitive
if (IDLType.isPrimitiveType(clazz))
return idlValue.getValue();
else if (idlValue.getValue().getClass().isArray()) {
Object[] array = idlValue.getValue();
if (clazz.isAssignableFrom(byte[].class))
return (T) ArrayUtils.toPrimitive((Byte[]) array);
if (clazz.isAssignableFrom(Byte[].class))
return (T) (Byte[]) array;
Object[] result = (Object[]) Array.newInstance(clazz.getComponentType(), array.length);
for (int i = 0; i < array.length; i++) {
result[i] = this.getValue(array[i], clazz.getComponentType());
}
return (T) result;
}
}
// handle RECORD and VARIANT
if (idlValue.getType() == Type.RECORD || idlValue.getType() == Type.VARIANT) {
return (T) this.getValue(idlValue.getValue(), clazz);
}
return idlValue.getValue();
}
T getValue(Object value, Class clazz) {
if (value == null)
return null;
if(clazz == null)
clazz = (Class) value.getClass();
if(JAXBElement.class.isAssignableFrom(clazz) )
{
try {
JAXBElement jaxbValue = (JAXBElement) clazz.newInstance();
value = this.getValue(value, jaxbValue.getDeclaredType());
jaxbValue.setValue(value);
return (T) jaxbValue;
} catch (InstantiationException | IllegalAccessException e) {
return null;
}
}
// handle XMLGregorianCalendar like nanosecond timestamp
if (XMLGregorianCalendar.class.isAssignableFrom(clazz)) {
long ts;
if (value instanceof BigInteger)
ts = ((BigInteger) value).longValue();
else
ts = (long) value;
Date date = new Date(ts / 1000000);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
try {
return (T) DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
} catch (DatatypeConfigurationException e) {
return null;
}
}
// handle Duration
if (Duration.class.isAssignableFrom(clazz)) {
return (T) org.ic4j.types.Duration.deserializeXML((Map