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.util.Resource; 18 19 import javax.jcr.RepositoryException; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.jsp.tagext.TagSupport ; 22 23 import org.apache.commons.lang.StringUtils; 24 import org.slf4j.Logger; 25 import org.slf4j.LoggerFactory; 26 27 28 32 public class BaseContentTag extends TagSupport { 33 34 37 private static final long serialVersionUID = 222L; 38 39 42 protected Logger log = LoggerFactory.getLogger(getClass()); 43 44 protected String nodeDataName; 45 46 protected String contentNodeName; 47 48 protected String contentNodeCollectionName; 49 50 protected boolean inherit; 51 52 56 public void setNodeDataName(String name) { 57 this.nodeDataName = name; 58 } 59 60 64 public void setContentNodeName(String name) { 65 this.contentNodeName = name; 66 } 67 68 72 public void setContentNodeCollectionName(String name) { 73 this.contentNodeCollectionName = name; 74 } 75 76 80 public void setInherit(boolean inherit) { 81 this.inherit = inherit; 82 } 83 84 88 protected Content getFirtMatchingNode() { 89 90 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 91 Content currentPage = Resource.getCurrentActivePage(request); 92 Content contentNode = resolveNode(currentPage); 93 if (contentNode == null) { 94 return null; 95 } 96 97 NodeData nodeData = contentNode.getNodeData(this.nodeDataName); 98 99 try { 100 while (inherit && currentPage.getLevel() > 0 && !nodeData.isExist()) { 101 currentPage = currentPage.getParent(); 102 contentNode = resolveNode(currentPage); 103 nodeData = contentNode.getNodeData(this.nodeDataName); 104 } 105 } 106 catch (RepositoryException e) { 107 log.error(e.getMessage(), e); 108 } 109 110 return contentNode; 111 } 112 113 protected Content resolveNode(Content currentPage) { 114 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 115 116 Content currentParagraph = Resource.getLocalContentNode(request); 117 118 if (StringUtils.isNotEmpty(contentNodeName)) { 119 try { 121 if (StringUtils.isEmpty(contentNodeCollectionName)) { 122 return currentPage.getContent(contentNodeName); 124 } 125 126 return currentPage.getContent(contentNodeCollectionName).getContent(contentNodeName); 129 130 } 131 catch (RepositoryException re) { 132 if (log.isDebugEnabled()) { 133 log.debug(re.getMessage()); 134 } 135 } 136 } 137 else { 138 if (currentParagraph == null) { 139 if (StringUtils.isEmpty(contentNodeCollectionName)) { 141 return currentPage; 145 } 146 } 149 else { 150 if (contentNodeName == null && contentNodeCollectionName == null) { 152 return currentParagraph; 154 } 155 else if ((contentNodeName != null && StringUtils.isEmpty(contentNodeName)) 156 || (contentNodeCollectionName != null && StringUtils.isEmpty(contentNodeCollectionName))) { 157 return currentPage; 160 } 161 } 162 } 163 return null; 164 } 165 166 169 public void release() { 170 super.release(); 171 172 this.nodeDataName = null; 173 this.contentNodeName = null; 174 this.contentNodeCollectionName = null; 175 this.inherit = false; 176 } 177 178 } 179 | Popular Tags |