KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > Utils


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 package org.netbeans.modules.ant.debugger;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.StringReader JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.logging.Handler JavaDoc;
31 import javax.swing.JEditorPane JavaDoc;
32 import javax.swing.SwingUtilities JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.Caret JavaDoc;
35 import javax.swing.text.DefaultEditorKit JavaDoc;
36 import javax.swing.text.EditorKit JavaDoc;
37 import javax.swing.text.StyledDocument JavaDoc;
38 import javax.xml.parsers.SAXParser JavaDoc;
39 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
58 import org.xml.sax.InputSource JavaDoc;
59 import org.xml.sax.Locator JavaDoc;
60 import org.xml.sax.SAXException JavaDoc;
61 import org.xml.sax.helpers.DefaultHandler JavaDoc;
62         
63         
64 /*
65  * AntTest.java
66  *
67  * Created on 19. leden 2004, 20:03
68  */

69
70 /**
71  *
72  * @author Honza
73  */

74 public class Utils {
75             
76     private static Object JavaDoc currentLine;
77     
78     static void markCurrent (final Object JavaDoc line) {
79         unmarkCurrent ();
80         
81         Annotatable[] annotatables = (Annotatable[]) line;
82         int i = 0, k = annotatables.length;
83         
84         // first line with icon in gutter
85
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         // other lines
98
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 // ((DebuggerAnnotation) currentLine).detach ();
118
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 JavaDoc line) {
127 // SwingUtilities.invokeLater (new Runnable () {
128
// public void run () {
129
// ((Line) line).show (Line.SHOW_GOTO);
130
// }
131
// });
132

133         final Annotatable[] a = (Annotatable[]) line;
134         SwingUtilities.invokeLater (new Runnable JavaDoc () {
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 JavaDoc ();
143             }
144         });
145     }
146     
147     static int getLineNumber (Object JavaDoc line) {
148 // return ((Line) line).getLineNumber ();
149

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 JavaDoc ();
158     }
159     
160     public static boolean contains (Object JavaDoc 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 JavaDoc getLine (
175         final AntEvent event
176     ) {
177         File JavaDoc 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 JavaDoc doc = editor.openDocument ();
193             InputSource JavaDoc in = createInputSource
194                 (fileObject, editor, doc);
195             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance ();
196             SAXParser JavaDoc parser = factory.newSAXParser ();
197             final int[] line = new int [4];
198             class Handler extends DefaultHandler JavaDoc {
199                 private Locator JavaDoc locator;
200                 public void setDocumentLocator (Locator JavaDoc l) {
201                     locator = l;
202                 }
203                 public void startElement (
204                     String JavaDoc uri,
205                     String JavaDoc localname,
206                     String JavaDoc qname,
207                     Attributes JavaDoc attr
208                 ) throws SAXException JavaDoc {
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 JavaDoc uri,
220                     String JavaDoc localname,
221                     String JavaDoc qname
222                 ) throws SAXException JavaDoc {
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 JavaDoc ());
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 JavaDoc e) {
244             e.printStackTrace ();
245         }
246         return null;
247     }
248     
249     static Object JavaDoc getLine (
250         final TargetLister.Target target,
251         String JavaDoc 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 JavaDoc (e);
265         }
266         try {
267             StyledDocument JavaDoc doc = editor.openDocument ();
268             InputSource JavaDoc in = createInputSource
269                 (fileObject, editor, doc);
270             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance ();
271             SAXParser JavaDoc parser = factory.newSAXParser ();
272             final int[] line = new int [4];
273             class Handler extends DefaultHandler JavaDoc {
274                 private Locator JavaDoc locator;
275                 public void setDocumentLocator (Locator JavaDoc l) {
276                     locator = l;
277                 }
278                 public void startElement (
279                     String JavaDoc uri,
280                     String JavaDoc localname,
281                     String JavaDoc qname,
282                     Attributes JavaDoc attr
283                 ) throws SAXException JavaDoc {
284                     if (line [0] == 0) {
285                         if (qname.equals ("target") && // NOI18N
286
target.getName ().equals (attr.getValue ("name")) // NOI18N
287
) {
288                             line[0] = locator.getLineNumber ();
289                             line[1] = locator.getColumnNumber ();
290                         }
291                     }
292                 }
293                 public void endElement (
294                     String JavaDoc uri,
295                     String JavaDoc localname,
296                     String JavaDoc qname
297                 ) throws SAXException JavaDoc {
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 JavaDoc ());
308             if (line [0] == 0) return null;
309             
310             int ln = line [0] - 1;
311             List JavaDoc annotatables = new ArrayList JavaDoc ();
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 JavaDoc e) {
329             e.printStackTrace ();
330         }
331         return null;
332     }
333     
334     private static int findIndexOf(String JavaDoc text, String JavaDoc 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                 // begins with some text => is not the target
340
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                     // ends with some text => is not the target
347
index++;
348                     continue;
349                 }
350             }
351             break;
352         }
353         return index;
354     }
355     
356     /**
357      * Utility method to get a properly configured XML input source for a script.
358      */

359     private static InputSource JavaDoc createInputSource (
360         FileObject fo,
361         EditorCookie editor,
362         final StyledDocument JavaDoc document
363     ) throws IOException JavaDoc, BadLocationException JavaDoc {
364         final StringWriter JavaDoc w = new StringWriter JavaDoc (document.getLength ());
365         final EditorKit JavaDoc kit = findKit (editor);
366         final IOException JavaDoc[] ioe = new IOException JavaDoc [1];
367         final BadLocationException JavaDoc[] ble = new BadLocationException JavaDoc [1];
368         document.render(new Runnable JavaDoc () {
369             public void run() {
370                 try {
371                     kit.write (w, document, 0, document.getLength ());
372                 } catch (IOException JavaDoc e) {
373                     ioe [0] = e;
374                 } catch (BadLocationException JavaDoc 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 JavaDoc in = new InputSource JavaDoc (new StringReader JavaDoc (w.toString ()));
385         if (fo != null) { // #10348
386
try {
387                 in.setSystemId (fo.getURL ().toExternalForm ());
388             } catch (FileStateInvalidException e) {
389                 assert false : e;
390             }
391             // [PENDING] Ant's ProjectHelper has an elaborate set of work-
392
// arounds for inconsistent parser behavior, e.g. file:foo.xml
393
// works in Ant but not with Xerces parser. You must use just foo.xml
394
// as the system ID. If necessary, Ant's algorithm could be copied
395
// here to make the behavior match perfectly, but it ought not be necessary.
396
}
397         return in;
398     }
399     
400     private static EditorKit JavaDoc findKit(final EditorCookie editor) {
401         if (SwingUtilities.isEventDispatchThread()) {
402             return findKit_(editor);
403         } else {
404             final EditorKit JavaDoc[] ek = new EditorKit JavaDoc[1];
405             try {
406                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
407                     public void run() {
408                         ek[0] = findKit_(editor);
409                     }
410                 });
411             } catch (InvocationTargetException JavaDoc ex) {
412                 ErrorManager.getDefault().notify(ex.getTargetException());
413             } catch (InterruptedException JavaDoc ex) {
414                 ErrorManager.getDefault().notify(ex);
415             }
416             return ek[0];
417         }
418     }
419     
420     private static EditorKit JavaDoc findKit_(EditorCookie editor) {
421         JEditorPane JavaDoc[] panes = editor.getOpenedPanes();
422         EditorKit JavaDoc kit;
423         if (panes != null) {
424             kit = panes[0].getEditorKit ();
425         } else {
426             kit = JEditorPane.createEditorKitForContentType ("text/xml"); // NOI18N
427
if (kit == null) {
428                 // #39301: fallback; can happen if xml/text-edit is disabled
429
kit = new DefaultEditorKit JavaDoc ();
430             }
431         }
432         assert kit != null;
433         return kit;
434     }
435 }
436
Popular Tags