1 /* 2 * Copyright (C) 2005 Alfresco, Inc. 3 * 4 * Licensed under the Mozilla Public License version 1.1 5 * with a permitted attribution clause. You may obtain a 6 * copy of the License at 7 * 8 * http://www.alfresco.org/legal/license.txt 9 * 10 * Unless required by applicable law or agreed to in writing, 11 * software distributed under the License is distributed on an 12 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 * either express or implied. See the License for the specific 14 * language governing permissions and limitations under the 15 * License. 16 */ 17 package org.alfresco.repo.template; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import org.alfresco.service.cmr.repository.TemplateNode; 23 import org.alfresco.service.namespace.QName; 24 25 import freemarker.ext.beans.BeanModel; 26 import freemarker.ext.beans.StringModel; 27 import freemarker.template.TemplateMethodModelEx; 28 import freemarker.template.TemplateModelException; 29 import freemarker.template.TemplateScalarModel; 30 31 /** 32 * @author Kevin Roast 33 * 34 * Custom FreeMarker Template language method. 35 * <p> 36 * Method returns whether a TemplateNode has a particular aspect applied to it. The aspect 37 * name can be either the fully qualified QName or the short prefixed name string. 38 * <p> 39 * Usage: hasAspect(TemplateNode node, String aspect) 40 */ 41 public final class HasAspectMethod implements TemplateMethodModelEx 42 { 43 /** 44 * @see freemarker.template.TemplateMethodModel#exec(java.util.List) 45 */ 46 public Object exec(List args) throws TemplateModelException 47 { 48 int result = 0; 49 50 if (args.size() == 2) 51 { 52 // arg 0 must be a wrapped TemplateNode object 53 BeanModel arg0 = (BeanModel)args.get(0); 54 55 // arg 1 can be either wrapped QName object or a String 56 String arg1String = null; 57 Object arg1 = args.get(1); 58 if (arg1 instanceof BeanModel) 59 { 60 arg1String = ((BeanModel)arg1).getWrappedObject().toString(); 61 } 62 else if (arg1 instanceof TemplateScalarModel) 63 { 64 arg1String = ((TemplateScalarModel)arg1).getAsString(); 65 } 66 if (arg0.getWrappedObject() instanceof TemplateNode) 67 { 68 // test to see if this node has the aspect 69 if ( ((TemplateNode)arg0.getWrappedObject()).hasAspect(arg1String) ) 70 { 71 result = 1; 72 } 73 } 74 } 75 76 return Integer.valueOf(result); 77 } 78 } 79