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