1 18 package org.apache.beehive.netui.tags.template; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.tags.AbstractClassicTag; 23 import org.apache.beehive.netui.util.Bundle; 24 import org.apache.beehive.netui.util.logging.Logger; 25 26 import javax.servlet.RequestDispatcher ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.ServletRequest ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.jsp.JspException ; 32 import javax.servlet.jsp.JspWriter ; 33 import java.io.IOException ; 34 import java.io.Writer ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 38 113 public class IncludeSection extends AbstractClassicTag 114 implements TemplateConstants 115 { 116 private static final Logger logger = Logger.getInstance(IncludeSection.class); 117 118 121 private String _name; 122 123 126 private String _default; 127 128 132 public String getTagName() { 133 return "IncludeSection"; 134 } 135 136 152 public void setName(String name) { 153 _name = name; 154 } 155 156 174 public void setDefaultPage(String defaultPage) { 175 _default = defaultPage; 176 } 177 178 187 public int doStartTag() 188 throws JspException { 189 ServletRequest req = pageContext.getRequest(); 190 Template.TemplateContext tc = (Template.TemplateContext) 191 req.getAttribute(TEMPLATE_SECTIONS); 192 if (tc == null) { 193 194 String s = Bundle.getString("Tags_TemplateContextMissing"); 195 logger.warn(stripBold(s)); 198 registerTagError(s,null); 199 reportErrors(); 200 localRelease(); 201 return SKIP_BODY; 202 } 203 if (tc.secs == null) { 204 if (_default != null) { 205 return callDefault(req); 206 } 207 String s = Bundle.getString("Tags_TemplateSectionMissing", 208 new Object []{_name}); 209 logger.warn(stripBold(s)); 212 registerTagError(s,null); 213 reportErrors(); 214 localRelease(); 215 return SKIP_BODY; 216 } 217 218 String val = (String ) tc.secs.get(_name); 219 if (val == null) { 220 221 if (_default == null) { 222 String s = Bundle.getString("Tags_TemplateSectionMissing", 223 new Object []{_name}); 224 logger.warn(stripBold(s)); 225 226 registerTagError(s,null); 229 reportErrors(); 230 localRelease(); 231 return SKIP_BODY; 232 } 233 return callDefault(req); 234 235 } 236 237 try { 238 Writer out = pageContext.getOut(); 239 out.write(val); 240 } 241 catch (IOException e) { 242 String reason = Bundle.getString("TempExcp_WritingContent"); 243 String s = Bundle.getString("TempExcp_Except", 244 new Object []{"IOException", 245 reason}); 246 logger.error(s); 247 throw new JspException (s,e); 248 } 249 localRelease(); 251 return SKIP_BODY; 252 } 253 254 private int callDefault(ServletRequest req) 255 throws JspException { 256 if (!defaultExists()) { 257 String s = Bundle.getString("TempExcp_MissingDefaultPage", 258 new Object []{_default}); 259 logger.error(s); 260 registerTagError(s,null); 261 reportErrors(); 262 localRelease(); 263 return SKIP_BODY; 264 } 265 266 try { 267 HttpServletResponse resp = (HttpServletResponse ) 268 pageContext.getResponse(); 269 270 RequestDispatcher rd = req.getRequestDispatcher(_default); 271 try { 275 JspWriter out = pageContext.getOut(); 276 out.flush(); 277 } 278 catch (IOException ignore) {} 279 rd.include(req,resp); 280 localRelease(); 281 return SKIP_BODY; 282 } 283 catch (IOException e) { 284 String s = Bundle.getString("TempExcp_ExceptIncludeDefault", 285 new Object []{"IOException", 286 _default}); 287 logger.error(s,e); 288 throw new JspException (s,e); 289 } 290 catch (ServletException se) { 291 String s = Bundle.getString("TempExcp_ExceptIncludeDefault", 292 new Object []{"ServletException", 293 _default}); 294 logger.error(s,se); 295 throw new JspException (s,se); 296 } 297 } 298 299 private boolean defaultExists() 300 throws JspException  301 { 302 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 303 String realURI = Template.getRealURI(req,_default); 304 try { 305 URL uri = pageContext.getServletContext().getResource(realURI); 306 return (uri != null); 307 } 308 catch (MalformedURLException e) { 309 String s = Bundle.getString("TempExcp_ExceptIncludeDefault", 310 new Object []{"MalformedURLException", 311 _default}); 312 logger.error(s,e); 313 throw new JspException (s,e); 314 } 315 } 316 317 320 protected void localRelease() { 321 super.localRelease(); 322 _name = null; 323 _default = null; 324 } 325 326 329 static String stripBold(String in) { 330 String boldStart = "<b>"; 331 String boldEnd = "</b>"; 332 int pos = in.indexOf(boldStart); 333 if (pos == -1) 334 return in; 335 InternalStringBuilder sb = new InternalStringBuilder(in.substring(0,pos)); 336 int fill = pos+boldStart.length(); 337 pos = in.indexOf(boldEnd,fill); 338 if (pos == -1) 339 return in; 340 sb.append(in.substring(fill,pos)); 341 pos += boldEnd.length(); 342 sb.append(in.substring(pos)); 343 return sb.toString(); 344 } 345 } 346 | Popular Tags |