io.github.nichetoolkit.rest.util.XmlUtils Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.util;
import io.github.nichetoolkit.rest.error.often.XmlMarshalException;
import io.github.nichetoolkit.rest.error.often.XmlReadException;
import io.github.nichetoolkit.rest.error.often.XmlWriteException;
import io.github.nichetoolkit.rest.helper.XmlHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXB;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
/**
* XmlUtils
* The xml utils class.
* @author Cyan ([email protected])
* @see lombok.extern.slf4j.Slf4j
* @since Jdk1.8
*/
@Slf4j
public class XmlUtils {
/**
* encode
* The encode method.
* @param filename {@link java.lang.String} The filename parameter is String
type.
* @param response {@link javax.servlet.http.HttpServletResponse} The response parameter is HttpServletResponse
type.
* @see java.lang.String
* @see javax.servlet.http.HttpServletResponse
*/
public static void encode(String filename, HttpServletResponse response) {
XmlHelper.encode(filename,response);
}
/**
* marshaller
* The marshaller method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return {@link javax.xml.bind.Marshaller} The marshaller return object is Marshaller
type.
* @see java.lang.Class
* @see javax.xml.bind.Marshaller
*/
public static Marshaller marshaller(Class clazz) {
try {
return XmlHelper.marshaller(clazz);
} catch (XmlMarshalException exception) {
log.error("It is failed when data marshaller with class type! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* unmarshaller
* The unmarshaller method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return {@link javax.xml.bind.Unmarshaller} The unmarshaller return object is Unmarshaller
type.
* @see java.lang.Class
* @see javax.xml.bind.Unmarshaller
*/
public static Unmarshaller unmarshaller(Class clazz) {
try {
return XmlHelper.unmarshaller(clazz);
} catch (XmlMarshalException exception) {
log.error("It is failed when data unmarshaller with class type! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* read
* The read method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param xmlFile {@link org.springframework.web.multipart.MultipartFile} The xml file parameter is MultipartFile
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return T The read return object is T
type.
* @see org.springframework.web.multipart.MultipartFile
* @see java.lang.Class
*/
public static T read(MultipartFile xmlFile, Class clazz) {
try {
return XmlHelper.read(xmlFile, clazz);
} catch (XmlReadException exception) {
log.error("It is failed when xml read with file and class type! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* read
* The read method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param xmlFile {@link java.io.File} The xml file parameter is File
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return T The read return object is T
type.
* @see java.io.File
* @see java.lang.Class
*/
public static T read(File xmlFile, Class clazz) {
try {
return XmlHelper.read(xmlFile, clazz);
} catch (XmlReadException exception) {
log.error("It is failed when xml file read with class type! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* read
* The read method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param unmarshaller {@link javax.xml.bind.Unmarshaller} The unmarshaller parameter is Unmarshaller
type.
* @param inputStream {@link java.io.InputStream} The input stream parameter is InputStream
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return T The read return object is T
type.
* @see javax.xml.bind.Unmarshaller
* @see java.io.InputStream
* @see java.lang.Class
*/
public static T read(Unmarshaller unmarshaller, InputStream inputStream, Class clazz) {
try {
return XmlHelper.read(unmarshaller,inputStream, clazz);
} catch (XmlReadException exception) {
log.error("It is failed when unmarshaller input stream read with class type! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* read
* The read method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param inputStream {@link java.io.InputStream} The input stream parameter is InputStream
type.
* @param clazz {@link java.lang.Class} The clazz parameter is Class
type.
* @return T The read return object is T
type.
* @see java.io.InputStream
* @see java.lang.Class
*/
public static T read(InputStream inputStream, Class clazz) {
try {
return XmlHelper.read(inputStream, clazz);
} catch (XmlReadException exception) {
log.error("It is failed when inputStream read! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
return null;
}
}
/**
* write
* The write method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param marshaller {@link javax.xml.bind.Marshaller} The marshaller parameter is Marshaller
type.
* @param xmlObject T The xml object parameter is T
type.
* @param filename {@link java.lang.String} The filename parameter is String
type.
* @param response {@link javax.servlet.http.HttpServletResponse} The response parameter is HttpServletResponse
type.
* @see javax.xml.bind.Marshaller
* @see java.lang.String
* @see javax.servlet.http.HttpServletResponse
*/
public static void write(Marshaller marshaller, T xmlObject, String filename, HttpServletResponse response) {
try {
XmlHelper.write(marshaller, xmlObject, filename,response);
} catch (XmlWriteException exception) {
log.error("It is failed when xml write with xml file and filename! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
}
}
/**
* write
* The write method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param marshaller {@link javax.xml.bind.Marshaller} The marshaller parameter is Marshaller
type.
* @param xmlObject T The xml object parameter is T
type.
* @param response {@link javax.servlet.http.HttpServletResponse} The response parameter is HttpServletResponse
type.
* @see javax.xml.bind.Marshaller
* @see javax.servlet.http.HttpServletResponse
*/
public static void write(Marshaller marshaller, T xmlObject, HttpServletResponse response) {
try {
XmlHelper.write(marshaller, xmlObject, response);
} catch (XmlWriteException exception) {
log.error("It is failed when xml write with xml file and response! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
}
}
/**
* write
* The write method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param marshaller {@link javax.xml.bind.Marshaller} The marshaller parameter is Marshaller
type.
* @param xmlObject T The xml object parameter is T
type.
* @param outputStream {@link java.io.OutputStream} The output stream parameter is OutputStream
type.
* @see javax.xml.bind.Marshaller
* @see java.io.OutputStream
*/
public static void write(Marshaller marshaller, T xmlObject, OutputStream outputStream) {
try {
XmlHelper.write(marshaller, xmlObject, outputStream);
} catch (XmlWriteException exception) {
log.error("It is failed when xml write with xml file and output stream! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
}
}
/**
* write
* The write method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param xmlObject T The xml object parameter is T
type.
* @param filename {@link java.lang.String} The filename parameter is String
type.
* @param response {@link javax.servlet.http.HttpServletResponse} The response parameter is HttpServletResponse
type.
* @see java.lang.String
* @see javax.servlet.http.HttpServletResponse
*/
public static void write(T xmlObject, String filename, HttpServletResponse response) {
try {
XmlHelper.write(xmlObject, filename, response);
} catch (XmlWriteException exception) {
log.error("It is failed when xml write with filename and response! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
}
}
/**
* write
* The write method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param xmlObject T The xml object parameter is T
type.
* @param response {@link javax.servlet.http.HttpServletResponse} The response parameter is HttpServletResponse
type.
* @see javax.servlet.http.HttpServletResponse
*/
public static void write(T xmlObject, HttpServletResponse response) {
try {
XmlHelper.write(xmlObject, response);
} catch (XmlWriteException exception) {
log.error("It is failed when xml write with file and response! {}", exception.getMessage());
GeneralUtils.printStackTrace(exception);
}
}
/**
* write
* The write method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param xmlObject T The xml object parameter is T
type.
* @param outputStream {@link java.io.OutputStream} The output stream parameter is OutputStream
type.
* @see java.io.OutputStream
*/
public static void write(T xmlObject, OutputStream outputStream) {
JAXB.marshal(xmlObject,outputStream);
}
}