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 TokenName 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 TokenName.ERROR;
96 private void parseIdentifiers(char[] charArray) {
98 Scanner scanner = new Scanner(false, false, false, false, true, null,
99 null, true /* taskCaseSensitive */);
100 scanner.setSource(charArray);
101 scanner.setPHPMode(false);
102 TokenName token = getNextToken(scanner);
104 while (token != TokenName.EOF) { // && fToken != TokenName.ERROR) {
105 if (token == TokenName.INCLUDE || token == TokenName.INCLUDE_ONCE
106 || token == TokenName.REQUIRE
107 || token == TokenName.REQUIRE_ONCE) {
108 while (token != TokenName.EOF && token != TokenName.ERROR
109 && token != TokenName.SEMICOLON
110 && token != TokenName.RPAREN
111 && token != TokenName.LBRACE
112 && token != TokenName.RBRACE) {
113 token = getNextToken(scanner);
114 if (token == TokenName.STRINGDOUBLEQUOTE
115 || token == TokenName.STRINGSINGLEQUOTE) {
116 char[] includeName = scanner
117 .getCurrentStringLiteralSource();
119 System.out.println(includeName);
120 file = getIncludeFile(new String(includeName));
122 } catch (Exception e) {
129 token = getNextToken(scanner);
131 } catch (SyntaxError e) {
132 // e.printStackTrace();
136 private IContainer getWorkingLocation(IFileEditorInput editorInput) {
137 if (editorInput == null || editorInput.getFile() == null) {
140 return editorInput.getFile().getParent();
143 public IFile getIncludeFile(String relativeFilename) {
144 IContainer container = getWorkingLocation(fEditorInput);
146 if (relativeFilename.startsWith("../")) {
147 Path path = new Path(relativeFilename);
148 file = container.getFile(path);
151 int index = relativeFilename.lastIndexOf('/');
154 Path path = new Path(relativeFilename);
155 file = fProject.getFile(path);
160 Path path = new Path(relativeFilename);
161 file = container.getFile(path);
167 * Returns a list of includes
169 * @return the determined list of includes
171 public List getList() {
172 ArrayList list = new ArrayList();
178 * @return Returns the set.
180 public Set getSet() {