KickJava   Java API By Example, From Geeks To Geeks.

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


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.struts.editor;
21
22
23 import java.io.IOException JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import org.netbeans.editor.BaseDocument;
28 import org.netbeans.editor.TokenItem;
29 import org.netbeans.editor.ext.ExtSyntaxSupport;
30 import org.netbeans.modules.schema2beans.BaseBean;
31 import org.netbeans.modules.web.struts.config.model.FormProperty;
32 import org.netbeans.modules.web.struts.config.model.Forward;
33 import org.netbeans.modules.web.struts.config.model.StrutsException;
34 import org.openide.ErrorManager;
35
36 /**
37  *
38  * @author Petr Pisl
39  */

40
41 public class StrutsEditorUtilities {
42     
43     /** The constant from XML editor
44      */

45     protected static int XML_ATTRIBUTE = 5;
46     protected static int XML_ELEMENT = 4;
47     protected static int XML_ATTRIBUTE_VALUE = 7;
48     
49     static public String JavaDoc END_LINE = System.getProperty("line.separator"); //NOI18N
50

51     
52     /** Returns the value of the path attribute, when there is an action
53      * definition on the offset possition. Otherwise returns null.
54      */

55     public static String JavaDoc getActionPath(BaseDocument doc, int offset){
56         try {
57             ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
58             TokenItem token = sup.getTokenChain(offset, offset+1);
59             // find the element, which is on the offset
60
while (token != null
61                     && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ELEMENT
62                         && !( token.getImage().equals("/>")
63                             || token.getImage().equals(">")
64                             || token.getImage().equals("<forward")
65                             || token.getImage().equals("<exception")
66                             || token.getImage().equals("</action")
67                             || token.getImage().equals("<description")
68                             || token.getImage().equals("<display-name")
69                             || token.getImage().equals("<set-property")
70                             || token.getImage().equals("<icon"))))
71                 token = token.getPrevious();
72             if (token != null && token.getImage().equals("<action")){ //NOI18N
73
token = token.getNext();
74                 while (token!= null
75                         && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
76                         && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE
77                         && token.getImage().equals("path"))) // NOI18N
78
token = token.getNext();
79                 if (token != null && token.getImage().equals("path")){ // NOI18N
80
token = token.getNext();
81                     while (token != null
82                             && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE_VALUE
83                             && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
84                             && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE)
85                         token = token.getNext();
86                     if (token != null && token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE_VALUE){
87                         String JavaDoc value = token.getImage().trim();
88                         value = value.substring(1);
89                         value = value.substring(0, value.length()-1);
90                         return value;
91                     }
92                 }
93             }
94             return null;
95         } catch (BadLocationException JavaDoc e) {
96             ErrorManager.getDefault().notify(e);
97         }
98         return null;
99     }
100     
101     /** Returns the value of the name attribute, when there is an ActionForm Bean
102      * definition on the offset possition. Otherwise returns null.
103      */

104     public static String JavaDoc getActionFormBeanName(BaseDocument doc, int offset){
105         try {
106             ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
107             TokenItem token = sup.getTokenChain(offset, offset+1);
108             // find the start of element, which is on the offset
109
while (token != null
110                     && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ELEMENT
111                         && !( token.getImage().equals("/>")
112                             || token.getImage().equals(">")
113                             || token.getImage().equals("<form-property")
114                             || token.getImage().equals("</form-bean")
115                             || token.getImage().equals("<description")
116                             || token.getImage().equals("<display-name")
117                             || token.getImage().equals("<set-property")
118                             || token.getImage().equals("<icon"))))
119                 token = token.getPrevious();
120             if (token != null && token.getImage().equals("<form-bean")){ //NOI18N
121
token = token.getNext();
122                 while (token!= null
123                         && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
124                         && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE
125                         && token.getImage().equals("name"))) // NOI18N
126
token = token.getNext();
127                 if (token != null && token.getImage().equals("name")){ // NOI18N
128
token = token.getNext();
129                     while (token != null
130                             && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE_VALUE
131                             && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
132                             && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE)
133                         token = token.getNext();
134                     if (token != null && token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE_VALUE){
135                         String JavaDoc value = token.getImage().trim();
136                         value = value.substring(1);
137                         value = value.substring(0, value.length()-1);
138                         return value;
139                     }
140                 }
141             }
142             return null;
143         } catch (BadLocationException JavaDoc e) {
144             ErrorManager.getDefault().notify(e);
145         }
146         return null;
147     }
148     
149     public static int writeForwardIntoAction(BaseDocument doc, Forward forward, String JavaDoc actionName)
150                     throws IOException JavaDoc{
151         return writeElementIntoFather(doc, forward, "action", actionName, "forward"); //NOI18N
152
}
153     
154     public static int writeExceptionIntoAction(BaseDocument doc, StrutsException ex, String JavaDoc actionName)
155                     throws IOException JavaDoc{
156         return writeElementIntoFather(doc, ex, "action", actionName, "exception"); //NOI18N
157
}
158     
159     public static int writePropertyIntoBean(BaseDocument doc, FormProperty prop, String JavaDoc beanName)
160                     throws IOException JavaDoc{
161         return writeElementIntoFather(doc, prop, "form-bean", beanName, "form-property"); //NOI18N
162
}
163     
164     private static int writeElementIntoFather(BaseDocument doc, BaseBean bean,
165             String JavaDoc father, String JavaDoc fatherName, String JavaDoc element) throws IOException JavaDoc{
166         int possition = -1;
167         ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
168         TokenItem token = null;
169         try {
170             int offset = 0;
171             // find the name as an attribute value
172
do{
173                 offset = doc.getText(0, doc.getLength()).indexOf("\""+fatherName+"\"", offset+1); //NOI18N
174
token = sup.getTokenChain(offset, offset+1);
175                 if (token != null && token.getTokenID().getNumericID() == XML_ATTRIBUTE_VALUE)
176                     while ( token != null
177                             && token.getTokenID().getNumericID() != XML_ELEMENT)
178                         token = token.getPrevious();
179             }
180             while (offset > 0 && token != null
181                         && !(token.getTokenID().getNumericID() == XML_ELEMENT
182                         && token.getImage().equals("<"+father))); //NOI18N
183
if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT
184                     && token.getImage().equals("<"+father)){ //NOI18N
185
token = token.getNext();
186                 // find the /> or >
187
while (token != null && token.getTokenID().getNumericID() != XML_ELEMENT )
188                     token = token.getNext();
189                 if (token != null && token.getImage().equals("/>")){ //NOI18N
190
StringBuffer JavaDoc text = new StringBuffer JavaDoc();
191                     offset = token.getOffset();
192                     text.append(">"); //NOI18N
193
text.append(END_LINE);
194                     text.append(addNewLines(bean));
195                     text.append(END_LINE);
196                     text.append("</"); text.append(father); text.append(">"); //NOI18N
197
try{
198                         doc.atomicLock();
199                         doc.remove(offset, 2);
200                         doc.insertString(offset, text.toString(), null);
201                         offset += doc.getFormatter().reformat(doc, offset, offset + text.length()-1);
202                         possition = offset;
203                     }
204                     finally{
205                         doc.atomicUnlock();
206                     }
207                 }
208                 if (token != null && token.getImage().equals(">")){ //NOI18N
209
offset = -1;
210                     while (token != null
211                             && !(token.getTokenID().getNumericID() == XML_ELEMENT
212                             && token.getImage().equals("</"+father))){ //NOI18N
213
while(token != null
214                                 && !(token.getTokenID().getNumericID() == XML_ELEMENT
215                                 && (token.getImage().equals("<"+element) //NOI18N
216
|| token.getImage().equals("</"+father)))) //NOI18N
217
token = token.getNext();
218                         if (token != null && token.getImage().equals("<"+element)){ //NOI18N
219
while (token!= null
220                                     && !(token.getTokenID().getNumericID() == XML_ELEMENT
221                                     && (token.getImage().equals("/>") //NOI18N
222
|| token.getImage().equals("</"+element)))) //NOI18N
223
token = token.getNext();
224                             if (token != null && token.getImage().equals("</"+element)) //NOI18N
225
while (token!= null
226                                         && !(token.getTokenID().getNumericID() == XML_ELEMENT
227                                         && token.getImage().equals(">"))) //NOI18N
228
token = token.getNext();
229                             if (token != null )
230                                 offset = token.getOffset() + token.getImage().length()-1;
231                         }
232                         if (token != null && token.getImage().equals("</"+father) && offset == -1){ //NOI18N
233
while (token!= null
234                                     && !(token.getTokenID().getNumericID() == XML_ELEMENT
235                                     && (token.getImage().equals("/>") //NOI18N
236
|| token.getImage().equals(">")))) //NOI18N
237
token = token.getPrevious();
238                             offset = token.getOffset()+token.getImage().length()-1;
239                         }
240                     }
241                     if (offset > 0)
242                         possition = writeString(doc, addNewLines(bean), offset);
243                 }
244             }
245                 
246         } catch (BadLocationException JavaDoc ex) {
247             ErrorManager.getDefault().notify(ex);
248         }
249         return possition;
250     }
251     
252     public static int writeBean(BaseDocument doc, BaseBean bean, String JavaDoc element, String JavaDoc section) throws IOException JavaDoc{
253         int possition = -1;
254         boolean addroot = false;
255         boolean addsection = false;
256         String JavaDoc sBean = addNewLines(bean);
257         StringBuffer JavaDoc appendText = new StringBuffer JavaDoc();
258         ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
259         TokenItem token;
260         int offset;
261         try {
262             String JavaDoc docText = doc.getText(0, doc.getLength());
263             //Check whether there is root element
264
String JavaDoc findString = "</" + element; //NOI18N
265

266             //find index of last definition
267
offset = docText.lastIndexOf(findString);
268             if (offset == -1){
269                 if ((offset = findEndOfElement(doc, "struts-config")) == -1){ //NOI18N
270
offset = doc.getLength();
271                     appendText = new StringBuffer JavaDoc();
272                     appendText.append("<struts-config>"); //NOI18N
273
appendText.append(END_LINE);
274                     if (section != null && section.length()>0 ){
275                         appendText.append("<"+section+">"); //NOI18N
276
appendText.append(END_LINE);
277                         appendText.append(sBean);
278                         appendText.append(END_LINE);
279                         appendText.append("</"+section+">"); //NOI18N
280
}
281                     else{
282                         appendText.append(sBean);
283                     }
284                     appendText.append(END_LINE);
285                     appendText.append("</struts-config>"); //NOI18N
286
appendText.append(END_LINE);
287                     possition = writeString(doc, appendText.toString(), offset);
288                 }
289                 else{
290                     if (section != null && section.length()>0){
291                         int offsetSection;
292                         if ((offsetSection = findEndOfElement(doc, section)) == -1){
293                             appendText.append("<"+section+">"); //NOI18N
294
appendText.append(END_LINE);
295                             appendText.append(sBean);
296                             appendText.append(END_LINE);
297                             appendText.append("</"+section+">"); //NOI18N
298
}
299                         else {
300                             appendText.append(sBean);
301                             offset = offsetSection;
302                         }
303                     }
304                     else
305                         appendText.append(sBean);
306                     token = sup.getTokenChain(offset, offset+1);
307                     if (token != null) token = token.getPrevious();
308                     while (token != null
309                             && !(token.getTokenID().getNumericID() == XML_ELEMENT
310                             && token.getImage().equals(">"))) //NOI18N
311
token = token.getPrevious();
312                     if (token != null)
313                         offset = token.getOffset();
314                     possition=writeString(doc, appendText.toString(), offset);
315                 }
316             }
317             else{
318                 token = sup.getTokenChain(offset, offset+1);
319                 if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT){
320                     while (token != null
321                             && !(token.getTokenID().getNumericID() == XML_ELEMENT
322                             && token.getImage().equals(">"))) //NOI18N
323
token = token.getPrevious();
324                     if (token != null)
325                         possition = writeString(doc, sBean, token.getOffset());
326                 }
327             }
328         } catch (BadLocationException JavaDoc ex) {
329             ErrorManager.getDefault().notify(ex);
330         }
331         return possition;
332     }
333     
334     private static String JavaDoc addNewLines(final BaseBean bean) throws IOException JavaDoc {
335         StringWriter JavaDoc sWriter = new StringWriter JavaDoc();
336         bean.writeNode(sWriter);
337         String JavaDoc sBean = sWriter.toString();
338         sBean = sBean.replaceAll("><", ">"+END_LINE+"<"); //NOI18N
339
return sBean;
340     }
341     
342     private static int writeString(BaseDocument doc, String JavaDoc text, int offset) throws BadLocationException JavaDoc {
343         int formatLength = 0;
344         try{
345             doc.atomicLock();
346             offset = doc.getFormatter().indentNewLine(doc, offset+1);
347             doc.insertString(Math.min(offset, doc.getLength()), text, null );
348             formatLength = doc.getFormatter().reformat(doc, offset, offset + text.length()-1);
349         }
350         finally{
351             doc.atomicUnlock();
352         }
353         return Math.min(offset + formatLength + 1, doc.getLength());
354     }
355     
356     private static int findStartOfElement(BaseDocument doc, String JavaDoc element) throws BadLocationException JavaDoc{
357         String JavaDoc docText = doc.getText(0, doc.getLength());
358         int offset = doc.getText(0, doc.getLength()).indexOf("<" + element); //NOI18N
359
ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
360         TokenItem token;
361         while (offset > 0){
362            token = sup.getTokenChain(offset, offset+1);
363            if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT)
364                return token.getOffset();
365            offset = doc.getText(0, doc.getLength()).indexOf("<" + element); //NOI18N
366
}
367         return -1;
368     }
369     
370     private static int findEndOfElement(BaseDocument doc, String JavaDoc element) throws BadLocationException JavaDoc{
371         String JavaDoc docText = doc.getText(0, doc.getLength());
372         int offset = doc.getText(0, doc.getLength()).indexOf("</" + element); //NOI18N
373
ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
374         TokenItem token;
375         while (offset > 0){
376            token = sup.getTokenChain(offset, offset+1);
377            if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT){
378                 offset = token.getOffset();
379                 token = token.getNext();
380                 while (token != null
381                         && !(token.getTokenID().getNumericID() == XML_ELEMENT
382                         && token.getImage().equals(">"))) //NOI18N
383
token = token.getNext();
384                 if (token != null)
385                     offset = token.getOffset();
386                return offset;
387            }
388            offset = doc.getText(0, doc.getLength()).indexOf("</" + element); //NOI18N
389
}
390         return -1;
391     }
392     
393 }
394
Popular Tags