1 19 20 package org.netbeans.modules.web.struts.editor; 21 22 23 import java.io.IOException ; 24 import java.io.StringWriter ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.JTextComponent ; 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 40 41 public class StrutsEditorUtilities { 42 43 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 END_LINE = System.getProperty("line.separator"); 51 52 55 public static String getActionPath(BaseDocument doc, int offset){ 56 try { 57 ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport(); 58 TokenItem token = sup.getTokenChain(offset, offset+1); 59 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")){ 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"))) token = token.getNext(); 79 if (token != null && token.getImage().equals("path")){ 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 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 e) { 96 ErrorManager.getDefault().notify(e); 97 } 98 return null; 99 } 100 101 104 public static String getActionFormBeanName(BaseDocument doc, int offset){ 105 try { 106 ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport(); 107 TokenItem token = sup.getTokenChain(offset, offset+1); 108 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")){ 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"))) token = token.getNext(); 127 if (token != null && token.getImage().equals("name")){ 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 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 e) { 144 ErrorManager.getDefault().notify(e); 145 } 146 return null; 147 } 148 149 public static int writeForwardIntoAction(BaseDocument doc, Forward forward, String actionName) 150 throws IOException { 151 return writeElementIntoFather(doc, forward, "action", actionName, "forward"); } 153 154 public static int writeExceptionIntoAction(BaseDocument doc, StrutsException ex, String actionName) 155 throws IOException { 156 return writeElementIntoFather(doc, ex, "action", actionName, "exception"); } 158 159 public static int writePropertyIntoBean(BaseDocument doc, FormProperty prop, String beanName) 160 throws IOException { 161 return writeElementIntoFather(doc, prop, "form-bean", beanName, "form-property"); } 163 164 private static int writeElementIntoFather(BaseDocument doc, BaseBean bean, 165 String father, String fatherName, String element) throws IOException { 166 int possition = -1; 167 ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport(); 168 TokenItem token = null; 169 try { 170 int offset = 0; 171 do{ 173 offset = doc.getText(0, doc.getLength()).indexOf("\""+fatherName+"\"", offset+1); 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))); if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT 184 && token.getImage().equals("<"+father)){ token = token.getNext(); 186 while (token != null && token.getTokenID().getNumericID() != XML_ELEMENT ) 188 token = token.getNext(); 189 if (token != null && token.getImage().equals("/>")){ StringBuffer text = new StringBuffer (); 191 offset = token.getOffset(); 192 text.append(">"); text.append(END_LINE); 194 text.append(addNewLines(bean)); 195 text.append(END_LINE); 196 text.append("</"); text.append(father); text.append(">"); 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(">")){ offset = -1; 210 while (token != null 211 && !(token.getTokenID().getNumericID() == XML_ELEMENT 212 && token.getImage().equals("</"+father))){ while(token != null 214 && !(token.getTokenID().getNumericID() == XML_ELEMENT 215 && (token.getImage().equals("<"+element) || token.getImage().equals("</"+father)))) token = token.getNext(); 218 if (token != null && token.getImage().equals("<"+element)){ while (token!= null 220 && !(token.getTokenID().getNumericID() == XML_ELEMENT 221 && (token.getImage().equals("/>") || token.getImage().equals("</"+element)))) token = token.getNext(); 224 if (token != null && token.getImage().equals("</"+element)) while (token!= null 226 && !(token.getTokenID().getNumericID() == XML_ELEMENT 227 && token.getImage().equals(">"))) 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){ while (token!= null 234 && !(token.getTokenID().getNumericID() == XML_ELEMENT 235 && (token.getImage().equals("/>") || token.getImage().equals(">")))) 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 ex) { 247 ErrorManager.getDefault().notify(ex); 248 } 249 return possition; 250 } 251 252 public static int writeBean(BaseDocument doc, BaseBean bean, String element, String section) throws IOException { 253 int possition = -1; 254 boolean addroot = false; 255 boolean addsection = false; 256 String sBean = addNewLines(bean); 257 StringBuffer appendText = new StringBuffer (); 258 ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport(); 259 TokenItem token; 260 int offset; 261 try { 262 String docText = doc.getText(0, doc.getLength()); 263 String findString = "</" + element; 266 offset = docText.lastIndexOf(findString); 268 if (offset == -1){ 269 if ((offset = findEndOfElement(doc, "struts-config")) == -1){ offset = doc.getLength(); 271 appendText = new StringBuffer (); 272 appendText.append("<struts-config>"); appendText.append(END_LINE); 274 if (section != null && section.length()>0 ){ 275 appendText.append("<"+section+">"); appendText.append(END_LINE); 277 appendText.append(sBean); 278 appendText.append(END_LINE); 279 appendText.append("</"+section+">"); } 281 else{ 282 appendText.append(sBean); 283 } 284 appendText.append(END_LINE); 285 appendText.append("</struts-config>"); 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+">"); appendText.append(END_LINE); 295 appendText.append(sBean); 296 appendText.append(END_LINE); 297 appendText.append("</"+section+">"); } 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(">"))) 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(">"))) token = token.getPrevious(); 324 if (token != null) 325 possition = writeString(doc, sBean, token.getOffset()); 326 } 327 } 328 } catch (BadLocationException ex) { 329 ErrorManager.getDefault().notify(ex); 330 } 331 return possition; 332 } 333 334 private static String addNewLines(final BaseBean bean) throws IOException { 335 StringWriter sWriter = new StringWriter (); 336 bean.writeNode(sWriter); 337 String sBean = sWriter.toString(); 338 sBean = sBean.replaceAll("><", ">"+END_LINE+"<"); return sBean; 340 } 341 342 private static int writeString(BaseDocument doc, String text, int offset) throws BadLocationException { 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 element) throws BadLocationException { 357 String docText = doc.getText(0, doc.getLength()); 358 int offset = doc.getText(0, doc.getLength()).indexOf("<" + element); 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); } 367 return -1; 368 } 369 370 private static int findEndOfElement(BaseDocument doc, String element) throws BadLocationException { 371 String docText = doc.getText(0, doc.getLength()); 372 int offset = doc.getText(0, doc.getLength()).indexOf("</" + element); 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(">"))) token = token.getNext(); 384 if (token != null) 385 offset = token.getOffset(); 386 return offset; 387 } 388 offset = doc.getText(0, doc.getLength()).indexOf("</" + element); } 390 return -1; 391 } 392 393 } 394 | Popular Tags |