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.SortedMap;
16 import java.util.StringTokenizer;
17 import java.util.TreeMap;
18 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
19 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
20 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
21 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
22 import net.sourceforge.phpdt.internal.compiler.util.Util;
23 import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.runtime.CoreException;
27 * Manages the identifer index information for a specific project
30 public class IdentifierIndexManager {
31 public class LineCreator implements ITerminalSymbols {
32 private Scanner fScanner;
34 public LineCreator() {
35 fScanner = new Scanner(true, false, false, false, true, null, null);
38 * Add the information of the current identifier to the line
40 * @param typeOfIdentifier
41 * the type of the identifier ('c'lass, 'd'efine, 'f'unction,
42 * 'm'ethod, 'v'ariable)
46 * Buffer for the current index line
48 * the offset of the PHPdoc comment if available
50 * the length of the PHPdoc comment if available
52 private void addIdentifierInformation(char typeOfIdentifier,
53 char[] identifier, StringBuffer line, int phpdocOffset, int phpdocLength) {
55 line.append(typeOfIdentifier);
56 line.append(identifier);
57 line.append("\to"); // Offset
58 line.append(fScanner.getCurrentTokenStartPosition());
59 if (phpdocOffset >= 0) {
60 line.append("\tp"); // phpdoc offset
61 line.append(phpdocOffset);
62 line.append("\tl"); // phpdoc length
63 line.append(phpdocLength);
67 * Get the next token from input
69 private void getNextToken() {
71 fToken = fScanner.getNextToken();
73 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
74 int currentStartPosition = fScanner.getCurrentTokenStartPosition();
75 System.out.print(currentStartPosition + "," + currentEndPosition
77 System.out.println(fScanner.toStringAction(fToken));
80 } catch (InvalidInputException e) {
84 fToken = TokenNameERROR;
86 private void parseDeclarations(char[] parent, StringBuffer buf,
91 int phpdocOffset = -1;
92 int phpdocLength = -1;
94 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
96 if (fToken == TokenNameCOMMENT_PHPDOC) {
97 phpdocOffset = fScanner.getCurrentTokenStartPosition();
98 phpdocLength = fScanner.getCurrentTokenEndPosition()
99 - fScanner.getCurrentTokenStartPosition() + 1;
101 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
105 if (fToken == TokenNamevar || fToken == TokenNamepublic
106 || fToken == TokenNameprotected || fToken == TokenNameprivate) {
108 if (fToken == TokenNameVariable) {
109 ident = fScanner.getCurrentIdentifierSource();
110 classVariable = new char[ident.length - 1];
111 System.arraycopy(ident, 1, classVariable, 0, ident.length - 1);
112 addIdentifierInformation('v', classVariable, buf, phpdocOffset,
116 } else if (fToken == TokenNamefunction) {
118 if (fToken == TokenNameAND) {
121 if (fToken == TokenNameIdentifier) {
122 ident = fScanner.getCurrentIdentifierSource();
123 if (parent != null && equalCharArrays(parent, ident)) {
124 // constructor function
125 addIdentifierInformation('k', ident, buf, phpdocOffset,
128 if (parent != null) {
129 // class method function
130 addIdentifierInformation('m', ident, buf, phpdocOffset,
133 // nested function ?!
134 addIdentifierInformation('f', ident, buf, phpdocOffset,
139 parseDeclarations(null, buf, true);
141 } else if (fToken == TokenNameclass) {
143 if (fToken == TokenNameIdentifier) {
144 ident = fScanner.getCurrentIdentifierSource();
145 addIdentifierInformation('c', ident, buf, phpdocOffset,
148 //skip tokens for classname, extends and others until we have
150 while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
151 && fToken != TokenNameERROR) {
154 parseDeclarations(ident, buf, true);
156 } else if (fToken == TokenNameIdentifier) {
157 ident = fScanner.getCurrentIdentifierSource();
159 if (ident.length==6 &&
166 if (fToken == TokenNameLPAREN) {
168 if (fToken == TokenNameStringLiteral) {
169 ident = fScanner.getCurrentStringLiteralSource();
170 addIdentifierInformation('d', ident, buf, phpdocOffset,
176 } else if (fToken == TokenNameLBRACE) {
179 } else if (fToken == TokenNameRBRACE) {
182 if (counter == 0 && goBack) {
189 } catch (SyntaxError e) {
190 // TODO Auto-generated catch block
194 public void parseIdentifiers(char[] charArray, StringBuffer buf) {
198 int phpdocOffset = -1;
199 int phpdocLength = -1;
200 fScanner.setSource(charArray);
201 fScanner.setPHPMode(false);
202 fToken = TokenNameEOF;
205 while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
207 if (fToken == TokenNameCOMMENT_PHPDOC) {
208 phpdocOffset = fScanner.getCurrentTokenStartPosition();
209 phpdocLength = fScanner.getCurrentTokenEndPosition()
210 - fScanner.getCurrentTokenStartPosition() + 1;
212 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
216 if (fToken == TokenNamefunction) {
218 if (fToken == TokenNameAND) {
221 if (fToken == TokenNameIdentifier) {
222 ident = fScanner.getCurrentIdentifierSource();
223 addIdentifierInformation('f', ident, buf, phpdocOffset,
226 parseDeclarations(null, buf, true);
228 } else if (fToken == TokenNameclass) {
230 if (fToken == TokenNameIdentifier) {
231 ident = fScanner.getCurrentIdentifierSource();
232 addIdentifierInformation('c', ident, buf, phpdocOffset,
235 //skip fTokens for classname, extends and others until we have
237 while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
238 && fToken != TokenNameERROR) {
241 parseDeclarations(ident, buf, true);
243 } else if (fToken == TokenNameIdentifier) {
244 ident = fScanner.getCurrentIdentifierSource();
246 if (ident.length==6 &&
253 if (fToken == TokenNameLPAREN) {
255 if (fToken == TokenNameStringLiteral) {
256 ident = fScanner.getCurrentStringLiteralSource();
257 addIdentifierInformation('d', ident, buf, phpdocOffset,
267 } catch (SyntaxError e) {
268 // TODO Auto-generated catch block
273 class StringComparator implements Comparator {
274 public int compare(Object o1, Object o2) {
275 String s1 = (String) o1;
276 String s2 = (String) o2;
277 return s1.compareTo(s2);
278 // return s1.toUpperCase().compareTo(s2.toUpperCase());
280 public boolean equals(Object o) {
281 String s = (String) o;
282 return compare(this, o) == 0;
285 private HashMap fFileMap;
286 private String fFilename;
287 private TreeMap fIndentifierMap;
288 public IdentifierIndexManager(String filename) {
289 fFilename = filename;
294 * Check if 2 char arrays are equal
300 private static boolean equalCharArrays(char[] a, char[] b) {
301 if (a.length != b.length) {
304 for (int i = 0; i < b.length; i++) {
311 public LineCreator createLineCreator() {
312 return new LineCreator();
315 * Add the information for a given IFile resource
318 public void addFile(IFile fileToParse) {
319 // InputStream iStream;
320 LineCreator lineCreator = createLineCreator();
322 // iStream = fileToParse.getContents();
324 // StringBuffer buf = new StringBuffer();
327 // while ((c0 = iStream.read()) != (-1)) {
328 // buf.append((char) c0);
330 // } catch (IOException e) {
334 addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getFullPath().toString(), lineCreator);
335 } catch (CoreException e1) {
336 // TODO Auto-generated catch block
337 e1.printStackTrace();
343 * @throws CoreException
345 public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
346 // InputStream stream;
348 // stream = new BufferedInputStream(fileToParse.getContents());
349 StringBuffer lineBuffer = new StringBuffer();
350 lineBuffer.append(filePath);
351 int lineLength = lineBuffer.length();
352 // lineCreator.parseIdentifiers(buf.toString().toCharArray(),
354 lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
356 if (lineLength != lineBuffer.length()) {
357 addLine(lineBuffer.toString());
359 } catch (IOException e) {
363 if (stream != null) {
366 } catch (IOException e) {
371 * Adds a line of the index file for function, class, class-method and
372 * class-variable names
376 private void addLine(String line) {
377 StringTokenizer tokenizer;
378 String phpFileName = null;
380 String identifier = null;
381 String classname = null;
382 String offset = null;
383 PHPIdentifierLocation phpIdentifier = null;
384 boolean tokenExists = false;
385 tokenizer = new StringTokenizer(line, "\t");
386 // first token contains the filename:
387 if (tokenizer.hasMoreTokens()) {
388 phpFileName = tokenizer.nextToken();
389 //System.out.println(token);
393 // all the other tokens are identifiers:
394 while (tokenizer.hasMoreTokens()) {
395 token = tokenizer.nextToken();
396 //System.out.println(token);
397 switch (token.charAt(0)) {
400 identifier = token.substring(1);
401 classname = identifier;
402 phpIdentifier = new PHPIdentifierLocation(identifier,
403 PHPIdentifier.CLASS, phpFileName);
407 identifier = token.substring(1);
408 phpIdentifier = new PHPIdentifierLocation(identifier,
409 PHPIdentifier.DEFINE, phpFileName);
413 identifier = token.substring(1);
414 phpIdentifier = new PHPIdentifierLocation(identifier,
415 PHPIdentifier.FUNCTION, phpFileName);
418 // constructor function name
419 identifier = token.substring(1);
420 phpIdentifier = new PHPIdentifierLocation(identifier,
421 PHPIdentifier.CONSTRUCTOR, phpFileName);
424 //method inside a class
425 identifier = token.substring(1);
426 phpIdentifier = new PHPIdentifierLocation(identifier,
427 PHPIdentifier.METHOD, phpFileName, classname);
430 // variable inside a class
431 identifier = token.substring(1);
432 phpIdentifier = new PHPIdentifierLocation(identifier,
433 PHPIdentifier.VARIABLE, phpFileName, classname);
436 // offset information
438 if (phpIdentifier != null) {
439 offset = token.substring(1);
440 phpIdentifier.setOffset(Integer.parseInt(offset));
444 // PHPdoc offset information
446 if (phpIdentifier != null) {
447 offset = token.substring(1);
448 phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
452 // PHPdoc length information
454 if (phpIdentifier != null) {
455 offset = token.substring(1);
456 phpIdentifier.setPHPDocLength(Integer.parseInt(offset));
461 phpIdentifier = null;
464 if (identifier != null && phpIdentifier != null) {
466 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
468 list = new ArrayList();
469 list.add(phpIdentifier);
470 fIndentifierMap.put(identifier, list);
472 boolean flag = false;
473 for (int i = 0; i < list.size(); i++) {
474 if (list.get(i).equals(phpIdentifier)) {
480 list.add(phpIdentifier);
486 fFileMap.put(phpFileName, line);
490 * Change the information for a given IFile resource
493 public void changeFile(IFile fileToParse) {
494 removeFile(fileToParse);
495 addFile(fileToParse);
498 * Get a list of all PHPIdentifierLocation object's associated with an
504 public List getLocations(String identifier) {
505 return (List) fIndentifierMap.get(identifier);
508 * Initialize (i.e. clear) the current index information
511 public void initialize() {
512 fIndentifierMap = new TreeMap(new StringComparator());
513 fFileMap = new HashMap();
515 private void readFile() {
516 FileReader fileReader;
518 fileReader = new FileReader(fFilename);
519 BufferedReader bufferedReader = new BufferedReader(fileReader);
521 while (bufferedReader.ready()) {
522 // all entries for one file are in a line
523 // separated by tabs !
524 line = bufferedReader.readLine();
528 } catch (FileNotFoundException e) {
530 // TODO DialogBox which asks the user if she/he likes to build new index?
531 } catch (IOException e) {
532 // TODO Auto-generated catch block
537 * Remove the information for a given IFile resource
540 public void removeFile(IFile fileToParse) {
541 // String line = (String)
542 // fFileMap.get(fileToParse.getLocation().toString());
543 String line = (String) fFileMap.get(fileToParse.getFullPath().toString());
549 * Removes a line of the index file for function, class, class-method and
550 * class-variable names
554 private void removeLine(String line) {
555 StringTokenizer tokenizer;
556 String phpFileName = null;
558 String identifier = null;
559 String classname = null;
560 PHPIdentifier phpIdentifier = null;
561 boolean tokenExists = false;
562 tokenizer = new StringTokenizer(line, "\t");
563 // first token contains the filename:
564 if (tokenizer.hasMoreTokens()) {
565 phpFileName = tokenizer.nextToken();
566 //System.out.println(token);
570 // all the other tokens are identifiers:
571 while (tokenizer.hasMoreTokens()) {
572 token = tokenizer.nextToken();
573 //System.out.println(token);
574 switch (token.charAt(0)) {
577 identifier = token.substring(1);
578 classname = identifier;
579 phpIdentifier = new PHPIdentifierLocation(identifier,
580 PHPIdentifier.CLASS, phpFileName);
584 identifier = token.substring(1);
585 phpIdentifier = new PHPIdentifierLocation(identifier,
586 PHPIdentifier.DEFINE, phpFileName);
590 identifier = token.substring(1);
591 phpIdentifier = new PHPIdentifierLocation(identifier,
592 PHPIdentifier.FUNCTION, phpFileName);
595 // constructor function name
596 identifier = token.substring(1);
597 phpIdentifier = new PHPIdentifierLocation(identifier,
598 PHPIdentifier.CONSTRUCTOR, phpFileName);
601 //method inside a class
602 identifier = token.substring(1);
603 phpIdentifier = new PHPIdentifierLocation(identifier,
604 PHPIdentifier.METHOD, phpFileName, classname);
607 // variable inside a class
608 identifier = token.substring(1);
609 phpIdentifier = new PHPIdentifierLocation(identifier,
610 PHPIdentifier.VARIABLE, phpFileName, classname);
614 phpIdentifier = null;
617 if (identifier != null && phpIdentifier != null) {
618 ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
621 for (int i = 0; i < list.size(); i++) {
622 if (list.get(i).equals(phpIdentifier)) {
627 if (list.size() == 0) {
628 fIndentifierMap.remove(identifier);
633 fFileMap.remove(phpFileName);
636 * Save the current index information in the projects index file
639 public void writeFile() {
640 FileWriter fileWriter;
642 fileWriter = new FileWriter(fFilename);
644 Collection collection = fFileMap.values();
645 Iterator iterator = collection.iterator();
646 while (iterator.hasNext()) {
647 line = (String) iterator.next();
648 fileWriter.write(line + '\n');
651 } catch (FileNotFoundException e) {
652 // ignore exception; project is deleted by user
653 } catch (IOException e) {
654 // TODO Auto-generated catch block
663 public SortedMap getIdentifierMap() {
664 return fIndentifierMap;