001    /*
002     @license.text@
003      */
004    package biz.hammurapi.xml.dom;
005    
006    import org.w3c.dom.Element;
007    
008    /**
009     * @author Pavel Vlasov
010     *
011     * @version $Revision: 1.2 $
012     */
013    public class ThrowableDomSerializer {
014    
015            public DomSerializable toDomSerializable(final Throwable th) {
016                    return new DomSerializable() {
017    
018                            public void toDom(Element holder) {
019                                    holder.appendChild(holder.getOwnerDocument().createTextNode(th.toString()));
020                                    if (th.getMessage()!=null && th.getMessage().trim().length()>0) {
021                                            holder.setAttribute("message", th.getMessage());
022                                    }
023                                    holder.setAttribute("type", th.getClass().getName());
024                                    holder.setAttribute("is-throwable", "yes");
025                                    StackTraceElement[] st = th.getStackTrace();
026                                for (int i=0; i<st.length; i++) {
027                                    Element sfe = holder.getOwnerDocument().createElement("stack-frame");
028                                            holder.appendChild(sfe);
029                                            sfe.setAttribute("class", st[i].getClassName());
030                                            sfe.setAttribute("file", st[i].getFileName());
031                                            sfe.setAttribute("method", st[i].getMethodName());
032                                            sfe.setAttribute("line", String.valueOf(st[i].getLineNumber()));
033                                            sfe.setAttribute("native", st[i].isNativeMethod() ? "yes" : "no");
034                                }
035                                
036                                if (th.getCause()!=null) {
037                                    Element ce = holder.getOwnerDocument().createElement("cause");
038                                            holder.appendChild(ce);
039                                            toDomSerializable(th.getCause()).toDom(ce);
040                                }
041                            }
042                            
043                    };
044            }
045    }