KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > completion > ELFunctions


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.core.syntax.completion;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import javax.servlet.jsp.tagext.FunctionInfo JavaDoc;
28 import javax.servlet.jsp.tagext.TagLibraryInfo JavaDoc;
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 /**
35  *
36  * @author Petr Pisl
37  */

38 public class ELFunctions {
39     public static class Function {
40         private String JavaDoc prefix;
41         private FunctionInfo JavaDoc info;
42         
43         public Function(String JavaDoc prefix, FunctionInfo JavaDoc info){
44             this.info = info;
45             this.prefix = prefix;
46         }
47         
48         public String JavaDoc getPrefix(){
49             return prefix;
50         }
51         
52         public FunctionInfo JavaDoc getFunctionInfo(){
53             return info;
54         }
55         
56         public String JavaDoc getName(){
57             return info.getName();
58         }
59         
60         public String JavaDoc getReturnType(){
61             String JavaDoc signature = info.getFunctionSignature().trim();
62             String JavaDoc 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 JavaDoc getParameters(){
74             String JavaDoc parameters = "";
75             String JavaDoc 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 JavaDoc st = new StringTokenizer JavaDoc(parameters, ",");
86                     String JavaDoc type;
87                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc /*<Function>*/ getFunctions(JspSyntaxSupport sup, String JavaDoc start){
104         List JavaDoc functions = new ArrayList JavaDoc();
105         JspParserAPI.ParseResult result = JspUtils.getCachedParseResult(sup.getDocument(), sup.getFileObject(), false, false);
106         if (result != null) {
107             Map JavaDoc libraries = result.getPageInfo().getTagLibraries();
108             Map JavaDoc prefixes = result.getPageInfo().getJspPrefixMapper();
109             Iterator JavaDoc iter = prefixes.keySet().iterator();
110             while (iter.hasNext()) {
111                 String JavaDoc prefix = (String JavaDoc)iter.next();
112                 TagLibraryInfo JavaDoc library = (TagLibraryInfo JavaDoc)libraries.get(prefixes.get(prefix));
113                 FunctionInfo JavaDoc[] 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     /** removes invalid function infos and prints a debug message with the problem description */
124     private static FunctionInfo JavaDoc[] getValidFunctions(TagLibraryInfo JavaDoc tli) {
125         ArrayList JavaDoc<FunctionInfo JavaDoc> fis = new ArrayList JavaDoc();
126         for(FunctionInfo JavaDoc fi :tli.getFunctions()) {
127             String JavaDoc msg = null;
128             if(fi.getFunctionClass() == null || fi.getFunctionClass().length() == 0) {
129                 msg = "Invalid function class '" + fi.getFunctionClass() + "' in " + tli.getShortName() + " tag library."; //NOI18N
130
} else if(fi.getName() == null || fi.getName().length() == 0) {
131                 msg = "Invalid function name '" + fi.getName() + "' in " + tli.getShortName() + " tag library.";//NOI18N
132
} else if(fi.getFunctionSignature() == null || fi.getFunctionSignature().length() == 0) {
133                 msg = "Invalid function signature '" + fi.getFunctionSignature() + "' in " + tli.getShortName() + " tag library.";//NOI18N
134
} 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.";//NOI18N
136
}
137             if(msg == null) {
138                 fis.add(fi);
139             } else {
140                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg);
141             }
142         }
143         return (FunctionInfo JavaDoc[])fis.toArray(new FunctionInfo JavaDoc[]{});
144     }
145     
146 }
147
Popular Tags