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.targets;
021:
022: import javax.xml.transform.TransformerException;
023:
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027: import org.xml.sax.SAXException;
028: import org.xml.sax.SAXParseException;
029:
030: import de.schlund.pfixxml.util.Xml;
031:
032: /**
033: * Exception subclass which represents errors during generation of a target.
034: * <br/>
035: * @author <a href="mailto: haecker@schlund.de">Joerg Haecker</a>
036: */
037: public class TargetGenerationException extends Exception {
038:
039: private String targetkey;
040:
041: public TargetGenerationException() {
042: super ();
043: }
044:
045: public TargetGenerationException(String msg) {
046: super (msg);
047: }
048:
049: public TargetGenerationException(String msg, Throwable cause) {
050: super (msg);
051: initCause(cause);
052: }
053:
054: public TargetGenerationException(Throwable cause) {
055: initCause(cause);
056: }
057:
058: /**
059: * @return
060: */
061: public String getTargetkey() {
062: return targetkey;
063: }
064:
065: /**
066: * @param string
067: */
068: public void setTargetkey(String key) {
069: targetkey = key;
070: }
071:
072: public Document toXMLRepresentation() {
073: return createErrorTree(this );
074: }
075:
076: private Document createErrorTree(TargetGenerationException targetex) {
077: Document doc = Xml.createDocument();
078: Element e0 = doc.createElement("error");
079: e0.setAttribute("type", "xslt");
080: doc.appendChild(e0);
081: printEx(targetex, doc, e0);
082: return doc;
083: }
084:
085: private void insertErrInfo(Element error, String key, String value) {
086: Element info = error.getOwnerDocument().createElement("info");
087: info.setAttribute("key", key);
088: info.setAttribute("value", value);
089: error.appendChild(info);
090: }
091:
092: private void printEx(Throwable e, Document doc, Node root) {
093: if (e == null) {
094: return;
095: }
096:
097: Element error = doc.createElement("exception");
098: root.appendChild(error);
099: error.setAttribute("type", e.getClass().getName());
100: insertErrInfo(error, "Message", e.getMessage());
101:
102: if (e instanceof SAXParseException) {
103: SAXParseException sex = (SAXParseException) e;
104: insertErrInfo(error, "Id", sex.getSystemId());
105: insertErrInfo(error, "Line", "" + sex.getLineNumber());
106: insertErrInfo(error, "Column", "" + sex.getColumnNumber());
107: printEx(sex.getException(), doc, root);
108: } else if (e instanceof TargetGenerationException) {
109: TargetGenerationException tagex = (TargetGenerationException) e;
110: insertErrInfo(error, "Key", tagex.getTargetkey());
111: printEx(tagex.getCause(), doc, root);
112: } else if (e instanceof TransformerException) {
113: TransformerException trex = (TransformerException) e;
114: insertErrInfo(error, "Location", trex.getLocationAsString());
115: printEx(trex.getCause(), doc, root);
116: } else if (e instanceof SAXException) {
117: SAXException saxex = (SAXException) e;
118: printEx(saxex.getException(), doc, root);
119: }
120: }
121:
122: public String toStringRepresentation() {
123: StringBuffer sb = new StringBuffer();
124: printEx(this , sb, " ");
125: return sb.toString();
126: }
127:
128: private void appendStr(StringBuffer buf, String indent, String key,
129: String value) {
130: buf.append("|").append(indent).append(key).append(": ").append(
131: value).append("\n");
132: }
133:
134: private void printEx(Throwable e, StringBuffer buf, String indent) {
135: String br = "\n";
136: if (e == null) {
137: return;
138: }
139: if (e instanceof SAXParseException) {
140: SAXParseException sex = (SAXParseException) e;
141: buf.append("|").append(br);
142: appendStr(buf, indent, "Type", sex.getClass().getName());
143: appendStr(buf, indent, "Message", sex.getMessage());
144: appendStr(buf, indent, "Id", sex.getSystemId());
145: appendStr(buf, indent, "Line", "" + sex.getLineNumber());
146: appendStr(buf, indent, "Column", "" + sex.getColumnNumber());
147: } else if (e instanceof TargetGenerationException) {
148: TargetGenerationException tgex = (TargetGenerationException) e;
149: buf.append("|").append(br);
150: appendStr(buf, indent, "Type", tgex.getClass().getName());
151: appendStr(buf, indent, "Message", tgex.getMessage());
152: appendStr(buf, indent, "Target", tgex.getTargetkey());
153: printEx(tgex.getCause(), buf, indent + " ");
154: } else if (e instanceof TransformerException) {
155: TransformerException trex = (TransformerException) e;
156: buf.append("|").append(br);
157: appendStr(buf, indent, "Type", trex.getClass().getName());
158: appendStr(buf, indent, "Message", trex.getMessage());
159: printEx(trex.getCause(), buf, indent + " ");
160: } else {
161: buf.append("|").append(br);
162: appendStr(buf, indent, "Type", e.getClass().getName());
163: appendStr(buf, indent, "Message", e.getMessage());
164: }
165: //printEx(e, buf, indent + " ");
166: }
167:
168: }
|