alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.web.core.syntax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.text.JTextComponent;

import org.netbeans.editor.ext.CompletionQuery;
import org.netbeans.editor.ext.ExtEditorUI;
import org.netbeans.editor.ext.java.*;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.Utilities;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.modules.editor.java.NbJavaCompletion;
import org.openide.ErrorManager;
import org.netbeans.modules.editor.java.JCFinderFactory;
import org.netbeans.modules.web.jsps.parserapi.PageInfo;


public class JspJavaCompletion extends NbJavaCompletion {
    
    public JspJavaCompletion(ExtEditorUI extEditorUI) {
        super(extEditorUI);
    }

    public static class JCJspClass extends AbstractClass {
        
        /** name, pakcage name & class name of variables predefined in JSP */
        private String[][] commonStandardVars = {
            {"request", "javax.servlet.http", "HttpServletRequest"},    // NOI18N
            {"response", "javax.servlet.http", "HttpServletResponse"},  // NOI18N
            {"out", "java.io","PrintWriter"},                           // NOI18N
            {"session", "javax.servlet.http","HttpSession"},            // NOI18N
            {"application", "javax.servlet", "ServletContext"},         // NOI18N
            {"config", "javax.servlet","ServletConfig"}                 // NOI18N
        };
        private String[][] jspStandardVars = {
            {"pageContext", "javax.servlet.jsp","PageContext"},         // NOI18N
            {"page", "java.lang", "Object"}                                        // NOI18N
        };
        
        private String[][] tagFileStandardVars = {
            {"jspContext", "javax.servlet.jsp", "JspContext"}                 // NOI18N
        };
        
        private JspSyntaxSupport support;
        private JspJavaSyntaxSupport javaSupport;
        
        /** This map holds lastest variables defined with the jsp:useBean.
         *  It contains pair name -> package class name
         */
        private HashMap definedBeans;
        /** This is array of fields, which are constructed from variables defined with the jsp:useBean tag.
         *  The constructing of the fields takes a long time, so the fields are cached. The cache is changed
         *  when the data from definedBeans map are different from data, which are obtained from jsp parser.
         */
        private JCField [] cacheBeanFields;
        
        public JCJspClass(String cls, String pkg, JspSyntaxSupport sup, JspJavaSyntaxSupport javaSup) {
            super (cls, pkg, false, 0);   // name, pkg name, iface, modifiers
            this.support = sup;
            this.javaSupport = javaSup;
            init();
            definedBeans = new HashMap();
            cacheBeanFields = new JCField[0];
        }
        
        public boolean isInterface() {
            return false;
        }
        
        
        /** The jsp page has free main sources for fields. 
         *  1 - implicit objects
         *  2 - variables defined through the jsp:useBean tag
         *  3 - variables defined in declarations or in the scriptlets.
         *  This method compose all three sources together. 
         */
        public JCField[] getFields() {
            // get all implicit objects
            JCField[] fields = super.getFields();
            // get variables defined with the jsp:useBean
            PageInfo.BeanData[] beanData = support.getBeanData();
            // get all variable, which are defined in scriplet or in declaration, until the current cursor possition.
            Map map = javaSupport.getLocalVariableMap(org.netbeans.editor.Utilities.getFocusedComponent().getCaret().getDot());
            
            JCField[] varFields = new JCField[0];
            
            // Contains the page jsp:useBean tag?
            if (beanData.length > 0){
                HashMap testMap = new HashMap();
                for (int i = 0; i < beanData.length; i++){
                    testMap.put(beanData[i].getId(), beanData[i].getClassName());
                }
                // Is there new change in data beans from jsp parser?
                if (!testMap.equals(definedBeans)){
                    // memory the latest data abou beans
                    definedBeans = testMap;
                    // construct new cache
                    cacheBeanFields = new JCField[beanData.length];
                    JCFinder finder = JCFinderFactory.getDefault().getGlobalFinder();
                    for (int i = 0; i < beanData.length; i++) {
                        JCClass cl;
                        try {
                            cl = getClass (finder, null,beanData[i].getClassName());
                        } catch (java.lang.IllegalArgumentException e){
                            cl = getClass (finder, null, "java.lang.Class");
                        }
                        cacheBeanFields[i] = new BaseField (this, beanData[i].getId(), 
                                new BaseType (cl, 0), 0);
                    }
                }
            }
            
            // construct fields from local variablec, which are defined in the scriptlets
            if (map != null && map.size() > 0){
                JCFinder finder = JCFinderFactory.getDefault().getGlobalFinder();
                varFields = new JCField[map.size()];
                Iterator iter = map.keySet().iterator();
                int i = 0;
                while (iter.hasNext()){
                    String name = (String)iter.next();
                    varFields[i++] = new BaseField (this, name, (BaseType)map.get(name), 0);
                }
            }
            
            // compose all fields together
            if (varFields.length > 0 || cacheBeanFields.length > 0){
                JCField [] nFields = new JCField[fields.length + varFields.length + cacheBeanFields.length];
                for (int i = 0; i < fields.length; i++){
                    nFields[i] = fields[i];
                }
                for (int i = 0; i < varFields.length; i++){
                    nFields[fields.length + i] = varFields[i];
                }
                for (int i = 0; i < cacheBeanFields.length; i++){
                    nFields[fields.length + varFields.length + i] = cacheBeanFields[i];
                }
                fields = nFields;
            }
            
            return fields;
        }
        
        /** Init internal representation  */
        protected void init () {
            body = new AbstractClass.Body ();
            
           
            // superclass: HttpServlet unless there is <% page extends="pkg.class" %> 
            // PENDING
            List clsList =JCFinderFactory.getDefault().getGlobalFinder().findClasses(null, "javax.servlet.http.HttpServlet", true);    // NOI18N
            if (clsList != null && clsList.size() > 0) {
                if (clsList.size() > 0) { // more matching classes
                    body.superClass = (JCClass)clsList.get(0); // get the first one
                }
            }
            
            // fields
            ArrayList lst = new ArrayList ();
            JCFinder finder = JCFinderFactory.getDefault().getFinder(this.support.getFileObject());
            lst.addAll(createFields(commonStandardVars, finder));
            
            if (NbEditorUtilities.getMimeType(support.getDocument()).equals(JspUtils.TAG_MIME_TYPE))
                lst.addAll(createFields(tagFileStandardVars, finder));
            else
                lst.addAll(createFields(jspStandardVars, finder));
            
            body.fields = new JCField[lst.size()];
            lst.toArray(body.fields);
            lst.clear();
            
            body.methods = new JCMethod[0];
            body.interfaces = new JCClass[0];
            body.constructors = new JCConstructor[0];
            body.tagOffset = 0;
        }
        
        /** Create fields from vars. 
         */
        private ArrayList createFields (String[][] vars, JCFinder finder){
            ArrayList fields = new ArrayList();
            for (int i = 0; i
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.