/* * Created on Mar 17, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package fr.lifl.stc.stan.policy.defaultPolicy; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Hashtable; import java.util.Enumeration; import java.util.Iterator; import fr.lifl.stc.stan.policy.ClassPolicy; import fr.lifl.stc.stan.policy.Policy; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import org.apache.bcel.classfile.JavaClass; /** * @author dorina * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class DefaultPolicy implements Policy { private String fileName; private HashMap classPolicy; public DefaultPolicy() { fileName = ""; classPolicy = new HashMap(); } /** * */ public DefaultPolicy(String name) throws FileNotFoundException { this(); if(name == null || name.equals("")) return; this.fileName = name; try{ BufferedReader bf = new BufferedReader(new FileReader(fileName)); String s; while((s = bf.readLine())!=null){ int pIdx = s.lastIndexOf('.'); if(pIdx>0) { String className = s.substring(0,pIdx); String attName = s.substring(pIdx+1); if(!className.equals("") && !attName.equals("")){ if(classPolicy.containsKey(className)){ ClassPolicy dcp= (DefaultClassPolicy)classPolicy.get(className); dcp.add(attName); } else { //Printer.println("new classPolicy "+className); ClassPolicy dcp= new DefaultClassPolicy(className); dcp.add(attName); classPolicy.put(className, dcp); } } } } bf.close(); }catch(IOException e){ System.out.println(e); } } /* (non-Javadoc) * @see fr.lifl.rd2p.jits.tools.ea.policy.Policy#findAttribute(java.lang.String, java.lang.String) */ public int findAttribute(String clas, String attribute) { if(classPolicy.containsKey(clas)){ ClassPolicy cp = classPolicy.get(clas); return cp.findAttribute(attribute); } return Policy.PUBLIC_ATTRIBUTE; } public boolean findClass(String clas){ return classPolicy.containsKey(clas); } public String toString(){ String s = "** "; Iterator it = classPolicy.keySet().iterator(); while(it.hasNext()){ s = s + (classPolicy.get(it.next())) + "\n"; } return s; } public void registerClass(JavaClass jc){ if(!findClass(jc.getClassName())) { ClassPolicy dcp= new DefaultClassPolicy(jc.getClassName()); classPolicy.put(jc.getClassName(), dcp); } String superClassName = jc.getSuperclassName(); if(!findClass(superClassName)) { ClassPolicy dcp= new DefaultClassPolicy(superClassName); classPolicy.put(superClassName, dcp); } ClassPolicy from = classPolicy.get(jc.getClassName()); ClassPolicy superClass = classPolicy.get(superClassName); from.addSuperClass(superClass); //vector with all the superclasses of superClass Hashtable v = superClass.getSuperClasses(); int k = 0; for(Enumeration e = v.keys(); e.hasMoreElements(); ) { ClassPolicy c = v.get(e.nextElement()); from.addSuperClass(c); k++; } Hashtable a = this.getSubClasses(from); v = from.getSuperClasses(); Iterator i = a.keySet().iterator(); for(; i.hasNext(); ) { Object obj = i.next(); classPolicy.get(obj).addSuperClasses(v); } } private Hashtable getSubClasses(ClassPolicy from) { Hashtable h = new Hashtable(); for(Iterator e = classPolicy.keySet().iterator(); e.hasNext(); ) { ClassPolicy c = classPolicy.get(e.next()); if(c.hasSuperClass(from.getClassName())) h.put(c.getClassName(), c); } return h; } public ClassPolicy getClassPolicy(String clas) { if( classPolicy.containsKey(clas)) return classPolicy.get(clas); return null; } }