1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.validate;
17
18 import org.apache.commons.jelly.JellyTagException;
19 import org.apache.commons.jelly.XMLOutput;
20 import org.apache.commons.jelly.tags.junit.JellyAssertionFailedError;
21
22 import org.xml.sax.SAXException;
23 import org.xml.sax.SAXParseException;
24
25 /***
26 * This tag performs an assertion that the tags body contains XML
27 * which matches a givem schema validation. This tag is used with
28 * JellyUnit to implement an assertion.
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 155420 $
32 */
33 public class AssertValidTag extends ValidateTag {
34
35 private StringBuffer buffer = new StringBuffer();
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public void doTag(final XMLOutput output) throws JellyTagException {
60 buffer.setLength(0);
61 super.doTag(output);
62 }
63
64
65
66
67 /***
68 * Processes whether or not the document is valid.
69 * Derived classes can overload this method to do different things, such
70 * as to throw assertion exceptions etc.
71 */
72 protected void handleValid(boolean valid) {
73 super.handleValid(valid);
74
75 if ( ! valid ) {
76 String message = buffer.toString();
77 throw new JellyAssertionFailedError( "The XML is not valid according to the schema: " + message );
78 }
79 }
80
81 /***
82 * Outputs the given validation exception as XML to the output
83 */
84 protected void outputException(XMLOutput output, String name, SAXParseException e) throws SAXException {
85 buffer.append( name );
86 buffer.append( " : line: " );
87 buffer.append( e.getLineNumber() );
88 buffer.append( " column: " );
89 buffer.append( e.getColumnNumber() );
90 buffer.append( " message: " );
91 buffer.append( e.getMessage() );
92 buffer.append( '\n' );
93 }
94
95 }