XML(XmlMapper 사용) 추가

master
mjkhan21 12 months ago
parent e36b0b3642
commit e6072fbfc8

@ -94,6 +94,12 @@
<version>2.3.1</version> <version>2.3.1</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.5</version>
</dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>

@ -0,0 +1,150 @@
package cokr.xit.foundation.data;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.function.Consumer;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import cokr.xit.foundation.AbstractComponent;
/**XML / .
* @author mjkhan
*/
public class XML extends AbstractComponent {
private XmlMapper xmlMapper;
/**XML / XmlMapper .
* @return XML / XmlMapper
*/
public XmlMapper getXmlMapper() {
if (xmlMapper == null) {
xmlMapper = new XmlMapper();
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
}
return xmlMapper;
}
/**XML / XmlMapper .
* @param xmlMapper XML / XmlMapper
* @return XML
*/
public XML setXmlMapper(XmlMapper xmlMapper) {
this.xmlMapper = xmlMapper;
return this;
}
/**XML / XmlMapper .
* @param configurer XmlMapper function
* @return XML
*/
public XML configure(Consumer<XmlMapper> configurer) {
if (configurer != null)
configurer.accept(getXmlMapper());
return this;
}
/**XML .
* @param <T>
* @param xml XML
* @param klass
* @return
*/
public <T> T parse(String xml, Class<T> klass) {
try {
return getXmlMapper().readValue(xml, klass);
} catch (Exception e) {
throw runtimeException(e);
}
}
public <T> T parse(String xml, TypeReference<T> typeRef) {
try {
return getXmlMapper().readValue(xml, typeRef);
} catch (Exception e) {
throw runtimeException(e);
}
}
/**input .
* @param <T>
* @param input XML input
* @param klass
* @return input
*/
public <T> T parse(InputStream input, Class<T> klass) {
try {
return getXmlMapper().readValue(input, klass);
} catch (Exception e) {
throw runtimeException(e);
}
}
public <T> T parse(InputStream input, TypeReference<T> typeRef) {
try {
return getXmlMapper().readValue(input, typeRef);
} catch (Exception e) {
throw runtimeException(e);
}
}
/** XML .
* @param obj
* @return XML
*/
public String stringify(Object obj) {
return stringify(obj, false);
}
/** XML .
* @param obj
* @param indent
* <ul><li> true</li>
* <li> false</li>
* </ul>
* @return XML
*/
public String stringify(Object obj, boolean indent) {
if (obj instanceof String)
return (String)obj;
XmlMapper mapper = getXmlMapper();
ObjectWriter writer = !indent ? mapper.writer() : mapper.writerWithDefaultPrettyPrinter();
try {
return writer.writeValueAsString(obj);
} catch (Exception e) {
throw runtimeException(e);
}
}
/** OutputStream .
* @param out OutputStream
* @param obj
* @param indent
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public void write(OutputStream out, Object obj, boolean indent) {
XmlMapper mapper = getXmlMapper();
ObjectWriter writer = !indent ? mapper.writer() : mapper.writerWithDefaultPrettyPrinter();
try {
writer.writeValue(out, obj);
} catch (Exception e) {
throw runtimeException(e);
}
}
/** OutputStream .
* @param out OutputStream
* @param obj
*/
public void write(OutputStream out, Object obj) {
write(out, obj, false);
}
}
Loading…
Cancel
Save