1 19 20 package org.netbeans.modules.web.jsf.editor; 21 22 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.StringWriter ; 26 import java.lang.reflect.InvocationTargetException ; 27 import javax.swing.JEditorPane ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.text.BadLocationException ; 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 45 46 public class JSFEditorUtilities { 47 48 50 protected final static int XML_ELEMENT = 4; 52 protected final static int XML_TEXT = 1; 53 public final static String END_LINE = System.getProperty("line.separator"); 55 58 public static String getNavigationRule(BaseDocument doc, int offset){ 59 try { 60 ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport(); 61 TokenItem token = sup.getTokenChain(offset, offset+1); 62 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 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 e) { 91 ErrorManager.getDefault().notify(e); 92 } 93 return null; 94 } 95 96 public static int[] getNavigationRuleDefinition(BaseDocument doc, String ruleName){ 97 try{ 98 String text = doc.getText(0, doc.getLength()); 99 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 while (token!=null 111 && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT 112 && !token.getImage().equals(">"))) 113 token = token.getPrevious(); 114 if (token != null && token.getImage().equals("<from-view-id")){ 116 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 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 e) { 146 ErrorManager.getDefault().notify(e); 147 } 148 return new int []{-1,-1}; 149 } 150 151 public static int[] getConverterDefinition(BaseDocument doc, String converterForClass){ 152 try{ 153 String text = doc.getText(0, doc.getLength()); 154 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 while (token!=null 166 && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT 167 && !token.getImage().equals(">"))) 168 token = token.getPrevious(); 169 if (token != null && token.getImage().equals("<converter-for-class")){ 171 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 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 e) { 202 ErrorManager.getDefault().notify(e); 203 } 204 return new int []{-1,-1}; 205 } 206 207 208 210 public static int writeBean(BaseDocument doc, BaseBean bean, String element) throws IOException { 211 String sBean = addNewLines(bean); 212 int possition = -1; 213 ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport(); 214 TokenItem token; 215 try { 216 String docText = doc.getText(0, doc.getLength()); 217 if (docText.indexOf("<faces-config") == -1){ doc.insertString(doc.getLength(), "<faces-config>" + END_LINE + "</faces-config>", null); docText = doc.getText(0, doc.getLength()); 222 } 223 String findString = "</" + element; 224 225 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(">"))) token = token.getNext(); 236 if (token != null) 237 possition = writeString(doc, sBean, token.getOffset()); 238 } 239 else { 240 if (token != null && token.getImage().equals(">")) token = token.getPrevious(); 243 while (token != null 244 && !(token.getTokenID().getNumericID() == XML_ELEMENT 245 && token.getImage().equals(">"))) token = token.getPrevious(); 247 if (token != null) 248 possition = writeString(doc, sBean, token.getOffset()); 249 } 250 } catch (BadLocationException ex) { 251 ErrorManager.getDefault().notify(ex); 252 } 253 return possition; 254 } 255 256 private static String addNewLines(final BaseBean bean) throws IOException { 257 StringWriter sWriter = new StringWriter (); 258 bean.writeNode(sWriter); 259 String sBean = sWriter.toString(); 260 sBean = sBean.replaceAll("><", ">"+END_LINE+"<"); return sBean; 262 } 263 264 private static int writeString(BaseDocument doc, String 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 ex){ 273 ErrorManager.getDefault().notify(ex); 274 } 275 finally { 276 doc.atomicUnlock(); 277 } 278 return offset + formatLength + 1; 279 } 280 281 283 public static int[] getManagedBeanDefinition(BaseDocument doc, String beanName){ 284 try{ 285 String 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")){ 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 e) { 329 ErrorManager.getDefault().notify(e); 330 } 331 return new int []{-1,-1}; 332 } 333 334 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 { 356 JEditorPane ep; 357 358 public void run (){ 359 ep = new JEditorPane ("text/xml", ""); 360 } 361 362 public JEditorPane getPane (){ 363 return ep; 364 } 365 } 366 367 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 ep = null; 378 CreateXMLPane run = new CreateXMLPane(); 379 try { 380 SwingUtilities.invokeAndWait(run); 381 document = new BaseDocument(run.getPane().getEditorKit().getClass(), false); 382 String 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 ex) { 387 ErrorManager.getDefault().notify(ex); 388 } catch (InvocationTargetException ex) { 389 ErrorManager.getDefault().notify(ex); 390 } catch (FileNotFoundException ex) { 391 ErrorManager.getDefault().notify(ex); 392 } catch (IOException ex) { 393 ErrorManager.getDefault().notify(ex); 394 } catch (BadLocationException ex) { 395 ErrorManager.getDefault().notify(ex); 396 } 397 } 398 } 399 return document; 400 } 401 } 402 | Popular Tags |