KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > i18n > TranslateFileChecker


1 package org.netbeans.modules.tasklist.i18n;
2
3 import java.text.MessageFormat JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import javax.swing.text.Document JavaDoc;
7 import javax.swing.text.StyledDocument JavaDoc;
8 import org.netbeans.api.mdr.MDRepository;
9 import org.netbeans.jmi.javamodel.Element;
10 import org.netbeans.jmi.javamodel.Resource;
11 import org.netbeans.jmi.javamodel.StringLiteral;
12 import org.netbeans.modules.javacore.api.JavaModel;
13 import org.netbeans.modules.tasklist.client.Suggestion;
14 import org.netbeans.modules.tasklist.client.SuggestionAgent;
15 import org.netbeans.modules.tasklist.client.SuggestionManager;
16 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
17 import org.netbeans.modules.tasklist.core.TLUtils;
18 import org.netbeans.modules.tasklist.core.util.TextPositionsMapper;
19 import org.netbeans.modules.tasklist.providers.SuggestionContext;
20 import org.openide.ErrorManager;
21 import org.openide.filesystems.FileObject;
22 import org.openide.loaders.DataObject;
23 import org.openide.loaders.DataObjectNotFoundException;
24
25 /**
26  * Checks // NOI18N comments in one file
27  *
28  * @author tl
29  */

30 public class TranslateFileChecker {
31     /**
32      * Found error
33      */

34     public static class Error {
35         /** line number (0 based) */
36         public int line;
37         
38         /** column number (0 based) */
39         public int column;
40         
41         /** content of the string constant */
42         public String JavaDoc constant;
43         
44         /**
45          * Constructor
46          *
47          * @param line line number (0 based)
48          * @param column column number (0 based)
49          * @param constant value of the constant
50          */

51         private Error(int line, int column, String JavaDoc constant) {
52             this.line = line;
53             this.column = column;
54             this.constant = constant;
55         }
56     }
57     
58     private FileObject fo;
59     
60     /* List<Error> */
61     private List JavaDoc problems = new ArrayList JavaDoc();
62     
63     private TextPositionsMapper m;
64     
65     public TranslateFileChecker(FileObject fo) {
66         this.fo = fo;
67     }
68     
69     /**
70      * Checks comment. This method cannot be called twice.
71      *
72      * @return found errors
73      */

74     public Error JavaDoc[] run() {
75         String JavaDoc suffix = fo.getExt();
76         if (!suffix.equalsIgnoreCase("java")) // NOI18N
77
return new Error JavaDoc[0];
78         
79         // try to get an implementation of Element from the lookup
80
// get repository
81
MDRepository rep = JavaModel.getJavaRepository();
82         // start a read transaction
83
rep.beginTrans(false);
84         try {
85             // set the active classpath to the default project
86
// classpath for a given fileobject
87
JavaModel.setClassPath(fo);
88             
89             // get a resource for a given FileObject
90
Resource res = JavaModel.getResource(fo);
91             m = new TextPositionsMapper(res.getSourceText());
92             
93             findErrors(res);
94         } finally {
95             // end transaction in finally block to make
96
// sure that the lock is released under any circumstances
97
rep.endTrans();
98         }
99         
100         return (Error JavaDoc[]) problems.toArray(new Error JavaDoc[problems.size()]);
101     }
102
103     /**
104      * Searches for string constants and checks // NOI18N
105      *
106      * @param el MDR element
107      */

108     private void findErrors(Element el) {
109         List JavaDoc ch = el.getChildren();
110         for (int i = 0; i < ch.size(); i++) {
111             Element e = (Element) ch.get(i);
112             if (e instanceof StringLiteral) {
113                 int[] pos = new int[2];
114                 m.findPosition(e.getStartOffset(), pos);
115                 String JavaDoc line = m.getLine(pos[0]);
116                 
117                 // this could be written better as it although
118
// finds lines for example with NOI18N as Java identifier
119
if (line.indexOf("NOI18N") == -1) {
120                     problems.add(new Error JavaDoc(pos[0], pos[1],
121                         ((StringLiteral) e).getValue()));
122                 }
123             } else {
124                 findErrors(e);
125             }
126         }
127     }
128 }
129
Popular Tags