Skip to content

Commit

Permalink
Prepare release
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Feb 12, 2019
2 parents a574b45 + e997448 commit e2d4bb5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* 【core】 NumberUtil增加toBytes和toInt方法
* 【core】 XmlUtil增加format方法,支持缩进
* 【http】 SoapRequest增加executeBody方法(issue#IRN6I@Gitee)
* 【core】 调整XmlUtil.toStr方法对编码的逻辑

### Bug修复
* 【core】 修复AnnotationUtil.getAnnotationValue获取对象错误问题(issue#271@Github)
Expand Down
17 changes: 9 additions & 8 deletions hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -232,7 +232,7 @@ public static String toStr(Document doc) {
* @since 3.0.9
*/
public static String toStr(Document doc, boolean isPretty) {
return toStr(doc, null, isPretty);
return toStr(doc, CharsetUtil.UTF_8, isPretty);
}

/**
Expand All @@ -246,13 +246,13 @@ public static String toStr(Document doc, boolean isPretty) {
* @since 3.0.9
*/
public static String toStr(Document doc, String charset, boolean isPretty) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final StringWriter writer = StrUtil.getWriter();
try {
write(doc, out, charset, isPretty ? INDENT_DEFAULT : 0);
write(doc, writer, charset, isPretty ? INDENT_DEFAULT : 0);
} catch (Exception e) {
throw new UtilException(e, "Trans xml document to string error!");
}
return out.toString();
return writer.toString();
}

/**
Expand Down Expand Up @@ -306,7 +306,7 @@ public static void toFile(Document doc, String path, String charset) {
BufferedWriter writer = null;
try {
writer = FileUtil.getWriter(path, charset, false);
write(doc, writer, INDENT_DEFAULT);
write(doc, writer, charset, INDENT_DEFAULT);
} finally {
IoUtil.close(writer);
}
Expand All @@ -317,11 +317,12 @@ public static void toFile(Document doc, String path, String charset) {
*
* @param node {@link Node} XML文档节点或文档本身
* @param writer 写出的Writer,Writer决定了输出XML的编码
* @param charset 编码
* @param indent 格式化输出中缩进量,小于1表示不格式化输出
* @since 3.0.9
*/
public static void write(Node node, Writer writer, int indent) {
transform(new DOMSource(node), new StreamResult(writer), null, indent);
public static void write(Node node, Writer writer, String charset, int indent) {
transform(new DOMSource(node), new StreamResult(writer), charset, indent);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void xpathTest() {
Object value = XmlUtil.getByXPath("//returnsms/message", docResult, XPathConstants.STRING);
Assert.assertEquals("ok", value);
}

@Test
public void xmlToMapTest() {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"//
Expand All @@ -77,7 +77,7 @@ public void xmlToMapTest() {
+ "<successCounts>1</successCounts>"//
+ "</returnsms>";
Map<String, Object> map = XmlUtil.xmlToMap(xml);

Assert.assertEquals(5, map.size());
Assert.assertEquals("Success", map.get("returnstatus"));
Assert.assertEquals("ok", map.get("message"));
Expand All @@ -88,7 +88,8 @@ public void xmlToMapTest() {

@Test
public void mapToXmlTest() {
Map<String, Object> map = MapBuilder.create(new LinkedHashMap<String, Object>()).put("name", "张三")//
Map<String, Object> map = MapBuilder.create(new LinkedHashMap<String, Object>())//
.put("name", "张三")//
.put("age", 12)//
.put("game", MapUtil.builder(new LinkedHashMap<String, Object>()).put("昵称", "Looly").put("level", 14).build())//
.build();
Expand All @@ -105,7 +106,7 @@ public void mapToXmlTest() {
+ "</user>", //
XmlUtil.toStr(doc, false));
}

@Test
public void readTest() {
Document doc = XmlUtil.readXML("test.xml");
Expand Down

0 comments on commit e2d4bb5

Please sign in to comment.