All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.nichetoolkit.rest.helper.XmlHelper Maven / Gradle / Ivy

The newest version!
package io.github.nichetoolkit.rest.helper;

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.util.GeneralUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/**
 * XmlHelper
 * 

The xml helper class.

* @author Cyan ([email protected]) * @since Jdk1.8 */ public class XmlHelper { /** * 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) { String fileName = new String(filename.trim().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8); try { fileName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException ignored) { } String content = "attachment;filename=".concat(fileName); response.setHeader("Content-Disposition", content); response.setHeader("Content-Type", "application/xml;charset=UTF-8"); response.setContentType("application/octet-stream"); } /** * 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.

* @throws XmlMarshalException {@link io.github.nichetoolkit.rest.error.often.XmlMarshalException}

The xml marshal exception is XmlMarshalException type.

* @see java.lang.Class * @see javax.xml.bind.Unmarshaller * @see io.github.nichetoolkit.rest.error.often.XmlMarshalException */ public static Unmarshaller unmarshaller (Class clazz) throws XmlMarshalException { try { JAXBContext context = JAXBContext.newInstance(clazz); return context.createUnmarshaller(); } catch (JAXBException exception) { throw new XmlMarshalException(exception.getMessage()); } } /** * 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.

* @throws XmlMarshalException {@link io.github.nichetoolkit.rest.error.often.XmlMarshalException}

The xml marshal exception is XmlMarshalException type.

* @see java.lang.Class * @see javax.xml.bind.Marshaller * @see io.github.nichetoolkit.rest.error.often.XmlMarshalException */ public static Marshaller marshaller(Class clazz) throws XmlMarshalException { try { JAXBContext context = JAXBContext.newInstance(clazz); return context.createMarshaller(); } catch (JAXBException exception) { throw new XmlMarshalException(exception.getMessage()); } } /** * 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.

* @throws XmlReadException {@link io.github.nichetoolkit.rest.error.often.XmlReadException}

The xml read exception is XmlReadException type.

* @see org.springframework.web.multipart.MultipartFile * @see java.lang.Class * @see io.github.nichetoolkit.rest.error.often.XmlReadException */ public static T read(MultipartFile xmlFile, Class clazz) throws XmlReadException { if (GeneralUtils.isEmpty(xmlFile)) { return null; } try (InputStream inputStream = xmlFile.getInputStream()) { return JAXB.unmarshal(inputStream, clazz); } catch (DataBindingException | IOException exception) { throw new XmlReadException(exception.getMessage()); } } /** * 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.

* @throws XmlReadException {@link io.github.nichetoolkit.rest.error.often.XmlReadException}

The xml read exception is XmlReadException type.

* @see java.io.File * @see java.lang.Class * @see io.github.nichetoolkit.rest.error.often.XmlReadException */ public static T read(File xmlFile, Class clazz) throws XmlReadException { if (GeneralUtils.isEmpty(xmlFile) || !xmlFile.exists()) { return null; } try (InputStream inputStream = Files.newInputStream(xmlFile.toPath())) { return JAXB.unmarshal(inputStream, clazz); } catch (DataBindingException | IOException exception) { throw new XmlReadException(exception.getMessage()); } } /** * 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.

* @throws XmlReadException {@link io.github.nichetoolkit.rest.error.often.XmlReadException}

The xml read exception is XmlReadException type.

* @see javax.xml.bind.Unmarshaller * @see java.io.InputStream * @see java.lang.Class * @see io.github.nichetoolkit.rest.error.often.XmlReadException */ public static T read(Unmarshaller unmarshaller, InputStream inputStream, Class clazz) throws XmlReadException { if (GeneralUtils.isEmpty(inputStream)) { return null; } try { JAXBElement jaxbElement = unmarshaller.unmarshal(new StreamSource(inputStream), clazz); return jaxbElement.getValue(); } catch ( JAXBException | DataBindingException exception) { throw new XmlReadException(exception.getMessage()); } } /** * 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.

* @throws XmlReadException {@link io.github.nichetoolkit.rest.error.often.XmlReadException}

The xml read exception is XmlReadException type.

* @see java.io.InputStream * @see java.lang.Class * @see io.github.nichetoolkit.rest.error.often.XmlReadException */ public static T read(InputStream inputStream, Class clazz) throws XmlReadException { if (GeneralUtils.isEmpty(inputStream)) { return null; } try { return JAXB.unmarshal(inputStream, clazz); } catch ( DataBindingException exception) { throw new XmlReadException(exception.getMessage()); } } /** * 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.

* @throws XmlWriteException {@link io.github.nichetoolkit.rest.error.often.XmlWriteException}

The xml write exception is XmlWriteException type.

* @see javax.xml.bind.Marshaller * @see java.lang.String * @see javax.servlet.http.HttpServletResponse * @see io.github.nichetoolkit.rest.error.often.XmlWriteException */ public static void write(Marshaller marshaller, T xmlObject, String filename, HttpServletResponse response) throws XmlWriteException { if (GeneralUtils.isEmpty(xmlObject)) { return; } try { encode(filename, response); ServletOutputStream outputStream = response.getOutputStream(); marshaller.marshal(xmlObject,outputStream); } catch (JAXBException| IOException exception) { throw new XmlWriteException(exception.getMessage()); } } /** * 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.

* @throws XmlWriteException {@link io.github.nichetoolkit.rest.error.often.XmlWriteException}

The xml write exception is XmlWriteException type.

* @see javax.xml.bind.Marshaller * @see javax.servlet.http.HttpServletResponse * @see io.github.nichetoolkit.rest.error.often.XmlWriteException */ public static void write(Marshaller marshaller, T xmlObject, HttpServletResponse response) throws XmlWriteException { if (GeneralUtils.isEmpty(xmlObject)) { return; } try { ServletOutputStream outputStream = response.getOutputStream(); marshaller.marshal(xmlObject,outputStream); } catch (JAXBException| IOException exception) { throw new XmlWriteException(exception.getMessage()); } } /** * 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.

* @throws XmlWriteException {@link io.github.nichetoolkit.rest.error.often.XmlWriteException}

The xml write exception is XmlWriteException type.

* @see javax.xml.bind.Marshaller * @see java.io.OutputStream * @see io.github.nichetoolkit.rest.error.often.XmlWriteException */ public static void write(Marshaller marshaller, T xmlObject, OutputStream outputStream) throws XmlWriteException { if (GeneralUtils.isEmpty(xmlObject)) { return; } try { marshaller.marshal(xmlObject,outputStream); } catch (JAXBException exception) { throw new XmlWriteException(exception.getMessage()); } } /** * 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.

* @throws XmlWriteException {@link io.github.nichetoolkit.rest.error.often.XmlWriteException}

The xml write exception is XmlWriteException type.

* @see java.lang.String * @see javax.servlet.http.HttpServletResponse * @see io.github.nichetoolkit.rest.error.often.XmlWriteException */ public static void write(T xmlObject, String filename, HttpServletResponse response) throws XmlWriteException { if (GeneralUtils.isEmpty(xmlObject)) { return; } try { encode(filename,response); ServletOutputStream outputStream = response.getOutputStream(); JAXB.marshal(xmlObject,outputStream); } catch (IOException exception) { throw new XmlWriteException(exception.getMessage()); } } /** * 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.

* @throws XmlWriteException {@link io.github.nichetoolkit.rest.error.often.XmlWriteException}

The xml write exception is XmlWriteException type.

* @see javax.servlet.http.HttpServletResponse * @see io.github.nichetoolkit.rest.error.often.XmlWriteException */ public static void write(T xmlObject, HttpServletResponse response) throws XmlWriteException { if (GeneralUtils.isEmpty(xmlObject)) { return; } try { ServletOutputStream outputStream = response.getOutputStream(); JAXB.marshal(xmlObject,outputStream); } catch (IOException exception) { throw new XmlWriteException(exception.getMessage()); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy