View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      public AssertValidTag() {
40          setErrorHandler(
41              new ErrorHandler() {
42                  public void error(SAXParseException exception) throws SAXException {
43                      outputException(output, "error", exception);
44                  }
45  
46                  public void fatalError(SAXParseException exception) throws SAXException {
47                      outputException(output, "fatalError", exception);
48                  }
49  
50                  public void warning(SAXParseException exception) throws SAXException {
51                      outputException(output, "warning", exception);
52                  }
53              }
54          );
55      }
56  */
57      // Tag interface
58      //-------------------------------------------------------------------------
59      public void doTag(final XMLOutput output) throws JellyTagException {
60          buffer.setLength(0);
61          super.doTag(output);
62      }
63  
64      // Implementation methods
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  }