|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
XMLException.java | 83.3% | 89.5% | 60% | 85.5% |
|
1 |
package junit.extensions.xml;
|
|
2 |
|
|
3 |
import org.w3c.dom.Element;
|
|
4 |
import org.w3c.dom.Node;
|
|
5 |
|
|
6 |
import java.io.ByteArrayOutputStream;
|
|
7 |
import java.io.IOException;
|
|
8 |
import java.io.PrintWriter;
|
|
9 |
|
|
10 |
|
|
11 |
/**
|
|
12 |
* This exception is thrown when the XML cannot be parsed properly.
|
|
13 |
*
|
|
14 |
* @author Kevin Wilson
|
|
15 |
*/
|
|
16 |
public class XMLException extends RuntimeException { |
|
17 |
/**
|
|
18 |
* Element which could not be processed.
|
|
19 |
*/
|
|
20 |
private Element m_element;
|
|
21 |
|
|
22 |
/**
|
|
23 |
* Exception which caused the error.
|
|
24 |
*/
|
|
25 |
private Throwable m_cause;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* Properties which may have been passed to the element
|
|
29 |
* which could not be processed.
|
|
30 |
*/
|
|
31 |
private XMLObjectCache m_properties;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Constructor accepting a String message.
|
|
35 |
* @param msg Message of the exception.
|
|
36 |
* @see java.lang.Throwable#Throwable(String)
|
|
37 |
*/
|
|
38 | 0 |
public XMLException(final String msg) {
|
39 | 0 |
this(msg, null, null, null); |
40 |
} |
|
41 |
|
|
42 |
/**
|
|
43 |
* Constructor accepting a String message.
|
|
44 |
* @param msg Message of the exception.
|
|
45 |
* @param e Element which throw the exception.
|
|
46 |
* @see java.lang.Throwable#Throwable(String)
|
|
47 |
*/
|
|
48 | 0 |
public XMLException(final String msg, final Element e) {
|
49 | 0 |
this(msg, null, e, null); |
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Constructor accepting a String message.
|
|
54 |
* @param msg Message of the exception.
|
|
55 |
* @param cause Exception or error which caused this exception
|
|
56 |
* to be thrown.
|
|
57 |
* @param element Element which throw the exception.
|
|
58 |
* @param properties Properties cache containing the values
|
|
59 |
* which were passed to the tag handler.
|
|
60 |
* @see java.lang.Throwable#Throwable(String)
|
|
61 |
*/
|
|
62 | 32 |
public XMLException(final String msg, final Throwable cause,
|
63 |
final Element element, final XMLObjectCache properties) { |
|
64 |
/** @todo when depricating 1.3 */
|
|
65 |
|
|
66 |
// super(msg, cause);
|
|
67 | 32 |
super(msg);
|
68 |
|
|
69 |
/** @todo remove when depricating 1.3 and implementing the above. */
|
|
70 | 32 |
m_cause = cause; |
71 | 32 |
m_element = element; |
72 | 32 |
m_properties = properties; |
73 |
} |
|
74 |
|
|
75 |
/**
|
|
76 |
* Get the element which through the exception.
|
|
77 |
* @return Element which through the exception.
|
|
78 |
*/
|
|
79 | 5 |
public Element getElement() {
|
80 | 5 |
return m_element;
|
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* Add context from the element.
|
|
85 |
* @return String representing the context of the exception.
|
|
86 |
*/
|
|
87 | 2 |
public String toString() {
|
88 | 2 |
try {
|
89 | 2 |
ByteArrayOutputStream os = new ByteArrayOutputStream(50000);
|
90 | 2 |
PrintWriter pw = new PrintWriter(os);
|
91 | 2 |
pw.println(super.toString());
|
92 |
|
|
93 | 2 |
XMLWriter xmlw = new XMLWriter(true); |
94 | 2 |
xmlw.setOutput(pw); |
95 |
|
|
96 | 2 |
Element e = getElement(); |
97 |
|
|
98 | 2 |
if (e != null) { |
99 | 1 |
pw.println("While processing element:");
|
100 | 1 |
xmlw.write(e); |
101 | 1 |
pw.println(); |
102 |
|
|
103 | 1 |
Node parent = e.getParentNode(); |
104 |
|
|
105 | 1 |
if (parent != null) { |
106 | 1 |
pw.println("Within:");
|
107 | 1 |
xmlw.write(parent); |
108 | 1 |
pw.println(); |
109 |
} |
|
110 |
|
|
111 | 1 |
if (m_properties != null) { |
112 | 1 |
String[] names = m_properties.getNames(); |
113 | 1 |
boolean first = true; |
114 |
|
|
115 | 1 |
for (int i = 0; i < names.length; i++) { |
116 | 3 |
if ((names[i] != null) && !names[i].startsWith("../")) { |
117 | 2 |
if (first) {
|
118 | 1 |
pw.println("Local Parameters:");
|
119 | 1 |
first = false;
|
120 |
} |
|
121 |
|
|
122 | 2 |
Object value = m_properties.get(names[i]); |
123 | 2 |
pw.println(names[i] + " = " + value);
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 | 2 |
pw.flush(); |
130 | 2 |
os.close(); |
131 |
|
|
132 | 2 |
return os.toString();
|
133 |
} catch (IOException ioe) {
|
|
134 | 0 |
ioe.printStackTrace(); |
135 |
} |
|
136 |
|
|
137 | 0 |
return super.toString(); |
138 |
} |
|
139 |
} |
|