1 19 20 package org.netbeans.modules.ant.debugger; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.StringReader ; 25 import java.io.StringWriter ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.logging.Handler ; 31 import javax.swing.JEditorPane ; 32 import javax.swing.SwingUtilities ; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.Caret ; 35 import javax.swing.text.DefaultEditorKit ; 36 import javax.swing.text.EditorKit ; 37 import javax.swing.text.StyledDocument ; 38 import javax.xml.parsers.SAXParser ; 39 import javax.xml.parsers.SAXParserFactory ; 40 import org.apache.tools.ant.module.api.support.TargetLister; 41 import org.apache.tools.ant.module.spi.AntEvent; 42 import org.apache.tools.ant.module.spi.TaskStructure; 43 import org.openide.ErrorManager; 44 import org.openide.cookies.EditorCookie; 45 import org.openide.cookies.LineCookie; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileStateInvalidException; 48 import org.openide.filesystems.FileUtil; 49 import org.openide.loaders.DataObject; 50 import org.openide.loaders.DataObjectNotFoundException; 51 import org.openide.nodes.Node; 52 import org.openide.text.Annotatable; 53 import org.openide.text.Annotation; 54 import org.openide.text.Line; 55 import org.openide.text.NbDocument; 56 import org.openide.windows.TopComponent; 57 import org.xml.sax.Attributes ; 58 import org.xml.sax.InputSource ; 59 import org.xml.sax.Locator ; 60 import org.xml.sax.SAXException ; 61 import org.xml.sax.helpers.DefaultHandler ; 62 63 64 69 70 74 public class Utils { 75 76 private static Object currentLine; 77 78 static void markCurrent (final Object line) { 79 unmarkCurrent (); 80 81 Annotatable[] annotatables = (Annotatable[]) line; 82 int i = 0, k = annotatables.length; 83 84 DebuggerAnnotation[] annotations = new DebuggerAnnotation [k]; 86 if (annotatables [i] instanceof Line.Part) 87 annotations [i] = new DebuggerAnnotation ( 88 DebuggerAnnotation.CURRENT_LINE_PART_ANNOTATION_TYPE, 89 annotatables [i] 90 ); 91 else 92 annotations [i] = new DebuggerAnnotation ( 93 DebuggerAnnotation.CURRENT_LINE_ANNOTATION_TYPE, 94 annotatables [i] 95 ); 96 97 for (i = 1; i < k; i++) 99 if (annotatables [i] instanceof Line.Part) 100 annotations [i] = new DebuggerAnnotation ( 101 DebuggerAnnotation.CURRENT_LINE_PART_ANNOTATION_TYPE2, 102 annotatables [i] 103 ); 104 else 105 annotations [i] = new DebuggerAnnotation ( 106 DebuggerAnnotation.CURRENT_LINE_ANNOTATION_TYPE2, 107 annotatables [i] 108 ); 109 currentLine = annotations; 110 111 showLine (line); 112 } 113 114 static void unmarkCurrent () { 115 if (currentLine != null) { 116 117 int i, k = ((DebuggerAnnotation[]) currentLine).length; 119 for (i = 0; i < k; i++) 120 ((DebuggerAnnotation[]) currentLine) [i].detach (); 121 122 currentLine = null; 123 } 124 } 125 126 static void showLine (final Object line) { 127 133 final Annotatable[] a = (Annotatable[]) line; 134 SwingUtilities.invokeLater (new Runnable () { 135 public void run () { 136 if (a [0] instanceof Line) 137 ((Line) a [0]).show (Line.SHOW_GOTO); 138 else 139 if (a [0] instanceof Line.Part) 140 ((Line.Part) a [0]).getLine ().show (Line.SHOW_GOTO); 141 else 142 throw new InternalError (); 143 } 144 }); 145 } 146 147 static int getLineNumber (Object line) { 148 150 final Annotatable[] a = (Annotatable[]) line; 151 if (a [0] instanceof Line) 152 return ((Line) a [0]).getLineNumber (); 153 else 154 if (a [0] instanceof Line.Part) 155 return ((Line.Part) a [0]).getLine ().getLineNumber (); 156 else 157 throw new InternalError (); 158 } 159 160 public static boolean contains (Object currentLine, Line line) { 161 if (currentLine == null) return false; 162 final Annotatable[] a = (Annotatable[]) currentLine; 163 int i, k = a.length; 164 for (i = 0; i < k; i++) { 165 if (a [i].equals (line)) return true; 166 if ( a [i] instanceof Line.Part && 167 ((Line.Part) a [i]).getLine ().equals (line) 168 ) return true; 169 } 170 return false; 171 } 172 173 174 static Object getLine ( 175 final AntEvent event 176 ) { 177 File file = event.getScriptLocation (); 178 final int lineNumber = event.getLine (); 179 if (file == null) return null; 180 if (lineNumber < 0) return null; 181 182 FileObject fileObject = FileUtil.toFileObject (file); 183 EditorCookie editor; 184 LineCookie lineCookie; 185 try { 186 DataObject d = DataObject.find (fileObject); 187 editor = (EditorCookie) d.getCookie (EditorCookie.class); 188 lineCookie = (LineCookie) d.getCookie (LineCookie.class); 189 assert editor != null; 190 assert lineCookie != null; 191 192 StyledDocument doc = editor.openDocument (); 193 InputSource in = createInputSource 194 (fileObject, editor, doc); 195 SAXParserFactory factory = SAXParserFactory.newInstance (); 196 SAXParser parser = factory.newSAXParser (); 197 final int[] line = new int [4]; 198 class Handler extends DefaultHandler { 199 private Locator locator; 200 public void setDocumentLocator (Locator l) { 201 locator = l; 202 } 203 public void startElement ( 204 String uri, 205 String localname, 206 String qname, 207 Attributes attr 208 ) throws SAXException { 209 if (line [0] == 0) { 210 if ( qname.equals (event.getTaskName ()) && 211 locator.getLineNumber () == lineNumber 212 ) { 213 line[0] = locator.getLineNumber (); 214 line[1] = locator.getColumnNumber () - 1; 215 } 216 } 217 } 218 public void endElement ( 219 String uri, 220 String localname, 221 String qname 222 ) throws SAXException { 223 if ( line [0] != 0 && 224 line [2] == 0 && 225 qname.equals (event.getTaskName ()) 226 ) { 227 line[2] = locator.getLineNumber (); 228 line[3] = locator.getColumnNumber () - 1; 229 } 230 } 231 } 232 parser.parse (in, new Handler ()); 233 if (line [0] == 0) return null; 234 Annotatable[] annotatables = new Annotatable [ 235 line [2] - line [0] + 1 236 ]; 237 int i = 0; 238 for (int ln = line [0]; ln <= line [2]; ln ++) { 239 Line l = lineCookie.getLineSet ().getCurrent (ln - 1); 240 annotatables [i++] = l; 241 } 242 return annotatables; 243 } catch (Exception e) { 244 e.printStackTrace (); 245 } 246 return null; 247 } 248 249 static Object getLine ( 250 final TargetLister.Target target, 251 String nextTargetName 252 ) { 253 FileObject fileObject = target.getScript ().getFileObject (); 254 assert fileObject != null : "No build script for " + target.getName (); 255 EditorCookie editor; 256 LineCookie lineCookie; 257 try { 258 DataObject d = DataObject.find (fileObject); 259 editor = (EditorCookie) d.getCookie (EditorCookie.class); 260 lineCookie = (LineCookie) d.getCookie (LineCookie.class); 261 assert editor != null; 262 assert lineCookie != null; 263 } catch (DataObjectNotFoundException e) { 264 throw new AssertionError (e); 265 } 266 try { 267 StyledDocument doc = editor.openDocument (); 268 InputSource in = createInputSource 269 (fileObject, editor, doc); 270 SAXParserFactory factory = SAXParserFactory.newInstance (); 271 SAXParser parser = factory.newSAXParser (); 272 final int[] line = new int [4]; 273 class Handler extends DefaultHandler { 274 private Locator locator; 275 public void setDocumentLocator (Locator l) { 276 locator = l; 277 } 278 public void startElement ( 279 String uri, 280 String localname, 281 String qname, 282 Attributes attr 283 ) throws SAXException { 284 if (line [0] == 0) { 285 if (qname.equals ("target") && target.getName ().equals (attr.getValue ("name")) ) { 288 line[0] = locator.getLineNumber (); 289 line[1] = locator.getColumnNumber (); 290 } 291 } 292 } 293 public void endElement ( 294 String uri, 295 String localname, 296 String qname 297 ) throws SAXException { 298 if ( line [0] != 0 && 299 line [2] == 0 && 300 qname.equals ("target") 301 ) { 302 line[2] = locator.getLineNumber (); 303 line[3] = locator.getColumnNumber (); 304 } 305 } 306 } 307 parser.parse (in, new Handler ()); 308 if (line [0] == 0) return null; 309 310 int ln = line [0] - 1; 311 List annotatables = new ArrayList (); 312 if (nextTargetName != null) { 313 Line fLine = lineCookie.getLineSet ().getCurrent (ln); 314 int inx = findIndexOf(fLine.getText (), nextTargetName); 315 if (inx >= 0) { 316 annotatables.add (fLine.createPart ( 317 inx, nextTargetName.length () 318 )); 319 ln ++; 320 } 321 } 322 if (annotatables.size () < 1) 323 for (; ln < line [2]; ln ++) { 324 Line l = lineCookie.getLineSet ().getCurrent (ln); 325 annotatables.add (l); 326 } 327 return annotatables.toArray (new Annotatable [annotatables.size ()]); 328 } catch (Exception e) { 329 e.printStackTrace (); 330 } 331 return null; 332 } 333 334 private static int findIndexOf(String text, String target) { 335 int index = 0; 336 while ((index = text.indexOf(target, index)) > 0) { 337 char c = text.charAt(index - 1); 338 if (!Character.isWhitespace(c) && c != ',' && c != '\"') { 339 index++; 341 continue; 342 } 343 if (text.length() > index + target.length()) { 344 c = text.charAt(index + target.length()); 345 if (!Character.isWhitespace(c) && c != ',' && c != '\"') { 346 index++; 348 continue; 349 } 350 } 351 break; 352 } 353 return index; 354 } 355 356 359 private static InputSource createInputSource ( 360 FileObject fo, 361 EditorCookie editor, 362 final StyledDocument document 363 ) throws IOException , BadLocationException { 364 final StringWriter w = new StringWriter (document.getLength ()); 365 final EditorKit kit = findKit (editor); 366 final IOException [] ioe = new IOException [1]; 367 final BadLocationException [] ble = new BadLocationException [1]; 368 document.render(new Runnable () { 369 public void run() { 370 try { 371 kit.write (w, document, 0, document.getLength ()); 372 } catch (IOException e) { 373 ioe [0] = e; 374 } catch (BadLocationException e) { 375 ble [0] = e; 376 } 377 } 378 }); 379 if (ioe[0] != null) { 380 throw ioe [0]; 381 } else if (ble [0] != null) { 382 throw ble [0]; 383 } 384 InputSource in = new InputSource (new StringReader (w.toString ())); 385 if (fo != null) { try { 387 in.setSystemId (fo.getURL ().toExternalForm ()); 388 } catch (FileStateInvalidException e) { 389 assert false : e; 390 } 391 } 397 return in; 398 } 399 400 private static EditorKit findKit(final EditorCookie editor) { 401 if (SwingUtilities.isEventDispatchThread()) { 402 return findKit_(editor); 403 } else { 404 final EditorKit [] ek = new EditorKit [1]; 405 try { 406 SwingUtilities.invokeAndWait(new Runnable () { 407 public void run() { 408 ek[0] = findKit_(editor); 409 } 410 }); 411 } catch (InvocationTargetException ex) { 412 ErrorManager.getDefault().notify(ex.getTargetException()); 413 } catch (InterruptedException ex) { 414 ErrorManager.getDefault().notify(ex); 415 } 416 return ek[0]; 417 } 418 } 419 420 private static EditorKit findKit_(EditorCookie editor) { 421 JEditorPane [] panes = editor.getOpenedPanes(); 422 EditorKit kit; 423 if (panes != null) { 424 kit = panes[0].getEditorKit (); 425 } else { 426 kit = JEditorPane.createEditorKitForContentType ("text/xml"); if (kit == null) { 428 kit = new DefaultEditorKit (); 430 } 431 } 432 assert kit != null; 433 return kit; 434 } 435 } 436 | Popular Tags |