1 package org.mr.core.util.patterns.flow; 2 3 import org.mr.core.util.exceptions.CreationException; 4 import org.w3c.dom.Document ; 5 import org.w3c.dom.Element ; 6 import org.w3c.dom.NodeList ; 7 8 import javax.xml.parsers.DocumentBuilder ; 9 import javax.xml.parsers.DocumentBuilderFactory ; 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 58 59 66 public class ConfigurationLoader { 67 68 public final static String FLOW_STEP_CONDITION_TAG_NAME = "condition"; 69 public final static String FLOW_STEP_CONDITIONS_TAG_NAME = "conditions"; 70 public final static String FLOW_STEP_CONDITION_PARAM_ATTR_NAME = "variable"; 71 public final static String FLOW_STEP_CONDITION_PARAM_VALUE_ATTR_NAME = "value"; 72 public final static String FLOW_STEP_CONDITION_CLASS_ATTR_NAME = "class"; 73 74 String m_filePath = null; 76 77 private static HashMap s_framworks = new HashMap (); 79 80 HashMap m_tagsAndAttributesNames = new HashMap (); 83 84 public ConfigurationLoader(String i_filePath, HashMap i_tagsAndAttributesNames) { 85 m_filePath = i_filePath; 86 if(i_tagsAndAttributesNames != null) 87 m_tagsAndAttributesNames.putAll(i_tagsAndAttributesNames); 88 } 89 90 94 protected FlowFramework generateEmptyFramework(){ 95 return new DefaultFlowFramework(); 96 } 97 98 102 protected Flow generateEmptyFlow(){ 103 return new DefaultFlow(); 104 } 105 106 protected Condition generateConditionSet(Element i_element) throws CreationException{ 107 Conditional conditional = new Conditional(); 108 conditional.configure(i_element); 109 NodeList conditions = i_element.getElementsByTagName(FLOW_STEP_CONDITION_TAG_NAME); 110 for(int j = 0; j < conditions.getLength() ; j++){ 111 Condition condition = generateCondition((Element )conditions.item(j)); 112 conditional.addCondition(condition); 113 } 114 conditions = i_element.getElementsByTagName(FLOW_STEP_CONDITIONS_TAG_NAME); 115 for(int j = 0; j < conditions.getLength() ; j++){ 116 Condition condition = generateConditionSet((Element )conditions.item(j)); 117 conditional.addCondition(condition); 118 } 119 return conditional; 120 } 121 122 protected Condition generateCondition(Element i_element) throws CreationException{ 123 Condition condition = null; 124 try { 125 126 String className = i_element.getAttribute(FLOW_STEP_CONDITION_CLASS_ATTR_NAME); 127 if(className != null && className.length() > 0){ 128 condition = (Condition)loadClass(className).newInstance(); 129 }else{ 130 condition = new StringCondition(); 131 } 132 condition.configure(i_element); 133 } catch (Exception e) { 134 e.printStackTrace(); 135 return null; 136 } 137 return condition; 138 } 139 140 145 protected String getConfigurationName(String i_name){ 146 String name = (String )m_tagsAndAttributesNames.get(i_name); 147 if(name == null) 148 return i_name; 149 return name; 150 } 151 152 156 protected void configureDeclaration(Declaration i_declaration){ 157 } 158 159 164 public FlowFramework loadFramework() throws Exception { 165 FlowFramework framework = (FlowFramework)s_framworks.get(m_filePath); 167 if(framework!= null) 168 return framework; 169 170 framework = generateEmptyFramework(); 172 173 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 174 DocumentBuilder db = dbf.newDocumentBuilder(); 175 176 Document document = db.parse(m_filePath); 178 179 HashMap declarations = new HashMap (); 181 NodeList list = document.getElementsByTagName(getConfigurationName(Declaration.DECLARATION_TAG_NAME)); 182 for (int i = 0 ; i < list.getLength() ; i++) { 183 Element dec = (Element )list.item(i); 184 String className = dec.getAttribute(getConfigurationName(Declaration.DECLARATION_CLASS_ATTR_NAME)); 186 String name = dec.getAttribute(getConfigurationName(Declaration.DECLARATION_NAME_ATTR_NAME)); 187 Declaration declaration = (Declaration)loadClass(className).newInstance(); 189 declaration.configure(dec); 190 configureDeclaration(declaration); 192 declarations.put(name, declaration); 193 } 216 217 list = document.getElementsByTagName(getConfigurationName(Flow.FLOW_TAG_NAME)); 219 Flow flow = generateEmptyFlow(); 220 if(list != null && list.getLength() == 1){ 222 Element element = (Element )list.item(0); 223 if(element.hasChildNodes()){ 224 list = element.getElementsByTagName(getConfigurationName(Flow.FLOW_STEP_TAG_NAME)); 225 for (int i = 0 ; i < list.getLength() ; i++) { 226 Element step = (Element )list.item(i); 227 NodeList conditionsTag = step.getElementsByTagName(getConfigurationName(FLOW_STEP_CONDITIONS_TAG_NAME)); 228 Condition condition = null; 229 if(conditionsTag != null && conditionsTag.getLength() == 1){ 230 Element cons = (Element )conditionsTag.item(0); 231 condition = generateConditionSet(cons); 244 } 245 246 flow.addStep(step.getAttribute(getConfigurationName(Flow.FLOW_STEP_ID_ATTR_NAME)), 247 (Declaration)declarations.get(step.getAttribute(getConfigurationName(Flow.FLOW_STEP_FROM_ATTR_NAME))), 248 (Declaration)declarations.get(step.getAttribute(getConfigurationName(Flow.FLOW_STEP_TO_ATTR_NAME))), 249 condition); 250 } 251 } 252 } 253 framework.setFlow(flow); 254 s_framworks.put(m_filePath,framework); 255 return framework; 256 } 257 258 262 private class StringCondition implements Condition{ 263 String m_name; 264 String m_value; 265 266 public void configure(Element i_object) throws CreationException { 267 m_name = i_object.getAttribute(getConfigurationName(FLOW_STEP_CONDITION_PARAM_ATTR_NAME)); 268 m_value = i_object.getAttribute(getConfigurationName(FLOW_STEP_CONDITION_PARAM_VALUE_ATTR_NAME)); 269 } 270 271 public boolean metCondition(Object i_param) { 272 String value = (String )((Map )i_param).get(m_name); 273 return m_value.equals(value); 274 } 275 } 276 277 public static void main(String [] args) { 278 try { 279 HashMap hashMap = new HashMap (); 280 hashMap.put(Declaration.DECLARATION_TAG_NAME, "plugin"); 281 hashMap.put(Flow.FLOW_TAG_NAME, "flow"); 282 ConfigurationLoader defaultLoader = new ConfigurationLoader("conf.xml", hashMap); 283 FlowFramework framwork = defaultLoader.loadFramework(); 284 } catch (Exception e) { 286 e.printStackTrace(); 287 } 288 } 289 290 public Class loadClass(String i_name)throws Exception { 291 try { 292 return Thread.currentThread().getContextClassLoader().loadClass(i_name); 293 } catch (NullPointerException e) { return Class.forName(i_name); 295 } 296 } 297 } 298 | Popular Tags |