Updating feature.xml with new version number.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / IncludesScanner.java
1 package net.sourceforge.phpeclipse.actions;
2
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;
8 import java.util.List;
9 import java.util.Set;
10
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;
16
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;
23
24 public class IncludesScanner implements ITerminalSymbols {
25         // private final PHPOpenAllIncludesEditorAction fOpenAllIncludesAction;
26         private IProject fProject;
27
28         private IFileEditorInput fEditorInput;
29
30         private HashSet fSet;
31
32         public IncludesScanner(IProject project, IFileEditorInput editorInput) {
33                 fProject = project;
34                 // fOpenAllIncludesAction = action;
35                 fEditorInput = editorInput;
36                 fSet = new HashSet();
37         }
38
39         /**
40          * Add the information for a given IFile resource
41          * 
42          */
43         public void addFile(IFile fileToParse) {
44
45                 try {
46                         if (fileToParse.exists()) {
47                                 addInputStream(new BufferedInputStream(fileToParse
48                                                 .getContents()), fileToParse.getProjectRelativePath()
49                                                 .toString());
50                         }
51                 } catch (CoreException e1) {
52                         e1.printStackTrace();
53                 }
54         }
55
56         private void addInputStream(InputStream stream, String filePath)
57                         throws CoreException {
58                 try {
59                         if (fSet.add(filePath)) { // new entry in set
60                                 parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
61                                                 null));
62                         }
63                 } catch (IOException e) {
64                         e.printStackTrace();
65                 } finally {
66                         try {
67                                 if (stream != null) {
68                                         stream.close();
69                                 }
70                         } catch (IOException e) {
71                         }
72                 }
73         }
74
75         /**
76          * Get the next token from input
77          */
78         private int getNextToken(Scanner scanner) {
79                 int token;
80                 try {
81                         token = scanner.getNextToken();
82                         if (Scanner.DEBUG) {
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));
89                         }
90                         return token;
91                 } catch (InvalidInputException e) {
92                 }
93                 return TokenNameERROR;
94         }
95
96         private void parseIdentifiers(char[] charArray) {
97                 IFile file;
98                 Scanner scanner = new Scanner(false, false, false, false, true, null,
99                                 null, true /* taskCaseSensitive */);
100                 scanner.setSource(charArray);
101                 scanner.setPHPMode(false);
102                 int token = getNextToken(scanner);
103                 try {
104                         while (token != TokenNameEOF) { // && fToken != TokenNameERROR) {
105                                 if (token == TokenNameinclude || token == TokenNameinclude_once
106                                                 || token == TokenNamerequire
107                                                 || token == TokenNamerequire_once) {
108                                         while (token != TokenNameEOF && token != TokenNameERROR
109                                                         && token != TokenNameSEMICOLON
110                                                         && token != TokenNameRPAREN
111                                                         && token != TokenNameLBRACE
112                                                         && token != TokenNameRBRACE) {
113                                                 token = getNextToken(scanner);
114                                                 if (token == TokenNameStringDoubleQuote
115                                                                 || token == TokenNameStringSingleQuote) {
116                                                         char[] includeName = scanner
117                                                                         .getCurrentStringLiteralSource();
118                                                         try {
119                                                             System.out.println(includeName);
120                                                                 file = getIncludeFile(new String(includeName));
121                                                                 addFile(file);
122                                                         } catch (Exception e) {
123                                                                 // ignore
124                                                         }
125                                                         break;
126                                                 }
127                                         }
128                                 }
129                                 token = getNextToken(scanner);
130                         }
131                 } catch (SyntaxError e) {
132                         // e.printStackTrace();
133                 }
134         }
135
136         private IContainer getWorkingLocation(IFileEditorInput editorInput) {
137                 if (editorInput == null || editorInput.getFile() == null) {
138                         return null;
139                 }
140                 return editorInput.getFile().getParent();
141         }
142
143         public IFile getIncludeFile(String relativeFilename) {
144                 IContainer container = getWorkingLocation(fEditorInput);
145                 IFile file = null;
146                 if (relativeFilename.startsWith("../")) {
147                         Path path = new Path(relativeFilename);
148                         file = container.getFile(path);
149                         return file;
150                 }
151                 int index = relativeFilename.lastIndexOf('/');
152
153                 if (index >= 0) {
154                         Path path = new Path(relativeFilename);
155                         file = fProject.getFile(path);
156                         if (file.exists()) {
157                                 return file;
158                         }
159                 }
160                 Path path = new Path(relativeFilename);
161                 file = container.getFile(path);
162
163                 return file;
164         }
165
166         /**
167          * Returns a list of includes
168          * 
169          * @return the determined list of includes
170          */
171         public List getList() {
172                 ArrayList list = new ArrayList();
173                 list.addAll(fSet);
174                 return list;
175         }
176
177         /**
178          * @return Returns the set.
179          */
180         public Set getSet() {
181                 return fSet;
182         }
183 }