View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/taglibs/beanshell/src/java/org/apache/commons/jelly/tags/beanshell/BeanShellExpressionFactory.java,v 1.1 2002/05/21 07:58:55 jstrachan Exp $ 3 * $Revision: 1.1 $ 4 * $Date: 2002/05/21 07:58:55 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: BeanShellExpressionFactory.java,v 1.1 2002/05/21 07:58:55 jstrachan Exp $ 61 */ 62 package org.apache.commons.jelly.tags.validate; 63 64 import org.apache.commons.jelly.JellyException; 65 import org.apache.commons.jelly.MissingAttributeException; 66 import org.apache.commons.jelly.TagSupport; 67 import org.apache.commons.jelly.XMLOutput; 68 69 import org.iso_relax.verifier.Verifier; 70 import org.iso_relax.verifier.VerifierFilter; 71 import org.iso_relax.verifier.VerifierHandler; 72 73 import org.xml.sax.ContentHandler; 74 import org.xml.sax.ErrorHandler; 75 import org.xml.sax.SAXException; 76 import org.xml.sax.SAXParseException; 77 import org.xml.sax.helpers.AttributesImpl; 78 79 /*** 80 * This tag validates its body using a schema Verifier which can 81 * validate against DTDs, XML Schema, RelaxNG, Relax or TREX. 82 * Any JARV compliant Verifier could be used. 83 * The error messages are output as XML events so that they can be styled by the parent tag. 84 * 85 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 86 * @version $Revision: 1.1 $ 87 */ 88 public class ValidateTag extends TagSupport { 89 90 /*** The verifier that this tag will use */ 91 private Verifier verifier; 92 93 /*** The SAX ErrorHandler */ 94 private ErrorHandler errorHandler; 95 96 /*** The boolean flag for whether the XML is valid */ 97 private String var; 98 99 // Tag interface 100 //------------------------------------------------------------------------- 101 public void doTag(final XMLOutput output) throws Exception { 102 if ( verifier == null ) { 103 throw new MissingAttributeException("verifier"); 104 } 105 boolean valid = false; 106 107 // evaluate the body using the current Verifier 108 if ( errorHandler != null ) { 109 // we are redirecting errors to another handler 110 // so just filter the body 111 VerifierFilter filter = verifier.getVerifierFilter(); 112 113 // now install the current output in the filter chain... 114 // #### 115 116 ContentHandler handler = filter.getContentHandler(); 117 handler.startDocument(); 118 invokeBody( new XMLOutput( handler ) ); 119 handler.endDocument(); 120 valid = filter.isValid(); 121 } 122 else { 123 // outputting the errors to the current output 124 verifier.setErrorHandler( 125 new ErrorHandler() { 126 public void error(SAXParseException exception) throws SAXException { 127 outputException(output, "error", exception); 128 } 129 130 public void fatalError(SAXParseException exception) throws SAXException { 131 outputException(output, "fatalError", exception); 132 } 133 134 public void warning(SAXParseException exception) throws SAXException { 135 outputException(output, "warning", exception); 136 } 137 } 138 ); 139 140 VerifierHandler handler = verifier.getVerifierHandler(); 141 handler.startDocument(); 142 invokeBody( new XMLOutput( handler ) ); 143 handler.endDocument(); 144 valid = handler.isValid(); 145 } 146 handleValid(valid); 147 } 148 149 // Properties 150 //------------------------------------------------------------------------- 151 152 /*** 153 * Sets the schema Verifier that this tag will use to verify its body 154 * 155 * @jelly:required 156 */ 157 public void setVerifier(Verifier verifier) { 158 this.verifier = verifier; 159 } 160 161 /*** 162 * @return the ErrorHandler used when validating 163 */ 164 public ErrorHandler getErrorHandler() { 165 return errorHandler; 166 } 167 168 /*** 169 * Sets the SAX ErrorHandler which is used to capture 170 * XML validation events. 171 * If an ErrorHandler is specified 172 * then this tag will output its body and redirect all error messages 173 * to the ErrorHandler. 174 * If no ErrorHandler is specified then this tag will just output the 175 * error messages as XML events 176 * 177 * @jelly:optional 178 */ 179 public void setErrorHandler(ErrorHandler errorHandler) { 180 this.errorHandler = errorHandler; 181 } 182 183 /*** 184 * Sets the name of the variable that will contain a boolean flag for whether or 185 * not the XML is valid. 186 * 187 * @jelly:optional 188 */ 189 public void setVar(String var) { 190 this.var = var; 191 } 192 193 // Implementation methods 194 //------------------------------------------------------------------------- 195 196 /*** 197 * Processes whether or not the document is valid. 198 * Derived classes can overload this method to do different things, such 199 * as to throw assertion exceptions etc. 200 */ 201 protected void handleValid(boolean valid) throws Exception { 202 if (var != null ) { 203 Boolean value = (valid) ? Boolean.TRUE : Boolean.FALSE; 204 context.setVariable(var, value); 205 } 206 } 207 208 /*** 209 * Outputs the given validation exception as XML to the output 210 */ 211 protected void outputException(XMLOutput output, String name, SAXParseException e) throws SAXException { 212 AttributesImpl attributes = new AttributesImpl(); 213 String uri = ""; 214 String type = "CDATA"; 215 attributes.addAttribute( uri, "column", "column", type, Integer.toString( e.getColumnNumber() ) ); 216 attributes.addAttribute( uri, "line", "line", type, Integer.toString( e.getLineNumber() ) ); 217 218 String publicID = e.getPublicId(); 219 if ( publicID != null && publicID.length() > 0 ) { 220 attributes.addAttribute( uri, "publicID", "publicID", type, publicID ); 221 } 222 String systemID = e.getSystemId(); 223 if ( systemID != null && systemID.length() > 0 ) { 224 attributes.addAttribute( uri, "systemID", "systemID", type, systemID ); 225 } 226 227 output.startElement( uri, name, name, attributes ); 228 output.write( e.getMessage() ); 229 output.endElement( uri, name, name ); 230 } 231 232 233 }

This page was automatically generated by Maven