KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > jsp > JspI18nSupport


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.i18n.jsp;
22
23
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27
28 import org.netbeans.modules.i18n.HardCodedString;
29 import org.netbeans.modules.i18n.I18nSupport;
30 import org.netbeans.modules.i18n.java.JavaI18nSupport;
31
32 import org.openide.loaders.DataObject;
33 import org.openide.util.Lookup;
34
35
36 /**
37  * Support for internationalizing strings in jsp sources.
38  * It support i18n-izing strings occured only in jsp scriptlets, declaractions and expressions.
39  *
40  * @author Peter Zavadsky
41  * @see org.netbeans.modules.i18n.JavaI18nSupport
42  */

43 public class JspI18nSupport extends JavaI18nSupport {
44
45
46     /** Constructor. */
47     public JspI18nSupport(DataObject sourceDataObject) {
48         super(sourceDataObject);
49     }
50     
51     
52     /** Creates <code>I18nFinder</code>. Implements superclass abstract method. */
53     protected I18nFinder createFinder() {
54         return new JspI18nFinder(document);
55     }
56  
57     /** Overrides superclass method.
58      * @return false */

59     public boolean hasAdditionalCustomizer() {
60         return false;
61     }
62     
63     /** Overrides superclass method.
64      * @return null */

65     public JPanel JavaDoc getAdditionalCustomizer() {
66         return null;
67     }
68     
69     /** Overrides superclass method. Does nothing. */
70     public void performAdditionalChanges() {
71     }
72     
73     
74     /** Finder which search hard coded strings in java sources. */
75     public static class JspI18nFinder extends JavaI18nFinder {
76
77         /** State when finder is in jsp code excluding parts where java code can occure. */
78         protected static final int STATE_JSP = 8;
79         /** State when finder is at the start of scripting tag where java code could occure. */
80         protected static final int STATE_JSP_START_SCRIPTING = 9;
81         /** State when finder is at beginnig of tag where java code occure. */
82         protected static final int STATE_JSP_SCRIPTING = 10;
83         /** State when finder is at the end of scripting tag where java code occures. */
84         protected static final int STATE_JSP_END_SCRIPTING = 11;
85
86         /** Helper array holding jsp scripting element tags for jsp using xml tags. */
87         private static final String JavaDoc[] jspStrings = new String JavaDoc[] {
88             "jsp:declaration", // NOI18N
89
"jsp:expression", // NOI18N
90
"jsp:scriptlet" // NOI18N
91
}; // PENDING<< Don't know if to use it.
92

93         
94         /** Helper variable. Stores old state of java code when possible end of srcipting element occured
95          * and state chaned to STATE_JSP_END_SCRIPTING. */

96         private int oldJavaState;
97         
98         /** Constructor. */
99         public JspI18nFinder(StyledDocument JavaDoc document) {
100             super(document);
101             
102             state = STATE_JSP;
103         }
104
105         
106         /** Resets finder. Overrides superclass method. */
107         protected void reset() {
108             super.reset();
109             
110             state = STATE_JSP;
111         }
112         
113         /** Handles state changes according next character. Overrides superclass method. */
114         protected HardCodedString handleCharacter(char character) {
115             if(state == STATE_JSP)
116                 return handleStateJsp(character);
117             else if(state == STATE_JSP_START_SCRIPTING)
118                 return handleStateJspStartScripting(character);
119             else if(state == STATE_JSP_SCRIPTING)
120                 return handleStateJspScripting(character);
121             else if(state == STATE_JSP_END_SCRIPTING)
122                 return handleStateJspEndScripting(character);
123             else {
124                 // Java code states.
125
if(character == '%') {
126                     // Could be end of scripting element.
127
state = STATE_JSP_END_SCRIPTING;
128                     oldJavaState = state;
129                     
130                     return null;
131                 } else if(character == '<') { // PENDING see above.
132
// Could be end jsp:expression, jsp:scriptlet or jsp:declaration tag.
133
for(int i=0; i<jspStrings.length; i++) {
134                         if(isNextString("</"+jspStrings[i]+">")) { // NOI18N
135

136                             position += jspStrings[i].length() + 2;
137                             state = STATE_JSP;
138                             
139                             return null;
140                         }
141                     }
142                 }
143                 
144                 return super.handleCharacter(character);
145             }
146         }
147
148         /** Handles state <code>STATE_JSP</code>.
149          * @param character char to proceede
150          * @return null */

151         protected HardCodedString handleStateJsp(char character) {
152             if(character == '<')
153                 state = STATE_JSP_START_SCRIPTING;
154                 
155             return null;
156         }
157
158         /** Handles state <code>STATE_JSP_START_SCRIPTING</code>.
159          * @param character char to proceede
160          * @return null */

161         protected HardCodedString handleStateJspStartScripting(char character) {
162             if(character == '%')
163                 state = STATE_JSP_SCRIPTING;
164             else if(character == 'j') { // PENDING see above.
165
// Could be jsp:expression, jsp:scriptlet or jsp:declaration tag.
166
for(int i=0; i<jspStrings.length; i++) {
167                     if(isNextString(jspStrings[i]+">")) { // NOI18N
168

169                         position += jspStrings[i].length();
170                         state = STATE_JAVA;
171                     }
172                 }
173             } else
174                 state = STATE_JSP;
175                 
176             return null;
177         }
178
179         /** Utility method.
180          * @return true if follows string in searched docuement */

181         private boolean isNextString(String JavaDoc nextString) {
182             // PENDING better would be operate on buffer tah document.
183

184             if(buffer.length < position + nextString.length())
185                 return false;
186             
187             try {
188                 if(nextString.equals(document.getText(position, nextString.length())))
189                     return true;
190             } catch(BadLocationException JavaDoc ble) {
191                 // It's OK just to catch it.
192
}
193             
194             return false;
195         }
196         
197         /** Handles state <code>STATE_JSP_SCRIPTING</code>.
198          * @param character char to proceede
199          * @return null */

200         protected HardCodedString handleStateJspScripting(char character) {
201             if(character == '@' || character == '-')
202                 state = STATE_JSP; // JSP directive or comment
203
else
204                 state = STATE_JAVA; // java code
205

206             return null;
207         }
208         
209         /** Handles state <code>STATE_JSP_END_SCRIPTING</code>.
210          * @param character char to proceede
211          * @return null */

212         protected HardCodedString handleStateJspEndScripting(char character) {
213             if(character == '>')
214                 state = STATE_JSP;
215             else
216                 state = oldJavaState;
217                 
218             return null;
219         }
220        
221     } // End of JavaI18nFinder nested class.
222

223     
224     /** Factory for <code>JspI18nSupport</code>. */
225     public static class Factory extends I18nSupport.Factory {
226         
227         /** Implements superclass abstract method. */
228         public I18nSupport createI18nSupport(DataObject dataObject) {
229             return new JspI18nSupport(dataObject);
230         }
231         
232         /** Gets class of supported <code>DataObject</code>.
233          * @return <code>JspDataObject</code> class or <code>null</code>
234          * if jsp module is not available */

235         public Class JavaDoc getDataObjectClass() {
236             // XXX Cleaner should be this code dependend on java module
237
// -> I18n API needed.
238
try {
239                 return Class.forName(
240                     "org.netbeans.modules.web.core.jsploader.JspDataObject", // NOI18N
241
false,
242                     (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class)
243                 );
244             } catch(ClassNotFoundException JavaDoc cnfe) {
245                 return null;
246             }
247         }
248
249     } // End of class Factory.
250
}
251
Popular Tags