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;
16 import java.util.SortedMap;
17 import java.util.StringTokenizer;
18 import java.util.TreeMap;
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;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.runtime.CoreException;
30 * Manages the identifer index information for a specific project
33 public class IdentifierIndexManager {
34 public class LineCreator implements ITerminalSymbols {
35 private Scanner fScanner;
37 public LineCreator() {
38 fScanner = new Scanner(true, false, false, false, true, null, null);
41 * Add the information of the current identifier to the line
43 * @param typeOfIdentifier
44 * the type of the identifier ('c'lass, 'd'efine, 'f'unction,
45 * 'm'ethod, 'v'ariable)
49 * Buffer for the current index line
51 * the offset of the PHPdoc comment if available
53 * the length of the PHPdoc comment if available
55 private void addIdentifierInformation(char typeOfIdentifier,
56 char[] identifier, StringBuffer line, int phpdocOffset, int phpdocLength) {
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);
70 * Get the next token from input
72 private void getNextToken() {
74 fToken = fScanner.getNextToken();
76 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
77 int currentStartPosition = fScanner.getCurrentTokenStartPosition();
78 System.out.print(currentStartPosition + "," + currentEndPosition
80 System.out.println(fScanner.toStringAction(fToken));
83 } catch (InvalidInputException e) {
85 // e.printStackTrace();
87 fToken = TokenNameERROR;
89 private void parseDeclarations(char[] parent, StringBuffer buf,
94 int phpdocOffset = -1;
95 int phpdocLength = -1;
97 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
99 if (fToken == TokenNameCOMMENT_PHPDOC) {
100 phpdocOffset = fScanner.getCurrentTokenStartPosition();
101 phpdocLength = fScanner.getCurrentTokenEndPosition()
102 - fScanner.getCurrentTokenStartPosition() + 1;
104 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
108 if (fToken == TokenNamevar || fToken == TokenNamepublic
109 || fToken == TokenNameprotected || fToken == TokenNameprivate) {
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,
119 } else if (fToken == TokenNamefunction) {
121 if (fToken == TokenNameAND) {
124 if (fToken == TokenNameIdentifier) {
125 ident = fScanner.getCurrentIdentifierSource();
126 if (parent != null && equalCharArrays(parent, ident)) {
127 // constructor function
128 addIdentifierInformation('k', ident, buf, phpdocOffset,
131 if (parent != null) {
132 // class method function
133 addIdentifierInformation('m', ident, buf, phpdocOffset,
136 // nested function ?!
137 addIdentifierInformation('f', ident, buf, phpdocOffset,
142 parseDeclarations(null, buf, true);
144 } else if (fToken == TokenNameclass) {
146 if (fToken == TokenNameIdentifier) {
147 ident = fScanner.getCurrentIdentifierSource();
148 addIdentifierInformation('c', ident, buf, phpdocOffset,
151 //skip tokens for classname, extends and others until we have
153 while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
154 && fToken != TokenNameERROR) {
157 parseDeclarations(ident, buf, true);
159 } else if (fToken == TokenNameIdentifier) {
160 ident = fScanner.getCurrentIdentifierSource();
162 if (ident.length==6 &&
169 if (fToken == TokenNameLPAREN) {
171 if (fToken == TokenNameStringLiteral) {
172 ident = fScanner.getCurrentStringLiteralSource();
173 addIdentifierInformation('d', ident, buf, phpdocOffset,
179 } else if (fToken == TokenNameLBRACE) {
182 } else if (fToken == TokenNameRBRACE) {
185 if (counter == 0 && goBack) {
192 } catch (SyntaxError e) {
193 // TODO Auto-generated catch block
197 synchronized public void parseIdentifiers(char[] charArray, StringBuffer buf) {
201 int phpdocOffset = -1;
202 int phpdocLength = -1;
203 fScanner.setSource(charArray);
204 fScanner.setPHPMode(false);
205 fToken = TokenNameEOF;
208 while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
210 if (fToken == TokenNameCOMMENT_PHPDOC) {
211 phpdocOffset = fScanner.getCurrentTokenStartPosition();
212 phpdocLength = fScanner.getCurrentTokenEndPosition()
213 - fScanner.getCurrentTokenStartPosition() + 1;
215 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
219 if (fToken == TokenNamefunction) {
221 if (fToken == TokenNameAND) {
224 if (fToken == TokenNameIdentifier) {
225 ident = fScanner.getCurrentIdentifierSource();
226 addIdentifierInformation('f', ident, buf, phpdocOffset,
229 parseDeclarations(null, buf, true);
231 } else if (fToken == TokenNameclass) {
233 if (fToken == TokenNameIdentifier) {
234 ident = fScanner.getCurrentIdentifierSource();
235 addIdentifierInformation('c', ident, buf, phpdocOffset,
238 //skip fTokens for classname, extends and others until we have
240 while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
241 && fToken != TokenNameERROR) {
244 parseDeclarations(ident, buf, true);
246 } else if (fToken == TokenNameIdentifier) {
247 ident = fScanner.getCurrentIdentifierSource();
249 if (ident.length==6 &&
256 if (fToken == TokenNameLPAREN) {
258 if (fToken == TokenNameStringLiteral) {
259 ident = fScanner.getCurrentStringLiteralSource();
260 addIdentifierInformation('d', ident, buf, phpdocOffset,
270 } catch (SyntaxError e) {
271 // TODO Auto-generated catch block
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());
283 public boolean equals(Object o) {
284 String s = (String) o;
285 return compare(this, o) == 0;
288 private HashMap fFileMap;
289 private String fFilename;
290 private TreeMap fIndentifierMap;
291 public IdentifierIndexManager(String filename) {
292 fFilename = filename;
297 * Check if 2 char arrays are equal
303 private static boolean equalCharArrays(char[] a, char[] b) {
304 if (a.length != b.length) {
307 for (int i = 0; i < b.length; i++) {
314 public LineCreator createLineCreator() {
315 return new LineCreator();
318 * Add the information for a given IFile resource
321 public void addFile(IFile fileToParse) {
322 // InputStream iStream;
323 LineCreator lineCreator = createLineCreator();
325 addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getFullPath().toString(), lineCreator);
326 } catch (CoreException e1) {
327 // TODO Auto-generated catch block
328 e1.printStackTrace();
334 * @throws CoreException
336 public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
338 StringBuffer lineBuffer = new StringBuffer();
339 lineBuffer.append(filePath);
340 int lineLength = lineBuffer.length();
341 lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
343 // if (lineLength != lineBuffer.length()) {
344 // always add the file for Open Include Action
345 addLine(lineBuffer.toString());
347 } catch (IOException e) {
351 if (stream != null) {
354 } catch (IOException e) {
359 * Adds a line of the index file for function, class, class-method and
360 * class-variable names
364 private void addLine(String line) {
365 StringTokenizer tokenizer;
366 String phpFileName = null;
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);
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)) {
388 identifier = token.substring(1);
389 classname = identifier;
390 phpIdentifier = new PHPIdentifierLocation(identifier,
391 PHPIdentifier.CLASS, phpFileName);
395 identifier = token.substring(1);
396 phpIdentifier = new PHPIdentifierLocation(identifier,
397 PHPIdentifier.DEFINE, phpFileName);
401 identifier = token.substring(1);
402 phpIdentifier = new PHPIdentifierLocation(identifier,
403 PHPIdentifier.FUNCTION, phpFileName);
406 // constructor function name
407 identifier = token.substring(1);
408 phpIdentifier = new PHPIdentifierLocation(identifier,
409 PHPIdentifier.CONSTRUCTOR, phpFileName);
412 //method inside a class
413 identifier = token.substring(1);
414 phpIdentifier = new PHPIdentifierLocation(identifier,
415 PHPIdentifier.METHOD, phpFileName, classname);
418 // variable inside a class
419 identifier = token.substring(1);
420 phpIdentifier = new PHPIdentifierLocation(identifier,
421 PHPIdentifier.VARIABLE, phpFileName, classname);
424 // offset information
426 if (phpIdentifier != null) {
427 offset = token.substring(1);
428 phpIdentifier.setOffset(Integer.parseInt(offset));
432 // PHPdoc offset information
434 if (phpIdentifier != null) {
435 offset = token.substring(1);
436 phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
440 // PHPdoc length information
442 if (phpIdentifier != null) {
443 offset = token.substring(1);
444 phpIdentifier.setPHPDocLength(Integer.parseInt(offset));
449 phpIdentifier = null;
452 if (identifier != null && phpIdentifier != null) {
454 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
456 list = new ArrayList();
457 list.add(phpIdentifier);
458 fIndentifierMap.put(identifier, list);
460 boolean flag = false;
461 for (int i = 0; i < list.size(); i++) {
462 if (list.get(i).equals(phpIdentifier)) {
468 list.add(phpIdentifier);
473 // if (tokenExists) {
474 fFileMap.put(phpFileName, line);
478 * Change the information for a given IFile resource
481 public void changeFile(IFile fileToParse) {
482 removeFile(fileToParse);
483 addFile(fileToParse);
486 * Get a list of all PHPIdentifierLocation object's associated with an
492 public List getLocations(String identifier) {
493 return (List) fIndentifierMap.get(identifier);
496 * Initialize (i.e. clear) the current index information
499 public void initialize() {
500 fIndentifierMap = new TreeMap(new StringComparator());
501 fFileMap = new HashMap();
503 private void readFile() {
504 FileReader fileReader;
506 fileReader = new FileReader(fFilename);
507 BufferedReader bufferedReader = new BufferedReader(fileReader);
509 while (bufferedReader.ready()) {
510 // all entries for one file are in a line
511 // separated by tabs !
512 line = bufferedReader.readLine();
516 } catch (FileNotFoundException e) {
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
525 * Remove the information for a given IFile resource
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());
537 * Removes a line of the index file for function, class, class-method and
538 * class-variable names
542 private void removeLine(String line) {
543 StringTokenizer tokenizer;
544 String phpFileName = null;
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);
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)) {
565 identifier = token.substring(1);
566 classname = identifier;
567 phpIdentifier = new PHPIdentifierLocation(identifier,
568 PHPIdentifier.CLASS, phpFileName);
572 identifier = token.substring(1);
573 phpIdentifier = new PHPIdentifierLocation(identifier,
574 PHPIdentifier.DEFINE, phpFileName);
578 identifier = token.substring(1);
579 phpIdentifier = new PHPIdentifierLocation(identifier,
580 PHPIdentifier.FUNCTION, phpFileName);
583 // constructor function name
584 identifier = token.substring(1);
585 phpIdentifier = new PHPIdentifierLocation(identifier,
586 PHPIdentifier.CONSTRUCTOR, phpFileName);
589 //method inside a class
590 identifier = token.substring(1);
591 phpIdentifier = new PHPIdentifierLocation(identifier,
592 PHPIdentifier.METHOD, phpFileName, classname);
595 // variable inside a class
596 identifier = token.substring(1);
597 phpIdentifier = new PHPIdentifierLocation(identifier,
598 PHPIdentifier.VARIABLE, phpFileName, classname);
602 phpIdentifier = null;
605 if (identifier != null && phpIdentifier != null) {
606 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
609 for (int i = 0; i < list.size(); i++) {
610 if (list.get(i).equals(phpIdentifier)) {
615 if (list.size() == 0) {
616 fIndentifierMap.remove(identifier);
621 fFileMap.remove(phpFileName);
624 * Save the current index information in the projects index file
627 public void writeFile() {
628 FileWriter fileWriter;
630 fileWriter = new FileWriter(fFilename);
632 Collection collection = fFileMap.values();
633 Iterator iterator = collection.iterator();
634 while (iterator.hasNext()) {
635 line = (String) iterator.next();
636 fileWriter.write(line + '\n');
639 } catch (FileNotFoundException e) {
640 // ignore exception; project is deleted by user
641 } catch (IOException e) {
642 // TODO Auto-generated catch block
651 public SortedMap getIdentifierMap() {
652 return fIndentifierMap;
655 synchronized public List getFileList(String filePattern) {
656 Set set = fFileMap.keySet();
660 Iterator iter = set.iterator();
661 ArrayList list = new ArrayList();
664 while(iter.hasNext()) {
665 fileName = (String) iter.next();
666 if ((index=fileName.indexOf(filePattern))!=-1 && fileName.length()==(index+filePattern.length())) {