1 23 package org.objectweb.medor.optim.jorm; 24 25 import org.objectweb.jorm.api.PMapper; 26 import org.objectweb.jorm.metainfo.api.Class; 27 import org.objectweb.jorm.metainfo.api.GenClassRef; 28 import org.objectweb.medor.api.MedorException; 29 import org.objectweb.medor.optim.lib.BasicRule; 30 import org.objectweb.medor.query.api.QueryNode; 31 import org.objectweb.medor.query.api.QueryTree; 32 import org.objectweb.medor.query.jorm.lib.ClassExtent; 33 import org.objectweb.medor.query.jorm.lib.GenClassExtent; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 36 import java.util.Map ; 37 38 57 public class JormAssignMapperRule extends BasicRule { 58 59 protected Map jormName2Mapper = null; 60 protected Map jormName2Project = null; 61 62 public JormAssignMapperRule() { 63 super("JormAssignMapperRule"); 64 } 65 66 public Map getJormName2Mapper() { 67 return jormName2Mapper; 68 } 69 70 75 public void setJormName2Mapper(Map jormName2Mapper, Map jormName2Project) { 76 this.jormName2Mapper = jormName2Mapper; 77 this.jormName2Project = jormName2Project; 78 } 79 80 public QueryTree rewrite(QueryTree qt, QueryNode _parent) 81 throws MedorException { 82 if (jormName2Mapper == null || jormName2Mapper.size()==0) 83 throw new MedorException("Unable to assign the mapper on the jorm leaves without non empty map"); 84 if (debug) 85 log.log(BasicLevel.DEBUG, "Assignation of mappers"); 86 assignMapper(qt); 87 return qt; 88 } 89 90 protected void assignMapper(QueryTree qt) throws MedorException { 91 if (qt instanceof ClassExtent) { 92 ClassExtent extent = (ClassExtent) qt; 93 String name = ((Class ) extent.getMetaObject()).getFQName(); 94 PMapper mapper = (PMapper) jormName2Mapper.get(name); 95 if (mapper != null) { 96 extent.setPMapper(mapper, (String )jormName2Project.get(name)); 97 if (debug) 98 log.log(BasicLevel.DEBUG, "Assign the mapper " 99 + mapper.getMapperName() + "to the ClassExtent" + extent.getName()); 100 } else 101 throw new MedorException("No mapper specified for the class " + name); 102 } 103 else if (qt instanceof GenClassExtent) { 104 GenClassExtent gce = (GenClassExtent) qt; 105 GenClassRef gcr = (GenClassRef) gce.getMetaObject(); 106 String name = ((Class ) gcr.getParent()).getFQName() + "." + gcr.getName(); 107 PMapper mapper = (PMapper) jormName2Mapper.get(name); 108 if (mapper != null) { 109 gce.setPMapper(mapper, (String )jormName2Project.get(name)); 110 if (debug) 111 log.log(BasicLevel.DEBUG, "Assign the mapper " 112 + mapper.getMapperName() + "to the GenClassExtent" + gce.getName()); 113 } else 114 throw new MedorException("No mapper specified for the GenClass " + name); 115 } 116 else if (qt instanceof QueryNode) { 117 QueryTree[] qts = ((QueryNode) qt).getChildren(); 118 for (int i = 0; i < qts.length; i++) { 119 assignMapper(qts[i]); 120 } 121 } 122 } 123 } 124 | Popular Tags |