1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package javax.servlet.jsp.tagext; 19 20 import java.util.Map; 21 22 /** 23 * Translation-time validator class for a JSP page. 24 * A validator operates on the XML view associated with the JSP page. 25 * 26 * <p> 27 * The TLD file associates a TagLibraryValidator class and some init 28 * arguments with a tag library. 29 * 30 * <p> 31 * The JSP container is reponsible for locating an appropriate 32 * instance of the appropriate subclass by 33 * 34 * <ul> 35 * <li> new a fresh instance, or reuse an available one 36 * <li> invoke the setInitParams(Map) method on the instance 37 * </ul> 38 * 39 * once initialized, the validate(String, String, PageData) method will 40 * be invoked, where the first two arguments are the prefix 41 * and uri for this tag library in the XML View. The prefix is intended 42 * to make it easier to produce an error message. However, it is not 43 * always accurate. In the case where a single URI is mapped to more 44 * than one prefix in the XML view, the prefix of the first URI is provided. 45 * Therefore, to provide high quality error messages in cases where the 46 * tag elements themselves are checked, the prefix parameter should be 47 * ignored and the actual prefix of the element should be used instead. 48 * TagLibraryValidators should always use the uri to identify elements 49 * as beloning to the tag library, not the prefix. 50 * 51 * <p> 52 * A TagLibraryValidator instance 53 * may create auxiliary objects internally to perform 54 * the validation (e.g. an XSchema validator) and may reuse it for all 55 * the pages in a given translation run. 56 * 57 * <p> 58 * The JSP container is not guaranteed to serialize invocations of 59 * validate() method, and TagLibraryValidators should perform any 60 * synchronization they may require. 61 * 62 * <p> 63 * As of JSP 2.0, a JSP container must provide a jsp:id attribute to 64 * provide higher quality validation errors. 65 * The container will track the JSP pages 66 * as passed to the container, and will assign to each element 67 * a unique "id", which is passed as the value of the jsp:id 68 * attribute. Each XML element in the XML view available will 69 * be extended with this attribute. The TagLibraryValidator 70 * can then use the attribute in one or more ValidationMessage 71 * objects. The container then, in turn, can use these 72 * values to provide more precise information on the location 73 * of an error. 74 * 75 * <p> 76 * The actual prefix of the <code>id</code> attribute may or may not be 77 * <code>jsp</code> but it will always map to the namespace 78 * <code>http://java.sun.com/JSP/Page</code>. A TagLibraryValidator 79 * implementation must rely on the uri, not the prefix, of the <code>id</code> 80 * attribute. 81 */ 82 83 abstract public class TagLibraryValidator { 84 85 /** 86 * Sole constructor. (For invocation by subclass constructors, 87 * typically implicit.) 88 */ 89 public TagLibraryValidator() { 90 } 91 92 /** 93 * Set the init data in the TLD for this validator. 94 * Parameter names are keys, and parameter values are the values. 95 * 96 * @param map A Map describing the init parameters 97 */ 98 public void setInitParameters(Map<String, Object> map) { 99 initParameters = map; 100 } 101 102 103 /** 104 * Get the init parameters data as an immutable Map. 105 * Parameter names are keys, and parameter values are the values. 106 * 107 * @return The init parameters as an immutable map. 108 */ 109 public Map<String, Object> getInitParameters() { 110 return initParameters; 111 } 112 113 /** 114 * Validate a JSP page. 115 * This will get invoked once per unique tag library URI in the 116 * XML view. This method will return null if the page is valid; otherwise 117 * the method should return an array of ValidationMessage objects. 118 * An array of length zero is also interpreted as no errors. 119 * 120 * @param prefix the first prefix with which the tag library is 121 * associated, in the XML view. Note that some tags may use 122 * a different prefix if the namespace is redefined. 123 * @param uri the tag library's unique identifier 124 * @param page the JspData page object 125 * @return A null object, or zero length array if no errors, an array 126 * of ValidationMessages otherwise. 127 */ 128 public ValidationMessage[] validate(String prefix, String uri, 129 PageData page) 130 { 131 return null; 132 } 133 134 /** 135 * Release any data kept by this instance for validation purposes. 136 */ 137 public void release() { 138 initParameters = null; 139 } 140 141 // Private data 142 private Map<String, Object> initParameters; 143 144 }