Source code: com/obinary/cms/taglibs/util/AHref.java
1 /**
2 *
3 * Magnolia and its source-code is licensed under the LGPL.
4 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6 * you are required to provide proper attribution to obinary.
7 * If you reproduce or distribute the document without making any substantive modifications to its content,
8 * please use the following attribution line:
9 *
10 * Copyright 1993-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17
18 package com.obinary.cms.taglibs.util;
19
20 import com.obinary.cms.util.Resource;
21 import com.obinary.cms.core.Container;
22 import com.obinary.cms.core.Atom;
23 import com.obinary.cms.core.Content;
24 import com.obinary.cms.core.HierarchyManager;
25 import com.obinary.cms.beans.ServerInfo;
26
27 import javax.servlet.jsp.tagext.BodyTagSupport;
28 import javax.servlet.jsp.JspWriter;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.jcr.PropertyType;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34
35
36 /**
37 * Date: Apr 28, 2003
38 * Time: 11:20:59 AM
39 * @author Marcel Salathe
40 * @version 1.0
41 */
42
43
44 public class AHref extends BodyTagSupport {
45
46 private String preHref = "";
47 private String postHref = "";
48 private String level = "0";
49 private String templateName = "";
50 private String atomName;
51
52 private Container container;
53 private Atom atom;
54
55 private ArrayList attributes;
56 private HttpServletRequest req;
57
58
59 /**
60 * <p>end of tag</p>
61 *
62 * @return int
63 */
64 public int doEndTag() {
65 req = (HttpServletRequest)pageContext.getRequest();
66 if (this.templateName.equals("")) {
67 if (this.atomName == null) {
68 this.writeLink("");
69 return EVAL_BODY_BUFFERED;
70 }
71 this.container = Resource.getLocalContainer(req);
72 if (this.container == null) {
73 this.container = Resource.getGlobalContainer(req);
74 if (this.container == null) {
75 this.writeLink("");
76 return EVAL_BODY_BUFFERED;
77 }
78 }
79 this.atom = this.container.getAtom(this.atomName);
80 if (!this.atom.isExist()) {
81 this.writeLink("");
82 return EVAL_BODY_BUFFERED;
83 }
84 int type = this.atom.getType();
85 if (type == PropertyType.STRING) {
86 if (this.atom.getString().equals("")) this.writeLink("");
87 else this.writeLink(this.atom.getString());
88 }
89 }
90 else {
91 int digree = new Integer(this.level).intValue();
92 Content startPage;
93 try {
94 startPage = Resource.getCurrentActivePage(req).getAncestor(digree);
95 HierarchyManager hm = Resource.getHierarchyManager(req);
96 Content resultPage = hm.getPage(startPage.getHandle(),this.templateName);
97 this.writeLink(resultPage.getHandle());
98 }
99 catch (Exception e) {
100 e.printStackTrace();
101 this.writeLink("");
102 }
103 }
104 return EVAL_BODY_BUFFERED;
105 }
106
107
108 /**
109 * <p></p>
110 */
111 private void writeLink(String path) {
112 JspWriter out = pageContext.getOut();
113 try {
114 if (!path.equals("")) {
115 if (Resource.getHierarchyManager(req).isPage(path)){
116 path += "." + ServerInfo.getDefaultExtension();
117 }
118 out.print("<a href=\"");
119 out.print(this.preHref + path + this.postHref);
120 out.print("\"");
121 if ((attributes != null) && (attributes.size()>0)) {
122 Iterator i = attributes.iterator();
123 while (i.hasNext()) {
124 String[] s = (String[])i.next();
125 out.print(" "+s[0]+"=\""+s[1]+"\"");
126 }
127 }
128 out.print(">");
129 }
130 out.print(getBodyContent().getString());
131 if (!path.equals("")) {
132 out.print("</a>");
133 }
134 }
135 catch (IOException e) {}
136 attributes = null;
137 }
138
139
140 /**
141 * @param name , antom name to evaluate
142 */
143 public void setAtomName(String name) {
144 this.atomName = name;
145 }
146
147
148
149 /**
150 * @param preHref , href part that is added before the atom content
151 */
152 public void setPreHref(String preHref) {
153 this.preHref = preHref;
154 }
155
156
157
158 /**
159 * @param postHref , href part that is added after the atom content
160 */
161 public void setPostHref(String postHref) {
162 this.postHref = postHref;
163 }
164
165
166 /**
167 * @param templateName , template name to search for
168 */
169 public void setTemplateName(String templateName) {
170 this.templateName = templateName;
171 }
172
173 /**
174 * @param level , level from where to start the template search
175 */
176 public void setLevel(String level) {
177 this.level = level;
178 }
179
180
181
182 /**
183 * @param name , name of attribute to add to the a element
184 * @param value , value of attribute to add to the a element
185 */
186 public void setAttribute(String name, String value) {
187 if (attributes == null) attributes = new ArrayList();
188 String [] attributeArray = new String[] {name,value};
189 attributes.add(attributeArray);
190
191 }
192
193
194 }