package th.co.ais.ssbsrfc.message;
import java.io.ByteArrayInputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import ec02.utils.AppLog;
import th.co.ais.ssbsrfc.instance.AdjustmentResponseIns;
import th.co.ais.ssbsrfc.instance.SOAPNamespaceMapper;
public class MessageParser
{
public static String toXml(Object obj) {
String strXml = "";
StringWriter sw = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(obj, sw);
strXml = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return strXml;
}
public static String toXmlWithoutRoot(Object obj) {
String strXml = "";
StringWriter sw = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(obj, sw);
strXml = sw.toString().replace("", "").replace("", "");
} catch (Exception e) {
e.printStackTrace();
}
return strXml;
}
public static String toXml(Object obj, String rootElement) {
String strXml = "";
StringWriter sw = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(obj, sw);
strXml = sw.toString().replace("root", rootElement);
} catch (Exception e) {
e.printStackTrace();
}
return strXml;
}
@SuppressWarnings("rawtypes")
public static String toXmlWithNamespace(Object obj, Class objClass, Object namespaceMapper) {
String strXml = "";
StringWriter sw = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(objClass);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", namespaceMapper);
marshaller.marshal(obj, sw);
strXml = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return strXml;
}
@SuppressWarnings("rawtypes")
public static Object fromXml(String strXml, Class objClass) {
Object obj = null;
try {
strXml = removeXmlVersion(strXml);
JAXBContext jc = JAXBContext.newInstance(objClass);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader reader = new StringReader(strXml);
obj = unmarshaller.unmarshal(reader);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
@SuppressWarnings("rawtypes")
public static Object fromXmlWithoutRoot(String strXml, Class objClass) {
Object obj = null;
try {
strXml = "" + removeXmlVersion(strXml) + "";
JAXBContext jc = JAXBContext.newInstance(objClass);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader reader = new StringReader(strXml);
obj = unmarshaller.unmarshal(reader);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static String toSoap(Object obj) {
String message = "";
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage outgoingMessage = messageFactory.createMessage();
SOAPPart soappart = outgoingMessage.getSOAPPart();
SOAPEnvelope envelope = soappart.getEnvelope();
envelope.addNamespaceDeclaration("def", "http://definition.webservices.daa.ema.com/");
envelope.removeNamespaceDeclaration("SOAP-ENV");
envelope.removeNamespaceDeclaration("cbs");
envelope.removeNamespaceDeclaration("arc");
envelope.removeNamespaceDeclaration("ars");
envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.addNamespaceDeclaration("cbs", "http://www.huawei.com/bme/cbsinterface/cbscommon");
envelope.addNamespaceDeclaration("arc", "http://cbs.huawei.com/ar/wsservice/arcommon");
envelope.addNamespaceDeclaration("ars", "http://www.huawei.com/bme/cbsinterface/arservices");
envelope.setPrefix("soapenv");
outgoingMessage.getSOAPHeader().setPrefix("soapenv");
outgoingMessage.getSOAPBody().setPrefix("soapenv");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new SOAPNamespaceMapper());
marshaller.marshal( obj, doc );
SOAPBody body = envelope.getBody();
body.addDocument(doc);
outgoingMessage.saveChanges();
StringWriter sw = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(outgoingMessage.getSOAPPart()), new StreamResult(sw));
message = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
public static String toSoap(Object obj, String namespacePrefix, String namespaceUrl) {
String message = "";
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage outgoingMessage = messageFactory.createMessage();
SOAPPart soappart = outgoingMessage.getSOAPPart();
SOAPEnvelope envelope = soappart.getEnvelope();
envelope.addNamespaceDeclaration(namespacePrefix, namespaceUrl);
envelope.removeNamespaceDeclaration("SOAP-ENV");
envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("soapenv");
outgoingMessage.getSOAPHeader().setPrefix("soapenv");
outgoingMessage.getSOAPBody().setPrefix("soapenv");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new SOAPNamespaceMapper() );
marshaller.marshal( obj, doc );
SOAPBody body = envelope.getBody();
body.addDocument(doc);
outgoingMessage.saveChanges();
StringWriter sw = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(outgoingMessage.getSOAPPart()), new StreamResult(sw));
message = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
@SuppressWarnings("rawtypes")
public static Object fromSoap(String strSoap, Class objClass) {
Object obj = null;
try {
SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(strSoap.getBytes()));
Unmarshaller unmarshaller = JAXBContext.newInstance(objClass).createUnmarshaller();
SOAPBody body = message.getSOAPBody();
if(body != null && !body.getTextContent().trim().equals("")) {
obj = unmarshaller.unmarshal(body.extractContentAsDocument());
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static AdjustmentResponseIns formsoap(String ss) throws XMLStreamException, JAXBException{
Reader reader = new StringReader(ss);
XMLInputFactory factory = XMLInputFactory.newInstance(); // Or newFactory()
XMLStreamReader xsr = factory.createXMLStreamReader(reader);
AppLog.d("IN:"+xsr);
xsr.nextTag();
int i =0;
while(!xsr.getLocalName().equals("ResultHeader")) {
xsr.nextTag();
AppLog.d("I HERE:"+i++);
}
JAXBContext jc = JAXBContext.newInstance(AdjustmentResponseIns.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement jb = unmarshaller.unmarshal(xsr, AdjustmentResponseIns.class);
xsr.close();
AdjustmentResponseIns customer = jb.getValue();
return customer;
}
public static String toJson(Object obj) {
String strJson = "";
try {
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
strJson = gson.toJson(obj, obj.getClass());
} catch (Exception e) {
e.printStackTrace();
}
return strJson;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object fromJson(String strJson, Class objClass) {
Object obj = null;
try {
Gson gson = new Gson();
obj = gson.fromJson(strJson, objClass);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static JsonObject toJsonObject(Object obj) {
JsonObject jsonObj = null;
try {
Gson gson = new Gson();
JsonElement element = gson.fromJson (MessageParser.toJson(obj), JsonElement.class);
jsonObj = element.getAsJsonObject();
} catch (Exception e) {
e.printStackTrace();
}
return jsonObj;
}
private static String removeXmlVersion(String value)
{
return value.replaceAll("\\<\\?xml(.+?)\\?\\>", "");
}
}