package tool.model;
import java.util.HashMap;
import java.util.Map;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import tool.ToolModelActivator;
import tool.ToolProjectSupport;
import tool.model.grammar.ForteParser;
import tool.model.grammar.ForteParser.cursorDeclaration_return;
import tool.model.grammar.IErrorReporter;
import tool.model.grammar.NoCaseStringStream;
import tool.model.grammar.ToolSQLLexer;
public class ToolCursor extends ToolComponent implements IProjectComponent, IErrorReporter{
private String planName;
private ToolPlan plan;
private TextFileDocumentProvider provider;
@Override
public String getLabelText() {
return getToolName();
}
@Override
public String getLabelText(int options) {
if ((options & LabelProvider.FULL_PATH) == LabelProvider.FULL_PATH){
return getPlanName() + "." + getLabelText();
}
return getLabelText();
}
public String getPlanName(){
return this.planName;
}
public void setPlanName(String planName){
propertyChangeSupport.firePropertyChange("planName", this.planName,
this.planName = planName);
}
public ToolPlan getPlan() {
if (this.plan == null){
this.plan = ToolPlan.fetch(getProject(), this.planName);
}
return this.plan;
}
@Override
public IFile getProjectFile() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setProjectFile(IFile file) {
// TODO Auto-generated method stub
}
@Override
public String getIconString() {
return "icons/cursor.gif";
}
public synchronized static ToolCursor fetch(IFile modelFile){
IProject project = modelFile.getProject();
String planName = modelFile.getParent().getName();
String cursorName = modelFile.getName().substring(0, modelFile.getName().lastIndexOf("."));
String keyString = (planName + "." + cursorName).toUpperCase();
ToolCursor model = (ToolCursor) getCursorCache(project).get(keyString);
if (model == null){
try {
model = new ToolCursor();
model.setFile(modelFile);
model.setPlanName(planName);
model.setToolName(cursorName);
model.provider = new TextFileDocumentProvider();
model.provider.connect(modelFile);
model.document = model.provider.getDocument(modelFile);
model.parse(model.document);
getCursorCache(project).put(keyString, model);
} catch (CoreException e) {
ToolModelActivator.log(IStatus.ERROR,"Error updating Class Model.", e);
}
}
return model;
}
public static Map<String, ToolCursor> getCursorCache(IProject project){
Map<String, ToolCursor> cache = null;
try {
cache = (Map<String, ToolCursor>)project.getSessionProperty(new QualifiedName(ToolProjectSupport.PROJECT_QUALIFIED_NAME, ToolProjectSupport.CURSOR_CACHE));
if (cache == null){
cache = new HashMap<String, ToolCursor>();
project.setSessionProperty(new QualifiedName(ToolProjectSupport.PROJECT_QUALIFIED_NAME, ToolProjectSupport.CURSOR_CACHE), cache);
}
} catch (CoreException e) {
ToolModelActivator.showError("Error getting Cursor cache", e);
}
return cache;
}
public void parse(IDocument document){
try {
String source = document.get();
CharStream stream =
new NoCaseStringStream(source);
ToolSQLLexer lexer = new ToolSQLLexer(stream);
TokenStream tokens = new CommonTokenStream(lexer);
ForteParser parser = new ForteParser(tokens);
// parser.setErrorReporter(this);
parser.setTreeAdaptor(new CommonTreeAdaptor(){
public Object create(Token payload){
return new CommonTree(payload);
}
});
cursorDeclaration_return result = parser.cursorDeclaration();
if (parser.getNumberOfSyntaxErrors() > 0){
ToolModelActivator.showError(parser.getNumberOfSyntaxErrors() + " Syntax error in cursor " + getFile().getName() + "\n"
+ this.parseErrors.toString(), null);
} else {
CommonTree tree = (CommonTree) result.getTree();
System.out.println(tree.toStringTree());
CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
nodes.setTokenStream(tokens);
// TODO write a tree walker
// FortePRXTree walker = new FortePRXTree(this, nodes);
// walker.project();
}
} catch (RecognitionException e) {
ToolModelActivator.showError("Cannot parse cursor " + getFile().getName(), e);
}
}
@Override
public void reportError(String error) {
this.parseErrors.append(error + "\n");
}
}