001 package biz.hammurapi.jms.adapter.converters; 002 003 import javax.jms.JMSException; 004 import javax.xml.transform.Result; 005 import javax.xml.transform.Source; 006 007 import org.w3c.dom.Document; 008 import org.w3c.dom.Element; 009 010 import biz.hammurapi.config.DomConfigFactory; 011 import biz.hammurapi.xml.dom.DOMUtils; 012 013 /** 014 * This class uses CompositeDomSerializer to convert 015 * Java objects to XML and DomConfigFactory to convert 016 * XML to Java objects. 017 * @author Pavel 018 * 019 */ 020 public class SimpleXmlConverter extends DomConverter { 021 022 /** 023 * Creates ojbect from result. In the case of exception tries to reproduce and throw the same exception 024 */ 025 protected Object processResult(Result result) throws Exception { 026 Element documentElement = ((Document) super.processResult(result)).getDocumentElement(); 027 if ("error".equals(documentElement.getNodeName())) { 028 if (documentElement.hasAttribute("type")) { 029 Exception toThrow = null; 030 try { 031 Class exceptionClass = Class.forName(documentElement.getAttribute("type")); 032 if (Exception.class.isAssignableFrom(exceptionClass)) { 033 if (documentElement.hasAttribute("message")) { 034 toThrow = (Exception) exceptionClass 035 .getConstructor(new Class[] {String.class}) 036 .newInstance(new String[] {documentElement.getAttribute("message")}); 037 } else { 038 toThrow = (Exception) exceptionClass.newInstance(); 039 } 040 } 041 } catch (Exception e) { 042 toThrow = new JMSException("Remote exception "+documentElement.getAttribute("type")+": "+documentElement.getAttribute("message")); 043 } 044 throw toThrow == null ? new JMSException("Remote exception "+documentElement.getAttribute("type")+": "+documentElement.getAttribute("message")) : toThrow; 045 } 046 throw new JMSException("Remote exception: "+documentElement.getAttribute("message")); 047 } 048 DomConfigFactory factory = new DomConfigFactory(); 049 return factory.create(documentElement); 050 } 051 052 /** 053 * Serializes object to XML using composite dom serializer 054 */ 055 protected Source convert(Object request) throws Exception { 056 Document doc = createDocument(); 057 Element root = doc.createElement("object"); 058 doc.appendChild(root); 059 DOMUtils.toDom(request, root); 060 return super.convert(doc); 061 } 062 }