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);
48 * Add the information of the current identifier to the line
50 * @param typeOfIdentifier
51 * the type of the identifier ('c'lass, 'd'efine, 'f'unction, 'm'ethod(class), 'v'ariable(class) 'g'lobal variable)
55 * Buffer for the current index line
57 * the offset of the PHPdoc comment if available
59 * the length of the PHPdoc comment if available
61 private void addIdentifierInformation(char typeOfIdentifier, char[] identifier, StringBuffer line, int phpdocOffset,
64 line.append(typeOfIdentifier);
65 line.append(identifier);
66 line.append("\to"); // Offset
67 line.append(fScanner.getCurrentTokenStartPosition());
68 if (phpdocOffset >= 0) {
69 line.append("\tp"); // phpdoc offset
70 line.append(phpdocOffset);
71 line.append("\tl"); // phpdoc length
72 line.append(phpdocLength);
76 private void addClassVariableInformation(char typeOfIdentifier, char[] identifier, StringBuffer line, int phpdocOffset,
79 line.append(typeOfIdentifier);
80 line.append(identifier);
81 line.append("\to"); // Offset
82 // we don't store the '$' in the index for class variables:
83 line.append(fScanner.getCurrentTokenStartPosition() + 1);
84 if (phpdocOffset >= 0) {
85 line.append("\tp"); // phpdoc offset
86 line.append(phpdocOffset);
87 line.append("\tl"); // phpdoc length
88 line.append(phpdocLength);
93 * Get the next token from input
95 private void getNextToken() {
97 fToken = fScanner.getNextToken();
99 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
100 int currentStartPosition = fScanner.getCurrentTokenStartPosition();
101 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
102 System.out.println(fScanner.toStringAction(fToken));
105 } catch (InvalidInputException e) {
107 // e.printStackTrace();
109 fToken = TokenNameERROR;
112 private void parseDeclarations(char[] parent, StringBuffer buf, boolean goBack) {
114 char[] classVariable;
116 boolean hasModifiers = false;
117 int phpdocOffset = -1;
118 int phpdocLength = -1;
120 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
122 hasModifiers = false;
123 if (fToken == TokenNameCOMMENT_PHPDOC) {
124 phpdocOffset = fScanner.getCurrentTokenStartPosition();
125 phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
127 while (fToken == TokenNamestatic || fToken == TokenNamefinal || fToken == TokenNamepublic
128 || fToken == TokenNameprotected || fToken == TokenNameprivate || fToken == TokenNameabstract) {
132 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
136 if (fToken == TokenNamefunction) {
138 if (fToken == TokenNameAND) {
141 if (fToken == TokenNameIdentifier) {
142 ident = fScanner.getCurrentIdentifierSource();
143 if (parent != null && equalCharArrays(parent, ident)) {
144 // constructor function
145 addIdentifierInformation('k', ident, buf, phpdocOffset, phpdocLength);
147 if (parent != null) {
148 // class method function
149 addIdentifierInformation('m', ident, buf, phpdocOffset, phpdocLength);
151 // nested function ?!
152 addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
156 parseDeclarations(null, buf, true);
158 } else if (fToken == TokenNameclass || fToken == TokenNameinterface) {
160 if (fToken == TokenNameIdentifier) {
161 ident = fScanner.getCurrentIdentifierSource();
162 addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
164 //skip tokens for classname, extends and others until we have
166 while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
169 parseDeclarations(ident, buf, true);
171 } else if (fToken == TokenNamevar || hasModifiers || fToken == TokenNamestatic || fToken == TokenNamefinal
172 || fToken == TokenNamepublic || fToken == TokenNameprotected || fToken == TokenNameprivate) {
173 while (fToken == TokenNamevar || fToken == TokenNamestatic || fToken == TokenNamefinal || fToken == TokenNamepublic
174 || fToken == TokenNameprotected || fToken == TokenNameprivate) {
177 while (fToken == TokenNameVariable) {
178 ident = fScanner.getCurrentIdentifierSource();
179 classVariable = new char[ident.length - 1];
180 System.arraycopy(ident, 1, classVariable, 0, ident.length - 1);
181 addClassVariableInformation('v', classVariable, buf, phpdocOffset, phpdocLength);
183 if (fToken == TokenNameCOMMA) {
187 } else if (!hasModifiers && fToken == TokenNameIdentifier) {
188 ident = fScanner.getCurrentIdentifierSource();
190 if (ident.length == 6 && ident[0] == 'd' && ident[1] == 'e' && ident[2] == 'f' && ident[3] == 'i' && ident[4] == 'n'
191 && ident[5] == 'e') {
192 if (fToken == TokenNameLPAREN) {
194 if (fToken == TokenNameStringDoubleQuote) {
195 ident = fScanner.getCurrentStringLiteralSource();
196 addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
201 } else if (fToken == TokenNameglobal) {
203 while (fToken != TokenNameEOF && fToken != TokenNameERROR && fToken != TokenNameSEMICOLON && fToken != TokenNameLBRACE
204 && fToken != TokenNameRBRACE) {
206 if (fToken == TokenNameVariable) {
207 ident = fScanner.getCurrentIdentifierSource();
208 addIdentifierInformation('g', ident, buf, phpdocOffset, phpdocLength);
211 } else if (fToken == TokenNameLBRACE) {
214 } else if (fToken == TokenNameRBRACE) {
217 if (counter == 0 && goBack) {
224 } catch (SyntaxError e) {
225 // TODO Auto-generated catch block
230 synchronized public void parseIdentifiers(char[] charArray, StringBuffer buf) {
234 boolean hasModifiers = false;
235 int phpdocOffset = -1;
236 int phpdocLength = -1;
237 fScanner.setSource(charArray);
238 fScanner.setPHPMode(false);
239 fToken = TokenNameEOF;
242 while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
244 hasModifiers = false;
245 if (fToken == TokenNameCOMMENT_PHPDOC) {
246 phpdocOffset = fScanner.getCurrentTokenStartPosition();
247 phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
249 while (fToken == TokenNamestatic || fToken == TokenNamefinal || fToken == TokenNamepublic
250 || fToken == TokenNameprotected || fToken == TokenNameprivate || fToken == TokenNameabstract) {
254 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
258 if (fToken == TokenNamefunction) {
260 if (fToken == TokenNameAND) {
263 if (fToken == TokenNameIdentifier) {
264 ident = fScanner.getCurrentIdentifierSource();
265 addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
267 parseDeclarations(null, buf, true);
269 } else if (fToken == TokenNameclass || fToken == TokenNameinterface) {
271 if (fToken == TokenNameIdentifier) {
272 ident = fScanner.getCurrentIdentifierSource();
273 addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
275 //skip fTokens for classname, extends and others until we have
277 while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
280 parseDeclarations(ident, buf, true);
282 } else if (fToken == TokenNameVariable) {
284 ident = fScanner.getCurrentIdentifierSource();
285 addIdentifierInformation('g', ident, buf, phpdocOffset, phpdocLength);
287 } else if (!hasModifiers && fToken == TokenNameIdentifier) {
288 ident = fScanner.getCurrentIdentifierSource();
290 if (ident.length == 6 && ident[0] == 'd' && ident[1] == 'e' && ident[2] == 'f' && ident[3] == 'i' && ident[4] == 'n'
291 && ident[5] == 'e') {
292 if (fToken == TokenNameLPAREN) {
294 if (fToken == TokenNameStringDoubleQuote) {
295 ident = fScanner.getCurrentStringLiteralSource();
296 addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
305 } catch (SyntaxError e) {
306 // TODO Auto-generated catch block
312 class StringComparator implements Comparator {
313 public int compare(Object o1, Object o2) {
314 String s1 = (String) o1;
315 String s2 = (String) o2;
316 return s1.compareTo(s2);
317 // return s1.toUpperCase().compareTo(s2.toUpperCase());
320 public boolean equals(Object o) {
321 String s = (String) o;
322 return compare(this, o) == 0;
326 private HashMap fFileMap;
328 private String fFilename;
330 private TreeMap fIndentifierMap;
332 public IdentifierIndexManager(String filename) {
333 fFilename = filename;
339 * Check if 2 char arrays are equal
345 private static boolean equalCharArrays(char[] a, char[] b) {
346 if (a.length != b.length) {
349 for (int i = 0; i < b.length; i++) {
357 public LineCreator createLineCreator() {
358 return new LineCreator();
362 * Add the information for a given IFile resource
365 public void addFile(IFile fileToParse) {
366 // InputStream iStream;
367 LineCreator lineCreator = createLineCreator();
369 addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getProjectRelativePath().toString(),
371 } catch (CoreException e1) {
372 // TODO Auto-generated catch block
373 e1.printStackTrace();
380 * @throws CoreException
382 public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
384 StringBuffer lineBuffer = new StringBuffer();
385 lineBuffer.append(filePath);
386 int lineLength = lineBuffer.length();
387 lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1, null), lineBuffer);
388 // if (lineLength != lineBuffer.length()) {
389 // always add the file for Open Include Action
390 addLine(lineBuffer.toString());
392 } catch (IOException e) {
396 if (stream != null) {
399 } catch (IOException e) {
405 * Adds a line of the index file for function, class, class-method and class-variable names
409 private void addLine(String line) {
410 StringTokenizer tokenizer;
411 String phpFileName = null;
413 String identifier = null;
414 String classname = null;
415 String offset = null;
416 PHPIdentifierLocation phpIdentifier = null;
417 boolean tokenExists = false;
418 tokenizer = new StringTokenizer(line, "\t");
419 // first token contains the filename:
420 if (tokenizer.hasMoreTokens()) {
421 phpFileName = tokenizer.nextToken();
422 //System.out.println(token);
426 // all the other tokens are identifiers:
427 while (tokenizer.hasMoreTokens()) {
428 token = tokenizer.nextToken();
429 //System.out.println(token);
430 switch (token.charAt(0)) {
433 identifier = token.substring(1);
434 classname = identifier;
435 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
439 identifier = token.substring(1);
440 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
444 identifier = token.substring(1);
445 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
449 identifier = token.substring(1);
450 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
453 // constructor function name
454 identifier = token.substring(1);
455 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
458 //method inside a class
459 identifier = token.substring(1);
460 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
463 // variable inside a class
464 identifier = token.substring(1);
465 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
468 // offset information
470 if (phpIdentifier != null) {
471 offset = token.substring(1);
472 phpIdentifier.setOffset(Integer.parseInt(offset));
476 // PHPdoc offset information
478 if (phpIdentifier != null) {
479 offset = token.substring(1);
480 phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
484 // PHPdoc length information
486 if (phpIdentifier != null) {
487 offset = token.substring(1);
488 phpIdentifier.setPHPDocLength(Integer.parseInt(offset));
492 PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: " + token.charAt(0));
494 phpIdentifier = null;
497 if (identifier != null && phpIdentifier != null) {
499 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
501 list = new ArrayList();
502 list.add(phpIdentifier);
503 fIndentifierMap.put(identifier, list);
505 boolean flag = false;
506 for (int i = 0; i < list.size(); i++) {
507 if (list.get(i).equals(phpIdentifier)) {
513 list.add(phpIdentifier);
518 // if (tokenExists) {
519 fFileMap.put(phpFileName, line);
524 * Change the information for a given IFile resource
527 public void changeFile(IFile fileToParse) {
528 removeFile(fileToParse);
529 addFile(fileToParse);
533 * Get a list of all PHPIdentifierLocation object's associated with an identifier
538 public List getLocations(String identifier) {
539 return (List) fIndentifierMap.get(identifier);
543 * Initialize (i.e. clear) the current index information
546 public void initialize() {
547 fIndentifierMap = new TreeMap(new StringComparator());
548 fFileMap = new HashMap();
551 private void readFile() {
552 FileReader fileReader;
554 fileReader = new FileReader(fFilename);
555 BufferedReader bufferedReader = new BufferedReader(fileReader);
557 while (bufferedReader.ready()) {
558 // all entries for one file are in a line
559 // separated by tabs !
560 line = bufferedReader.readLine();
564 } catch (FileNotFoundException e) {
566 // TODO DialogBox which asks the user if she/he likes to build new index?
567 } catch (IOException e) {
568 // TODO Auto-generated catch block
574 * Remove the information for a given IFile resource
577 public void removeFile(IFile fileToParse) {
578 // String line = (String)
579 // fFileMap.get(fileToParse.getLocation().toString());
580 String line = (String) fFileMap.get(fileToParse.getProjectRelativePath().toString());
587 * Removes a line of the index file for function, class, class-method and class-variable names
591 private void removeLine(String line) {
592 StringTokenizer tokenizer;
593 String phpFileName = null;
595 String identifier = null;
596 String classname = null;
597 PHPIdentifier phpIdentifier = null;
598 boolean tokenExists = false;
599 tokenizer = new StringTokenizer(line, "\t");
600 // first token contains the filename:
601 if (tokenizer.hasMoreTokens()) {
602 phpFileName = tokenizer.nextToken();
603 //System.out.println(token);
608 // all the other tokens are identifiers:
609 while (tokenizer.hasMoreTokens()) {
610 token = tokenizer.nextToken();
611 //System.out.println(token);
612 switch (token.charAt(0)) {
615 identifier = token.substring(1);
616 classname = identifier;
617 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
621 identifier = token.substring(1);
622 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
626 identifier = token.substring(1);
627 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
631 identifier = token.substring(1);
632 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
635 // constructor function name
636 identifier = token.substring(1);
637 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
640 //method inside a class
641 identifier = token.substring(1);
642 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
645 // offset information
649 // PHPdoc offset information
653 // PHPdoc length information
657 // variable inside a class
658 identifier = token.substring(1);
659 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
662 PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: " + token.charAt(0));
664 phpIdentifier = null;
667 if (identifier != null && phpIdentifier != null) {
668 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
671 for (int i = 0; i < list.size(); i++) {
672 if (list.get(i).equals(phpIdentifier)) {
677 if (list.size() == 0) {
678 fIndentifierMap.remove(identifier);
683 fFileMap.remove(phpFileName);
687 * Save the current index information in the projects index file
690 public void writeFile() {
691 FileWriter fileWriter;
693 fileWriter = new FileWriter(fFilename);
695 Collection collection = fFileMap.values();
696 Iterator iterator = collection.iterator();
697 while (iterator.hasNext()) {
698 line = (String) iterator.next();
699 fileWriter.write(line + '\n');
702 } catch (FileNotFoundException e) {
703 // ignore exception; project is deleted by user
704 } catch (IOException e) {
705 // TODO Auto-generated catch block
715 public SortedMap getIdentifierMap() {
716 return fIndentifierMap;
719 synchronized public List getFileList(String filePattern) {
720 Set set = fFileMap.keySet();
724 Iterator iter = set.iterator();
725 ArrayList list = new ArrayList();
728 while (iter.hasNext()) {
729 fileName = (String) iter.next();
730 if ((index = fileName.indexOf(filePattern)) != -1 && fileName.length() == (index + filePattern.length())) {