1 13 package info.magnolia.cms.taglibs.util; 14 15 import info.magnolia.cms.beans.config.Server; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.NodeData; 19 import info.magnolia.cms.security.SessionAccessControl; 20 import info.magnolia.cms.util.Resource; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.jcr.PropertyType; 28 import javax.jcr.RepositoryException; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.jsp.JspWriter ; 31 import javax.servlet.jsp.tagext.BodyTagSupport ; 32 33 import org.apache.commons.lang.StringUtils; 34 import org.apache.commons.lang.exception.NestableRuntimeException; 35 import org.apache.log4j.Logger; 36 37 38 44 public class AHref extends BodyTagSupport { 45 46 49 private static final long serialVersionUID = 222L; 50 51 54 private static Logger log = Logger.getLogger(AHref.class); 55 56 59 private String preHref; 60 61 64 private String postHref; 65 66 69 private int level; 70 71 74 private String templateName; 75 76 79 private String nodeDataName; 80 81 84 private transient List attributes; 85 86 90 public void setAtomName(String name) { 91 this.setNodeDataName(name); 92 } 93 94 98 public void setNodeDataName(String name) { 99 this.nodeDataName = name; 100 } 101 102 106 public void setPreHref(String preHref) { 107 this.preHref = preHref; 108 } 109 110 114 public void setPostHref(String postHref) { 115 this.postHref = postHref; 116 } 117 118 122 public void setTemplateName(String templateName) { 123 this.templateName = templateName; 124 } 125 126 130 public void setLevel(int level) { 131 this.level = level; 132 } 133 134 139 public void setAttribute(String name, String value) { 140 if (attributes == null) { 141 attributes = new ArrayList (); 142 } 143 String [] attributeArray = new String []{name, value}; 144 attributes.add(attributeArray); 145 } 146 147 150 public int doEndTag() { 151 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 152 if (StringUtils.isEmpty(this.templateName)) { 153 if (this.nodeDataName == null) { 154 this.writeLink(StringUtils.EMPTY); 155 return EVAL_BODY_BUFFERED; 156 } 157 Content contentNode = Resource.getLocalContentNode(req); 158 if (contentNode == null) { 159 contentNode = Resource.getGlobalContentNode(req); 160 if (contentNode == null) { 161 this.writeLink(StringUtils.EMPTY); 162 return EVAL_BODY_BUFFERED; 163 } 164 } 165 166 NodeData nodeData = contentNode.getNodeData(this.nodeDataName); 167 168 if ((nodeData == null) || !nodeData.isExist()) { 169 this.writeLink(StringUtils.EMPTY); 170 return EVAL_BODY_BUFFERED; 171 } 172 int type = nodeData.getType(); 173 if (type == PropertyType.STRING) { 174 if (StringUtils.isEmpty(nodeData.getString())) { 175 this.writeLink(StringUtils.EMPTY); 176 } 177 else { 178 this.writeLink(nodeData.getString()); 179 } 180 } 181 } 182 else { 183 Content startPage; 184 try { 185 startPage = Resource.getCurrentActivePage(req).getAncestor(this.level); 186 HierarchyManager hm = SessionAccessControl.getHierarchyManager(req); 187 Content resultPage = hm.getPage(startPage.getHandle(), this.templateName); 188 this.writeLink(resultPage.getHandle()); 189 } 190 catch (RepositoryException e) { 191 log.error(e.getMessage()); 192 this.writeLink(StringUtils.EMPTY); 193 } 194 } 195 return EVAL_BODY_BUFFERED; 196 } 197 198 202 private void writeLink(String path) { 203 JspWriter out = pageContext.getOut(); 204 try { 205 if (StringUtils.isNotEmpty(path)) { 206 207 out.print("<a HREF=\""); if (this.preHref != null) { 209 out.print(this.preHref); 210 } 211 out.print(path); 212 if (SessionAccessControl 213 .getHierarchyManager((HttpServletRequest ) pageContext.getRequest()) 214 .isPage(path)) { 215 out.print("."); out.print(Server.getDefaultExtension()); 217 } 218 if (this.postHref != null) { 219 out.print(this.postHref); 220 } 221 out.print("\""); if ((attributes != null) && (attributes.size() > 0)) { 223 Iterator i = attributes.iterator(); 224 while (i.hasNext()) { 225 String [] s = (String []) i.next(); 226 out.print(" "); out.print(s[0]); 228 out.print("=\""); out.print(s[1]); 230 out.print("\""); } 232 } 233 out.print(">"); } 235 out.print(getBodyContent().getString()); 236 if (StringUtils.isNotEmpty(path)) { 237 out.print("</a>"); } 239 } 240 catch (RepositoryException e) { 241 log.error(e.getMessage(), e); 242 } 243 catch (IOException e) { 244 throw new NestableRuntimeException(e); 245 } 246 attributes = null; 247 } 248 249 252 public void release() { 253 this.preHref = null; 254 this.postHref = null; 255 this.level = 0; 256 this.templateName = null; 257 this.nodeDataName = null; 258 this.attributes = null; 259 super.release(); 260 } 261 262 } 263 | Popular Tags |