-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJaxbHandler.java
141 lines (123 loc) · 3.69 KB
/
JaxbHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.ValidationEventCollector;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
/*
* java 与 pojo 进行互转的工具类
*/
public class JaxbHandler {
private static final Logger logger = Logger.getLogger(JaxbHandler.class);
private JAXBContext jaxbContext;
private Marshaller marshaller;
private Unmarshaller unmarshaller;
public JaxbHandler(String contextPath) {
this(null, contextPath);
}
public JaxbHandler(String xsdPath, String contextPath) {
try {
this.jaxbContext = JAXBContext.newInstance(contextPath);
this.marshaller = this.jaxbContext.createMarshaller();
this.unmarshaller = this.jaxbContext.createUnmarshaller();
if (null != xsdPath) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL source = this.getClass().getClassLoader().getResource(xsdPath);
Schema schema = schemaFactory.newSchema(source);
ValidationEventCollector vec = new ValidationEventCollector();
this.marshaller.setSchema(schema); // 设置schema校验
this.marshaller.setEventHandler(vec);
this.marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 设置编码
this.unmarshaller.setSchema(schema);
this.unmarshaller.setEventHandler(vec);
this.unmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
}
} catch (JAXBException e) {
logger.error("create jaxcontext instance error: ", e);
} catch (SAXException e) {
logger.error("create schema instance error: ", e);
}
}
/**
*
* Function:
* 将pojo转换为xml, 如果异常则返回null
* @param obj
* 将被转换的pojo
* @return
* 转换后的xml字符串
* @version 1.0
*/
public String pojo2XMl(Object obj) {
if (null == obj) {
return null;
}
String ret = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
this.marshaller.marshal(obj, outputStream);
byte[] bytes = outputStream.toByteArray();
ret = new String(bytes, "UTF-8");
} catch (JAXBException e) {
logger.error("can not marshal obj: ", e);
} catch (UnsupportedEncodingException e) {
logger.error("can not create string with utf-8: ", e);
} finally {
try {
outputStream.close();
} catch (IOException e) {
logger.warn("output stream can not close.");
}
}
return ret;
}
/**
*
* Function:
* 将xml转换为pojo, 如果异常则返回null
* @param <T>
* 泛型方法
* @param xml
* 将要转换的xml内容
* @param clazz
* 指定泛型类
* @return
* 实体对象
* @version 1.0
*/
public <T> T XML2Pojo(String xml, Class<T> clazz) {
if (null == xml || null == clazz) {
return null;
}
Object obj = null;
ByteArrayInputStream inputStream = null;
try {
byte[] bytes = xml.getBytes("UTF-8");
inputStream = new ByteArrayInputStream(bytes);
obj = this.unmarshaller.unmarshal(inputStream);
} catch (JAXBException e) {
logger.error("can not unmarshal xml: ", e);
} catch (UnsupportedEncodingException e) {
logger.error("can not get bytes with utf-8: ", e);
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
logger.warn("input stream can not close.");
}
}
}
return clazz.cast(obj);
}
}