Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package freemarker.ext.jsp;
 
 import java.util.Map;
 
 
 {
     protected final String tagName;
     private final Class tagClass;
     private final Method dynaSetter;
     private final Map propertySetters = new HashMap();
     
     protected JspTagModelBase(String tagNameClass tagClassthrows IntrospectionException {
         this. = tagName;
         this. = tagClass;
         BeanInfo bi = Introspector.getBeanInfo(tagClass);
         PropertyDescriptor[] pda = bi.getPropertyDescriptors();
         for (int i = 0; i < pda.lengthi++) {
             PropertyDescriptor pd = pda[i];
             Method m = pd.getWriteMethod();
             if(m != null) {
                 .put(pd.getName(), m);
             }
         }
         // Check to see if the tag implements the JSP2.0 DynamicAttributes
         // interface, to allow setting of arbitrary attributes
         Method dynaSetter;
         try {
             dynaSetter = tagClass.getMethod("setDynamicAttribute",
                             new Class[] {String.classString.classObject.class});
         }
         catch (NoSuchMethodException nsme) {
             dynaSetter = null;
         }
         this. = dynaSetter;
     }
     
         return .newInstance();
     }
     
     void setupTag(Object tagMap argsObjectWrapper wrapper)
     throws 
         TemplateModelException
         InvocationTargetException
         IllegalAccessException
     {
         if(args != null && !args.isEmpty()) {
             ObjectWrapperAndUnwrapper unwrapper = 
                     wrapper instanceof ObjectWrapperAndUnwrapper ? (ObjectWrapperAndUnwrapperwrapper
                             : BeansWrapper.getDefaultInstance();  // [2.4] Throw exception in this case
             final Object[] argArray = new Object[1];
             for (Iterator iter = args.entrySet().iterator(); iter.hasNext();)
             {
                 final Map.Entry entry = (Map.Entryiter.next();
                 final Object arg = unwrapper.unwrap((TemplateModelentry.getValue());
                 argArray[0] = arg;
                 final Object paramName = entry.getKey();
                 Method setterMethod = (Method).get(paramName);
                 if (setterMethod == null) {
                     if ( == null) {
                         throw new TemplateModelException("Unknown property "
                                + StringUtil.jQuote(paramName.toString())
                                + " on instance of " + .getName());
                    }
                    else {
                        .invoke(tagnew Object[] {nullparamNameargArray[0]});
                    }
                }
                else {
                    if(arg instanceof BigDecimal) {
                        argArray[0] = BeansWrapper.coerceBigDecimal(
                                (BigDecimal)argsetterMethod.getParameterTypes()[0]);
                    }
                    try {
                        setterMethod.invoke(tagargArray);
                    } catch (Exception e) {
                        final Class setterType = setterMethod.getParameterTypes()[0];
                        final _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(new Object[] {
                                "Failed to set JSP tag parameter "new _DelayedJQuote(paramName),
                                " (declared type: "new _DelayedShortClassName(setterType)
                                + ", actual value's type: ",
                                (argArray[0] != null
                                        ? (Objectnew _DelayedShortClassName(argArray[0].getClass()) : "Null"),
                                "). See cause exception for the more specific cause..."
                                });
                        if (e instanceof IllegalArgumentException && !(setterType.isAssignableFrom(String.class))
                                && argArray[0] != null && argArray[0] instanceof String) {
                            desc.tip(new Object[] {
                                    "This problem is often caused by unnecessary parameter quotation. Paramters "
                                    + "aren't quoted in FTL, similarly as they aren't quoted in most languages. "
                                    + "For example, these parameter assignments are wrong: ",
                                    "<@my.tag p1=\"true\" p2=\"10\" p3=\"${someVariable}\" p4=\"${x+1}\" />",
                                    ". The correct form is: ",
                                    "<@my.tag p1=true p2=10 p3=someVariable p4=x+1 />",
                                    ". Only string literals are quoted (regardless of where they occur): ",
                                    "<@my.box style=\"info\" message=\"Hello ${name}!\" width=200 />",
                                    "."});
                        }
                        throw new _TemplateModelException(enulldesc);
                    }
                }
            }
        }
    }
        if (e instanceof RuntimeException && !isCommonRuntimeException((RuntimeExceptione)) {
            throw (RuntimeExceptione;
        }
        if (e instanceof TemplateModelException) {
            throw (TemplateModelExceptione;
        }
        if (e instanceof TemplateExceptionWrapperJspException) {
            return (TemplateModelExceptione.getCause();
        }
        return new _TemplateModelException(enew Object[] {
                "Error while invoking the "new _DelayedJQuote(), " JSP custom tag; see cause exception" });
    }

    
Runtime exceptions that we don't want to propagate, instead we warp them into a more helpful exception. These are the ones where it's very unlikely that someone tries to catch specifically these around freemarker.template.Template.process(java.lang.Object,java.io.Writer).
    private boolean isCommonRuntimeException(RuntimeException e) {
        final Class eClass = e.getClass();
        // We deliberately don't accept sub-classes. Those are possibly application specific and some want to catch them
        // outside the template.
        return eClass == NullPointerException.class
                || eClass == IllegalArgumentException.class
                || eClass == ClassCastException.class
                || eClass == IndexOutOfBoundsException.class;
    }
    
New to GrepCode? Check out our FAQ X