import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
+
import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
import net.sourceforge.phpdt.core.compiler.InvalidInputException;
import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
import net.sourceforge.phpdt.internal.compiler.util.Util;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
+
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
/**
* Manages the identifer index information for a specific project
*
private Scanner fScanner;
private int fToken;
public LineCreator() {
- fScanner = new Scanner(true, false);
+ fScanner = new Scanner(true, false, false, false, true, null, null);
}
/**
* Add the information of the current identifier to the line
*
* @param typeOfIdentifier
* the type of the identifier ('c'lass, 'd'efine, 'f'unction,
- * 'm'ethod, 'v'ariable)
+ * 'm'ethod(class), 'v'ariable(class) 'g'lobal variable)
* @param identifier
* current identifier
* @param line
return;
} catch (InvalidInputException e) {
// ignore errors
+// e.printStackTrace();
}
fToken = TokenNameERROR;
}
ident[5]=='e') {
if (fToken == TokenNameLPAREN) {
getNextToken();
- if (fToken == TokenNameStringLiteral) {
+ if (fToken == TokenNameStringDoubleQuote) {
ident = fScanner.getCurrentStringLiteralSource();
addIdentifierInformation('d', ident, buf, phpdocOffset,
phpdocLength);
}
}
}
- } else if ((fToken == TokenNameLBRACE)
- || (fToken == TokenNameDOLLAR_LBRACE)) {
+ } else if (fToken == TokenNameglobal) {
+ // global variable
+ while (fToken != TokenNameEOF && fToken != TokenNameERROR &&
+ fToken != TokenNameSEMICOLON &&
+ fToken != TokenNameLBRACE &&
+ fToken != TokenNameRBRACE ) {
+ getNextToken();
+ if (fToken == TokenNameVariable) {
+ ident = fScanner.getCurrentIdentifierSource();
+ addIdentifierInformation('g', ident, buf, phpdocOffset,
+ phpdocLength);
+ }
+ }
+ } else if (fToken == TokenNameLBRACE) {
getNextToken();
counter++;
} else if (fToken == TokenNameRBRACE) {
e.printStackTrace();
}
}
- public void parseIdentifiers(char[] charArray, StringBuffer buf) {
+ synchronized public void parseIdentifiers(char[] charArray, StringBuffer buf) {
char[] ident;
String identifier;
int counter = 0;
fToken = TokenNameEOF;
getNextToken();
try {
- while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
+ while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
phpdocOffset = -1;
if (fToken == TokenNameCOMMENT_PHPDOC) {
phpdocOffset = fScanner.getCurrentTokenStartPosition();
}
parseDeclarations(ident, buf, true);
}
+ } else if (fToken == TokenNameVariable) {
+ // global variable
+ ident = fScanner.getCurrentIdentifierSource();
+ addIdentifierInformation('g', ident, buf, phpdocOffset,
+ phpdocLength);
+ getNextToken();
} else if (fToken == TokenNameIdentifier) {
ident = fScanner.getCurrentIdentifierSource();
getNextToken();
ident[5]=='e') {
if (fToken == TokenNameLPAREN) {
getNextToken();
- if (fToken == TokenNameStringLiteral) {
+ if (fToken == TokenNameStringDoubleQuote) {
ident = fScanner.getCurrentStringLiteralSource();
addIdentifierInformation('d', ident, buf, phpdocOffset,
phpdocLength);
}
return true;
}
+ public LineCreator createLineCreator() {
+ return new LineCreator();
+ }
/**
* Add the information for a given IFile resource
*
*/
public void addFile(IFile fileToParse) {
// InputStream iStream;
- LineCreator lineCreator = new LineCreator();
+ LineCreator lineCreator = createLineCreator();
try {
- // iStream = fileToParse.getContents();
- //
- // StringBuffer buf = new StringBuffer();
- // int c0;
- // try {
- // while ((c0 = iStream.read()) != (-1)) {
- // buf.append((char) c0);
- // }
- // } catch (IOException e) {
- // return;
- // }
- InputStream stream = null;
+ addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getProjectRelativePath().toString(), lineCreator);
+ } catch (CoreException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ }
+ /**
+ * @param fileToParse
+ * @param lineCreator
+ * @throws CoreException
+ */
+ public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
+ try {
+ StringBuffer lineBuffer = new StringBuffer();
+ lineBuffer.append(filePath);
+ int lineLength = lineBuffer.length();
+ lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
+ null), lineBuffer);
+// if (lineLength != lineBuffer.length()) {
+ // always add the file for Open Include Action
+ addLine(lineBuffer.toString());
+// }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
try {
- stream = new BufferedInputStream(fileToParse.getContents());
- StringBuffer lineBuffer = new StringBuffer();
- lineBuffer.append(fileToParse.getFullPath().toString());
- int lineLength = lineBuffer.length();
- // lineCreator.parseIdentifiers(buf.toString().toCharArray(),
- // lineBuffer);
- lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
- null), lineBuffer);
- if (lineLength != lineBuffer.length()) {
- addLine(lineBuffer.toString());
+ if (stream != null) {
+ stream.close();
}
} catch (IOException e) {
- return;
- } finally {
- try {
- if (stream != null) {
- stream.close();
- }
- } catch (IOException e) {
- }
}
- } catch (CoreException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
}
}
/**
phpIdentifier = new PHPIdentifierLocation(identifier,
PHPIdentifier.FUNCTION, phpFileName);
break;
+ case 'g' :
+ // global variable
+ identifier = token.substring(1);
+ phpIdentifier = new PHPIdentifierLocation(identifier,
+ PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
+ break;
case 'k' :
// constructor function name
identifier = token.substring(1);
}
break;
default :
+ PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: "+token.charAt(0));
identifier = null;
phpIdentifier = null;
classname = null;
}
}
}
- if (tokenExists) {
+// if (tokenExists) {
fFileMap.put(phpFileName, line);
- }
+// }
}
/**
* Change the information for a given IFile resource
//System.out.println(token);
} else {
return;
- }
+ }
+ int offset = -1;
// all the other tokens are identifiers:
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
phpIdentifier = new PHPIdentifierLocation(identifier,
PHPIdentifier.FUNCTION, phpFileName);
break;
+ case 'g' :
+ // global variable
+ identifier = token.substring(1);
+ phpIdentifier = new PHPIdentifierLocation(identifier,
+ PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
+ break;
case 'k' :
// constructor function name
identifier = token.substring(1);
phpIdentifier = new PHPIdentifierLocation(identifier,
PHPIdentifier.METHOD, phpFileName, classname);
break;
+ case 'o' :
+ // offset information
+ identifier = null;
+ break;
+ case 'p' :
+ // PHPdoc offset information
+ identifier = null;
+ break;
+ case 'l' :
+ // PHPdoc length information
+ identifier = null;
+ break;
case 'v' :
// variable inside a class
identifier = token.substring(1);
PHPIdentifier.VARIABLE, phpFileName, classname);
break;
default :
+ PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: "+token.charAt(0));
identifier = null;
phpIdentifier = null;
classname = null;
public SortedMap getIdentifierMap() {
return fIndentifierMap;
}
+
+ synchronized public List getFileList(String filePattern) {
+ Set set = fFileMap.keySet();
+ if (set.isEmpty()) {
+ return null;
+ }
+ Iterator iter = set.iterator();
+ ArrayList list = new ArrayList();
+ String fileName;
+ int index;
+ while(iter.hasNext()) {
+ fileName = (String) iter.next();
+ if ((index=fileName.indexOf(filePattern))!=-1 && fileName.length()==(index+filePattern.length())) {
+ list.add(fileName);
+ }
+ }
+ return list;
+ }
}
\ No newline at end of file