001 /*
002 * Copyright 2004 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package javax.servlet.jsp.tagext;
018
019 import java.util.Map;
020
021 /**
022 * Translation-time validator class for a JSP page.
023 * A validator operates on the XML view associated with the JSP page.
024 *
025 * <p>
026 * The TLD file associates a TagLibraryValidator class and some init
027 * arguments with a tag library.
028 *
029 * <p>
030 * The JSP container is reponsible for locating an appropriate
031 * instance of the appropriate subclass by
032 *
033 * <ul>
034 * <li> new a fresh instance, or reuse an available one
035 * <li> invoke the setInitParams(Map) method on the instance
036 * </ul>
037 *
038 * once initialized, the validate(String, String, PageData) method will
039 * be invoked, where the first two arguments are the prefix
040 * and uri for this tag library in the XML View. The prefix is intended
041 * to make it easier to produce an error message. However, it is not
042 * always accurate. In the case where a single URI is mapped to more
043 * than one prefix in the XML view, the prefix of the first URI is provided.
044 * Therefore, to provide high quality error messages in cases where the
045 * tag elements themselves are checked, the prefix parameter should be
046 * ignored and the actual prefix of the element should be used instead.
047 * TagLibraryValidators should always use the uri to identify elements
048 * as beloning to the tag library, not the prefix.
049 *
050 * <p>
051 * A TagLibraryValidator instance
052 * may create auxiliary objects internally to perform
053 * the validation (e.g. an XSchema validator) and may reuse it for all
054 * the pages in a given translation run.
055 *
056 * <p>
057 * The JSP container is not guaranteed to serialize invocations of
058 * validate() method, and TagLibraryValidators should perform any
059 * synchronization they may require.
060 *
061 * <p>
062 * As of JSP 2.0, a JSP container must provide a jsp:id attribute to
063 * provide higher quality validation errors.
064 * The container will track the JSP pages
065 * as passed to the container, and will assign to each element
066 * a unique "id", which is passed as the value of the jsp:id
067 * attribute. Each XML element in the XML view available will
068 * be extended with this attribute. The TagLibraryValidator
069 * can then use the attribute in one or more ValidationMessage
070 * objects. The container then, in turn, can use these
071 * values to provide more precise information on the location
072 * of an error.
073 *
074 * <p>
075 * The actual prefix of the <code>id</code> attribute may or may not be
076 * <code>jsp</code> but it will always map to the namespace
077 * <code>http://java.sun.com/JSP/Page</code>. A TagLibraryValidator
078 * implementation must rely on the uri, not the prefix, of the <code>id</code>
079 * attribute.
080 */
081
082 abstract public class TagLibraryValidator {
083
084 /**
085 * Sole constructor. (For invocation by subclass constructors,
086 * typically implicit.)
087 */
088 public TagLibraryValidator() {
089 }
090
091 /**
092 * Set the init data in the TLD for this validator.
093 * Parameter names are keys, and parameter values are the values.
094 *
095 * @param map A Map describing the init parameters
096 */
097 public void setInitParameters(Map map) {
098 initParameters = map;
099 }
100
101 /**
102 * Get the init parameters data as an immutable Map.
103 * Parameter names are keys, and parameter values are the values.
104 *
105 * @return The init parameters as an immutable map.
106 */
107 public Map getInitParameters() {
108 return initParameters;
109 }
110
111 /**
112 * Validate a JSP page.
113 * This will get invoked once per unique tag library URI in the
114 * XML view. This method will return null if the page is valid; otherwise
115 * the method should return an array of ValidationMessage objects.
116 * An array of length zero is also interpreted as no errors.
117 *
118 * @param prefix the first prefix with which the tag library is
119 * associated, in the XML view. Note that some tags may use
120 * a different prefix if the namespace is redefined.
121 * @param uri the tag library's unique identifier
122 * @param page the JspData page object
123 * @return A null object, or zero length array if no errors, an array
124 * of ValidationMessages otherwise.
125 */
126 public ValidationMessage[] validate(String prefix, String uri,
127 PageData page) {
128 return null;
129 }
130
131 /**
132 * Release any data kept by this instance for validation purposes.
133 */
134 public void release() {
135 initParameters = null;
136 }
137
138 // Private data
139 private Map initParameters;
140
141 }
|