1 package net.sourceforge.phpeclipse.builder;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedReader;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Comparator;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.SortedMap;
18 import java.util.StringTokenizer;
19 import java.util.TreeMap;
21 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
22 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
23 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
24 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
25 import net.sourceforge.phpdt.internal.compiler.util.Util;
26 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
27 //import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IStatus;
34 * Manages the identifer index information for a specific project
37 public class IdentifierIndexManager {
38 public class LineCreator implements ITerminalSymbols {
39 private Scanner fScanner;
43 public LineCreator() {
44 fScanner = new Scanner(true, false, false, false, true, null, null,
45 true /* taskCaseSensitive */);
49 * Add the information of the current identifier to the line
51 * @param typeOfIdentifier
52 * the type of the identifier ('c'lass, 'd'efine, 'f'unction,
53 * 'm'ethod(class), 'v'ariable(class) 'g'lobal variable)
57 * Buffer for the current index line
59 private void addIdentifierInformation(char typeOfIdentifier,
60 char[] identifier, StringBuffer line) {
62 line.append(typeOfIdentifier);
63 line.append(identifier);
67 * Add the information of the current identifier to the line
69 * @param typeOfIdentifier
70 * the type of the identifier ('c'lass, 'd'efine, 'f'unction,
71 * 'm'ethod(class), 'v'ariable(class) 'g'lobal variable)
75 * Buffer for the current index line
77 * the offset of the PHPdoc comment if available
79 * the length of the PHPdoc comment if available
81 private void addIdentifierInformation(char typeOfIdentifier,
82 char[] identifier, StringBuffer line, int phpdocOffset,
85 line.append(typeOfIdentifier);
86 line.append(identifier);
87 line.append("\to"); // Offset
88 line.append(fScanner.getCurrentTokenStartPosition());
89 if (phpdocOffset >= 0) {
90 line.append("\tp"); // phpdoc offset
91 line.append(phpdocOffset);
92 line.append("\tl"); // phpdoc length
93 line.append(phpdocLength);
97 private void addClassVariableInformation(char typeOfIdentifier,
98 char[] identifier, StringBuffer line, int phpdocOffset,
101 line.append(typeOfIdentifier);
102 line.append(identifier);
103 line.append("\to"); // Offset
104 // we don't store the '$' in the index for class variables:
105 line.append(fScanner.getCurrentTokenStartPosition() + 1);
106 if (phpdocOffset >= 0) {
107 line.append("\tp"); // phpdoc offset
108 line.append(phpdocOffset);
109 line.append("\tl"); // phpdoc length
110 line.append(phpdocLength);
115 * Get the next token from input
117 private void getNextToken() throws InvalidInputException {
119 fToken = fScanner.getNextToken();
121 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
122 int currentStartPosition = fScanner
123 .getCurrentTokenStartPosition();
124 System.out.print(currentStartPosition + ","
125 + currentEndPosition + ": ");
126 System.out.println(fScanner.toStringAction(fToken));
131 private void skipComments()
135 while (fToken == TokenNameCOMMENT_BLOCK || fToken == TokenNameCOMMENT_PHPDOC) {
138 } catch (InvalidInputException e1) {
139 // TODO Auto-generated catch block
140 e1.printStackTrace();
144 private void parseDeclarations(char[] parent, StringBuffer buf,
147 char[] classVariable;
149 boolean hasModifiers = false;
150 int phpdocOffset = -1;
151 int phpdocLength = -1;
153 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
155 hasModifiers = false;
156 if (fToken == TokenNameCOMMENT_PHPDOC) {
157 phpdocOffset = fScanner.getCurrentTokenStartPosition();
158 phpdocLength = fScanner.getCurrentTokenEndPosition()
159 - fScanner.getCurrentTokenStartPosition() + 1;
161 while (fToken == TokenNamestatic
162 || fToken == TokenNamefinal
163 || fToken == TokenNamepublic
164 || fToken == TokenNameprotected
165 || fToken == TokenNameprivate
166 || fToken == TokenNameabstract) {
170 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
174 if (fToken == TokenNamefunction) {
176 if (fToken == TokenNameAND) {
179 if (fToken == TokenNameIdentifier) {
180 ident = fScanner.getCurrentIdentifierSource();
182 && equalCharArrays(parent, ident)) {
183 // constructor function
184 addIdentifierInformation('k', ident, buf,
185 phpdocOffset, phpdocLength);
187 if (parent != null) {
188 // class method function
189 addIdentifierInformation('m', ident, buf,
190 phpdocOffset, phpdocLength);
192 // nested function ?!
193 addIdentifierInformation('f', ident, buf,
194 phpdocOffset, phpdocLength);
198 parseDeclarations(null, buf, true);
200 } else if (fToken == TokenNameclass
201 || fToken == TokenNameinterface) {
203 if (fToken == TokenNameIdentifier) {
204 ident = fScanner.getCurrentIdentifierSource();
205 addIdentifierInformation('c', ident, buf,
206 phpdocOffset, phpdocLength);
208 if (fToken == TokenNameextends) {
210 while (fToken == TokenNameIdentifier) {
212 .getCurrentIdentifierSource();
214 addIdentifierInformation('e', ident, buf);
216 if (fToken == TokenNameCOMMA) {
221 if (fToken == TokenNameimplements) {
223 while (fToken == TokenNameIdentifier) {
225 .getCurrentIdentifierSource();
227 addIdentifierInformation('e', ident, buf);
229 if (fToken == TokenNameCOMMA) {
235 // skip tokens for classname, extends and others
238 while (fToken != TokenNameLBRACE
239 && fToken != TokenNameEOF
240 && fToken != TokenNameERROR) {
243 parseDeclarations(ident, buf, true);
245 } else if (fToken == TokenNamevar || hasModifiers
246 || fToken == TokenNamestatic
247 || fToken == TokenNamefinal
248 || fToken == TokenNamepublic
249 || fToken == TokenNameprotected
250 || fToken == TokenNameprivate) {
251 while (fToken == TokenNamevar
252 || fToken == TokenNamestatic
253 || fToken == TokenNamefinal
254 || fToken == TokenNamepublic
255 || fToken == TokenNameprotected
256 || fToken == TokenNameprivate) {
259 while (fToken == TokenNameVariable) {
260 ident = fScanner.getCurrentIdentifierSource();
261 classVariable = new char[ident.length - 1];
262 System.arraycopy(ident, 1, classVariable, 0,
264 addClassVariableInformation('v', classVariable,
265 buf, phpdocOffset, phpdocLength);
267 if (fToken == TokenNameCOMMA) {
271 } else if (!hasModifiers && fToken == TokenNameIdentifier) {
272 ident = fScanner.getCurrentIdentifierSource();
274 if (ident.length == 6 && ident[0] == 'd'
275 && ident[1] == 'e' && ident[2] == 'f'
276 && ident[3] == 'i' && ident[4] == 'n'
277 && ident[5] == 'e') {
278 if (fToken == TokenNameLPAREN) {
280 if (fToken == TokenNameStringDoubleQuote) {
282 .getCurrentStringLiteralSource();
283 addIdentifierInformation('d', ident, buf,
284 phpdocOffset, phpdocLength);
286 } else if (fToken == TokenNameStringSingleQuote) {
288 .getCurrentStringLiteralSource();
289 addIdentifierInformation('d', ident, buf,
290 phpdocOffset, phpdocLength);
295 } else if (fToken == TokenNameglobal) {
297 while (fToken != TokenNameEOF
298 && fToken != TokenNameERROR
299 && fToken != TokenNameSEMICOLON
300 && fToken != TokenNameLBRACE
301 && fToken != TokenNameRBRACE) {
303 if (fToken == TokenNameVariable) {
304 ident = fScanner.getCurrentIdentifierSource();
305 addIdentifierInformation('g', ident, buf,
306 phpdocOffset, phpdocLength);
309 } else if (fToken == TokenNameLBRACE) {
312 } else if (fToken == TokenNameRBRACE) {
315 if (counter == 0 && goBack) {
322 } catch (InvalidInputException e) {
324 } catch (SyntaxError e) {
325 // TODO Auto-generated catch block
330 synchronized public void parseIdentifiers(char[] charArray,
334 boolean hasModifiers = false;
335 int phpdocOffset = -1;
336 int phpdocLength = -1;
337 fScanner.setSource(charArray);
338 fScanner.setPHPMode(false);
339 fToken = TokenNameEOF;
342 while (fToken != TokenNameEOF) { // && fToken !=
345 hasModifiers = false;
347 case TokenNameCOMMENT_PHPDOC:
348 phpdocOffset = fScanner.getCurrentTokenStartPosition();
349 phpdocLength = fScanner.getCurrentTokenEndPosition()
350 - fScanner.getCurrentTokenStartPosition() + 1;
352 while (fToken == TokenNamestatic
353 || fToken == TokenNamefinal
354 || fToken == TokenNamepublic
355 || fToken == TokenNameprotected
356 || fToken == TokenNameprivate
357 || fToken == TokenNameabstract) {
361 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
366 case TokenNamefunction:
368 if (fToken == TokenNameAND) {
371 if (fToken == TokenNameIdentifier) {
372 ident = fScanner.getCurrentIdentifierSource();
373 addIdentifierInformation('f', ident, buf,
374 phpdocOffset, phpdocLength);
376 if (fToken == TokenNameLPAREN) {
379 if (fToken == TokenNameVariable) {
380 ident = fScanner.getCurrentIdentifierSource();
381 addIdentifierInformation('v', ident, buf,
382 phpdocOffset, phpdocLength);
384 if (fToken == TokenNameCOMMA) {
388 } while (fToken != TokenNameRPAREN );
390 parseDeclarations(null, buf, true);
395 case TokenNameinterface:
397 if (fToken == TokenNameIdentifier) {
398 ident = fScanner.getCurrentIdentifierSource();
399 addIdentifierInformation('c', ident, buf,
400 phpdocOffset, phpdocLength);
402 if (fToken == TokenNameextends) {
404 while (fToken == TokenNameIdentifier) {
406 .getCurrentIdentifierSource();
408 addIdentifierInformation('e', ident, buf);
410 if (fToken == TokenNameCOMMA) {
415 if (fToken == TokenNameimplements) {
417 while (fToken == TokenNameIdentifier) {
419 .getCurrentIdentifierSource();
421 addIdentifierInformation('e', ident, buf);
423 if (fToken == TokenNameCOMMA) {
428 // skip fTokens for classname, extends and others
431 while (fToken != TokenNameLBRACE
432 && fToken != TokenNameEOF
433 && fToken != TokenNameERROR) {
436 parseDeclarations(ident, buf, true);
440 case TokenNameVariable:
442 ident = fScanner.getCurrentIdentifierSource();
443 addIdentifierInformation('g', ident, buf, phpdocOffset,
453 } catch (InvalidInputException e) {
455 } catch (SyntaxError e) {
456 // TODO Auto-generated catch block
462 class StringComparator implements Comparator {
463 public int compare(Object o1, Object o2) {
464 String s1 = (String) o1;
465 String s2 = (String) o2;
466 return s1.compareTo(s2);
467 // return s1.toUpperCase().compareTo(s2.toUpperCase());
470 public boolean equals(Object o) {
471 //String s = (String) o;
472 return compare(this, o) == 0;
476 private HashMap fFileMap;
478 private String fFilename;
480 private TreeMap fIndentifierMap;
482 public IdentifierIndexManager(String filename) {
483 fFilename = filename;
489 * Check if 2 char arrays are equal
495 private static boolean equalCharArrays(char[] a, char[] b) {
496 if (a.length != b.length) {
499 for (int i = 0; i < b.length; i++) {
507 public LineCreator createLineCreator() {
508 return new LineCreator();
512 * Add the information for a given IFile resource
515 public void addFile(IFile fileToParse) {
516 LineCreator lineCreator = createLineCreator();
518 addInputStream(new BufferedInputStream(fileToParse.getContents()),
519 fileToParse.getProjectRelativePath().toString(),
520 lineCreator, fileToParse.getCharset());
521 } catch (CoreException e1) {
522 // TODO Auto-generated catch block
523 e1.printStackTrace();
530 * @throws CoreException
532 public void addInputStream(InputStream stream, String filePath,
533 LineCreator lineCreator, String charset) throws CoreException {
535 StringBuffer lineBuffer = new StringBuffer();
536 lineBuffer.append(filePath);
537 lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream,
538 -1, charset), lineBuffer);
539 addLine(lineBuffer.toString());
540 } catch (IOException e) {
544 if (stream != null) {
547 } catch (IOException e) {
554 * Adds a line of the index file for function, class, class-method and
555 * class-variable names
559 private void addLine(String line) {
560 addLine(fIndentifierMap, fFileMap, line, null);
563 public TreeMap getIdentifiers(IFile file) {
564 TreeMap treeMap = new TreeMap(new StringComparator());
565 addIdentifiers(treeMap, file);
569 public TreeMap getIdentifiers(String startClazz) {
570 TreeMap treeMap = new TreeMap(new StringComparator());
571 addIdentifiers(treeMap, startClazz);
575 public void addIdentifiers(TreeMap treeMap, IFile file) {
576 String line = (String) fFileMap.get(file.getProjectRelativePath()
579 //PHPIdentifierLocation ident;
580 ArrayList allClassNames = new ArrayList();
581 addLine(treeMap, null, line, allClassNames);
583 while (i < allClassNames.size()) {
584 String clazz = (String) allClassNames.get(i++);
585 addClassName(treeMap, clazz, allClassNames);
590 public void addIdentifiers(TreeMap treeMap, String startClazz) {
591 //PHPIdentifierLocation ident;
592 ArrayList allClassNames = new ArrayList();
593 addClassName(treeMap, startClazz, allClassNames);
595 while (i < allClassNames.size()) {
596 String clazz = (String) allClassNames.get(i++);
597 addClassName(treeMap, clazz, allClassNames);
604 * @param allClassNames
606 private boolean addClassName(TreeMap treeMap, String clazz,
607 List allClassNames) {
609 PHPIdentifierLocation ident;
610 List list = getLocations(clazz);
614 boolean result = false;
615 for (int i = 0; i < list.size(); i++) {
616 ident = (PHPIdentifierLocation) list.get(i);
617 if (ident.isClass()) {
618 line = (String) fFileMap.get(ident.getFilename());
619 addLine(treeMap, null, line, allClassNames);
627 * Adds a line of the index file for function, class, class-method and
628 * class-variable names
632 public void addLine(TreeMap treeMap, HashMap fileMap, String line,
633 List allClassNames) {
634 StringTokenizer tokenizer;
635 String phpFileName = null;
637 String identifier = null;
638 String classname = null;
639 String offset = null;
640 PHPIdentifierLocation phpIdentifier = null;
641 boolean tokenExists = false;
642 tokenizer = new StringTokenizer(line, "\t");
643 // first token contains the filename:
645 if (tokenizer.hasMoreTokens()) {
646 phpFileName = tokenizer.nextToken();
647 // System.out.println(token);
651 // all the other tokens are identifiers:
652 while (tokenizer.hasMoreTokens()) {
653 token = tokenizer.nextToken();
654 // System.out.println(token);
655 switch (token.charAt(0)) {
658 identifier = token.substring(1);
659 classname = identifier;
660 phpIdentifier = new PHPIdentifierLocation(identifier,
661 PHPIdentifier.CLASS, phpFileName);
665 identifier = token.substring(1);
666 phpIdentifier = new PHPIdentifierLocation(identifier,
667 PHPIdentifier.DEFINE, phpFileName);
670 // extends <class name>
673 phpIdentifier = null;
674 if (allClassNames != null) {
675 String extName = token.substring(1);
676 if (!allClassNames.contains(extName)) {
677 allClassNames.add(extName);
683 identifier = token.substring(1);
684 phpIdentifier = new PHPIdentifierLocation(identifier,
685 PHPIdentifier.FUNCTION, phpFileName);
689 identifier = token.substring(1);
690 phpIdentifier = new PHPIdentifierLocation(identifier,
691 PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
694 // implements <class name>
697 phpIdentifier = null;
698 if (allClassNames != null) {
699 String implName = token.substring(1);
700 if (!allClassNames.contains(implName)) {
701 allClassNames.add(implName);
706 // constructor function name
707 identifier = token.substring(1);
708 phpIdentifier = new PHPIdentifierLocation(identifier,
709 PHPIdentifier.CONSTRUCTOR, phpFileName);
712 // method inside a class
713 identifier = token.substring(1);
714 phpIdentifier = new PHPIdentifierLocation(identifier,
715 PHPIdentifier.METHOD, phpFileName, classname);
718 // variable inside a class
719 identifier = token.substring(1);
720 phpIdentifier = new PHPIdentifierLocation(identifier,
721 PHPIdentifier.VARIABLE, phpFileName, classname);
724 // offset information
726 if (phpIdentifier != null) {
727 offset = token.substring(1);
728 phpIdentifier.setOffset(Integer.parseInt(offset));
732 // PHPdoc offset information
734 if (phpIdentifier != null) {
735 offset = token.substring(1);
736 phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
740 // PHPdoc length information
742 if (phpIdentifier != null) {
743 offset = token.substring(1);
744 phpIdentifier.setPHPDocLength(Integer.parseInt(offset));
748 PHPeclipsePlugin.log(IStatus.ERROR,
749 "Unknown token character in IdentifierIndexManager: "
752 phpIdentifier = null;
755 if (identifier != null && phpIdentifier != null) {
757 ArrayList list = (ArrayList) treeMap.get(identifier);
759 list = new ArrayList();
760 list.add(phpIdentifier);
761 treeMap.put(identifier, list);
763 boolean flag = false;
764 for (int i = 0; i < list.size(); i++) {
765 if (list.get(i).equals(phpIdentifier)) {
771 list.add(phpIdentifier);
776 if (fileMap != null) {
777 fileMap.put(phpFileName, line);
779 } catch (Throwable e) {
780 // write to workspace/.metadata/.log file
781 PHPeclipsePlugin.log(e);
783 // if (tokenExists) {
789 * Change the information for a given IFile resource
792 public void changeFile(IFile fileToParse) {
793 removeFile(fileToParse);
794 addFile(fileToParse);
798 * Get a list of all PHPIdentifierLocation object's associated with an
804 public List getLocations(String identifier) {
805 List list = (List) fIndentifierMap.get(identifier);
809 return new ArrayList();
813 * Initialize (i.e. clear) the current index information
816 public void initialize() {
817 fIndentifierMap = new TreeMap(new StringComparator());
818 fFileMap = new HashMap();
821 private void readFile() {
822 FileReader fileReader;
824 fileReader = new FileReader(fFilename);
825 BufferedReader bufferedReader = new BufferedReader(fileReader);
827 while (bufferedReader.ready()) {
828 // all entries for one file are in a line
829 // separated by tabs !
830 line = bufferedReader.readLine();
834 } catch (FileNotFoundException e) {
836 // TODO DialogBox which asks the user if she/he likes to build new
838 } catch (IOException e) {
839 // TODO Auto-generated catch block
845 * Remove the information for a given IFile resource
848 public void removeFile(IFile fileToParse) {
849 // String line = (String)
850 // fFileMap.get(fileToParse.getLocation().toString());
851 String line = (String) fFileMap.get(fileToParse
852 .getProjectRelativePath().toString());
859 * Removes a line of the index file for function, class, class-method and
860 * class-variable names
864 private void removeLine(String line) {
865 StringTokenizer tokenizer;
866 String phpFileName = null;
868 String identifier = null;
869 String classname = null;
870 PHPIdentifier phpIdentifier = null;
871 boolean tokenExists = false;
872 tokenizer = new StringTokenizer(line, "\t");
873 // first token contains the filename:
874 if (tokenizer.hasMoreTokens()) {
875 phpFileName = tokenizer.nextToken();
876 // System.out.println(token);
881 // all the other tokens are identifiers:
882 while (tokenizer.hasMoreTokens()) {
883 token = tokenizer.nextToken();
884 // System.out.println(token);
885 switch (token.charAt(0)) {
888 identifier = token.substring(1);
889 classname = identifier;
890 phpIdentifier = new PHPIdentifierLocation(identifier,
891 PHPIdentifier.CLASS, phpFileName);
895 identifier = token.substring(1);
896 phpIdentifier = new PHPIdentifierLocation(identifier,
897 PHPIdentifier.DEFINE, phpFileName);
900 // extends <class name>
902 phpIdentifier = null;
906 identifier = token.substring(1);
907 phpIdentifier = new PHPIdentifierLocation(identifier,
908 PHPIdentifier.FUNCTION, phpFileName);
912 identifier = token.substring(1);
913 phpIdentifier = new PHPIdentifierLocation(identifier,
914 PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
917 // implements <class name>
919 phpIdentifier = null;
922 // constructor function name
923 identifier = token.substring(1);
924 phpIdentifier = new PHPIdentifierLocation(identifier,
925 PHPIdentifier.CONSTRUCTOR, phpFileName);
928 // method inside a class
929 identifier = token.substring(1);
930 phpIdentifier = new PHPIdentifierLocation(identifier,
931 PHPIdentifier.METHOD, phpFileName, classname);
934 // offset information
938 // PHPdoc offset information
942 // PHPdoc length information
946 // variable inside a class
947 identifier = token.substring(1);
948 phpIdentifier = new PHPIdentifierLocation(identifier,
949 PHPIdentifier.VARIABLE, phpFileName, classname);
952 PHPeclipsePlugin.log(IStatus.ERROR,
953 "Unknown token character in IdentifierIndexManager: "
956 phpIdentifier = null;
959 if (identifier != null && phpIdentifier != null) {
960 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
963 for (int i = 0; i < list.size(); i++) {
964 if (list.get(i).equals(phpIdentifier)) {
969 if (list.size() == 0) {
970 fIndentifierMap.remove(identifier);
975 fFileMap.remove(phpFileName);
979 * Save the current index information in the projects index file
982 public void writeFile() {
983 FileWriter fileWriter;
985 fileWriter = new FileWriter(fFilename);
987 Collection collection = fFileMap.values();
988 Iterator iterator = collection.iterator();
989 while (iterator.hasNext()) {
990 line = (String) iterator.next();
991 fileWriter.write(line + '\n');
994 } catch (FileNotFoundException e) {
995 // ignore exception; project is deleted by user
996 } catch (IOException e) {
997 // TODO Auto-generated catch block
1007 public SortedMap getIdentifierMap() {
1008 return fIndentifierMap;
1011 synchronized public List getFileList(String filePattern) {
1012 Set set = fFileMap.keySet();
1013 if (set.isEmpty()) {
1016 Iterator iter = set.iterator();
1017 ArrayList list = new ArrayList();
1020 while (iter.hasNext()) {
1021 fileName = (String) iter.next();
1022 if ((index = fileName.indexOf(filePattern)) != -1
1023 && fileName.length() == (index + filePattern.length())) {