| 1 package org.sapia.regis.loader; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import org.sapia.regis.DuplicateNodeException; 7 import org.sapia.regis.RWNode; 8 import org.sapia.util.xml.confix.ConfigurationException; 9 import org.sapia.util.xml.confix.ObjectHandlerIF; 10 11 public class NodeTag extends BaseTag implements ObjectHandlerIF { 12 13 public static final String OP_CREATE = "create"; 14 public static final String OP_UPDATE = "update"; 15 public static final String OP_UPDATE_OVW = "update-overwrite"; 16 public static final String OP_DELETE = "delete"; 17 18 static final Class [] ALLOWED = new Class []{ 19 IncludeTag.class, 20 NodeTag.class, 21 LinkTag.class, 22 PropertyTag.class, 23 String .class 24 }; 25 26 private String name, id; 27 private List nodes = new ArrayList (); 28 private List links = new ArrayList (); 29 private List includes = new ArrayList (); 30 private List properties = new ArrayList (); 31 private String operation; 32 private boolean inherits; 33 34 public String getId() { 35 return id; 36 } 37 38 public void setId(String id) { 39 this.id = id; 40 } 41 42 public void setOperation(String operation) { 43 this.operation = operation; 44 } 45 46 public String getOperation(){ 47 return this.operation; 48 } 49 50 public void setName(String name) { 51 this.name = name; 52 } 53 54 public void setInheritsParent(boolean inherits) { 55 this.inherits = inherits; 56 } 57 58 public LinkTag createLink(){ 59 LinkTag link = new LinkTag(); 60 links.add(link); 61 return link; 62 } 63 64 public IncludeTag createInclude(){ 65 IncludeTag incl = new IncludeTag(); 66 includes.add(incl); 67 return incl; 68 } 69 70 public NodeTag createNode(){ 71 NodeTag node = new NodeTag(); 72 nodes.add(node); 73 return node; 74 } 75 76 public PropertyTag createProperty(){ 77 PropertyTag prop = new PropertyTag(); 78 properties.add(prop); 79 return prop; 80 } 81 82 void create(ConfigContext ctx) throws DuplicateNodeException{ 83 if(operation == null){ 84 operation = ctx.getDefaultOperation(); 85 } 86 if(name == null){ 87 throw new IllegalStateException ("Registry node name not set"); 88 } 89 if(operation.equals(OP_DELETE)){ 90 ctx.getParent().deleteChild(name); 91 return; 92 } 93 RWNode node = (RWNode)ctx.getParent().getChild(name); 94 if(node == null){ 95 node = (RWNode)ctx.getParent().createChild(name); 96 } 97 if(operation.equals(OP_CREATE)){ 98 node.deleteChildren(); 99 node.deleteProperties(); 100 node.deleteIncludes(); 101 node.deleteLinks(); 102 } 103 else if(operation.equals(OP_UPDATE_OVW)){ 104 node.deleteProperties(); 105 } 106 107 node.setInheritsParent(inherits); 108 109 if(id != null){ 110 if(ctx.getNodes().containsKey(id)){ 111 throw new IllegalStateException ("node element already defined for id: " + id); 112 } 113 ctx.getNodes().put(id, node); 114 } 115 116 for(int i = 0; i < properties.size(); i++){ 117 PropertyTag prop = (PropertyTag)properties.get(i); 118 if(prop.getName() != null && prop.getValue() != null){ 119 node.setProperty(prop.getName(), prop.getValue()); 120 } 121 } 122 ConfigContext child = new ConfigContext(node, ctx.getNodes()); 123 child.setDefaultOperation(ctx.getDefaultOperation()); 124 for(int i = 0; i < links.size(); i++){ 125 LinkTag linkCfg = (LinkTag)links.get(i); 126 linkCfg.create(child); 127 } 128 129 for(int i = 0; i < includes.size(); i++){ 130 IncludeTag includeCfg = (IncludeTag)includes.get(i); 131 includeCfg.create(child); 132 } 133 134 for(int i = 0; i < nodes.size(); i++){ 135 NodeTag nodeCfg = (NodeTag)nodes.get(i); 136 nodeCfg.create(child); 137 } 138 } 139 140 public void handleObject(String name, Object child) throws ConfigurationException { 141 ConfigUtils.checkAllowed(super.getTagName(), name, child, ALLOWED); 142 if(child instanceof NodeTag){ 143 this.nodes.add(child); 144 } 145 else if(child instanceof PropertyTag){ 146 this.properties.add(child); 147 } 148 else if(child instanceof LinkTag){ 149 this.links.add(child); 150 } 151 else if(child instanceof IncludeTag){ 152 this.includes.add(child); 153 } 154 else if (child instanceof String ) { 155 this.properties.add(CustomPropertyTag.create(name, (String ) child)); 156 } 157 } 158 159 } 160 | Popular Tags |