KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > jsploader > JspParserAccess


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
21 package org.netbeans.modules.web.core.jsploader;
22
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
32 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
33 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
34
35 import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
36 import org.netbeans.modules.web.api.webmodule.WebModule;
37 import org.openide.ErrorManager;
38 import org.openide.cookies.EditorCookie;
39 import org.openide.filesystems.FileObject;
40 import org.openide.loaders.DataObject;
41 import org.openide.text.CloneableEditorSupport;
42 import org.openide.util.WeakListeners;
43
44 public class JspParserAccess {
45     
46     public static synchronized JspParserAPI.WebModule getJspParserWM(WebModule webModule) {
47         return new WM(webModule);
48     }
49     
50     // PENDING - will also need this:
51
//public static JspParserAPI.WebModule getParserWMOutsideWM (FileObject packageRoot) { }
52

53     private static final class WM extends JspParserAPI.WebModule implements PropertyChangeListener JavaDoc {
54         
55         WebModule webModule;
56         PropertyChangeSupport JavaDoc pcs;
57         
58         /** Creates an instance of a new web module for the parser.
59          * @param docBase the document base of the web module. May be null if
60          * we are parsing a tag file that it outside of a web module.
61          */

62         private WM(WebModule webModule) {
63             this.webModule = webModule;
64             pcs = new PropertyChangeSupport JavaDoc(this);
65             //Listen on the changes for libraries
66
if(webModule != null) {
67                 ClassPath cp = ClassPath.getClassPath(webModule.getDocumentBase(), ClassPath.EXECUTE);
68                 cp.addPropertyChangeListener(WeakListeners.propertyChange(this, cp));
69             }
70         }
71         
72         public File JavaDoc[] getExtraClasspathEntries(){
73             File JavaDoc[] entries = null;
74             if (webModule != null){
75                 Project p = FileOwnerQuery.getOwner(webModule.getDocumentBase());
76                 J2eeModuleProvider jprovider = (J2eeModuleProvider)p.getLookup().lookup(J2eeModuleProvider.class);
77                 if (jprovider != null){
78                     String JavaDoc serverID = jprovider.getServerInstanceID();
79                     J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(serverID);
80                     // platform can be null, if the target server is not resolved.
81
if (platform != null)
82                         entries = platform.getClasspathEntries();
83                 }
84                 
85                 
86             }
87             return entries;
88         }
89         
90         public FileObject getDocumentBase() {
91             if (webModule != null)
92                 return webModule.getDocumentBase();
93             return null;
94         }
95         
96         /** Returns InputStream for the file open in editor or null
97          * if the file is not open.
98          */

99         public java.io.InputStream JavaDoc getEditorInputStream(FileObject fo) {
100             InputStream JavaDoc result = null;
101             EditorCookie ec = null;
102             try {
103                 
104                 ec = (EditorCookie)(DataObject.find(fo)).getCookie(EditorCookie.class);
105             } catch (org.openide.loaders.DataObjectNotFoundException e){
106                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
107             }
108             if (ec != null && (ec instanceof CloneableEditorSupport)) {
109                 try {
110                     
111                     result = ((CloneableEditorSupport)ec).getInputStream();
112                 } catch (IOException JavaDoc e) {
113                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
114                 }
115             }
116             return result;
117         }
118         
119         
120         private void fireLibraries() {
121             firePropertyChange(JspParserAPI.WebModule.PROP_LIBRARIES);
122         }
123         
124         private void firePackageRoots() {
125             firePropertyChange(JspParserAPI.WebModule.PROP_PACKAGE_ROOTS);
126         }
127         
128         private void firePropertyChange(String JavaDoc propertyName) {
129             pcs.firePropertyChange(propertyName, null, null);
130         }
131         
132 /* public FileObject[] getPackageRoots() {
133             FileObject[] roots = ClassPath.getClassPath(webModule.getDocumentBase(), ClassPath.EXECUTE).getRoots();
134             ArrayList folders = new ArrayList();
135             for (int i = 0; i < roots.length; i++){
136                 if (!roots[i].isData ()) { //NOI18N
137                     folders.add(roots[i]);
138                 }
139             }
140             return (FileObject[])folders.toArray(new FileObject[folders.size()]);
141         }
142  */

143         
144 /* public FileObject[] getLibraries() {
145             // PENDING - better implementation when we will be able distinguish the libraries.
146             FileObject[] roots = ClassPath.getClassPath(webModule.getDocumentBase(), ClassPath.EXECUTE).getRoots();
147             ArrayList lib = new ArrayList();
148             try{
149                 for (int i = 0; i < roots.length; i++){
150                     if (roots[i].getURL().getProtocol().equals("jar")) { //NOI18N
151                         lib.add(roots[i]);
152                     }
153                 }
154             }
155             catch(org.openide.filesystems.FileStateInvalidException e){
156                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
157             }
158             return (FileObject[])lib.toArray(new FileObject[lib.size()]);
159  
160         }*/

161         
162         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
163             pcs.removePropertyChangeListener(l);
164         }
165         
166         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
167             pcs.addPropertyChangeListener(l);
168         }
169         
170         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
171             if (ClassPath.PROP_ENTRIES.equals(evt.getPropertyName())){
172                 fireLibraries();
173             }
174             if (ClassPath.PROP_ROOTS.equals(evt.getPropertyName())){
175                 firePackageRoots();
176             }
177         }
178         
179     }
180 }
181
Popular Tags