1 19 20 package org.netbeans.modules.web.core.syntax.completion; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.StringTokenizer ; 27 import javax.servlet.jsp.tagext.FunctionInfo ; 28 import javax.servlet.jsp.tagext.TagLibraryInfo ; 29 import org.netbeans.modules.web.core.syntax.JspSyntaxSupport; 30 import org.netbeans.modules.web.core.syntax.JspUtils; 31 import org.netbeans.modules.web.jsps.parserapi.JspParserAPI; 32 import org.openide.ErrorManager; 33 34 38 public class ELFunctions { 39 public static class Function { 40 private String prefix; 41 private FunctionInfo info; 42 43 public Function(String prefix, FunctionInfo info){ 44 this.info = info; 45 this.prefix = prefix; 46 } 47 48 public String getPrefix(){ 49 return prefix; 50 } 51 52 public FunctionInfo getFunctionInfo(){ 53 return info; 54 } 55 56 public String getName(){ 57 return info.getName(); 58 } 59 60 public String getReturnType(){ 61 String signature = info.getFunctionSignature().trim(); 62 String type = null; 63 64 if (signature != null || !signature.equals("")){ 65 int index = signature.indexOf(info.getName()); 66 if (index > -1) 67 type = signature.substring(0, index).trim(); 68 } 69 70 return type; 71 } 72 73 public String getParameters(){ 74 String parameters = ""; 75 String signature = info.getFunctionSignature().trim(); 76 77 if (signature != null || !signature.equals("")){ 78 int index = signature.indexOf(info.getName()); 79 if (index > -1){ 80 parameters = signature.substring(index+1).trim(); 81 if (parameters.indexOf('(')>-1) 82 parameters = parameters.substring(parameters.indexOf('(')+1); 83 if (parameters.indexOf(')')>-1) 84 parameters = parameters.substring(0, parameters.indexOf(')')); 85 StringTokenizer st = new StringTokenizer (parameters, ","); 86 String type; 87 StringBuffer sb = new StringBuffer (); 88 while(st.hasMoreTokens()){ 89 type = st.nextToken(); 90 if (type.lastIndexOf('.')>-1) 91 type = type.substring(type.lastIndexOf('.') +1); 92 sb.append(type); 93 if (st.hasMoreTokens()) 94 sb.append(", "); 95 } 96 parameters = sb.toString(); 97 } 98 } 99 return parameters; 100 } 101 } 102 103 public static List getFunctions(JspSyntaxSupport sup, String start){ 104 List functions = new ArrayList (); 105 JspParserAPI.ParseResult result = JspUtils.getCachedParseResult(sup.getDocument(), sup.getFileObject(), false, false); 106 if (result != null) { 107 Map libraries = result.getPageInfo().getTagLibraries(); 108 Map prefixes = result.getPageInfo().getJspPrefixMapper(); 109 Iterator iter = prefixes.keySet().iterator(); 110 while (iter.hasNext()) { 111 String prefix = (String )iter.next(); 112 TagLibraryInfo library = (TagLibraryInfo )libraries.get(prefixes.get(prefix)); 113 FunctionInfo [] fun = getValidFunctions(library); 114 for (int i = 0; i < fun.length; i++) { 115 if ((prefix+":"+fun[i].getName()).startsWith(start)) 116 functions.add(new Function(prefix, fun[i])); 117 } 118 } 119 } 120 return functions; 121 } 122 123 124 private static FunctionInfo [] getValidFunctions(TagLibraryInfo tli) { 125 ArrayList <FunctionInfo > fis = new ArrayList (); 126 for(FunctionInfo fi :tli.getFunctions()) { 127 String msg = null; 128 if(fi.getFunctionClass() == null || fi.getFunctionClass().length() == 0) { 129 msg = "Invalid function class '" + fi.getFunctionClass() + "' in " + tli.getShortName() + " tag library."; } else if(fi.getName() == null || fi.getName().length() == 0) { 131 msg = "Invalid function name '" + fi.getName() + "' in " + tli.getShortName() + " tag library."; } else if(fi.getFunctionSignature() == null || fi.getFunctionSignature().length() == 0) { 133 msg = "Invalid function signature '" + fi.getFunctionSignature() + "' in " + tli.getShortName() + " tag library."; } else if(fi.getFunctionSignature().indexOf(fi.getName()) == -1) { 135 msg = "Invalid function signature '" + fi.getFunctionSignature() + "' (doesn't contain function name) in " + tli.getShortName() + " tag library."; } 137 if(msg == null) { 138 fis.add(fi); 139 } else { 140 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg); 141 } 142 } 143 return (FunctionInfo [])fis.toArray(new FunctionInfo []{}); 144 } 145 146 } 147 | Popular Tags |