package tool.model;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.content.IContentDescriber;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProvider;
import tool.ToolModelActivator;
import tool.model.describer.InterfaceContentDescriber;
public class ToolInterface extends ToolType {
protected Map<String, ToolMethod> methods;
protected Map<String, ToolConstant> constants;
protected Map<String, ToolEventHandler> eventHandlers;
protected Map<String, ToolVirtualAttribute> virtualAttributes;
protected Map<String, ToolEvent> events;
protected IFile methodFile;
protected String superInterface;
public static final String WHOLE_INTERFACE ="interface\\s+(?s).+?\\s+end interface;";
public static final String INTERFACE_REGEX ="interface\\s+([A-Za-z0-9_]+)\\s+(.*inherits from\\s+([A-Za-z0-9_\\.]+)|)";
public static final Pattern pattern = Pattern.compile(INTERFACE_REGEX);
private static InterfaceContentDescriber describer = new InterfaceContentDescriber();
public static final Pattern whole_pattern = Pattern.compile(WHOLE_INTERFACE, Pattern.MULTILINE);
private ToolInterface(){
super();
}
public static ToolInterface fetch(IProject project, String fullName) {
ToolInterface model = (ToolInterface) getTypeCache(project).get(fullName.toUpperCase());
return model;
}
public synchronized static ToolInterface fetch(IProject project, String plan, String interfaceName){
ToolInterface model = fetch(project, plan + "." + interfaceName);
return model;
}
public synchronized static ToolInterface fetch(IProject project, IFile modelFile){
String plan = modelFile.getParent().getName();
String interfaceName = modelFile.getName().substring(0, modelFile.getName().lastIndexOf("."));
ToolInterface model = fetch(project, plan, interfaceName);
if (model == null){
try {
if (describer.describe(modelFile.getContents(), null) != IContentDescriber.VALID)
return null;
ToolInterface inter = new ToolInterface();
inter.setFile(modelFile);
inter.setPlanName(plan);
IDocumentProvider provider = new TextFileDocumentProvider();
provider.connect(modelFile);
IDocument document = provider.getDocument(modelFile);
inter.parse(document);
getTypeCache(project).put((plan + "." + interfaceName).toUpperCase(), model);
model = inter;
} catch (CoreException e) {
ToolModelActivator.log(IStatus.ERROR,"Error updating Interface Model.", e);
} catch (NotAnInterfaceException e) {
ToolModelActivator.log(IStatus.WARNING, "This file is not an interface", e);
} catch (IOException e) {
ToolModelActivator.log(IStatus.ERROR,"Error updating Interface Model.", e);
}
}
return model;
}
public static ToolInterface createFromSource(ToolPlan toolPlan,
String source) {
try {
ToolInterface model = new ToolInterface();
model.setParent(toolPlan);
model.setFile(null);
model.parse(source);
String keyString = (toolPlan.getToolName() + "." + model.getLabelText()).toUpperCase();
if (toolPlan.getProject() == null)
getForteTypeCache().put(keyString, model);
else
getTypeCache(toolPlan.getProject()).put(keyString, model);
return model;
} catch (NotAnInterfaceException e) {
ToolModelActivator.log(IStatus.WARNING, "Cannot create interface", e);
}
return null;
}
public IClassComponent[] getComponents(){
int kidsSize = ((eventHandlers!= null) ? eventHandlers.size() : 0)+
((events!= null) ? events.size() : 0)+
((methods!= null) ? methods.size() : 0) +
((constants!= null) ? constants.size() : 0) +
((virtualAttributes!= null) ? virtualAttributes.size() : 0);
IClassComponent[] allKids = new IClassComponent[kidsSize];
int j = 0;
//Constants
if (constants != null){
Object[] consts = constants.values().toArray();
for (int i = 0; i < constants.size(); i++, j++){
allKids[j] = (IClassComponent) consts[i];
}
}
//Methods
if (methods != null){
Object[] meths = methods.values().toArray();
for (int i = 0; i < methods.size(); i++, j++){
allKids[j] = (IClassComponent) meths[i];
}
}
//Event Handlers
if (eventHandlers != null){
Object[] ehs = eventHandlers.values().toArray();
for (int i = 0; i < eventHandlers.size(); i++, j++){
allKids[j] = (IClassComponent) ehs[i];
}
}
//Event
if (events != null){
Object[] ehs = events.values().toArray();
for (int i = 0; i < events.size(); i++, j++){
allKids[j] = (IClassComponent) ehs[i];
}
}
//Virtual Attributes
if (virtualAttributes != null){
Object[] ehs = virtualAttributes.values().toArray();
for (int i = 0; i < virtualAttributes.size(); i++, j++){
allKids[j] = (IClassComponent) ehs[i];
}
}
return allKids;
}
public void parse(IDocument doc) throws NotAnInterfaceException{
this.document = doc;
String source = document.get();
String fileName = this.file.getName();
this.toolName = fileName.substring(0, fileName.lastIndexOf('.'));
IFolder folder = ((IFolder)this.file.getParent());
String name = this.file.getName();
parse(source);
}
public void parse(String source) throws NotAnInterfaceException{
parseInterface(source);
parseConstants(source);
parseMethods(source);
parseEvents(source);
parseEventHandlers(source);
}
private void parseInterface(String source) throws NotAnInterfaceException{
Matcher cls = pattern.matcher(source);
if (cls.find()){
String wholeMatch = cls.group(0);
String name = cls.group(1);
if (name.startsWith("qq"))
name = name.substring(5);
setToolName(name);
superInterface = cls.group(3);
} else {
throw new NotAnInterfaceException(getToolName());
}
}
private void parseEvents(String source) {
//Events
Matcher sig = ToolEvent.pattern.matcher(source);
while (sig.find()){
String wholeMatch = sig.group(0);
ToolEvent eh = new ToolEvent(this, wholeMatch);
if (events == null)
events = new HashMap<String, ToolEvent>();
events.put(eh.getLabelText(), eh);
}
} private void parseEventHandlers(String source) {
//Event Handlers
Matcher sig = ToolEventHandler.pattern.matcher(source);
while (sig.find()){
//process signature
String wholeMatch = sig.group(0);
ToolEventHandler eh = new ToolEventHandler(this, wholeMatch);
if (eventHandlers == null)
eventHandlers = new HashMap<String, ToolEventHandler>();
eventHandlers.put(eh.getLabelText(), eh);
}
}
private void parseMethods(String source) {
//Methods
Matcher sig = ToolMethod.declarationPattern.matcher(source);
while (sig.find()){
//process method signature
String wholeMatch = sig.group(0);
ToolMethod method = new ToolMethod(this, wholeMatch);
method.setFile(this.file); // for an interface there is no CEX file
if (methods == null)
methods = new HashMap<String, ToolMethod>();
methods.put(method.getLabelText(), method);
}
}
public void parseConstants(String source){
//Constants
Matcher matcher = ToolConstant.pattern.matcher(source);
while (matcher.find()){
String wholeMatch = matcher.group(0);
ToolConstant con = new ToolConstant(this, wholeMatch);
if (constants == null)
constants = new HashMap<String, ToolConstant>();
constants.put(con.getName(), con);
}
}
@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 static Map<String, ToolInterface> getInterfaceCache(IProject project){
// Map<String, ToolInterface> cache = null;
// try {
// cache = (Map<String, ToolInterface>)project.getSessionProperty(new QualifiedName(ToolProjectSupport.PROJECT_QUALIFIED_NAME, ToolProjectSupport.INTERFACE_CACHE));
// if (cache == null){
// cache = new HashMap<String, ToolInterface>();
// project.setSessionProperty(new QualifiedName(ToolProjectSupport.PROJECT_QUALIFIED_NAME, ToolProjectSupport.INTERFACE_CACHE), cache);
// }
// } catch (CoreException e) {
// ToolModelActivator.showError("Error getting interface cache", e);
// }
// return cache;
// }
@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/interface.gif";
}
public void add(ToolEvent ev){
if (events == null)
events = new HashMap<String, ToolEvent>();
events.put(ev.getLabelText(), ev);
}
public void add(ToolMethod method){
if (methods == null)
methods = new HashMap<String, ToolMethod>();
methods.put(method.getLabelText(), method);
}
public void add(ToolVirtualAttribute att){
if (virtualAttributes == null)
virtualAttributes = new HashMap<String, ToolVirtualAttribute>();
virtualAttributes.put(att.getName(), att);
}
}