1 19 20 21 package org.netbeans.modules.i18n.jsp; 22 23 24 import javax.swing.JPanel ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.StyledDocument ; 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 43 public class JspI18nSupport extends JavaI18nSupport { 44 45 46 47 public JspI18nSupport(DataObject sourceDataObject) { 48 super(sourceDataObject); 49 } 50 51 52 53 protected I18nFinder createFinder() { 54 return new JspI18nFinder(document); 55 } 56 57 59 public boolean hasAdditionalCustomizer() { 60 return false; 61 } 62 63 65 public JPanel getAdditionalCustomizer() { 66 return null; 67 } 68 69 70 public void performAdditionalChanges() { 71 } 72 73 74 75 public static class JspI18nFinder extends JavaI18nFinder { 76 77 78 protected static final int STATE_JSP = 8; 79 80 protected static final int STATE_JSP_START_SCRIPTING = 9; 81 82 protected static final int STATE_JSP_SCRIPTING = 10; 83 84 protected static final int STATE_JSP_END_SCRIPTING = 11; 85 86 87 private static final String [] jspStrings = new String [] { 88 "jsp:declaration", "jsp:expression", "jsp:scriptlet" }; 93 94 96 private int oldJavaState; 97 98 99 public JspI18nFinder(StyledDocument document) { 100 super(document); 101 102 state = STATE_JSP; 103 } 104 105 106 107 protected void reset() { 108 super.reset(); 109 110 state = STATE_JSP; 111 } 112 113 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 if(character == '%') { 126 state = STATE_JSP_END_SCRIPTING; 128 oldJavaState = state; 129 130 return null; 131 } else if(character == '<') { for(int i=0; i<jspStrings.length; i++) { 134 if(isNextString("</"+jspStrings[i]+">")) { 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 151 protected HardCodedString handleStateJsp(char character) { 152 if(character == '<') 153 state = STATE_JSP_START_SCRIPTING; 154 155 return null; 156 } 157 158 161 protected HardCodedString handleStateJspStartScripting(char character) { 162 if(character == '%') 163 state = STATE_JSP_SCRIPTING; 164 else if(character == 'j') { for(int i=0; i<jspStrings.length; i++) { 167 if(isNextString(jspStrings[i]+">")) { 169 position += jspStrings[i].length(); 170 state = STATE_JAVA; 171 } 172 } 173 } else 174 state = STATE_JSP; 175 176 return null; 177 } 178 179 181 private boolean isNextString(String nextString) { 182 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 ble) { 191 } 193 194 return false; 195 } 196 197 200 protected HardCodedString handleStateJspScripting(char character) { 201 if(character == '@' || character == '-') 202 state = STATE_JSP; else 204 state = STATE_JAVA; 206 return null; 207 } 208 209 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 } 223 224 225 public static class Factory extends I18nSupport.Factory { 226 227 228 public I18nSupport createI18nSupport(DataObject dataObject) { 229 return new JspI18nSupport(dataObject); 230 } 231 232 235 public Class getDataObjectClass() { 236 try { 239 return Class.forName( 240 "org.netbeans.modules.web.core.jsploader.JspDataObject", false, 242 (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class) 243 ); 244 } catch(ClassNotFoundException cnfe) { 245 return null; 246 } 247 } 248 249 } } 251 | Popular Tags |