KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > editor > JSFEditorUtilities


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 package org.netbeans.modules.web.jsf.editor;
21
22
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import javax.swing.JEditorPane JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.text.BadLocationException JavaDoc;
30 import org.netbeans.editor.BaseDocument;
31 import org.netbeans.editor.TokenItem;
32 import org.netbeans.editor.ext.ExtSyntaxSupport;
33 import org.netbeans.modules.schema2beans.BaseBean;
34 import org.netbeans.modules.web.jsf.JSFConfigDataObject;
35 import org.netbeans.modules.web.jsf.JSFFrameworkProvider;
36 import org.openide.ErrorManager;
37 import org.openide.loaders.DataObject;
38 import org.openide.nodes.Node;
39 import org.openide.text.CloneableEditorSupport;
40
41 /**
42  *
43  * @author Petr Pisl
44  */

45
46 public class JSFEditorUtilities {
47     
48     /** The constant from XML editor
49      */

50     // The constant are taken from class org.netbeans.modules.xml.text.syntax.XMLTokenIDs
51
protected final static int XML_ELEMENT = 4;
52     protected final static int XML_TEXT = 1;
53     public final static String JavaDoc END_LINE = System.getProperty("line.separator"); //NOI18N
54

55     /** Returns the value of from-view-id element of navigation rule definition on the offset possition.
56      * If there is not the navigation rule definition on the offset, then returns null.
57      */

58     public static String JavaDoc getNavigationRule(BaseDocument doc, int offset){
59         try {
60             ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
61             TokenItem token = sup.getTokenChain(offset, offset+1);
62             // find the srart of the navigation rule definition
63
while (token != null
64                     && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
65                     && (token.getImage().equals("<navigation-rule")
66                     || token.getImage().equals("<managed-bean"))))
67                 token = token.getPrevious();
68             if (token != null && token.getImage().equals("<navigation-rule")){
69                 // find the from-view-ide element
70
while (token != null
71                         && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
72                         && (token.getImage().equals("</navigation-rule")
73                         || token.getImage().equals("<from-view-id"))))
74                     token = token.getNext();
75                 if (token!= null && token.getImage().equals("<from-view-id")){
76                     token = token.getNext();
77                     while (token!=null
78                             && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
79                             && token.getImage().equals(">")))
80                         token = token.getNext();
81                     while (token != null
82                             && token.getTokenID().getNumericID() != JSFEditorUtilities.XML_TEXT
83                             && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT)
84                         token = token.getNext();
85                     if (token != null && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_TEXT)
86                         return token.getImage().trim();
87                 }
88             }
89         }
90         catch (BadLocationException JavaDoc e) {
91             ErrorManager.getDefault().notify(e);
92         }
93         return null;
94     }
95     
96     public static int[] getNavigationRuleDefinition(BaseDocument doc, String JavaDoc ruleName){
97         try{
98             String JavaDoc text = doc.getText(0, doc.getLength());
99             //find first possition of text that is the ruleName
100
int offset = text.indexOf(ruleName);
101             int start = 0;
102             int end = 0;
103             ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
104             TokenItem token;
105             
106             while (offset != -1){
107                 token = sup.getTokenChain(offset, offset+1);
108                 if (token != null && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_TEXT){
109                     // find first xml element before the ruleName
110
while (token!=null
111                             && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
112                             && !token.getImage().equals(">")))
113                         token = token.getPrevious();
114                     // is it the rule definition?
115
if (token != null && token.getImage().equals("<from-view-id")){
116                         // find start of the rule definition
117
while (token != null
118                                 && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
119                                 && token.getImage().equals("<navigation-rule")))
120                             token = token.getPrevious();
121                         if(token != null && token.getImage().equals("<navigation-rule")){
122                             start = token.getOffset();
123                             token = sup.getTokenChain(offset, offset+1);
124                             // find the end of the rule definition
125
while (token != null
126                                     && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
127                                     && token.getImage().equals("</navigation-rule")))
128                                 token = token.getNext();
129                             if (token!=null && token.getImage().equals("</navigation-rule")){
130                                 while (token != null
131                                         && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
132                                         && token.getImage().equals(">")))
133                                     token = token.getNext();
134                                 if (token!=null && token.getImage().equals(">")){
135                                     end = token.getOffset()+1;
136                                     return new int[]{start, end};
137                                 }
138                             }
139                             return new int[]{start, text.length()};
140                         }
141                     }
142                 }
143                 offset = text.indexOf(ruleName, offset+ruleName.length());
144             }
145         } catch (BadLocationException JavaDoc e) {
146             ErrorManager.getDefault().notify(e);
147         }
148         return new int []{-1,-1};
149     }
150     
151     public static int[] getConverterDefinition(BaseDocument doc, String JavaDoc converterForClass){
152         try{
153             String JavaDoc text = doc.getText(0, doc.getLength());
154             //find first possition of text that is the ruleName
155
int offset = text.indexOf(converterForClass);
156             int start = 0;
157             int end = 0;
158             ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
159             TokenItem token;
160             
161             while (offset != -1){
162                 token = sup.getTokenChain(offset, offset+1);
163                 if (token != null && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_TEXT){
164                     // find first xml element before the ruleName
165
while (token!=null
166                             && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
167                             && !token.getImage().equals(">")))
168                         token = token.getPrevious();
169                     // is it the rule definition?
170
if (token != null && token.getImage().equals("<converter-for-class")){
171                         // find start of the rule definition
172
while (token != null
173                                 && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
174                                 && token.getImage().equals("<converter")))
175                             token = token.getPrevious();
176                         if(token != null && token.getImage().equals("<converter")){
177                             start = token.getOffset();
178                             token = sup.getTokenChain(offset, offset+1);
179                             // find the end of the rule definition
180
while (token != null
181                                     && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
182                                     && token.getImage().equals("</converter")))
183                                 token = token.getNext();
184                             if (token!=null && token.getImage().equals("</converter")){
185                                 while (token != null
186                                         && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
187                                         && token.getImage().equals(">")))
188                                     token = token.getNext();
189                                 if (token!=null && token.getImage().equals(">")){
190                                     end = token.getOffset()+1;
191                                     return new int[]{start, end};
192                                 }
193                             }
194                             return new int[]{start, text.length()};
195                         }
196                     }
197                 }
198                 offset = text.indexOf(converterForClass, offset+converterForClass.length());
199             }
200             
201         } catch (BadLocationException JavaDoc e) {
202             ErrorManager.getDefault().notify(e);
203         }
204         return new int []{-1,-1};
205     }
206     
207
208     /** Writes new bean to the document directly under <faces-config> element
209      */

210     public static int writeBean(BaseDocument doc, BaseBean bean, String JavaDoc element) throws IOException JavaDoc{
211         String JavaDoc sBean = addNewLines(bean);
212         int possition = -1;
213         ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
214         TokenItem token;
215         try {
216             String JavaDoc docText = doc.getText(0, doc.getLength());
217             //Check whether there is root element
218
if (docText.indexOf("<faces-config") == -1){ //NOI18N
219
doc.insertString(doc.getLength(), "<faces-config>" //NOI18N
220
+ END_LINE + "</faces-config>", null); //NOI18N
221
docText = doc.getText(0, doc.getLength());
222             }
223             String JavaDoc findString = "</" + element;
224             
225             //find index of last definition
226
int offset = docText.lastIndexOf(findString);
227             if (offset == -1)
228                 offset = docText.length() - 2;
229             token = sup.getTokenChain(offset, offset+1);
230             if (offset < (docText.length() - 2)
231                     && token != null && token.getTokenID().getNumericID() == XML_ELEMENT){
232                 while (token != null
233                         && !(token.getTokenID().getNumericID() == XML_ELEMENT
234                         && token.getImage().equals(">"))) //NOI18N
235
token = token.getNext();
236                 if (token != null)
237                     possition = writeString(doc, sBean, token.getOffset());
238             }
239             else {
240                 // write to end
241
if (token != null && token.getImage().equals(">")) //NOI18N
242
token = token.getPrevious();
243                 while (token != null
244                         && !(token.getTokenID().getNumericID() == XML_ELEMENT
245                         && token.getImage().equals(">"))) //NOI18N
246
token = token.getPrevious();
247                 if (token != null)
248                     possition = writeString(doc, sBean, token.getOffset());
249             }
250         } catch (BadLocationException JavaDoc ex) {
251             ErrorManager.getDefault().notify(ex);
252         }
253         return possition;
254     }
255     
256     private static String JavaDoc addNewLines(final BaseBean bean) throws IOException JavaDoc {
257         StringWriter JavaDoc sWriter = new StringWriter JavaDoc();
258         bean.writeNode(sWriter);
259         String JavaDoc sBean = sWriter.toString();
260         sBean = sBean.replaceAll("><", ">"+END_LINE+"<"); //NOI18N
261
return sBean;
262     }
263     
264     private static int writeString(BaseDocument doc, String JavaDoc text, int offset){
265         int formatLength = 0;
266         try{
267             doc.atomicLock();
268             offset = doc.getFormatter().indentNewLine(doc, offset+1);
269             doc.insertString(offset, text, null );
270             formatLength = doc.getFormatter().reformat(doc, offset, offset + text.length()-1);
271         }
272         catch(BadLocationException JavaDoc ex){
273             ErrorManager.getDefault().notify(ex);
274         }
275         finally {
276             doc.atomicUnlock();
277         }
278         return offset + formatLength + 1;
279     }
280     
281     /* Returns offset, where starts the definition of the manage bean
282      **/

283     public static int[] getManagedBeanDefinition(BaseDocument doc, String JavaDoc beanName){
284         try{
285             String JavaDoc text = doc.getText(0, doc.getLength());
286             int offset = text.indexOf(beanName);
287             int start = 0;
288             int end = 0;
289             ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
290             TokenItem token;
291             
292             while (offset != -1){
293                 token = sup.getTokenChain(offset, offset+1);
294                 if (token != null && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_TEXT){
295                     while (token!=null
296                             && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
297                             && !token.getImage().equals(">")))
298                         token = token.getPrevious();
299                     if (token != null && token.getImage().equals("<managed-bean-name")){ //NOI18N
300
while (token != null
301                                 && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
302                                 && token.getImage().equals("<managed-bean")))
303                             token = token.getPrevious();
304                         if(token != null && token.getImage().equals("<managed-bean")){
305                             start = token.getOffset();
306                             token = sup.getTokenChain(offset, offset+1);
307                             while (token != null
308                                     && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
309                                     && token.getImage().equals("</managed-bean")))
310                                 token = token.getNext();
311                             if (token!=null && token.getImage().equals("</managed-bean")){
312                                 while (token != null
313                                         && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
314                                         && token.getImage().equals(">")))
315                                     token = token.getNext();
316                                 if (token!=null && token.getImage().equals(">")){
317                                     end = token.getOffset()+1;
318                                     return new int[]{start, end};
319                                 }
320                             }
321                             return new int[]{start, text.length()};
322                         }
323                     }
324                 }
325                 offset = text.indexOf(beanName, offset+beanName.length());
326             }
327         }
328         catch (BadLocationException JavaDoc e) {
329             ErrorManager.getDefault().notify(e);
330         }
331         return new int []{-1,-1};
332     }
333     
334     /**
335      * Method that allows to find its
336      * CloneableEditorSupport from given DataObject
337      * @return the support or null if the CloneableEditorSupport
338      * was not found
339      * This method is hot fix for issue #53309
340      * this methd was copy/pasted from OpenSupport.Env class
341      * @param dob an instance of DataObject
342      */

343     public static CloneableEditorSupport findCloneableEditorSupport(DataObject dob) {
344         Node.Cookie obj = dob.getCookie(org.openide.cookies.OpenCookie.class);
345         if (obj instanceof CloneableEditorSupport) {
346             return (CloneableEditorSupport)obj;
347         }
348         obj = dob.getCookie(org.openide.cookies.EditorCookie.class);
349         if (obj instanceof CloneableEditorSupport) {
350             return (CloneableEditorSupport)obj;
351         }
352         return null;
353     }
354     
355     private static class CreateXMLPane implements Runnable JavaDoc{
356         JEditorPane JavaDoc ep;
357
358         public void run (){
359             ep = new JEditorPane JavaDoc("text/xml", "");
360         }
361
362         public JEditorPane JavaDoc getPane (){
363             return ep;
364         }
365     }
366     
367     /** This method returns a BaseDocument for the configuration file. If the configuration
368      * file is not opened, then the document is not created yet and this method returns
369      * in this case fake document.
370      */

371     public static BaseDocument getBaseDocument(JSFConfigDataObject config){
372         BaseDocument document = null;
373         CloneableEditorSupport editor = JSFEditorUtilities.findCloneableEditorSupport(config);
374         if (editor != null){
375             document = (BaseDocument)editor.getDocument();
376             if (document == null) {
377                 JEditorPane JavaDoc ep = null;
378                 CreateXMLPane run = new CreateXMLPane();
379                 try {
380                     SwingUtilities.invokeAndWait(run);
381                     document = new BaseDocument(run.getPane().getEditorKit().getClass(), false);
382                     String JavaDoc text = "";
383                     text = JSFFrameworkProvider.readResource(config.getPrimaryFile().getInputStream(), "UTF-8");
384                     document.remove(0, document.getLength());
385                     document.insertString(0, text, null);
386                 } catch (InterruptedException JavaDoc ex) {
387                     ErrorManager.getDefault().notify(ex);
388                 } catch (InvocationTargetException JavaDoc ex) {
389                     ErrorManager.getDefault().notify(ex);
390                 } catch (FileNotFoundException JavaDoc ex) {
391                     ErrorManager.getDefault().notify(ex);
392                 } catch (IOException JavaDoc ex) {
393                     ErrorManager.getDefault().notify(ex);
394                 } catch (BadLocationException JavaDoc ex) {
395                     ErrorManager.getDefault().notify(ex);
396                 }
397             }
398         }
399         return document;
400     }
401 }
402
Popular Tags