View Javadoc

1   package net.sf.cantina.tags;
2   
3   import net.sf.cantina.DataSource;
4   import net.sf.cantina.Document;
5   import net.sf.cantina.decorator.EscapeXmlDecorator;
6   import net.sf.cantina.exceptions.ObjectNotFoundException;
7   import net.sf.cantina.util.Localizer;
8   import org.apache.log4j.Logger;
9   
10  import javax.servlet.jsp.JspException;
11  import javax.servlet.jsp.tagext.TagSupport;
12  import java.util.Locale;
13  
14  /***
15   * @author Stephane JAIS
16   */
17  public class WriteTag
18  extends TagSupport
19  {
20    private static final Logger logger = Logger.getLogger(WriteTag.class);
21    private String documentId;
22    private Locale locale;
23    private String escapeXml = "false";
24  
25    public String getEscapeXml()
26    {
27      return escapeXml;
28    }
29  
30    public void setEscapeXml(String escapeXml)
31    {
32      this.escapeXml = escapeXml;
33    }
34  
35    public String getDocumentId()
36    {
37      return documentId;
38    }
39  
40    public void setDocumentId(String documentId)
41    {
42      this.documentId = documentId;
43    }
44  
45    public String getLocale()
46    {
47      return locale.toString();
48    }
49  
50    public void setLocale(String localeString)
51    {
52      this.locale = Localizer.parseLocaleString(localeString);
53    }
54  
55    public int doStartTag()
56    throws JspException
57    {
58      Locale locale;
59      if (this.locale == null)
60        locale = pageContext.getResponse().getLocale();
61      else
62        locale = this.locale;
63      try
64      {
65        logger.debug("Loading document ["+documentId+"] in locale ["+locale+"]");
66        Document doc = DataSource.getInstance().loadDocument(documentId);
67  
68        //apply decorators
69        if (getEscapeXml().equals("true"))
70          doc = new EscapeXmlDecorator(doc);
71  
72        String content = doc.getContentAsString(locale);
73        pageContext.getOut().print(content);
74      } catch (ObjectNotFoundException e )
75      {
76        logger.warn("Document id ["+documentId+"] was not found.");
77      }
78      catch (Exception e)
79      {
80        throw new JspException(e);
81      }
82      return SKIP_BODY;
83    }
84  
85    public void release()
86    {
87      documentId = null;
88      locale = null;
89    }
90  
91  }