01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc;
05:
06: import org.apache.xmlbeans.XmlObject;
07: import org.w3c.dom.Node;
08: import org.w3c.dom.NodeList;
09:
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: } else {
59: Include include = m_instrumentedClasses.addNewInclude();
60: include.setClassExpression(expr);
61: xmlObject = include;
62: }
63:
64: replace(Rule.create(xmlObject));
65: }
66:
67: public void setType(int type) {
68: if (type != getType()) {
69: toggleRuleType();
70: }
71: }
72:
73: public String getExpression() {
74: return m_rule.getExpression();
75: }
76:
77: public void setExpression(String expr) {
78: m_rule.setExpression(expr);
79: }
80:
81: public RuleDetail getDetails() {
82: return m_rule.getDetails();
83: }
84:
85: public void setDetails(RuleDetail details) {
86: m_rule.setDetails(details);
87: }
88: }
|