improved "Open Include"
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / IdentifierIndexManager.java
1 package net.sourceforge.phpeclipse.builder;
2 import java.io.BufferedInputStream;
3 import java.io.BufferedReader;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Comparator;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.SortedMap;
17 import java.util.StringTokenizer;
18 import java.util.TreeMap;
19
20 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
21 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
22 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
23 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
24 import net.sourceforge.phpdt.internal.compiler.util.Util;
25 import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
26
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.runtime.CoreException;
29 /**
30  * Manages the identifer index information for a specific project
31  *  
32  */
33 public class IdentifierIndexManager {
34   public class LineCreator implements ITerminalSymbols {
35     private Scanner fScanner;
36     private int fToken;
37     public LineCreator() {
38       fScanner = new Scanner(true, false, false, false, true, null, null);
39     }
40     /**
41      * Add the information of the current identifier to the line
42      * 
43      * @param typeOfIdentifier
44      *            the type of the identifier ('c'lass, 'd'efine, 'f'unction,
45      *            'm'ethod, 'v'ariable)
46      * @param identifier
47      *            current identifier
48      * @param line
49      *            Buffer for the current index line
50      * @param phpdocOffset
51      *            the offset of the PHPdoc comment if available
52      * @param phpdocLength
53      *            the length of the PHPdoc comment if available
54      */
55     private void addIdentifierInformation(char typeOfIdentifier,
56         char[] identifier, StringBuffer line, int phpdocOffset, int phpdocLength) {
57       line.append('\t');
58       line.append(typeOfIdentifier);
59       line.append(identifier);
60       line.append("\to"); // Offset
61       line.append(fScanner.getCurrentTokenStartPosition());
62       if (phpdocOffset >= 0) {
63         line.append("\tp"); // phpdoc offset
64         line.append(phpdocOffset);
65         line.append("\tl"); // phpdoc length
66         line.append(phpdocLength);
67       }
68     }
69     /**
70      * Get the next token from input
71      */
72     private void getNextToken() {
73       try {
74         fToken = fScanner.getNextToken();
75         if (Scanner.DEBUG) {
76           int currentEndPosition = fScanner.getCurrentTokenEndPosition();
77           int currentStartPosition = fScanner.getCurrentTokenStartPosition();
78           System.out.print(currentStartPosition + "," + currentEndPosition
79               + ": ");
80           System.out.println(fScanner.toStringAction(fToken));
81         }
82         return;
83       } catch (InvalidInputException e) {
84         // ignore errors
85 //        e.printStackTrace();
86       }
87       fToken = TokenNameERROR;
88     }
89     private void parseDeclarations(char[] parent, StringBuffer buf,
90         boolean goBack) {
91       char[] ident;
92       char[] classVariable;
93       int counter = 0;
94       int phpdocOffset = -1;
95       int phpdocLength = -1;
96       try {
97         while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
98           phpdocOffset = -1;
99           if (fToken == TokenNameCOMMENT_PHPDOC) {
100             phpdocOffset = fScanner.getCurrentTokenStartPosition();
101             phpdocLength = fScanner.getCurrentTokenEndPosition()
102                 - fScanner.getCurrentTokenStartPosition() + 1;
103             getNextToken();
104             if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
105               break;
106             }
107           }
108           if (fToken == TokenNamevar || fToken == TokenNamepublic
109               || fToken == TokenNameprotected || fToken == TokenNameprivate) {
110             getNextToken();
111             if (fToken == TokenNameVariable) {
112               ident = fScanner.getCurrentIdentifierSource();
113               classVariable = new char[ident.length - 1];
114               System.arraycopy(ident, 1, classVariable, 0, ident.length - 1);
115               addIdentifierInformation('v', classVariable, buf, phpdocOffset,
116                   phpdocLength);
117               getNextToken();
118             }
119           } else if (fToken == TokenNamefunction) {
120             getNextToken();
121             if (fToken == TokenNameAND) {
122               getNextToken();
123             }
124             if (fToken == TokenNameIdentifier) {
125               ident = fScanner.getCurrentIdentifierSource();
126               if (parent != null && equalCharArrays(parent, ident)) {
127                 // constructor function
128                 addIdentifierInformation('k', ident, buf, phpdocOffset,
129                     phpdocLength);
130               } else {
131                 if (parent != null) {
132                   // class method function
133                   addIdentifierInformation('m', ident, buf, phpdocOffset,
134                       phpdocLength);
135                 } else {
136                   // nested function ?!
137                   addIdentifierInformation('f', ident, buf, phpdocOffset,
138                       phpdocLength);
139                 }
140               }
141               getNextToken();
142               parseDeclarations(null, buf, true);
143             }
144           } else if (fToken == TokenNameclass) {
145             getNextToken();
146             if (fToken == TokenNameIdentifier) {
147               ident = fScanner.getCurrentIdentifierSource();
148               addIdentifierInformation('c', ident, buf, phpdocOffset,
149                   phpdocLength);
150               getNextToken();
151               //skip tokens for classname, extends and others until we have
152               // the opening '{'
153               while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
154                   && fToken != TokenNameERROR) {
155                 getNextToken();
156               }
157               parseDeclarations(ident, buf, true);
158             }
159           } else if (fToken == TokenNameIdentifier) {
160             ident = fScanner.getCurrentIdentifierSource();
161             getNextToken();
162             if (ident.length==6 && 
163                 ident[0]=='d' && 
164                 ident[1]=='e' && 
165                 ident[2]=='f' && 
166                 ident[3]=='i' && 
167                 ident[4]=='n' && 
168                 ident[5]=='e') {
169               if (fToken == TokenNameLPAREN) {
170                 getNextToken();
171                 if (fToken == TokenNameStringLiteral) {
172                   ident = fScanner.getCurrentStringLiteralSource();
173                   addIdentifierInformation('d', ident, buf, phpdocOffset,
174                       phpdocLength);
175                   getNextToken();
176                 }
177               }
178             }
179           } else if (fToken == TokenNameLBRACE) {
180             getNextToken();
181             counter++;
182           } else if (fToken == TokenNameRBRACE) {
183             getNextToken();
184             --counter;
185             if (counter == 0 && goBack) {
186               return;
187             }
188           } else {
189             getNextToken();
190           }
191         }
192       } catch (SyntaxError e) {
193         // TODO Auto-generated catch block
194         e.printStackTrace();
195       }
196     }
197     synchronized public void parseIdentifiers(char[] charArray, StringBuffer buf) {
198       char[] ident;
199       String identifier;
200       int counter = 0;
201       int phpdocOffset = -1;
202       int phpdocLength = -1;
203       fScanner.setSource(charArray);
204       fScanner.setPHPMode(false);
205       fToken = TokenNameEOF;
206       getNextToken();
207       try {
208         while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
209           phpdocOffset = -1;
210           if (fToken == TokenNameCOMMENT_PHPDOC) {
211             phpdocOffset = fScanner.getCurrentTokenStartPosition();
212             phpdocLength = fScanner.getCurrentTokenEndPosition()
213                 - fScanner.getCurrentTokenStartPosition() + 1;
214             getNextToken();
215             if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
216               break;
217             }
218           }
219           if (fToken == TokenNamefunction) {
220             getNextToken();
221             if (fToken == TokenNameAND) {
222               getNextToken();
223             }
224             if (fToken == TokenNameIdentifier) {
225               ident = fScanner.getCurrentIdentifierSource();
226               addIdentifierInformation('f', ident, buf, phpdocOffset,
227                   phpdocLength);
228               getNextToken();
229               parseDeclarations(null, buf, true);
230             }
231           } else if (fToken == TokenNameclass) {
232             getNextToken();
233             if (fToken == TokenNameIdentifier) {
234               ident = fScanner.getCurrentIdentifierSource();
235               addIdentifierInformation('c', ident, buf, phpdocOffset,
236                   phpdocLength);
237               getNextToken();
238               //skip fTokens for classname, extends and others until we have
239               // the opening '{'
240               while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
241                   && fToken != TokenNameERROR) {
242                 getNextToken();
243               }
244               parseDeclarations(ident, buf, true);
245             }
246           } else if (fToken == TokenNameIdentifier) {
247             ident = fScanner.getCurrentIdentifierSource();
248             getNextToken();
249             if (ident.length==6 && 
250                 ident[0]=='d' && 
251                 ident[1]=='e' && 
252                 ident[2]=='f' && 
253                 ident[3]=='i' && 
254                 ident[4]=='n' && 
255                 ident[5]=='e') {
256               if (fToken == TokenNameLPAREN) {
257                 getNextToken();
258                 if (fToken == TokenNameStringLiteral) {
259                   ident = fScanner.getCurrentStringLiteralSource();
260                   addIdentifierInformation('d', ident, buf, phpdocOffset,
261                       phpdocLength);
262                   getNextToken();
263                 }
264               }
265             }
266           } else {
267             getNextToken();
268           }
269         }
270       } catch (SyntaxError e) {
271         // TODO Auto-generated catch block
272         e.printStackTrace();
273       }
274     }
275   }
276   class StringComparator implements Comparator {
277     public int compare(Object o1, Object o2) {
278       String s1 = (String) o1;
279       String s2 = (String) o2;
280       return s1.compareTo(s2);
281       //        return s1.toUpperCase().compareTo(s2.toUpperCase());
282     }
283     public boolean equals(Object o) {
284       String s = (String) o;
285       return compare(this, o) == 0;
286     }
287   }
288   private HashMap fFileMap;
289   private String fFilename;
290   private TreeMap fIndentifierMap;
291   public IdentifierIndexManager(String filename) {
292     fFilename = filename;
293     initialize();
294     readFile();
295   }
296   /**
297    * Check if 2 char arrays are equal
298    * 
299    * @param a
300    * @param b
301    * @return
302    */
303   private static boolean equalCharArrays(char[] a, char[] b) {
304     if (a.length != b.length) {
305       return false;
306     }
307     for (int i = 0; i < b.length; i++) {
308       if (a[i] != b[i]) {
309         return false;
310       }
311     }
312     return true;
313   }
314   public LineCreator createLineCreator() {
315     return new LineCreator();
316   }
317   /**
318    * Add the information for a given IFile resource
319    *  
320    */
321   public void addFile(IFile fileToParse) {
322     //    InputStream iStream;
323     LineCreator lineCreator = createLineCreator();
324     try {
325       addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getFullPath().toString(), lineCreator);
326     } catch (CoreException e1) {
327       // TODO Auto-generated catch block
328       e1.printStackTrace();
329     }
330   }
331   /**
332    * @param fileToParse
333    * @param lineCreator
334    * @throws CoreException
335    */
336   public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
337     try {
338       StringBuffer lineBuffer = new StringBuffer();
339       lineBuffer.append(filePath);
340       int lineLength = lineBuffer.length();
341       lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
342           null), lineBuffer);
343 //      if (lineLength != lineBuffer.length()) {
344       // always add the file for Open Include Action  
345       addLine(lineBuffer.toString());
346 //      }
347     }  catch (IOException e) {
348         e.printStackTrace();
349     } finally {
350       try {
351         if (stream != null) {
352           stream.close();
353         }
354       } catch (IOException e) {
355       }
356     }
357   }
358   /**
359    * Adds a line of the index file for function, class, class-method and
360    * class-variable names
361    * 
362    * @param line
363    */
364   private void addLine(String line) {
365     StringTokenizer tokenizer;
366     String phpFileName = null;
367     String token;
368     String identifier = null;
369     String classname = null;
370     String offset = null;
371     PHPIdentifierLocation phpIdentifier = null;
372     boolean tokenExists = false;
373     tokenizer = new StringTokenizer(line, "\t");
374     // first token contains the filename:
375     if (tokenizer.hasMoreTokens()) {
376       phpFileName = tokenizer.nextToken();
377       //System.out.println(token);
378     } else {
379       return;
380     }
381     // all the other tokens are identifiers:
382     while (tokenizer.hasMoreTokens()) {
383       token = tokenizer.nextToken();
384       //System.out.println(token);
385       switch (token.charAt(0)) {
386         case 'c' :
387           // class name
388           identifier = token.substring(1);
389           classname = identifier;
390           phpIdentifier = new PHPIdentifierLocation(identifier,
391               PHPIdentifier.CLASS, phpFileName);
392           break;
393         case 'd' :
394           // define
395           identifier = token.substring(1);
396           phpIdentifier = new PHPIdentifierLocation(identifier,
397               PHPIdentifier.DEFINE, phpFileName);
398           break;
399         case 'f' :
400           // function name
401           identifier = token.substring(1);
402           phpIdentifier = new PHPIdentifierLocation(identifier,
403               PHPIdentifier.FUNCTION, phpFileName);
404           break;
405         case 'k' :
406           // constructor function name
407           identifier = token.substring(1);
408           phpIdentifier = new PHPIdentifierLocation(identifier,
409               PHPIdentifier.CONSTRUCTOR, phpFileName);
410           break;
411         case 'm' :
412           //method inside a class
413           identifier = token.substring(1);
414           phpIdentifier = new PHPIdentifierLocation(identifier,
415               PHPIdentifier.METHOD, phpFileName, classname);
416           break;
417         case 'v' :
418           // variable inside a class
419           identifier = token.substring(1);
420           phpIdentifier = new PHPIdentifierLocation(identifier,
421               PHPIdentifier.VARIABLE, phpFileName, classname);
422           break;
423         case 'o' :
424           // offset information
425           identifier = null;
426           if (phpIdentifier != null) {
427             offset = token.substring(1);
428             phpIdentifier.setOffset(Integer.parseInt(offset));
429           }
430           break;
431         case 'p' :
432           // PHPdoc offset information
433           identifier = null;
434           if (phpIdentifier != null) {
435             offset = token.substring(1);
436             phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
437           }
438           break;
439         case 'l' :
440           // PHPdoc length information
441           identifier = null;
442           if (phpIdentifier != null) {
443             offset = token.substring(1);
444             phpIdentifier.setPHPDocLength(Integer.parseInt(offset));
445           }
446           break;
447         default :
448           identifier = null;
449           phpIdentifier = null;
450           classname = null;
451       }
452       if (identifier != null && phpIdentifier != null) {
453         tokenExists = true;
454         ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
455         if (list == null) {
456           list = new ArrayList();
457           list.add(phpIdentifier);
458           fIndentifierMap.put(identifier, list);
459         } else {
460           boolean flag = false;
461           for (int i = 0; i < list.size(); i++) {
462             if (list.get(i).equals(phpIdentifier)) {
463               flag = true;
464               break;
465             }
466           }
467           if (flag == false) {
468             list.add(phpIdentifier);
469           }
470         }
471       }
472     }
473 //    if (tokenExists) {
474       fFileMap.put(phpFileName, line);
475 //    } 
476   }
477   /**
478    * Change the information for a given IFile resource
479    *  
480    */
481   public void changeFile(IFile fileToParse) {
482     removeFile(fileToParse);
483     addFile(fileToParse);
484   }
485   /**
486    * Get a list of all PHPIdentifierLocation object's associated with an
487    * identifier
488    * 
489    * @param identifier
490    * @return
491    */
492   public List getLocations(String identifier) {
493     return (List) fIndentifierMap.get(identifier);
494   }
495   /**
496    * Initialize (i.e. clear) the current index information
497    *  
498    */
499   public void initialize() {
500     fIndentifierMap = new TreeMap(new StringComparator());
501     fFileMap = new HashMap();
502   }
503   private void readFile() {
504     FileReader fileReader;
505     try {
506       fileReader = new FileReader(fFilename);
507       BufferedReader bufferedReader = new BufferedReader(fileReader);
508       String line;
509       while (bufferedReader.ready()) {
510         // all entries for one file are in a line
511         // separated by tabs !
512         line = bufferedReader.readLine();
513         addLine(line);
514       }
515       fileReader.close();
516     } catch (FileNotFoundException e) {
517       // ignore this
518       // TODO DialogBox which asks the user if she/he likes to build new index?
519     } catch (IOException e) {
520       // TODO Auto-generated catch block
521       e.printStackTrace();
522     }
523   }
524   /**
525    * Remove the information for a given IFile resource
526    *  
527    */
528   public void removeFile(IFile fileToParse) {
529     //    String line = (String)
530     // fFileMap.get(fileToParse.getLocation().toString());
531     String line = (String) fFileMap.get(fileToParse.getFullPath().toString());
532     if (line != null) {
533       removeLine(line);
534     }
535   }
536   /**
537    * Removes a line of the index file for function, class, class-method and
538    * class-variable names
539    * 
540    * @param line
541    */
542   private void removeLine(String line) {
543     StringTokenizer tokenizer;
544     String phpFileName = null;
545     String token;
546     String identifier = null;
547     String classname = null;
548     PHPIdentifier phpIdentifier = null;
549     boolean tokenExists = false;
550     tokenizer = new StringTokenizer(line, "\t");
551     // first token contains the filename:
552     if (tokenizer.hasMoreTokens()) {
553       phpFileName = tokenizer.nextToken();
554       //System.out.println(token);
555     } else {
556       return;
557     }
558     // all the other tokens are identifiers:
559     while (tokenizer.hasMoreTokens()) {
560       token = tokenizer.nextToken();
561       //System.out.println(token);
562       switch (token.charAt(0)) {
563         case 'c' :
564           // class name
565           identifier = token.substring(1);
566           classname = identifier;
567           phpIdentifier = new PHPIdentifierLocation(identifier,
568               PHPIdentifier.CLASS, phpFileName);
569           break;
570         case 'd' :
571           // define
572           identifier = token.substring(1);
573           phpIdentifier = new PHPIdentifierLocation(identifier,
574               PHPIdentifier.DEFINE, phpFileName);
575           break;
576         case 'f' :
577           // function name
578           identifier = token.substring(1);
579           phpIdentifier = new PHPIdentifierLocation(identifier,
580               PHPIdentifier.FUNCTION, phpFileName);
581           break;
582         case 'k' :
583           // constructor function name
584           identifier = token.substring(1);
585           phpIdentifier = new PHPIdentifierLocation(identifier,
586               PHPIdentifier.CONSTRUCTOR, phpFileName);
587           break;
588         case 'm' :
589           //method inside a class
590           identifier = token.substring(1);
591           phpIdentifier = new PHPIdentifierLocation(identifier,
592               PHPIdentifier.METHOD, phpFileName, classname);
593           break;
594         case 'v' :
595           // variable inside a class
596           identifier = token.substring(1);
597           phpIdentifier = new PHPIdentifierLocation(identifier,
598               PHPIdentifier.VARIABLE, phpFileName, classname);
599           break;
600         default :
601           identifier = null;
602           phpIdentifier = null;
603           classname = null;
604       }
605       if (identifier != null && phpIdentifier != null) {
606         ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
607         if (list == null) {
608         } else {
609           for (int i = 0; i < list.size(); i++) {
610             if (list.get(i).equals(phpIdentifier)) {
611               list.remove(i);
612               break;
613             }
614           }
615           if (list.size() == 0) {
616             fIndentifierMap.remove(identifier);
617           }
618         }
619       }
620     }
621     fFileMap.remove(phpFileName);
622   }
623   /**
624    * Save the current index information in the projects index file
625    *  
626    */
627   public void writeFile() {
628     FileWriter fileWriter;
629     try {
630       fileWriter = new FileWriter(fFilename);
631       String line;
632       Collection collection = fFileMap.values();
633       Iterator iterator = collection.iterator();
634       while (iterator.hasNext()) {
635         line = (String) iterator.next();
636         fileWriter.write(line + '\n');
637       }
638       fileWriter.close();
639     } catch (FileNotFoundException e) {
640       // ignore exception; project is deleted by user
641     } catch (IOException e) {
642       // TODO Auto-generated catch block
643       e.printStackTrace();
644     }
645   }
646   /**
647    * @param fromKey
648    * @param toKey
649    * @return
650    */
651   public SortedMap getIdentifierMap() {
652     return fIndentifierMap;
653   }
654
655   synchronized public List getFileList(String filePattern) {
656     Set set = fFileMap.keySet();
657     if (set.isEmpty()) {
658       return null;
659     }
660     Iterator iter = set.iterator();
661     ArrayList list = new ArrayList();
662     String fileName;
663     int index;
664     while(iter.hasNext()) {
665       fileName = (String) iter.next();
666       if ((index=fileName.indexOf(filePattern))!=-1 && fileName.length()==(index+filePattern.length())) {
667         list.add(fileName);
668       }
669     }
670     return list;
671   }
672 }