Source code: com/obinary/cms/taglibs/IfNotEmpty.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;
19
20 import com.obinary.cms.core.Container;
21 import com.obinary.cms.core.Atom;
22 import com.obinary.cms.core.ContainerList;
23 import com.obinary.cms.core.Content;
24 import com.obinary.cms.util.Resource;
25
26 import javax.servlet.jsp.tagext.BodyTagSupport;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.jcr.RepositoryException;
29
30
31 /**
32 * Date: Apr 28, 2003
33 * Time: 11:20:59 AM
34 * @author Marcel Salathe
35 * @version 1.0
36 */
37
38
39 public class IfNotEmpty extends BodyTagSupport {
40
41
42 private String atomName = "";
43 private String containerName = "";
44 private String containerListName = "";
45 private ContainerList containerList;
46 private Content container;
47 private Atom atom;
48 private String actpage = "false";
49
50
51
52 /**
53 * <p>start of tag</p>
54 *
55 * @return int
56 */
57 public int doStartTag() {
58 HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
59 // in the case where a containerListName is provided
60 if (!this.containerListName.equals("")) {
61 try {
62 this.containerList = Resource.getCurrentActivePage(req).getContainerList(this.containerListName);
63 }
64 catch (RepositoryException re) {}
65 if (this.containerList == null) return SKIP_BODY;
66 if (!this.containerList.hasChildren()) return SKIP_BODY;
67 return EVAL_BODY_INCLUDE;
68 }
69 // now the case where no containerListName is provided
70 else {
71 // if only containerName is provided, it checks if this container exists
72 if (!this.containerName.equals("") && this.atomName.equals("")) {
73 try {
74 this.container = Resource.getCurrentActivePage(req).getContainer(this.containerName);
75 } catch (RepositoryException re) {re.printStackTrace();}
76 if (this.container != null) {
77 //container exists, evaluate body
78 return EVAL_BODY_INCLUDE;
79 }
80 }
81 //if both containerName and atomName are set, it checks if that atom of that container exitsts and is not empty
82 else if (!this.containerName.equals("") && !this.atomName.equals("")) {
83 try {
84 this.container = Resource.getCurrentActivePage(req).getContainer(this.containerName);
85 } catch (RepositoryException re) {re.printStackTrace();}
86 if (this.container != null) {
87 this.atom = this.container.getAtom(this.atomName);
88 if (this.atom.isExist() && !this.atom.getString().equals("")) return EVAL_BODY_INCLUDE;
89 }
90 }
91 // if only atomName is provided, it checks if that atom of the current container exists and is not empty
92 else if (this.containerName.equals("") && !this.atomName.equals("")) {
93 if (this.actpage.equals("true")) {
94 this.container = Resource.getCurrentActivePage((HttpServletRequest)pageContext.getRequest());
95 }
96 else {
97 this.container = Resource.getLocalContainer((HttpServletRequest)pageContext.getRequest());
98 if (this.container == null) {
99 this.container = Resource.getGlobalContainer((HttpServletRequest)pageContext.getRequest());
100 }
101 }
102 if (this.container != null) {
103 this.atom = this.container.getAtom(this.atomName);
104 if (this.atom.isExist() && !this.atom.getString().equals("")) return EVAL_BODY_INCLUDE;
105 }
106 }
107 // if both containerName and atomName are not provided, it checks if the current container exists
108 else {
109 this.container = Resource.getLocalContainer((HttpServletRequest)pageContext.getRequest());
110 if (this.container == null) {
111 this.container = Resource.getGlobalContainer((HttpServletRequest)pageContext.getRequest());
112 }
113 if (this.container != null) {
114 return EVAL_BODY_INCLUDE;
115 }
116 }
117 }
118 return SKIP_BODY;
119 }
120
121
122 /**
123 * @param name , antom name to evaluate
124 */
125 public void setAtomName(String name) {
126 this.atomName = name;
127 }
128
129
130
131 /**
132 * @param containerName , containerName to check
133 */
134 public void setContainerName(String containerName) {
135 this.containerName = containerName;
136 }
137
138
139 /**
140 * @param containerListName , containerListName to check
141 */
142 public void setContainerListName(String containerListName) {
143 this.containerListName = containerListName;
144 }
145
146 /**
147 * <p>set the actpage</p>
148 *
149 * @param set
150 */
151 public void setActpage(String set) {
152 this.actpage = set;
153 }
154
155
156
157 }
158