| 1 | package org.apache.velocity.app.event; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2001,2004 The Apache Software Foundation. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import org.apache.velocity.context.InternalEventContext; |
| 20 | import org.apache.velocity.context.Context; |
| 21 | |
| 22 | /** |
| 23 | * 'Package' of event handlers... |
| 24 | * |
| 25 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| 26 | * @author <a href="mailto:j_a_fernandez@yahoo.com">Jose Alberto Fernandez</a> |
| 27 | * @version $Id: EventCartridge.java,v 1.3.4.1 2004/03/03 23:22:53 geirm Exp $ |
| 28 | */ |
| 29 | public class EventCartridge implements ReferenceInsertionEventHandler, |
| 30 | NullSetEventHandler, |
| 31 | MethodExceptionEventHandler |
| 32 | { |
| 33 | private ReferenceInsertionEventHandler rieh = null; |
| 34 | private NullSetEventHandler nseh = null; |
| 35 | private MethodExceptionEventHandler meeh = null; |
| 36 | |
| 37 | /** |
| 38 | * Adds an event handler(s) to the Cartridge. This method |
| 39 | * will find all possible event handler interfaces supported |
| 40 | * by the passed in object. |
| 41 | * |
| 42 | * @param ev object impementing a valid EventHandler-derived interface |
| 43 | * @return true if a supported interface, false otherwise or if null |
| 44 | */ |
| 45 | public boolean addEventHandler( EventHandler ev ) |
| 46 | { |
| 47 | if (ev == null) |
| 48 | { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | boolean found = false; |
| 53 | |
| 54 | if ( ev instanceof ReferenceInsertionEventHandler) |
| 55 | { |
| 56 | rieh = (ReferenceInsertionEventHandler) ev; |
| 57 | found = true; |
| 58 | } |
| 59 | |
| 60 | if ( ev instanceof NullSetEventHandler ) |
| 61 | { |
| 62 | nseh = (NullSetEventHandler) ev; |
| 63 | found = true; |
| 64 | } |
| 65 | |
| 66 | if ( ev instanceof MethodExceptionEventHandler ) |
| 67 | { |
| 68 | meeh = (MethodExceptionEventHandler) ev; |
| 69 | found = true; |
| 70 | } |
| 71 | |
| 72 | return found; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Removes an event handler(s) from the Cartridge. This method |
| 77 | * will find all possible event handler interfaces supported |
| 78 | * by the passed in object and remove them. |
| 79 | * |
| 80 | * @param ev object impementing a valid EventHandler-derived interface |
| 81 | * @return true if a supported interface, false otherwise or if null |
| 82 | */ |
| 83 | public boolean removeEventHandler(EventHandler ev) |
| 84 | { |
| 85 | if ( ev == null ) |
| 86 | { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | boolean found = false; |
| 91 | |
| 92 | if (ev == rieh) |
| 93 | { |
| 94 | rieh = null; |
| 95 | found = true; |
| 96 | } |
| 97 | |
| 98 | if (ev == nseh) |
| 99 | { |
| 100 | nseh = null; |
| 101 | found = true; |
| 102 | } |
| 103 | |
| 104 | if (ev == meeh) |
| 105 | { |
| 106 | meeh = null; |
| 107 | found = true; |
| 108 | } |
| 109 | |
| 110 | return found; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Implementation of ReferenceInsertionEventHandler method |
| 115 | * <code>referenceInsert()</code>. |
| 116 | * |
| 117 | * Called during Velocity merge before a reference value will |
| 118 | * be inserted into the output stream. |
| 119 | * |
| 120 | * @param reference reference from template about to be inserted |
| 121 | * @param value value about to be inserted (after toString() ) |
| 122 | * @return Object on which toString() should be called for output. |
| 123 | */ |
| 124 | public Object referenceInsert( String reference, Object value ) |
| 125 | { |
| 126 | if (rieh == null) |
| 127 | { |
| 128 | return value; |
| 129 | } |
| 130 | |
| 131 | return rieh.referenceInsert( reference, value ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Implementation of NullSetEventHandler method |
| 136 | * <code>shouldLogOnNullSet()</code>. |
| 137 | * |
| 138 | * Called during Velocity merge to determine if when |
| 139 | * a #set() results in a null assignment, a warning |
| 140 | * is logged. |
| 141 | * |
| 142 | * @param reference reference from template about to be inserted |
| 143 | * @return true if to be logged, false otherwise |
| 144 | */ |
| 145 | public boolean shouldLogOnNullSet( String lhs, String rhs ) |
| 146 | { |
| 147 | if ( nseh == null) |
| 148 | { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | return nseh.shouldLogOnNullSet( lhs, rhs ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Implementation of MethodExceptionEventHandler method |
| 157 | * <code>methodException()</code>. |
| 158 | * |
| 159 | * Called during Velocity merge if a reference is null |
| 160 | * |
| 161 | * @param claz Class that is causing the exception |
| 162 | * @param method method called that causes the exception |
| 163 | * @param e Exception thrown by the method |
| 164 | * @return Object to return as method result |
| 165 | * @throws exception to be wrapped and propogated to app |
| 166 | */ |
| 167 | public Object methodException( Class claz, String method, Exception e ) |
| 168 | throws Exception |
| 169 | { |
| 170 | /* |
| 171 | * if we don't have a handler, just throw what we were handed |
| 172 | */ |
| 173 | if (meeh == null) |
| 174 | { |
| 175 | throw e; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * otherwise, call it.. |
| 180 | */ |
| 181 | return meeh.methodException( claz, method, e ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Attached the EventCartridge to the context |
| 186 | * |
| 187 | * Final because not something one should mess with lightly :) |
| 188 | * |
| 189 | * @param context context to attach to |
| 190 | * @return true if successful, false otherwise |
| 191 | */ |
| 192 | public final boolean attachToContext( Context context ) |
| 193 | { |
| 194 | if ( context instanceof InternalEventContext ) |
| 195 | { |
| 196 | InternalEventContext iec = (InternalEventContext) context; |
| 197 | |
| 198 | iec.attachEventCartridge( this ); |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | } |