1 package net.sourceforge.phpeclipse.actions;
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.ArrayList;
7 import java.util.HashSet;
10 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
11 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
12 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
13 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
14 import net.sourceforge.phpdt.internal.compiler.util.Util;
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.ui.IFileEditorInput;
24 public class IncludesScanner implements ITerminalSymbols {
25 private final PHPOpenAllIncludesEditorAction fLineCreator;
26 private IProject fProject;
27 private IFileEditorInput fEditorInput;
29 public IncludesScanner(PHPOpenAllIncludesEditorAction action, IProject project, IFileEditorInput editorInput) {
31 this.fLineCreator = action;
32 fEditorInput = editorInput;
36 * Add the information for a given IFile resource
39 public void addFile(IFile fileToParse) {
42 if (fileToParse.exists()) {
43 addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getFullPath().toString());
45 } catch (CoreException e1) {
50 private void addInputStream(InputStream stream, String filePath) throws CoreException {
52 if (fSet.add(filePath)) { // new entry in set
53 parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1, null));
55 } catch (IOException e) {
62 } catch (IOException e) {
68 * Get the next token from input
70 private int getNextToken(Scanner scanner) {
73 token = scanner.getNextToken();
75 int currentEndPosition = scanner.getCurrentTokenEndPosition();
76 int currentStartPosition = scanner.getCurrentTokenStartPosition();
77 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
78 System.out.println(scanner.toStringAction(token));
81 } catch (InvalidInputException e) {
83 return TokenNameERROR;
86 private void parseIdentifiers(char[] charArray) {
92 Scanner scanner = new Scanner(false, false, false, false, true, null, null);
93 scanner.setSource(charArray);
94 scanner.setPHPMode(false);
95 int token = getNextToken(scanner);
97 while (token != TokenNameEOF) { // && fToken != TokenNameERROR) {
98 if (token == TokenNameinclude || token == TokenNameinclude_once || token == TokenNamerequire
99 || token == TokenNamerequire_once) {
100 while (token != TokenNameEOF && token != TokenNameERROR && token != TokenNameSEMICOLON && token != TokenNameRPAREN
101 && token != TokenNameLBRACE && token != TokenNameRBRACE) {
102 token = getNextToken(scanner);
103 if (token == TokenNameStringDoubleQuote || token == TokenNameStringSingleQuote) {
104 char[] includeName = scanner.getCurrentStringLiteralSource();
106 file = getIncludeFile(new String(includeName));
108 } catch (Exception e) {
115 token = getNextToken(scanner);
117 } catch (SyntaxError e) {
118 // e.printStackTrace();
121 public IFile getIncludeFile(String relativeFilename) {
122 IContainer container = this.fLineCreator.getWorkingLocation(fEditorInput);
123 String fullPath = fProject.getLocation().toString();
125 if (relativeFilename.startsWith("../")) {
126 Path path = new Path(relativeFilename);
127 file = container.getFile(path);
130 int index = relativeFilename.lastIndexOf('/');
133 Path path = new Path(relativeFilename);
134 file = fProject.getFile(path);
139 Path path = new Path(relativeFilename);
140 file = container.getFile(path);
145 * Returns a list of includes
146 * @return the determined list of includes
148 public List getList() {
149 ArrayList list = new ArrayList();