001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.exceptionprocessor.util;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024:
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Text;
031:
032: /**
033: * @author jh
034: *
035: * To change the template for this generated type comment go to
036: * Window - Preferences - Java - Code Generation - Code and Comments
037: */
038: public class XMLCreatorVisitor implements ExceptionDataValueVisitor {
039:
040: private Document doc;
041:
042: /* (non-Javadoc)
043: * @see de.schlund.jmsexceptionhandler.rmiobj.ExceptionDataValueVisitor#visit(de.schlund.jmsexceptionhandler.rmiobj.ExceptionDataValue)
044: */
045: public void visit(ExceptionDataValue data) {
046: DocumentBuilderFactory dbfac = DocumentBuilderFactory
047: .newInstance();
048: dbfac.setNamespaceAware(false);
049: dbfac.setValidating(false);
050: try {
051: doc = dbfac.newDocumentBuilder().newDocument();
052: } catch (ParserConfigurationException e) {
053: e.printStackTrace();
054: return;
055: }
056: Element e = doc.createElement("error");
057: e
058: .setAttribute("type", data.getThrowable().getClass()
059: .getName());
060:
061: Element sess_info = doc.createElement("sessioninfo");
062: Text sess_info_txt = doc.createTextNode(data.getSessionid());
063: sess_info.appendChild(sess_info_txt);
064: e.appendChild(sess_info);
065:
066: Element req_params = doc.createElement("requestparams");
067: HashMap<String, String> params = data.getRequestParams();
068: for (Iterator<String> iter = params.keySet().iterator(); iter
069: .hasNext();) {
070: String key = iter.next();
071: String value = params.get(key);
072: Element req_p = doc.createElement("param");
073: req_p.setAttribute("key", key);
074: Text req_p_txt = doc.createTextNode(value);
075: req_p.appendChild(req_p_txt);
076: req_params.appendChild(req_p);
077: }
078: e.appendChild(req_params);
079:
080: Element last_steps = doc.createElement("laststeps");
081: for (Iterator<String> iter = data.getLastSteps().iterator(); iter
082: .hasNext();) {
083: Element step = doc.createElement("step");
084: Text step_txt = doc.createTextNode(iter.next());
085: step.appendChild(step_txt);
086: last_steps.appendChild(step);
087: }
088: e.appendChild(last_steps);
089:
090: Element sess_keysnvals = doc.createElement("session_dump");
091: HashMap<String, String> map = data.getSessionKeysAndValues();
092: for (Iterator<String> iter = map.keySet().iterator(); iter
093: .hasNext();) {
094: String key = iter.next();
095: String val = map.get(key);
096: Element pair = doc.createElement("pair");
097: pair.setAttribute("key", key);
098: Text cd = doc.createTextNode(val);
099: pair.appendChild(cd);
100: sess_keysnvals.appendChild(pair);
101: }
102: e.appendChild(sess_keysnvals);
103:
104: appendThrowable(e, data.getThrowable());
105:
106: doc.appendChild(e);
107: data.setXMLRepresentation(doc);
108: }
109:
110: private void appendThrowable(Element elem, Throwable throwable) {
111: if (throwable != null) {
112: Element exElem = doc.createElement("exception");
113: exElem.setAttribute("type", throwable.getClass().getName());
114: exElem.setAttribute("msg", throwable.getMessage());
115: Element stackElem = doc.createElement("stacktrace");
116: StackTraceElement[] strace = throwable.getStackTrace();
117: for (int i = 0; i < strace.length; i++) {
118: Element lineElem = doc.createElement("line");
119: Text lineText = doc
120: .createTextNode(strace[i].toString());
121: lineElem.appendChild(lineText);
122: stackElem.appendChild(lineElem);
123: }
124: exElem.appendChild(stackElem);
125: elem.appendChild(exElem);
126: if (throwable.getCause() != null)
127: appendThrowable(exElem, throwable.getCause());
128: }
129: }
130:
131: }
|