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;
11 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
12 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
13 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
14 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
15 import net.sourceforge.phpdt.internal.compiler.util.Util;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.ui.IFileEditorInput;
25 public class IncludesScanner implements ITerminalSymbols {
26 // private final PHPOpenAllIncludesEditorAction fOpenAllIncludesAction;
27 private IProject fProject;
28 private IFileEditorInput fEditorInput;
30 public IncludesScanner(IProject project, IFileEditorInput editorInput) {
32 // fOpenAllIncludesAction = action;
33 fEditorInput = editorInput;
37 * Add the information for a given IFile resource
40 public void addFile(IFile fileToParse) {
43 if (fileToParse.exists()) {
44 addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getProjectRelativePath().toString());
46 } catch (CoreException e1) {
51 private void addInputStream(InputStream stream, String filePath) throws CoreException {
53 if (fSet.add(filePath)) { // new entry in set
54 parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1, null));
56 } catch (IOException e) {
63 } catch (IOException e) {
69 * Get the next token from input
71 private int getNextToken(Scanner scanner) {
74 token = scanner.getNextToken();
76 int currentEndPosition = scanner.getCurrentTokenEndPosition();
77 int currentStartPosition = scanner.getCurrentTokenStartPosition();
78 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
79 System.out.println(scanner.toStringAction(token));
82 } catch (InvalidInputException e) {
84 return TokenNameERROR;
87 private void parseIdentifiers(char[] charArray) {
93 Scanner scanner = new Scanner(false, false, false, false, true, null, null, true /*taskCaseSensitive*/);
94 scanner.setSource(charArray);
95 scanner.setPHPMode(false);
96 int token = getNextToken(scanner);
98 while (token != TokenNameEOF) { // && fToken != TokenNameERROR) {
99 if (token == TokenNameinclude || token == TokenNameinclude_once || token == TokenNamerequire
100 || token == TokenNamerequire_once) {
101 while (token != TokenNameEOF && token != TokenNameERROR && token != TokenNameSEMICOLON && token != TokenNameRPAREN
102 && token != TokenNameLBRACE && token != TokenNameRBRACE) {
103 token = getNextToken(scanner);
104 if (token == TokenNameStringDoubleQuote || token == TokenNameStringSingleQuote) {
105 char[] includeName = scanner.getCurrentStringLiteralSource();
107 file = getIncludeFile(new String(includeName));
109 } catch (Exception e) {
116 token = getNextToken(scanner);
118 } catch (SyntaxError e) {
119 // e.printStackTrace();
123 private IContainer getWorkingLocation(IFileEditorInput editorInput) {
124 if (editorInput == null || editorInput.getFile() == null) {
127 return editorInput.getFile().getParent();
129 public IFile getIncludeFile(String relativeFilename) {
130 IContainer container = getWorkingLocation(fEditorInput);
131 String fullPath = fProject.getLocation().toString();
133 if (relativeFilename.startsWith("../")) {
134 Path path = new Path(relativeFilename);
135 file = container.getFile(path);
138 int index = relativeFilename.lastIndexOf('/');
141 Path path = new Path(relativeFilename);
142 file = fProject.getFile(path);
147 Path path = new Path(relativeFilename);
148 file = container.getFile(path);
153 * Returns a list of includes
154 * @return the determined list of includes
156 public List getList() {
157 ArrayList list = new ArrayList();
163 * @return Returns the set.
165 public Set getSet() {