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 import org.w3c.dom.html.*; 27 28 import org.enhydra.barracuda.core.util.dom.DOMUtil; 29 import org.enhydra.barracuda.plankton.*; 30 import org.enhydra.barracuda.plankton.data.*; 31 32 37 public class DefaultTemplateView extends DefaultView implements TemplateView { 38 39 protected static final Logger logger = Logger.getLogger(DefaultTemplateView.class.getName()); 40 41 protected Node masterTemplate = null; 42 protected Map dirMap = null; protected StateMap idMap = null; protected String idAttrName = "id"; 45 protected String dirAttrName = "class"; 46 47 52 public DefaultTemplateView() {} 53 54 59 public DefaultTemplateView(Node node) { 60 if (node!=null) setNode(node); 61 } 62 63 73 public DefaultTemplateView(Node node, String idAttrName, String dirAttrName) { 74 if (idAttrName!=null) setIDAttrName(idAttrName); 75 if (dirAttrName!=null) setDirAttrName(dirAttrName); 76 if (node!=null) setNode(node); 77 } 78 79 87 public DefaultTemplateView(Node node, String idAttrName, StateMap idMap) { 88 if (idAttrName!=null) setIDAttrName(idAttrName); 89 if (idMap!=null) setDirIDMap(idMap); 90 if (node!=null) setNode(node); 91 } 92 93 94 100 public Node getMasterTemplate() { 101 return masterTemplate; 102 } 103 104 111 public void setDirIDMap(StateMap iidMap) { 112 idMap = iidMap; 113 } 114 115 121 public StateMap getDirIDMap() { 122 return idMap; 123 } 124 125 131 public void setIDAttrName(String iidAttrName) { 132 idAttrName = iidAttrName; 133 } 134 135 140 public String getIDAttrName() { 141 return idAttrName; 142 } 143 144 151 public void setDirAttrName(String idirAttrName) { 152 dirAttrName = idirAttrName; 153 } 154 155 160 public String getDirAttrName() { 161 return dirAttrName; 162 } 163 164 173 public TemplateDirective lookupDir(String dirStr) { 174 return (TemplateDirective) dirMap.get(dirStr); 175 } 176 177 183 public List lookupDirsByID(String idStr) { 184 if (idMap==null || idStr==null || idStr.trim().length()<1) return null; 185 Object val = idMap.getState(idStr); 186 if (logger.isDebugEnabled()) logger.debug("Looking up directives by id: "+idStr+" val: "+val); 187 if (val==null) return null; 188 if (val instanceof List) return (List) val; 189 else { 190 List list = TemplateDirective.getAllInstances(val.toString()); 191 idMap.putState(idStr, list); 192 return list; 193 } 194 } 195 196 197 201 protected void customSearchForTemplates(Node curnode) { 202 if (logger.isDebugEnabled()) logger.debug("custom search for templates on node:"+curnode); 203 204 if (masterTemplate==null) { 206 masterTemplate = node.cloneNode(true); 207 dirMap = new HashMap(); 208 } 210 211 if (curnode instanceof Element) { 216 Element el = (Element) curnode; 217 String classAttr = el.getAttribute(getDirAttrName()); 218 if (classAttr==null) return; 219 if (logger.isDebugEnabled()) logger.debug("dir attr("+getDirAttrName()+"):"+classAttr); 220 List dirList = TemplateDirective.getAllInstances(classAttr); 221 Iterator it = dirList.iterator(); 222 while (it.hasNext()) { 223 TemplateDirective td = (TemplateDirective) it.next(); 224 if (logger.isDebugEnabled()) logger.debug("dir:"+td.toString()); 225 dirMap.put(td.toString(), td); 226 } 227 } 228 } 229 230 public String toString() { 231 return "TemplateView:"+getName()+" (bound to Node:"+DOMUtil.getID(node)+")"; 232 } 233 234 241 public Object clone() { 242 try { 243 DefaultTemplateView dtv = (DefaultTemplateView) super.clone(); 244 dtv.masterTemplate = null; 245 return dtv; 246 } catch (Exception e) { 247 return null; 248 } 249 } 250 } | Popular Tags |