mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-24 04:53:50 +08:00
issue #71 替换掉JAXB,避免因OpenJDK造成无法deserialize xml的错误
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
package me.chanjar.weixin.common.util.xml;
|
||||
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* http://stackoverflow.com/questions/14193944/jaxb-marshalling-unmarshalling-with-cdata
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
public class AdapterCDATA extends XmlAdapter<String, String> {
|
||||
|
||||
@Override
|
||||
public String marshal(String arg0) throws Exception {
|
||||
return "<![CDATA[" + arg0 + "]]>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String unmarshal(String arg0) throws Exception {
|
||||
return arg0;
|
||||
}
|
||||
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
package me.chanjar.weixin.common.util.xml;
|
||||
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class MediaIdMarshaller extends XmlAdapter<String, String> {
|
||||
|
||||
@Override
|
||||
public String marshal(String arg0) throws Exception {
|
||||
return "<MediaId><![CDATA[" + arg0 + "]]></MediaId>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String unmarshal(String arg0) throws Exception {
|
||||
// do nothing
|
||||
return arg0;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package me.chanjar.weixin.common.util.xml;
|
||||
|
||||
import com.thoughtworks.xstream.converters.basic.StringConverter;
|
||||
|
||||
/**
|
||||
* Created by qianjia on 15/1/19.
|
||||
*/
|
||||
public class XStreamCDataConverter extends StringConverter {
|
||||
|
||||
@Override
|
||||
public String toString(Object obj) {
|
||||
return "<![CDATA[" + super.toString(obj) + "]]>";
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
package me.chanjar.weixin.common.util.xml;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.converters.basic.DoubleConverter;
|
||||
import com.thoughtworks.xstream.converters.basic.FloatConverter;
|
||||
import com.thoughtworks.xstream.converters.basic.IntConverter;
|
||||
import com.thoughtworks.xstream.core.util.QuickWriter;
|
||||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
||||
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
|
||||
import com.thoughtworks.xstream.io.xml.XppDriver;
|
||||
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* Created by qianjia on 15/1/19.
|
||||
*/
|
||||
public class XStreamInitializer {
|
||||
|
||||
public static XStream getInstance() {
|
||||
XStream xstream = new XStream(new XppDriver() {
|
||||
@Override
|
||||
public HierarchicalStreamWriter createWriter(Writer out) {
|
||||
return new PrettyPrintWriter(out, getNameCoder()) {
|
||||
protected String PREFIX_CDATA = "<![CDATA[";
|
||||
protected String SUFFIX_CDATA = "]]>";
|
||||
protected String PREFIX_MEDIA_ID = "<MediaId>";
|
||||
protected String SUFFIX_MEDIA_ID = "</MediaId>";
|
||||
@Override
|
||||
protected void writeText(QuickWriter writer, String text) {
|
||||
if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
|
||||
writer.write(text);
|
||||
} else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
|
||||
writer.write(text);
|
||||
} else {
|
||||
super.writeText(writer, text);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
xstream.setMode(XStream.NO_REFERENCES);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package me.chanjar.weixin.common.util.xml;
|
||||
|
||||
/**
|
||||
* Created by qianjia on 15/1/19.
|
||||
*/
|
||||
public class XStreamMediaIdConverter extends XStreamCDataConverter {
|
||||
@Override
|
||||
public String toString(Object obj) {
|
||||
return "<MediaId>" + super.toString(obj) + "</MediaId>";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user