1 4 package com.tc; 5 6 import org.apache.xmlbeans.XmlObject; 7 import org.w3c.dom.Node ; 8 import org.w3c.dom.NodeList ; 9 10 import com.terracottatech.config.ClassExpression; 11 import com.terracottatech.config.Include; 12 import com.terracottatech.config.InstrumentedClasses; 13 14 public class RuleHolder { 15 private InstrumentedClasses m_instrumentedClasses; 16 private Rule m_rule; 17 18 public RuleHolder(InstrumentedClasses instrumentedClasses, Rule rule) { 19 m_instrumentedClasses = instrumentedClasses; 20 m_rule = rule; 21 } 22 23 public Rule getRule() { 24 return m_rule; 25 } 26 27 public void replace(Rule newRule) { 28 Node newRuleNode = newRule.getXmlObject().getDomNode(); 29 Node ruleNode = m_rule.getXmlObject().getDomNode(); 30 Node topNode = m_instrumentedClasses.getDomNode(); 31 NodeList topNodeList = topNode.getChildNodes(); 32 int listSize = topNodeList.getLength(); 33 34 m_rule = newRule; 35 topNode.removeChild(newRuleNode); 36 37 for(int i = 0; i < listSize; i++) { 38 if(ruleNode == topNodeList.item(i)) { 39 topNode.insertBefore(newRuleNode, ruleNode); 40 topNode.removeChild(ruleNode); 41 return; 42 } 43 } 44 } 45 46 public int getType() { 47 return m_rule.getType(); 48 } 49 50 public void toggleRuleType() { 51 String expr = getExpression(); 52 XmlObject xmlObject; 53 54 if(m_rule.isIncludeRule()) { 55 ClassExpression ce = m_instrumentedClasses.addNewExclude(); 56 ce.setStringValue(expr); 57 xmlObject = ce; 58 } 59 else { 60 Include include = m_instrumentedClasses.addNewInclude(); 61 include.setClassExpression(expr); 62 xmlObject = include; 63 } 64 65 replace(Rule.create(xmlObject)); 66 } 67 68 public void setType(int type) { 69 if(type != getType()) { 70 toggleRuleType(); 71 } 72 } 73 74 public String getExpression() { 75 return m_rule.getExpression(); 76 } 77 78 public void setExpression(String expr) { 79 m_rule.setExpression(expr); 80 } 81 82 public RuleDetail getDetails() { 83 return m_rule.getDetails(); 84 } 85 86 public void setDetails(RuleDetail details) { 87 m_rule.setDetails(details); 88 } 89 } 90 | Popular Tags |