|
8 | 8 | import com.thoughtworks.xstream.io.HierarchicalStreamWriter; |
9 | 9 | import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; |
10 | 10 | import com.thoughtworks.xstream.io.xml.XppDriver; |
11 | | -import com.thoughtworks.xstream.security.NullPermission; |
12 | | -import com.thoughtworks.xstream.security.PrimitiveTypePermission; |
13 | 11 |
|
14 | 12 | public class XStreamInitializer { |
15 | | - |
16 | | - public static XStream getInstance() { |
17 | | - XStream xstream = new XStream(new PureJavaReflectionProvider(), new XppDriver() { |
18 | | - |
19 | | - @Override |
20 | | - public HierarchicalStreamWriter createWriter(Writer out) { |
21 | | - return new PrettyPrintWriter(out, getNameCoder()) { |
22 | | - protected String PREFIX_CDATA = "<![CDATA["; |
23 | | - protected String SUFFIX_CDATA = "]]>"; |
24 | | - protected String PREFIX_MEDIA_ID = "<MediaId>"; |
25 | | - protected String SUFFIX_MEDIA_ID = "</MediaId>"; |
26 | | - |
27 | | - @Override |
28 | | - protected void writeText(QuickWriter writer, String text) { |
29 | | - if (text.startsWith(this.PREFIX_CDATA) && text.endsWith(this.SUFFIX_CDATA)) { |
30 | | - writer.write(text); |
31 | | - } else if (text.startsWith(this.PREFIX_MEDIA_ID) && text.endsWith(this.SUFFIX_MEDIA_ID)) { |
32 | | - writer.write(text); |
33 | | - } else { |
34 | | - super.writeText(writer, text); |
35 | | - } |
36 | | - |
| 13 | + private static final XppDriver XPP_DRIVER = new XppDriver() { |
| 14 | + @Override |
| 15 | + public HierarchicalStreamWriter createWriter(Writer out) { |
| 16 | + return new PrettyPrintWriter(out, getNameCoder()) { |
| 17 | + private static final String PREFIX_CDATA = "<![CDATA["; |
| 18 | + private static final String SUFFIX_CDATA = "]]>"; |
| 19 | + private static final String PREFIX_MEDIA_ID = "<MediaId>"; |
| 20 | + private static final String SUFFIX_MEDIA_ID = "</MediaId>"; |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void writeText(QuickWriter writer, String text) { |
| 24 | + if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) { |
| 25 | + writer.write(text); |
| 26 | + } else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) { |
| 27 | + writer.write(text); |
| 28 | + } else { |
| 29 | + super.writeText(writer, text); |
37 | 30 | } |
38 | 31 |
|
39 | | - @Override |
40 | | - public String encodeNode(String name) { |
41 | | - //防止将_转换成__ |
42 | | - return name; |
43 | | - } |
44 | | - }; |
45 | | - } |
46 | | - }); |
| 32 | + } |
47 | 33 |
|
| 34 | + @Override |
| 35 | + public String encodeNode(String name) { |
| 36 | + //防止将_转换成__ |
| 37 | + return name; |
| 38 | + } |
| 39 | + }; |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + public static XStream getInstance() { |
| 44 | + XStream xstream = new XStream(new PureJavaReflectionProvider(), XPP_DRIVER); |
48 | 45 | xstream.ignoreUnknownElements(); |
49 | 46 | xstream.setMode(XStream.NO_REFERENCES); |
50 | | - xstream.addPermission(NullPermission.NULL); |
51 | | - xstream.addPermission(PrimitiveTypePermission.PRIMITIVES); |
| 47 | + XStream.setupDefaultSecurity(xstream); |
52 | 48 | xstream.allowTypesByWildcard(new String[]{ |
53 | 49 | "me.chanjar.weixin.**", "cn.binarywang.wx.**", "com.github.binarywang.**" |
54 | 50 | }); |
55 | 51 |
|
56 | | - XStream.setupDefaultSecurity(xstream); |
57 | | - |
58 | 52 | xstream.setClassLoader(Thread.currentThread().getContextClassLoader()); |
59 | 53 | return xstream; |
60 | 54 | } |
|
0 commit comments