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);
198 } else if (fToken == TokenNameStringSingleQuote) {
199 ident = fScanner.getCurrentStringLiteralSource();
200 addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
205 } else if (fToken == TokenNameglobal) {
207 while (fToken != TokenNameEOF && fToken != TokenNameERROR && fToken != TokenNameSEMICOLON && fToken != TokenNameLBRACE
208 && fToken != TokenNameRBRACE) {
210 if (fToken == TokenNameVariable) {
211 ident = fScanner.getCurrentIdentifierSource();
212 addIdentifierInformation('g', ident, buf, phpdocOffset, phpdocLength);
215 } else if (fToken == TokenNameLBRACE) {
218 } else if (fToken == TokenNameRBRACE) {
221 if (counter == 0 && goBack) {
228 } catch (SyntaxError e) {
229 // TODO Auto-generated catch block
234 synchronized public void parseIdentifiers(char[] charArray, StringBuffer buf) {
238 boolean hasModifiers = false;
239 int phpdocOffset = -1;
240 int phpdocLength = -1;
241 fScanner.setSource(charArray);
242 fScanner.setPHPMode(false);
243 fToken = TokenNameEOF;
246 while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
248 hasModifiers = false;
249 if (fToken == TokenNameCOMMENT_PHPDOC) {
250 phpdocOffset = fScanner.getCurrentTokenStartPosition();
251 phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
253 while (fToken == TokenNamestatic || fToken == TokenNamefinal || fToken == TokenNamepublic
254 || fToken == TokenNameprotected || fToken == TokenNameprivate || fToken == TokenNameabstract) {
258 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
262 if (fToken == TokenNamefunction) {
264 if (fToken == TokenNameAND) {
267 if (fToken == TokenNameIdentifier) {
268 ident = fScanner.getCurrentIdentifierSource();
269 addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
271 parseDeclarations(null, buf, true);
273 } else if (fToken == TokenNameclass || fToken == TokenNameinterface) {
275 if (fToken == TokenNameIdentifier) {
276 ident = fScanner.getCurrentIdentifierSource();
277 addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
279 //skip fTokens for classname, extends and others until we have
281 while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
284 parseDeclarations(ident, buf, true);
286 } else if (fToken == TokenNameVariable) {
288 ident = fScanner.getCurrentIdentifierSource();
289 addIdentifierInformation('g', ident, buf, phpdocOffset, phpdocLength);
291 } else if (!hasModifiers && fToken == TokenNameIdentifier) {
292 ident = fScanner.getCurrentIdentifierSource();
294 if (ident.length == 6 && ident[0] == 'd' && ident[1] == 'e' && ident[2] == 'f' && ident[3] == 'i' && ident[4] == 'n'
295 && ident[5] == 'e') {
296 if (fToken == TokenNameLPAREN) {
298 if (fToken == TokenNameStringDoubleQuote) {
299 ident = fScanner.getCurrentStringLiteralSource();
300 addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
302 } else if (fToken == TokenNameStringSingleQuote) {
303 ident = fScanner.getCurrentStringLiteralSource();
304 addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
313 } catch (SyntaxError e) {
314 // TODO Auto-generated catch block
320 class StringComparator implements Comparator {
321 public int compare(Object o1, Object o2) {
322 String s1 = (String) o1;
323 String s2 = (String) o2;
324 return s1.compareTo(s2);
325 // return s1.toUpperCase().compareTo(s2.toUpperCase());
328 public boolean equals(Object o) {
329 String s = (String) o;
330 return compare(this, o) == 0;
334 private HashMap fFileMap;
336 private String fFilename;
338 private TreeMap fIndentifierMap;
340 public IdentifierIndexManager(String filename) {
341 fFilename = filename;
347 * Check if 2 char arrays are equal
353 private static boolean equalCharArrays(char[] a, char[] b) {
354 if (a.length != b.length) {
357 for (int i = 0; i < b.length; i++) {
365 public LineCreator createLineCreator() {
366 return new LineCreator();
370 * Add the information for a given IFile resource
373 public void addFile(IFile fileToParse) {
374 // InputStream iStream;
375 LineCreator lineCreator = createLineCreator();
377 addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getProjectRelativePath().toString(),
379 } catch (CoreException e1) {
380 // TODO Auto-generated catch block
381 e1.printStackTrace();
388 * @throws CoreException
390 public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
392 StringBuffer lineBuffer = new StringBuffer();
393 lineBuffer.append(filePath);
394 int lineLength = lineBuffer.length();
395 lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1, null), lineBuffer);
396 // if (lineLength != lineBuffer.length()) {
397 // always add the file for Open Include Action
398 addLine(lineBuffer.toString());
400 } catch (IOException e) {
404 if (stream != null) {
407 } catch (IOException e) {
413 * Adds a line of the index file for function, class, class-method and class-variable names
417 private void addLine(String line) {
418 StringTokenizer tokenizer;
419 String phpFileName = null;
421 String identifier = null;
422 String classname = null;
423 String offset = null;
424 PHPIdentifierLocation phpIdentifier = null;
425 boolean tokenExists = false;
426 tokenizer = new StringTokenizer(line, "\t");
427 // first token contains the filename:
428 if (tokenizer.hasMoreTokens()) {
429 phpFileName = tokenizer.nextToken();
430 //System.out.println(token);
434 // all the other tokens are identifiers:
435 while (tokenizer.hasMoreTokens()) {
436 token = tokenizer.nextToken();
437 //System.out.println(token);
438 switch (token.charAt(0)) {
441 identifier = token.substring(1);
442 classname = identifier;
443 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
447 identifier = token.substring(1);
448 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
452 identifier = token.substring(1);
453 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
457 identifier = token.substring(1);
458 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
461 // constructor function name
462 identifier = token.substring(1);
463 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
466 //method inside a class
467 identifier = token.substring(1);
468 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
471 // variable inside a class
472 identifier = token.substring(1);
473 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
476 // offset information
478 if (phpIdentifier != null) {
479 offset = token.substring(1);
480 phpIdentifier.setOffset(Integer.parseInt(offset));
484 // PHPdoc offset information
486 if (phpIdentifier != null) {
487 offset = token.substring(1);
488 phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
492 // PHPdoc length information
494 if (phpIdentifier != null) {
495 offset = token.substring(1);
496 phpIdentifier.setPHPDocLength(Integer.parseInt(offset));
500 PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: " + token.charAt(0));
502 phpIdentifier = null;
505 if (identifier != null && phpIdentifier != null) {
507 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
509 list = new ArrayList();
510 list.add(phpIdentifier);
511 fIndentifierMap.put(identifier, list);
513 boolean flag = false;
514 for (int i = 0; i < list.size(); i++) {
515 if (list.get(i).equals(phpIdentifier)) {
521 list.add(phpIdentifier);
526 // if (tokenExists) {
527 fFileMap.put(phpFileName, line);
532 * Change the information for a given IFile resource
535 public void changeFile(IFile fileToParse) {
536 removeFile(fileToParse);
537 addFile(fileToParse);
541 * Get a list of all PHPIdentifierLocation object's associated with an identifier
546 public List getLocations(String identifier) {
547 return (List) fIndentifierMap.get(identifier);
551 * Initialize (i.e. clear) the current index information
554 public void initialize() {
555 fIndentifierMap = new TreeMap(new StringComparator());
556 fFileMap = new HashMap();
559 private void readFile() {
560 FileReader fileReader;
562 fileReader = new FileReader(fFilename);
563 BufferedReader bufferedReader = new BufferedReader(fileReader);
565 while (bufferedReader.ready()) {
566 // all entries for one file are in a line
567 // separated by tabs !
568 line = bufferedReader.readLine();
572 } catch (FileNotFoundException e) {
574 // TODO DialogBox which asks the user if she/he likes to build new index?
575 } catch (IOException e) {
576 // TODO Auto-generated catch block
582 * Remove the information for a given IFile resource
585 public void removeFile(IFile fileToParse) {
586 // String line = (String)
587 // fFileMap.get(fileToParse.getLocation().toString());
588 String line = (String) fFileMap.get(fileToParse.getProjectRelativePath().toString());
595 * Removes a line of the index file for function, class, class-method and class-variable names
599 private void removeLine(String line) {
600 StringTokenizer tokenizer;
601 String phpFileName = null;
603 String identifier = null;
604 String classname = null;
605 PHPIdentifier phpIdentifier = null;
606 boolean tokenExists = false;
607 tokenizer = new StringTokenizer(line, "\t");
608 // first token contains the filename:
609 if (tokenizer.hasMoreTokens()) {
610 phpFileName = tokenizer.nextToken();
611 //System.out.println(token);
616 // all the other tokens are identifiers:
617 while (tokenizer.hasMoreTokens()) {
618 token = tokenizer.nextToken();
619 //System.out.println(token);
620 switch (token.charAt(0)) {
623 identifier = token.substring(1);
624 classname = identifier;
625 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
629 identifier = token.substring(1);
630 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
634 identifier = token.substring(1);
635 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
639 identifier = token.substring(1);
640 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
643 // constructor function name
644 identifier = token.substring(1);
645 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
648 //method inside a class
649 identifier = token.substring(1);
650 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
653 // offset information
657 // PHPdoc offset information
661 // PHPdoc length information
665 // variable inside a class
666 identifier = token.substring(1);
667 phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
670 PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: " + token.charAt(0));
672 phpIdentifier = null;
675 if (identifier != null && phpIdentifier != null) {
676 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
679 for (int i = 0; i < list.size(); i++) {
680 if (list.get(i).equals(phpIdentifier)) {
685 if (list.size() == 0) {
686 fIndentifierMap.remove(identifier);
691 fFileMap.remove(phpFileName);
695 * Save the current index information in the projects index file
698 public void writeFile() {
699 FileWriter fileWriter;
701 fileWriter = new FileWriter(fFilename);
703 Collection collection = fFileMap.values();
704 Iterator iterator = collection.iterator();
705 while (iterator.hasNext()) {
706 line = (String) iterator.next();
707 fileWriter.write(line + '\n');
710 } catch (FileNotFoundException e) {
711 // ignore exception; project is deleted by user
712 } catch (IOException e) {
713 // TODO Auto-generated catch block
723 public SortedMap getIdentifierMap() {
724 return fIndentifierMap;
727 synchronized public List getFileList(String filePattern) {
728 Set set = fFileMap.keySet();
732 Iterator iter = set.iterator();
733 ArrayList list = new ArrayList();
736 while (iter.hasNext()) {
737 fileName = (String) iter.next();
738 if ((index = fileName.indexOf(filePattern)) != -1 && fileName.length() == (index + filePattern.length())) {