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;
24 public class IncludesScanner implements ITerminalSymbols {
25 // private final PHPOpenAllIncludesEditorAction fOpenAllIncludesAction;
26 private IProject fProject;
28 private IFileEditorInput fEditorInput;
32 public IncludesScanner(IProject project, IFileEditorInput editorInput) {
34 // fOpenAllIncludesAction = action;
35 fEditorInput = editorInput;
40 * Add the information for a given IFile resource
43 public void addFile(IFile fileToParse) {
46 if (fileToParse.exists()) {
47 addInputStream(new BufferedInputStream(fileToParse
48 .getContents()), fileToParse.getProjectRelativePath()
51 } catch (CoreException e1) {
56 private void addInputStream(InputStream stream, String filePath)
57 throws CoreException {
59 if (fSet.add(filePath)) { // new entry in set
60 parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
63 } catch (IOException e) {
70 } catch (IOException e) {
76 * Get the next token from input
78 private int getNextToken(Scanner scanner) {
81 token = scanner.getNextToken();
83 int currentEndPosition = scanner.getCurrentTokenEndPosition();
84 int currentStartPosition = scanner
85 .getCurrentTokenStartPosition();
86 System.out.print(currentStartPosition + ","
87 + currentEndPosition + ": ");
88 System.out.println(scanner.toStringAction(token));
91 } catch (InvalidInputException e) {
93 return TokenNameERROR;
96 private void parseIdentifiers(char[] charArray) {
102 Scanner scanner = new Scanner(false, false, false, false, true, null,
103 null, true /* taskCaseSensitive */);
104 scanner.setSource(charArray);
105 scanner.setPHPMode(false);
106 int token = getNextToken(scanner);
108 while (token != TokenNameEOF) { // && fToken != TokenNameERROR) {
109 if (token == TokenNameinclude || token == TokenNameinclude_once
110 || token == TokenNamerequire
111 || token == TokenNamerequire_once) {
112 while (token != TokenNameEOF && token != TokenNameERROR
113 && token != TokenNameSEMICOLON
114 && token != TokenNameRPAREN
115 && token != TokenNameLBRACE
116 && token != TokenNameRBRACE) {
117 token = getNextToken(scanner);
118 if (token == TokenNameStringDoubleQuote
119 || token == TokenNameStringSingleQuote) {
120 char[] includeName = scanner
121 .getCurrentStringLiteralSource();
123 file = getIncludeFile(new String(includeName));
125 } catch (Exception e) {
132 token = getNextToken(scanner);
134 } catch (SyntaxError e) {
135 // e.printStackTrace();
139 private IContainer getWorkingLocation(IFileEditorInput editorInput) {
140 if (editorInput == null || editorInput.getFile() == null) {
143 return editorInput.getFile().getParent();
146 public IFile getIncludeFile(String relativeFilename) {
147 IContainer container = getWorkingLocation(fEditorInput);
148 String fullPath = fProject.getLocation().toString();
150 if (relativeFilename.startsWith("../")) {
151 Path path = new Path(relativeFilename);
152 file = container.getFile(path);
155 int index = relativeFilename.lastIndexOf('/');
158 Path path = new Path(relativeFilename);
159 file = fProject.getFile(path);
164 Path path = new Path(relativeFilename);
165 file = container.getFile(path);
171 * Returns a list of includes
173 * @return the determined list of includes
175 public List getList() {
176 ArrayList list = new ArrayList();
182 * @return Returns the set.
184 public Set getSet() {