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.jsploader;

import java.beans.*;
import java.util.Map;
import java.util.TreeMap;
import java.io.IOException;

import org.w3c.dom.*;
import org.xml.sax.*;

import org.openide.nodes.Node;
import org.openide.loaders.DataObject;
import org.openide.loaders.XMLDataObject;
import org.openide.filesystems.FileChangeAdapter;
import org.openide.filesystems.FileEvent;
import org.openide.filesystems.FileRenameEvent;
import org.openide.ErrorManager;

/** Support for providing information about web.xml files. The information 
 *  extracted is a Map containing entries of (taglib-uri, taglib-location)
 *  about tag libraries used by the web application. Used by syntax coloring
 *  and code completion.
 *
 * @author  Petr Jiricka
 * @version 
 */
public class WebXMLSupport implements Node.Cookie {
    
    public static final String PROP_TAGLIBRARYMAP = "tagLibraryMap";    // NOI18N

    protected DataObject dobj;
    protected Map tagLibraryMap;
    private transient PropertyChangeSupport pcs;
    
    /** Creates new WebXMLSupport */
    public WebXMLSupport(DataObject dobj) {
        this.dobj = dobj;
        dobj.getPrimaryFile().addFileChangeListener(
            new FileChangeAdapter() {
            
                public void fileChanged(FileEvent fe) {
                    invalidate();
                }
                
                public void fileDeleted(FileEvent fe) {
                    invalidate();
                }
                
                public void fileRenamed(FileRenameEvent fe) {
                    invalidate();
                }
            }
        );
          
    }
    
    /** Returns tag library information represented by this web.xml object
    * as a map of (String taglib-uri ; String taglib-location).*/
    public Map getTagLibraryMap() {
        if (tagLibraryMap == null) {
            synchronized (this) {
                if (tagLibraryMap == null)
                    tagLibraryMap = createTagLibraryMap();
            }
        }
        return tagLibraryMap;
    }
    
    private Map createTagLibraryMap() {
        Map map = new TreeMap();
        try {
            Document webxml = XMLDataObject.parse(dobj.getPrimaryFile().getURL(), 
                new ErrorHandler() {
                    public void error(SAXParseException exception) throws SAXException {
                    }

                    public void fatalError(SAXParseException exception) throws SAXException {
                        throw exception;
                    }

                    public void warning(SAXParseException exception) throws SAXException {
                    }
                }, true);
                 
            NodeList nList =  webxml.getElementsByTagName("taglib");    // NOI18N
            if (nList.getLength() != 0) {
                for(int i = 0; i < nList.getLength(); i++) {
                    Element e = (Element) nList.item(i);
                    NodeList list = e.getChildNodes();
                    String tagLoc = null;
                    String uri = null;
                    for(int j = 0; j < list.getLength(); j++) {
                        org.w3c.dom.Node em = list.item(j);
                        String tname = em.getNodeName();
                        if (tname.equals("taglib-location")) {  // NOI18N
                            Text t = (Text) em.getFirstChild();
                            if (t != null) {
                                tagLoc = t.getData();
                                if (tagLoc != null) {
                                    tagLoc = tagLoc.trim();
                                    if (!tagLoc.startsWith("/"))    // NOI18N
                                        tagLoc = "/WEB-INF/" + tagLoc;  // NOI18N
                                }
                            }
                        }
                        if (tname.equals("taglib-uri")) {   // NOI18N
                            Text t = (Text) em.getFirstChild();
                            if (t != null) {
                                uri =  t.getData();
                                if (uri != null) {
                                    uri = uri.trim();
                                }
                            }
                        }
                    }
                    if ((tagLoc != null) && (uri != null))
                        map.put(uri, tagLoc);
                }
            }
        }
        catch (IOException e) {
            ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e);
        }
        catch (SAXException e) { 
            if (Boolean.getBoolean("netbeans.debug.exceptions")) { // NOI18N
                // only report the error in netbeans.debug.exceptions mode, as this is a usual 
                // and common exception, which does not imply a bug or an unusual condition
                ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e);
            }
        }
        return map;
    }
    
    private synchronized void invalidate() {
        tagLibraryMap = null;
        fireWebXMLChange();
    }
    
    public void addPropertyChangeListener(PropertyChangeListener pcl) {
        if (pcs == null)
            pcs = new PropertyChangeSupport(this);
        pcs.addPropertyChangeListener(pcl);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener pcl) {
        if (pcs == null)
            return;
        pcs.removePropertyChangeListener(pcl);
    }
    
    void fireWebXMLChange() {
        if (pcs != null)
            pcs.firePropertyChange(PROP_TAGLIBRARYMAP, null, null);
    }
    
    private static final Map association = new java.util.WeakHashMap(); // Map
    /** Returns WebXMLSupport attached to DataObject dobj. If no support is attached,
    * creates a new one and attaches it to the object. 
    * @param dobj web.xml object for which the support should be created
    */
    public static WebXMLSupport getWebXMLSupport(DataObject dobj) {
        WebXMLSupport sup = (WebXMLSupport)association.get(dobj);
        if (sup == null) {
            sup = new WebXMLSupport(dobj);
            association.put(dobj, sup);
        }
        return sup;
    }

}
... 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.