1 13 package info.magnolia.cms.taglibs; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.util.Resource; 17 18 import javax.jcr.RepositoryException; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.jsp.JspException ; 21 import javax.servlet.jsp.jstl.core.ConditionalTagSupport; 22 23 import org.apache.commons.lang.StringUtils; 24 import org.apache.log4j.Logger; 25 26 27 31 public class IfExisting extends ConditionalTagSupport { 32 33 36 private static final long serialVersionUID = 222L; 37 38 41 private static Logger log = Logger.getLogger(IfExisting.class); 42 43 private String nodeDataName = StringUtils.EMPTY; 44 45 private String contentNodeName = StringUtils.EMPTY; 46 47 private String contentNodeCollectionName = StringUtils.EMPTY; 48 49 private transient Content contentNode; 50 51 private boolean actpage; 52 53 56 public void setNodeDataName(String name) { 57 this.nodeDataName = name; 58 } 59 60 63 public void setContentNodeName(String contentNodeName) { 64 this.contentNodeName = contentNodeName; 65 } 66 67 70 public void setContentNodeCollectionName(String name) { 71 this.contentNodeCollectionName = name; 72 } 73 74 78 public void setActpage(boolean set) { 79 this.actpage = set; 80 } 81 82 85 protected boolean condition() { 86 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 87 if (StringUtils.isNotEmpty(this.contentNodeCollectionName)) { 89 try { 90 if (Resource.getCurrentActivePage(req).hasContent(this.contentNodeCollectionName)) { 91 return true; 92 } 93 } 94 catch (RepositoryException re) { 95 return false; 96 } 97 return false; 98 } 99 if (StringUtils.isNotEmpty(this.contentNodeName) && StringUtils.isEmpty(this.nodeDataName)) { 101 try { 102 if (Resource.getCurrentActivePage(req).hasContent(this.contentNodeName)) { 103 return true; 104 } 105 } 106 catch (RepositoryException re) { 107 return false; 108 } 109 return false; 110 } 111 else if (StringUtils.isNotEmpty(this.contentNodeName) && StringUtils.isNotEmpty(this.nodeDataName)) { 113 try { 114 this.contentNode = Resource.getCurrentActivePage(req).getContent(this.contentNodeName); 115 } 116 catch (RepositoryException re) { 117 log.debug("Repository exception while reading " + this.contentNodeName + ": " + re.getMessage()); } 119 if (this.contentNode == null) { 120 return false; 121 } 122 if (this.contentNode != null) { 123 124 try { 125 if (this.contentNode.hasNodeData(this.nodeDataName)) { 126 return true; 127 } 128 } 129 catch (RepositoryException e) { 130 return false; 131 } 132 } 133 } 134 else if (StringUtils.isEmpty(this.contentNodeName) && StringUtils.isNotEmpty(this.nodeDataName)) { 136 if (this.actpage) { 137 this.contentNode = Resource.getCurrentActivePage((HttpServletRequest ) pageContext.getRequest()); 138 } 139 else { 140 this.contentNode = Resource.getLocalContentNode((HttpServletRequest ) pageContext.getRequest()); 141 if (this.contentNode == null) { 142 this.contentNode = Resource.getGlobalContentNode((HttpServletRequest ) pageContext.getRequest()); 143 } 144 } 145 if (this.contentNode == null) { 146 return false; 147 } 148 if (this.contentNode != null) { 149 150 try { 151 if (this.contentNode.hasNodeData(this.nodeDataName)) { 152 return true; 153 } 154 } 155 catch (RepositoryException e) { 156 return false; 157 } 158 } 159 } 160 else { 162 this.contentNode = Resource.getLocalContentNode((HttpServletRequest ) pageContext.getRequest()); 163 if (this.contentNode == null) { 164 this.contentNode = Resource.getGlobalContentNode((HttpServletRequest ) pageContext.getRequest()); 165 } 166 if (this.contentNode == null) { 167 return false; 168 } 169 } 170 return false; 171 } 172 173 176 public int doEndTag() throws JspException { 177 this.contentNode = null; 178 return super.doEndTag(); 179 } 180 181 184 public void release() { 185 this.nodeDataName = StringUtils.EMPTY; 186 this.contentNodeName = StringUtils.EMPTY; 187 this.contentNodeCollectionName = StringUtils.EMPTY; 188 this.actpage = false; 189 } 190 191 } 192 | Popular Tags |