1 13 package info.magnolia.cms.taglibs; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.NodeData; 17 import info.magnolia.cms.gui.misc.FileProperties; 18 import info.magnolia.cms.util.Resource; 19 20 import java.util.Collection ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import javax.jcr.PropertyType; 25 import javax.jcr.RepositoryException; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.jsp.PageContext ; 28 import javax.servlet.jsp.tagext.TagSupport ; 29 30 import org.apache.commons.lang.StringUtils; 31 import org.apache.log4j.Logger; 32 33 34 50 public class SetNode extends TagSupport { 51 52 55 protected static final Logger log = Logger.getLogger(SetNode.class); 56 57 60 private static final long serialVersionUID = 222L; 61 62 65 private String contentNodeName; 66 67 70 private String contentNodeCollectionName; 71 72 75 private String var; 76 77 81 private int scope = PageContext.PAGE_SCOPE; 82 83 87 public void setContentNodeName(String name) { 88 this.contentNodeName = name; 89 } 90 91 95 public void setContentNodeCollectionName(String name) { 96 this.contentNodeCollectionName = name; 97 } 98 99 103 public void setVar(String var) { 104 this.var = var; 105 } 106 107 112 public void setScope(String scope) { 113 if ("request".equalsIgnoreCase(scope)) { this.scope = PageContext.REQUEST_SCOPE; 115 } 116 else if ("session".equalsIgnoreCase(scope)) { this.scope = PageContext.SESSION_SCOPE; 118 } 119 else if ("application".equalsIgnoreCase(scope)) { this.scope = PageContext.APPLICATION_SCOPE; 121 } 122 else { 123 this.scope = PageContext.PAGE_SCOPE; 125 } 126 } 127 128 132 public int doEndTag() { 133 134 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 135 Content local = Resource.getLocalContentNode(request); 136 Content actpage = Resource.getCurrentActivePage(request); 137 138 Content contentNode = null; 140 141 if (StringUtils.isNotEmpty(contentNodeName)) { 142 try { 144 if (StringUtils.isEmpty(contentNodeCollectionName)) { 145 contentNode = actpage.getContent(contentNodeName); 147 } 148 else { 149 contentNode = actpage.getContent(contentNodeCollectionName).getContent(contentNodeName); 152 } 153 } 154 catch (RepositoryException re) { 155 log.debug(re.getMessage()); 156 } 157 } 158 else { 159 if (local == null) { 160 if (StringUtils.isEmpty(contentNodeCollectionName)) { 162 contentNode = actpage; 165 } 166 } 170 else { 171 if (contentNodeName == null && contentNodeCollectionName == null) { 173 contentNode = local; 175 } 176 else if ((contentNodeName != null && StringUtils.isEmpty(contentNodeName)) 177 || (contentNodeCollectionName != null && StringUtils.isEmpty(contentNodeCollectionName))) { 178 contentNode = actpage; 181 } 182 } 183 } 184 185 if (contentNode != null) { 187 pageContext.setAttribute(this.var, new NodeMapWrapper(contentNode), this.scope); 188 } 189 else { 190 pageContext.removeAttribute(this.var); 191 } 192 193 return EVAL_PAGE; 194 } 195 196 199 public void release() { 200 this.contentNodeCollectionName = null; 201 this.contentNodeName = null; 202 this.var = null; 203 this.scope = PageContext.PAGE_SCOPE; 204 super.release(); 205 } 206 207 212 public static class NodeMapWrapper implements Map { 213 214 217 private Content wrappedNode; 218 219 223 public NodeMapWrapper(Content node) { 224 wrappedNode = node; 225 } 226 227 230 public int size() { 231 return 0; 233 } 234 235 238 public boolean isEmpty() { 239 return false; 241 } 242 243 246 public boolean containsKey(Object key) { 247 return false; 249 } 250 251 254 public boolean containsValue(Object value) { 255 return false; 257 } 258 259 263 public Object get(Object key) { 264 NodeData nodeData; 265 266 nodeData = this.wrappedNode.getNodeData((String ) key); 267 Object value; 268 int type = nodeData.getType(); 269 if (type == PropertyType.DATE) { 270 value = nodeData.getDate(); 271 } 272 else if (type == PropertyType.BINARY) { 273 FileProperties props = new FileProperties(this.wrappedNode, (String ) key); 275 value = props.getProperty(StringUtils.EMPTY); 276 } 277 else { 278 value = nodeData.getString(); 279 } 280 return value; 281 } 282 283 286 public Object put(Object arg0, Object arg1) { 287 return null; 289 } 290 291 294 public Object remove(Object key) { 295 return null; 297 } 298 299 302 public void putAll(Map t) { 303 } 305 306 309 public void clear() { 310 } 312 313 316 public Set keySet() { 317 return null; 319 } 320 321 324 public Collection values() { 325 return null; 327 } 328 329 332 public Set entrySet() { 333 return null; 335 } 336 } 337 338 } 339 | Popular Tags |