1 20 package org.enhydra.barracuda.core.comp; 21 22 import java.util.*; 23 24 import org.apache.log4j.*; 25 import org.w3c.dom.*; 26 27 import org.enhydra.barracuda.core.util.dom.DOMUtil; 28 29 40 public class DefaultView implements View { 41 42 protected static final Logger logger = Logger.getLogger(DefaultView.class.getName()); 43 44 private final static String DEFAULT = "~DEFAULT~"; 45 46 protected Node node = null; 47 private String name = null; 48 protected Map templateNodes = null; 49 protected LocalElementFactory localFactory = null; 50 51 55 public DefaultView() { 56 this (null, null); 57 } 58 59 64 public DefaultView(String name) { 65 this (name, null); 66 } 67 68 73 public DefaultView(Node node) { 74 this (null, node); 75 } 76 77 83 public DefaultView(String name, Node node) { 84 if (name!=null) setName(name); 85 if (node!=null) setNode(node); 86 } 87 88 89 90 96 public void setNode(Node inode) { 97 if (logger.isDebugEnabled()) logger.debug("Setting node in "+this+" to:"+node); 99 node = inode; 100 101 searchForTemplates(node); 103 } 104 105 110 public Node getNode() { 111 return node; 112 } 113 114 119 public void setName(String iname) { 120 name = iname; 121 } 122 123 128 public String getName() { 129 if (name==null) name = "@"+Integer.toHexString(this.hashCode()); 130 return name; 131 } 132 133 138 public ElementFactory getElementFactory() { 139 if (localFactory==null) localFactory = new LocalElementFactory(); 140 return localFactory; 141 } 142 143 146 public String toString() { 147 return "View:"+getName()+" (bound to Node:"+DOMUtil.getID(node)+")"; 148 } 149 150 154 protected void searchForTemplates(Node curnode) { 155 if (curnode==null) { 156 logger.warn("The current node in which templates are to be searched is null"); 158 return; 159 } 160 161 if (logger.isDebugEnabled()) logger.debug("Looking for templates for Node:"+curnode+"..."); 162 163 customSearchForTemplates(curnode); 165 166 if (curnode.hasChildNodes()) { 168 NodeList nl = curnode.getChildNodes(); 169 170 for (int i=0,max=nl.getLength(); i<max; i++) { 172 Node child = nl.item(i); 173 if (logger.isDebugEnabled()) logger.debug("Found child:"+child); 174 175 if (child instanceof Element) { 177 if (templateNodes==null) templateNodes = new HashMap(); 179 181 if (templateNodes.get(DEFAULT)==null) { 183 if (logger.isDebugEnabled()) logger.debug("Setting default in xref!"); 184 templateNodes.put(DEFAULT, child); 185 } 186 187 String childName = ((Element) child).getAttribute("name") ; 205 if (childName!=null && !childName.equals("") && !templateNodes.containsKey(childName)) { 206 if (logger.isDebugEnabled()) logger.debug("Adding child name in xref:"+childName); 207 templateNodes.put(childName, child); 208 templateNodes.put(childName.toUpperCase(), child); 209 templateNodes.put(childName.toLowerCase(), child); 210 } 211 212 229 231 String idName = ((Element) child).getAttribute("id"); 233 if (idName!=null && !templateNodes.containsKey(idName)) { 234 if (logger.isDebugEnabled()) logger.debug("Adding id name in xref:"+idName); 235 templateNodes.put(idName, child); 236 templateNodes.put(idName.toUpperCase(), child); 237 templateNodes.put(idName.toLowerCase(), child); 238 } 239 240 String clname = child.getClass().getName(); 242 if (!templateNodes.containsKey(clname)) { 243 if (logger.isDebugEnabled()) logger.debug("Adding class name in xref:"+clname); 244 templateNodes.put(clname, child); 245 246 Class [] cl = child.getClass().getInterfaces(); 248 for (int j=0,jmax=cl.length; j<jmax; j++) { 249 clname = cl[j].getName(); 250 if (!templateNodes.containsKey(clname)) { 251 if (logger.isDebugEnabled()) logger.debug("Adding interface name in xref:"+clname); 252 templateNodes.put(clname, child); 253 } 254 } 255 } 256 } 257 258 searchForTemplates(child); 260 261 if (logger.isDebugEnabled()) logger.debug("Finished check on child!"); 262 } 263 } 264 } 265 266 271 protected void customSearchForTemplates(Node curnode) { 272 } 274 275 276 280 class LocalElementFactory implements ElementFactory { 281 282 public Node getDefaultElement() { 283 if (templateNodes==null) return null; 284 return (Node) templateNodes.get(DEFAULT); 285 } 286 287 public Node getElement(String key) { 288 if (templateNodes==null) return null; 289 return (Node) templateNodes.get(key); 290 } 291 292 public List getElementKeys() { 293 if (templateNodes==null) return null; 294 return new ArrayList(templateNodes.keySet()); 295 } 296 297 public Document getDocument() { 298 return node.getOwnerDocument(); 299 } 300 } 301 302 309 public Object clone() { 310 try { 311 DefaultView dv = (DefaultView) super.clone(); 312 dv.node = null; 313 return dv; 314 } catch (Exception e) { 315 return null; 316 } 317 } 318 } | Popular Tags |