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);
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) {
83 fToken = TokenNameERROR;
85 private void parseDeclarations(char[] parent, StringBuffer buf,
90 int phpdocOffset = -1;
91 int phpdocLength = -1;
93 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
95 if (fToken == TokenNameCOMMENT_PHPDOC) {
96 phpdocOffset = fScanner.getCurrentTokenStartPosition();
97 phpdocLength = fScanner.getCurrentTokenEndPosition()
98 - fScanner.getCurrentTokenStartPosition() + 1;
100 if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
104 if (fToken == TokenNamevar || fToken == TokenNamepublic
105 || fToken == TokenNameprotected || fToken == TokenNameprivate) {
107 if (fToken == TokenNameVariable) {
108 ident = fScanner.getCurrentIdentifierSource();
109 classVariable = new char[ident.length - 1];
110 System.arraycopy(ident, 1, classVariable, 0, ident.length - 1);
111 addIdentifierInformation('v', classVariable, buf, phpdocOffset,
115 } else if (fToken == TokenNamefunction) {
117 if (fToken == TokenNameAND) {
120 if (fToken == TokenNameIdentifier) {
121 ident = fScanner.getCurrentIdentifierSource();
122 if (parent != null && equalCharArrays(parent, ident)) {
123 // constructor function
124 addIdentifierInformation('k', ident, buf, phpdocOffset,
127 if (parent != null) {
128 // class method function
129 addIdentifierInformation('m', ident, buf, phpdocOffset,
132 // nested function ?!
133 addIdentifierInformation('f', ident, buf, phpdocOffset,
138 parseDeclarations(null, buf, true);
140 } else if (fToken == TokenNameclass) {
142 if (fToken == TokenNameIdentifier) {
143 ident = fScanner.getCurrentIdentifierSource();
144 addIdentifierInformation('c', ident, buf, phpdocOffset,
147 //skip tokens for classname, extends and others until we have
149 while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
150 && fToken != TokenNameERROR) {
153 parseDeclarations(ident, buf, true);
155 } else if (fToken == TokenNameIdentifier) {
156 ident = fScanner.getCurrentIdentifierSource();
158 if (ident.length==6 &&
165 if (fToken == TokenNameLPAREN) {
167 if (fToken == TokenNameStringLiteral) {
168 ident = fScanner.getCurrentStringLiteralSource();
169 addIdentifierInformation('d', ident, buf, phpdocOffset,
175 } else if ((fToken == TokenNameLBRACE)
176 || (fToken == TokenNameDOLLAR_LBRACE)) {
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++) {
312 * Add the information for a given IFile resource
315 public void addFile(IFile fileToParse) {
316 // InputStream iStream;
317 LineCreator lineCreator = new LineCreator();
319 // iStream = fileToParse.getContents();
321 // StringBuffer buf = new StringBuffer();
324 // while ((c0 = iStream.read()) != (-1)) {
325 // buf.append((char) c0);
327 // } catch (IOException e) {
330 InputStream stream = null;
332 stream = new BufferedInputStream(fileToParse.getContents());
333 StringBuffer lineBuffer = new StringBuffer();
334 lineBuffer.append(fileToParse.getFullPath().toString());
335 int lineLength = lineBuffer.length();
336 // lineCreator.parseIdentifiers(buf.toString().toCharArray(),
338 lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
340 if (lineLength != lineBuffer.length()) {
341 addLine(lineBuffer.toString());
343 } catch (IOException e) {
347 if (stream != null) {
350 } catch (IOException e) {
353 } catch (CoreException e1) {
354 // TODO Auto-generated catch block
355 e1.printStackTrace();
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);
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;