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 java.io.ByteArrayInputStream; 65 import java.io.InputStream; 66 67 import org.apache.commons.jelly.JellyException; 68 import org.apache.commons.jelly.MissingAttributeException; 69 import org.apache.commons.jelly.TagSupport; 70 import org.apache.commons.jelly.XMLOutput; 71 72 import org.iso_relax.verifier.Schema; 73 import org.iso_relax.verifier.Verifier; 74 import org.iso_relax.verifier.VerifierFactory; 75 76 import org.xml.sax.ErrorHandler; 77 import org.xml.sax.SAXException; 78 import org.xml.sax.SAXParseException; 79 import org.xml.sax.helpers.AttributesImpl; 80 81 /*** 82 * This tag creates a new Verifier of a schema as a variable 83 * so that it can be used by a <validate> tag. 84 * 85 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 86 * @version $Revision: 1.1 $ 87 */ 88 public class VerifierTag extends TagSupport { 89 90 /*** the variable name to export the Verifier as */ 91 private String var; 92 93 /*** The URI to load the schema from */ 94 private String uri; 95 96 /*** The system ID to use when parsing the schema */ 97 private String systemId; 98 99 /*** The factory used to create new schema verifier objects */ 100 private VerifierFactory factory; 101 102 // Tag interface 103 //------------------------------------------------------------------------- 104 public void doTag(final XMLOutput output) throws Exception { 105 if ( var == null ) { 106 throw new MissingAttributeException("var"); 107 } 108 109 if ( factory == null ) { 110 factory = new com.sun.msv.verifier.jarv.TheFactoryImpl(); 111 } 112 InputStream in = null; 113 if ( uri != null ) { 114 in = context.getResourceAsStream( uri ); 115 if ( in == null ) { 116 throw new JellyException( "Could not find resource for uri: " + uri ); 117 } 118 } 119 else { 120 String text = getBodyText(); 121 byte[] data = text.getBytes(); 122 in = new ByteArrayInputStream( text.getBytes() ); 123 } 124 125 Schema schema = null; 126 if (systemId != null) { 127 schema = factory.compileSchema(in, systemId); 128 } 129 else if ( uri != null ) { 130 schema = factory.compileSchema(in, uri); 131 } 132 else{ 133 schema = factory.compileSchema(in); 134 } 135 if ( schema == null ) { 136 throw new JellyException( "Could not create a valid schema" ); 137 } 138 Verifier verifier = schema.newVerifier(); 139 context.setVariable(var, verifier); 140 } 141 142 // Properties 143 //------------------------------------------------------------------------- 144 145 /*** 146 * Sets the name of the variable that will be set to the new Verifier 147 * 148 * @jelly:required 149 */ 150 public void setVar(String var) { 151 this.var = var; 152 } 153 154 /*** 155 * Sets the URI of the schema file to parse. If no URI is specified then the 156 * body of this tag is used as the source of the schema 157 * 158 * @jelly:optional 159 */ 160 public void setUri(String uri) { 161 this.uri = uri; 162 } 163 164 /*** 165 * Sets the system ID used when parsing the schema 166 * 167 * @jelly:optional 168 */ 169 public void setSystemId(String systemId) { 170 this.systemId = systemId; 171 } 172 173 /*** 174 * Sets the factory used to create new schema verifier objects. 175 * If none is provided then the default MSV factory is used. 176 * 177 * @jelly:optional 178 */ 179 public void setFactory(VerifierFactory factory) { 180 this.factory = factory; 181 } 182 183 // Implementation methods 184 //------------------------------------------------------------------------- 185 186 187 }

This page was automatically generated by Maven