KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > struts > editor > StrutsConfigHyperlinkProvider


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 package org.netbeans.modules.web.struts.editor;
20
21 import java.io.IOException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import javax.lang.model.element.TypeElement;
26 import javax.lang.model.util.Elements;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29 import org.netbeans.api.java.source.ClasspathInfo;
30 import org.netbeans.api.java.source.CompilationController;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.UiUtils;
33 import org.netbeans.editor.BaseDocument;
34 import org.netbeans.editor.TokenItem;
35 import org.netbeans.editor.Utilities;
36 import org.netbeans.editor.ext.ExtSyntaxSupport;
37 //import org.netbeans.jmi.javamodel.JavaClass;
38
import org.netbeans.lib.editor.hyperlink.spi.HyperlinkProvider;
39 import org.netbeans.modules.editor.NbEditorUtilities;
40 import org.netbeans.modules.j2ee.common.source.AbstractTask;
41 //import org.netbeans.modules.editor.java.JMIUtils;
42
import org.netbeans.modules.web.api.webmodule.WebModule;
43 import org.netbeans.modules.web.struts.StrutsConfigDataObject;
44 import org.netbeans.modules.web.struts.StrutsConfigUtilities;
45 import org.openide.ErrorManager;
46 import org.openide.cookies.OpenCookie;
47 import org.openide.filesystems.FileObject;
48 import org.openide.loaders.DataObject;
49 import org.openide.loaders.DataObjectNotFoundException;
50 import org.openide.nodes.Node;
51 import org.openide.util.NbBundle;
52 import org.openide.util.RequestProcessor;
53
54 /**
55  *
56  * @author petr
57  */

58 public class StrutsConfigHyperlinkProvider implements HyperlinkProvider {
59     
60     static private boolean debug = false;
61     private static Hashtable JavaDoc hyperlinkTable;
62     
63     private final int JAVA_CLASS = 0;
64     private final int FORM_NAME = 1;
65     private final int RESOURCE_PATH = 2;
66     
67     {
68         hyperlinkTable = new Hashtable JavaDoc();
69         hyperlinkTable.put("data-source#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
70
hyperlinkTable.put("data-source#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
71
hyperlinkTable.put("form-beans#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
72
hyperlinkTable.put("form-bean#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
73
hyperlinkTable.put("form-bean#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
74
hyperlinkTable.put("form-property#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
75
hyperlinkTable.put("form-property#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
76
hyperlinkTable.put("exception#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
77
hyperlinkTable.put("exception#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
78
hyperlinkTable.put("exception#handler", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
79
hyperlinkTable.put("exception#path", new Integer JavaDoc(RESOURCE_PATH)); //NOI18N
80
hyperlinkTable.put("global-forwards#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
81
hyperlinkTable.put("forward#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
82
hyperlinkTable.put("forward#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
83
hyperlinkTable.put("forward#path", new Integer JavaDoc(RESOURCE_PATH)); //NOI18N
84
hyperlinkTable.put("action-mappings#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
85
hyperlinkTable.put("action#name", new Integer JavaDoc(FORM_NAME)); //NOI18N
86
hyperlinkTable.put("action#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
87
hyperlinkTable.put("action#type", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
88
hyperlinkTable.put("action#forward", new Integer JavaDoc(RESOURCE_PATH)); //NOI18N
89
hyperlinkTable.put("action#include", new Integer JavaDoc(RESOURCE_PATH)); //NOI18N
90
hyperlinkTable.put("action#input", new Integer JavaDoc(RESOURCE_PATH)); //NOI18N
91
hyperlinkTable.put("action#path", new Integer JavaDoc(RESOURCE_PATH)); //NOI18N
92
hyperlinkTable.put("controller#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
93
hyperlinkTable.put("controller#processorClass", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
94
hyperlinkTable.put("controller#multipartClass", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
95
hyperlinkTable.put("message-resources#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
96
hyperlinkTable.put("message-resources#factory", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
97
hyperlinkTable.put("plug-in#className", new Integer JavaDoc(JAVA_CLASS)); //NOI18N
98
}
99     
100     private int valueOffset;
101     private String JavaDoc [] eav = null;
102     /** Creates a new instance of StrutsHyperlinkProvider */
103     public StrutsConfigHyperlinkProvider() {
104     }
105     
106     public int[] getHyperlinkSpan(javax.swing.text.Document JavaDoc doc, int offset) {
107         if (debug) debug(":: getHyperlinkSpan");
108         if (eav != null){
109             return new int []{valueOffset, valueOffset + eav[2].length() -1};
110         }
111         return null;
112     }
113     
114     public boolean isHyperlinkPoint(javax.swing.text.Document JavaDoc doc, int offset) {
115         if (debug) debug(":: isHyperlinkSpan - offset: " + offset); //NOI18N
116

117         // PENDING - this check should be removed, when
118
// the issue #61704 is solved.
119
DataObject dObject = NbEditorUtilities.getDataObject(doc);
120         if (! (dObject instanceof StrutsConfigDataObject))
121             return false;
122         
123         eav = getElementAttrValue(doc, offset);
124         if (eav != null){
125             if (hyperlinkTable.get(eav[0]+"#"+eav[1])!= null)
126                 return true;
127         }
128         return false;
129     }
130     
131     public void performClickAction(javax.swing.text.Document JavaDoc doc, int offset) {
132         if (debug) debug(":: performClickAction");
133         if (hyperlinkTable.get(eav[0]+"#"+eav[1])!= null){
134             int type = ((Integer JavaDoc)hyperlinkTable.get(eav[0]+"#"+eav[1])).intValue();
135             switch (type){
136                 case JAVA_CLASS: findJavaClass(eav[2], doc); break;
137                 case FORM_NAME: findForm(eav[2], (BaseDocument)doc);break;
138                 case RESOURCE_PATH: findResourcePath(eav[2], (BaseDocument)doc);break;
139             }
140         }
141     }
142     
143     static void debug(String JavaDoc message){
144         System.out.println("StrutsHyperlinkProvider: " + message); //NoI18N
145
}
146     /** This method finds the value for an attribute of element of on the offset.
147      * @return Returns null, when the offset is not a value of an attribute. If the there is value
148      * of an attribute, then returns String array [element, attribute, value].
149      */

150     private String JavaDoc[] getElementAttrValue(javax.swing.text.Document JavaDoc doc, int offset){
151         String JavaDoc attribute = null;
152         String JavaDoc tag = null;
153         String JavaDoc value = null;
154         
155         try {
156             BaseDocument bdoc = (BaseDocument) doc;
157             JTextComponent JavaDoc target = Utilities.getFocusedComponent();
158             
159             if (target == null || target.getDocument() != bdoc)
160                 return null;
161             
162             ExtSyntaxSupport sup = (ExtSyntaxSupport)bdoc.getSyntaxSupport();
163             //TokenID tokenID = sup.getTokenID(offset);
164
TokenItem token = sup.getTokenChain(offset, offset+1);
165             //if (debug) debug ("token: " +token.getTokenID().getNumericID() + ":" + token.getTokenID().getName());
166
// when it's not a value -> do nothing.
167
if (token == null || token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE_VALUE)
168                 return null;
169             value = token.getImage();
170             if (value != null){
171                 // value = value.substring(0, offset - token.getOffset());
172
//if (debug) debug ("value to cursor: " + value);
173
value = value.trim();
174                 valueOffset = token.getOffset();
175                 if (value.charAt(0) == '"') {
176                     value = value.substring(1);
177                     valueOffset ++;
178                 }
179                 
180                 if (value.length() > 0 && value.charAt(value.length()-1) == '"') value = value.substring(0, value.length()-1);
181                 value = value.trim();
182                 //if (debug) debug ("value: " + value);
183
}
184             
185             //if (debug) debug ("Token: " + token);
186
// Find attribute and tag
187
// 5 - attribute
188
// 4 - tag
189
while(token != null && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE
190                     && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT)
191                 token = token.getPrevious();
192             if (token != null && token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE){
193                 attribute = token.getImage();
194                 while(token != null && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT)
195                     token = token.getPrevious();
196                 if (token != null && token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ELEMENT)
197                     tag = token.getImage();
198             }
199             if (attribute == null || tag == null)
200                 return null;
201             tag = tag.substring(1);
202             if (debug) debug("element: " + tag ); // NOI18N
203
if (debug) debug("attribute: " + attribute ); //NOI18N
204
if (debug) debug("value: " + value ); //NOI18N
205
return new String JavaDoc[]{tag, attribute, value};
206         } catch (BadLocationException JavaDoc e) {
207         }
208         return null;
209     }
210     
211     private void findJavaClass(final String JavaDoc fqn, javax.swing.text.Document JavaDoc doc){
212         FileObject fo = NbEditorUtilities.getFileObject(doc);
213         if (fo != null){
214             WebModule wm = WebModule.getWebModule(fo);
215             if (wm != null){
216                 try {
217                     final ClasspathInfo cpi = ClasspathInfo.create(wm.getDocumentBase());
218                     JavaSource js = JavaSource.create(cpi, Collections.EMPTY_LIST);
219                     
220                     js.runUserActionTask(new AbstractTask<CompilationController>() {
221                         
222                         public void run(CompilationController cc) throws Exception JavaDoc {
223                             Elements elements = cc.getElements();
224                             TypeElement element = elements.getTypeElement(fqn.trim());
225                             if (element != null) {
226                                 if (!UiUtils.open(cpi, element)){
227                                     String JavaDoc key = "goto_source_not_found"; // NOI18N
228
String JavaDoc msg = NbBundle.getBundle(StrutsConfigHyperlinkProvider.class).getString(key);
229                                     org.openide.awt.StatusDisplayer.getDefault().setStatusText(MessageFormat.format(msg, new Object JavaDoc [] { fqn } ));
230                                     
231                                 }
232                             }
233                         }
234                     }, false);
235                 } catch (IOException JavaDoc ex) {
236                     java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
237                             ex.getMessage(), ex);
238                 };
239             }
240         }
241     }
242     
243     private void findForm(String JavaDoc name, BaseDocument doc){
244         ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
245         
246         int offset = findDefinitionInSection(sup, "form-beans", "form-bean", "name", name);
247         if (offset > 0){
248             JTextComponent JavaDoc target = Utilities.getFocusedComponent();
249             target.setCaretPosition(offset);
250         } else {
251             String JavaDoc key = "goto_formbean_not_found"; // NOI18N
252
String JavaDoc msg = NbBundle.getBundle(StrutsConfigHyperlinkProvider.class).getString(key);
253             org.openide.awt.StatusDisplayer.getDefault().setStatusText(MessageFormat.format(msg, new Object JavaDoc [] { name } ));
254         }
255     }
256     
257     private void findResourcePath(String JavaDoc path, BaseDocument doc){
258         path = path.trim();
259         if (debug) debug("path: " + path);
260         if (path.indexOf('?') > 0){
261             // remove query from the path
262
path = path.substring(0, path.indexOf('?'));
263         }
264         WebModule wm = WebModule.getWebModule(NbEditorUtilities.getFileObject(doc));
265         if (wm != null){
266             FileObject docBase= wm.getDocumentBase();
267             FileObject fo = docBase.getFileObject(path);
268             if (fo == null){
269                 // maybe an action
270
String JavaDoc servletMapping = StrutsConfigUtilities.getActionServletMapping(wm.getDeploymentDescriptor());
271                 if (servletMapping != null){
272                     String JavaDoc actionPath = null;
273                     if (servletMapping != null && servletMapping.lastIndexOf('.')>0){
274                         // the mapping is in *.xx way
275
String JavaDoc extension = servletMapping.substring(servletMapping.lastIndexOf('.'));
276                         if (path.endsWith(extension))
277                             actionPath = path.substring(0, path.length()-extension.length());
278                         else
279                             actionPath = path;
280                     } else{
281                         // the mapping is /xx/* way
282
servletMapping = servletMapping.trim();
283                         String JavaDoc prefix = servletMapping.substring(0, servletMapping.length()-2);
284                         if (path.startsWith(prefix))
285                             actionPath = path.substring(prefix.length(), path.length());
286                         else
287                             actionPath = path;
288                     }
289                     if (debug) debug(" actionPath: " + actionPath);
290                     if(actionPath != null){
291                         ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
292                         int offset = findDefinitionInSection(sup, "action-mappings","action","path", actionPath);
293                         if (offset > 0){
294                             JTextComponent JavaDoc target = Utilities.getFocusedComponent();
295                             target.setCaretPosition(offset);
296                         }
297                     }
298                 }
299             } else
300                 openInEditor(fo);
301         }
302     }
303     
304     private int findDefinitionInSection(ExtSyntaxSupport sup, String JavaDoc section, String JavaDoc tag, String JavaDoc attribute, String JavaDoc value){
305         TokenItem token;
306         String JavaDoc startSection = "<"+ section;
307         String JavaDoc endSection = "</" + section;
308         String JavaDoc element = "<" + tag;
309         String JavaDoc attributeValue = "\""+ value + "\"";
310         int tagOffset = 0;
311         try{
312             token = sup.getTokenChain(0, 1);
313             //find section
314
while (token != null
315                     && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ELEMENT
316                     && token.getImage().equals(startSection))){
317                 token = token.getNext();
318             }
319             if (token.getImage().equals(startSection)){
320                 //find out, whether the section is empty
321
token = token.getNext();
322                 while (token != null
323                         && (token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
324                         || token.getImage().equals(">")))
325                     token = token.getNext();
326                 if(token.getImage().equals("/>") || token.getImage().equals(endSection))
327                     //section is empty
328
return -1;
329                 while(token != null
330                         && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ELEMENT
331                         && token.getImage().equals(endSection))){
332                     //find tag
333
while (token != null
334                             && (token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
335                             || (!token.getImage().equals(endSection)
336                             && !token.getImage().equals(element))) )
337                         token = token.getNext();
338                     if (token == null) return -1;
339                     tagOffset = token.getOffset();
340                     if (token.getImage().equals(element)){
341                         //find attribute
342
token = token.getNext();
343                         while (token != null
344                                 && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
345                                 && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE
346                                 && token.getImage().equals(attribute)))
347                             token = token.getNext();
348                         if (token == null) return -1;
349                         if (token.getImage().equals(attribute)){
350                             //find value
351
token = token.getNext();
352                             while (token != null
353                                     && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE_VALUE
354                                     && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
355                                     && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE)
356                                 token = token.getNext();
357                             if (token.getImage().equals(attributeValue))
358                                 return tagOffset;
359                         }
360                     } else
361                         token = token.getNext();
362                 }
363             }
364         } catch (BadLocationException JavaDoc e){
365             e.printStackTrace(System.out);
366         }
367         return -1;
368     }
369     
370     private void openInEditor(FileObject fObj){
371         if (fObj != null){
372             DataObject dobj = null;
373             try{
374                 dobj = DataObject.find(fObj);
375             } catch (DataObjectNotFoundException e){
376                 ErrorManager.getDefault().notify(e);
377                 return;
378             }
379             if (dobj != null){
380                 Node.Cookie cookie = dobj.getCookie(OpenCookie.class);
381                 if (cookie != null)
382                     ((OpenCookie)cookie).open();
383             }
384         }
385     }
386 }
387
Popular Tags