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.*;
import javax.swing.text.BadLocationException;

import org.netbeans.editor.ext.java.*;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.TokenItem;
import org.netbeans.editor.ext.html.HTMLTokenContext;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.modules.editor.java.NbJavaSyntaxSupport;

import org.netbeans.modules.web.jsps.parserapi.PageInfo;
import org.netbeans.modules.editor.java.JCFinderFactory;
import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
import org.openide.filesystems.FileObject;

/**
 *
 * @author  pjiricka
 */
public class JspJavaSyntaxSupport extends NbJavaSyntaxSupport {
    
    JspSyntaxSupport jspSup;
    
    /** Hold all imported classes. */
    private HashMap importClasses;
    private HashMap importClassesCache;
    private JCClass cc;
    
    /** Creates new JspJavaSyntaxSupport */
    public JspJavaSyntaxSupport(BaseDocument doc, JspSyntaxSupport jspSup) {
        super(doc);
        this.jspSup = jspSup;
        /** Holds imported expressions. It's used for comparing, whether 
         *   a expression was added or removed.
         **/
        importClassesCache = null;
        /** Chache for all imported classes. If the imported expression ends with
         *  '.*', then holds all classes in the package.
         **/
        importClasses = null;
        
        cc = null;
    }
    
    protected int getMethodStartPosition(int pos) {
        return 0;
    }
    
    /** Builds the global variable map by JSP rules, i.e. there are three sources of
     * variables
     * 
    *
  • the standard JPS variables (request, application, page, ...)
  • *
  • beans imported by useBean action
  • *
  • JSP valiables declared by declaration elements in this JSP - not implemented yet
  • *
  • JSP variables declared by custom tags - not implemented yet
  • *
*/ protected Map buildGlobalVariableMap(int pos) { Map globMap = new HashMap(); // implicit JSP objects globMap.put("request", getClazzRepresentation("javax.servlet.http.HttpServletRequest")); // NOI18N globMap.put("response", getClazzRepresentation("javax.servlet.http.HttpServletResponse")); // NOI18N globMap.put("session", getClazzRepresentation("javax.servlet.http.HttpSession")); // NOI18N globMap.put("application", getClazzRepresentation("javax.servlet.ServletContext")); // NOI18N globMap.put("out", getClazzRepresentation("javax.servlet.jsp.JspWriter")); // NOI18N globMap.put("config", getClazzRepresentation("javax.servlet.ServletConfig")); // NOI18N if (NbEditorUtilities.getMimeType(jspSup.getDocument()).equals(JspUtils.TAG_MIME_TYPE)){ globMap.put("jspContext", getClazzRepresentation("javax.servlet.jsp.JspContext")); // NOI18N } else{ globMap.put("page", getClazzRepresentation("java.lang.Object")); // NOI18N globMap.put("pageContext", getClazzRepresentation("javax.servlet.jsp.PageContext")); // NOI18N } // used beans PageInfo.BeanData[] beanData = jspSup.getBeanData(); for (int i = 0; i < beanData.length; i++) { globMap.put(beanData[i].getId(), getClazzRepresentation(beanData[i].getClassName())); } return globMap; } private Object getClazzRepresentation(String decType) { JCClass cls = getClassFromName(decType, true); if (cls != null) { return JavaCompletion.getType(cls, 0); } return null; } public boolean isStaticBlock(int pos) { return false; } public JCClass getTopClass(){ return getClass(0); } public JCClass getClass(int pos) { if (cc == null){ org.openide.filesystems.FileObject fo = jspSup.getFileObject(); cc =new JspJavaCompletion.JCJspClass (fo.getName()+"_"+fo.getExt(), createPackageName(fo) , jspSup, this); // NOI18N } return cc; } private String createPackageName(FileObject fo) { FileObject wmRoot = JspUtils.guessWebModuleRoot(getDocument(), fo); if (wmRoot == null) return ""; // the jsp page is outside a webmodule. String path = JspUtils.findRelativeContextPath(wmRoot, fo); if (path.length()-1 > fo.getNameExt().length() ){ path = path.substring(1, path.indexOf(fo.getNameExt())-1); path = path.replace('/','.'); } else{ path = ""; } return path; } /** Removes all classes which are imported with the * impExp. */ private void removeImport(String impExp, JCFinder finder){ if (impExp.endsWith("*")){ // NOI18N impExp = impExp.substring(0, impExp.lastIndexOf('.')); JCPackage pkg = finder.getExactPackage(impExp); if (pkg != null) { JCClass[] classes = pkg.getClasses(); for (int j = 0; j < classes.length; j++) { importClasses.remove(classes[j].getName()); } } } else { JCClass cls = finder.getExactClass(impExp); if (cls != null){ importClasses.remove(cls.getName()); } } } /** Adds all classes which are imported with the impExp */ private void addImport(String impExp, JCFinder finder){ if (impExp.endsWith("*")){ // NOI18N impExp = impExp.substring(0, impExp.lastIndexOf('.')); JCPackage pkg = finder.getExactPackage(impExp); if (pkg != null) { JCClass[] classes = pkg.getClasses(); for (int j = 0; j < classes.length; j++) { importClasses.put(classes[j].getName(), classes[j]); } } } else { JCClass cls = finder.getExactClass(impExp); if (cls != null){ importClasses.put(cls.getName(), cls); } } } /** Are there any changes in the imports? */ private void updateImportClasses(){ boolean remove = false; boolean update = false; if (importClasses == null){ importClasses = new HashMap(); importClassesCache = new HashMap(); update = true; } JspParserAPI.ParseResult pre = jspSup.getParseResult(); if (pre != null){ List imports = pre.getPageInfo().getImports(); JCFinder finder = JCFinderFactory.getDefault().getFinder(jspSup.getFileObject()); Iterator iter = importClassesCache.keySet().iterator(); String impExp; ArrayList removeList = new ArrayList(); while (iter.hasNext()){ impExp = (String)iter.next(); if (!imports.contains(impExp)){ remove = true; update = true; removeList.add(impExp); } } for (int i = 0; i < removeList.size(); i++){ impExp = (String)removeList.get(i); importClassesCache.remove(impExp); removeImport(impExp, finder); } for (int i = 0; i < imports.size(); i ++){ impExp = (String)imports.get(i); if (!importClassesCache.containsKey(impExp)){ importClassesCache.put(impExp, null); addImport(impExp, finder); update = true; } } } if (update){ JavaImport res = super.getJavaImport(); if (remove){ // clear the map in the java CC res.appendCustomImportsMap(null); } res.appendCustomImportsMap(importClasses); } java.util.Date stop = new java.util.Date(); } /*public synchronized JavaImport getJavaImport(){ updateImportClasses(); return super.getJavaImport(); }*/ public void refreshClassInfo() { if (!jcValid) { JCClass classToAppend = getClass (0); if (classToAppend != null) { updateImportClasses(); JCFinderFactory.getDefault().appendClass(classToAppend); } jcValid = true; } } }
... 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.