001: package org.apache.velocity.app.event.implement;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.apache.velocity.app.event.InvalidReferenceEventHandler;
026: import org.apache.velocity.context.Context;
027: import org.apache.velocity.exception.ParseErrorException;
028: import org.apache.velocity.runtime.RuntimeServices;
029: import org.apache.velocity.util.RuntimeServicesAware;
030: import org.apache.velocity.util.introspection.Info;
031:
032: /**
033: * Use this event handler to flag invalid references. Since this
034: * is intended to be used for a specific request, this should be
035: * used as a local event handler attached to a specific context
036: * instead of being globally defined in the Velocity properties file.
037: *
038: * <p>
039: * Note that InvalidReferenceHandler can be used
040: * in two modes. If the Velocity properties file contains the following:
041: * <pre>
042: * eventhandler.invalidreference.exception = true
043: * </pre>
044: * then the event handler will throw a ParseErrorRuntimeException upon
045: * hitting the first invalid reference. This stops processing and is
046: * passed through to the application code. The ParseErrorRuntimeException
047: * contain information about the template name, line number, column number,
048: * and invalid reference.
049: *
050: * <p>
051: * If this configuration setting is false or omitted then the page
052: * will be processed as normal, but all invalid references will be collected
053: * in a List of InvalidReferenceInfo objects.
054: *
055: * <p>This feature should be regarded as experimental.
056: *
057: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
058: * @version $Id: ReportInvalidReferences.java 470265 2006-11-02 07:45:02Z wglass $
059: */
060: public class ReportInvalidReferences implements
061: InvalidReferenceEventHandler, RuntimeServicesAware {
062:
063: public static final String EVENTHANDLER_INVALIDREFERENCE_EXCEPTION = "eventhandler.invalidreference.exception";
064:
065: /**
066: * List of InvalidReferenceInfo objects
067: */
068: List invalidReferences = new ArrayList();
069:
070: /**
071: * If true, stop at the first invalid reference and throw an exception.
072: */
073: private boolean stopOnFirstInvalidReference = false;
074:
075: /**
076: * Collect the error and/or throw an exception, depending on configuration.
077: *
078: * @param context the context when the reference was found invalid
079: * @param reference string with complete invalid reference
080: * @param object the object referred to, or null if not found
081: * @param property the property name from the reference
082: * @param info contains template, line, column details
083: * @return always returns null
084: * @throws ParseErrorException
085: */
086: public Object invalidGetMethod(Context context, String reference,
087: Object object, String property, Info info) {
088: reportInvalidReference(reference, info);
089: return null;
090: }
091:
092: /**
093: * Collect the error and/or throw an exception, depending on configuration.
094: *
095: * @param context the context when the reference was found invalid
096: * @param reference complete invalid reference
097: * @param object the object referred to, or null if not found
098: * @param method the property name from the reference
099: * @param info contains template, line, column details
100: * @return always returns null
101: * @throws ParseErrorException
102: */
103: public Object invalidMethod(Context context, String reference,
104: Object object, String method, Info info) {
105: if (reference == null) {
106: reportInvalidReference(object.getClass().getName() + "."
107: + method, info);
108: } else {
109: reportInvalidReference(reference, info);
110: }
111: return null;
112: }
113:
114: /**
115: * Collect the error and/or throw an exception, depending on configuration.
116: *
117: * @param context the context when the reference was found invalid
118: * @param leftreference left reference being assigned to
119: * @param rightreference invalid reference on the right
120: * @param info contains info on template, line, col
121: * @return loop to end -- always returns false
122: */
123: public boolean invalidSetMethod(Context context,
124: String leftreference, String rightreference, Info info) {
125: reportInvalidReference(leftreference, info);
126: return false;
127: }
128:
129: /**
130: * Check for an invalid reference and collect the error or throw an exception
131: * (depending on configuration).
132: *
133: * @param reference the invalid reference
134: * @param info line, column, template name
135: */
136: private void reportInvalidReference(String reference, Info info) {
137: InvalidReferenceInfo invalidReferenceInfo = new InvalidReferenceInfo(
138: reference, info);
139: invalidReferences.add(invalidReferenceInfo);
140:
141: if (stopOnFirstInvalidReference) {
142: throw new ParseErrorException(
143: "Error in page - invalid reference. ", info,
144: invalidReferenceInfo.getInvalidReference());
145: }
146: }
147:
148: /**
149: * All invalid references during the processing of this page.
150: * @return a List of InvalidReferenceInfo objects
151: */
152: public List getInvalidReferences() {
153: return invalidReferences;
154: }
155:
156: /**
157: * Called automatically when event cartridge is initialized.
158: * @param rs RuntimeServices object assigned during initialization
159: */
160: public void setRuntimeServices(RuntimeServices rs) {
161: stopOnFirstInvalidReference = rs.getConfiguration().getBoolean(
162: EVENTHANDLER_INVALIDREFERENCE_EXCEPTION, false);
163: }
164:
165: }
|