001: package ru.emdev.EmForge.wiki.web.component;
002:
003: import java.io.IOException;
004:
005: import javax.faces.component.UIOutput;
006: import javax.faces.context.FacesContext;
007: import javax.faces.context.ResponseWriter;
008: import javax.faces.el.ValueBinding;
009:
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013:
014: import ru.emdev.EmForge.util.Helper;
015: import ru.emdev.EmForge.wiki.WikiTextToXHTMLConverter;
016:
017: import com.ecyrd.jspwiki.WikiEngine;
018: import com.ecyrd.jspwiki.WikiPage;
019:
020: public class WikiViewerComponent extends UIOutput {
021: protected final Log logger = LogFactory.getLog(getClass());
022: // private static final String FAMILY = "WIKIFAMILY";
023:
024: private static final String STYLE_CLASS = "wikibox";
025:
026: private WikiTextToXHTMLConverter converter = new WikiTextToXHTMLConverter();
027:
028: // FIXME: Create something like ProviderUtil for selecting Provider class
029: public WikiViewerComponent() {
030: super ();
031: }
032:
033: private String m_pageName;
034: private Integer m_version = -1;
035: private Integer m_trimText = null;
036:
037: public String getPageName() {
038: if (m_pageName != null) {
039: return m_pageName;
040: }
041:
042: ValueBinding vb = getValueBinding("pageName");
043: String v = vb != null ? (String) vb.getValue(getFacesContext())
044: : null;
045: return v;
046: }
047:
048: public void setPageName(String i_pageName) {
049: m_pageName = i_pageName;
050: }
051:
052: public Integer getVersion() {
053: if (m_version != null) {
054: return m_version;
055: }
056:
057: ValueBinding vb = getValueBinding("version");
058: Integer v = vb != null ? (Integer) vb
059: .getValue(getFacesContext()) : null;
060: return v;
061: }
062:
063: public void setVersion(Integer i_version) {
064: m_version = i_version;
065: }
066:
067: public void setTrimText(Integer i_trimText) {
068: m_trimText = i_trimText;
069: }
070:
071: public Integer getTrimText() {
072: if (m_trimText != null) {
073: return m_trimText;
074: }
075:
076: ValueBinding vb = getValueBinding("trimText");
077: Integer v = vb != null ? (Integer) vb
078: .getValue(getFacesContext()) : null;
079: return v;
080: }
081:
082: public void encodeEnd(FacesContext context) throws IOException {
083: String pageName = getPageName();
084: Integer version = getVersion();
085:
086: if (getPageName() != null) {
087: ResponseWriter writer = context.getResponseWriter();
088: String pageTextOutput = null;
089:
090: try {
091: boolean pageExists = false;
092: WikiEngine engine = Helper.getWikiEngine(context);
093:
094: pageExists = engine.pageExists(pageName, m_version);
095:
096: if (pageExists) {
097:
098: // TODO: Change with versioning
099: // TODO: Also think about XHTMLRenderer in faces tag context
100: WikiPage pageInfo = engine.getPage(pageName,
101: version);
102: pageTextOutput = converter.getAsString(context,
103: null, pageInfo);
104: if (getTrimText() != null) {
105: if (pageTextOutput.length() > getTrimText()) {
106: pageTextOutput = StringUtils.substring(
107: pageTextOutput, 0, getTrimText())
108: + "...";
109: }
110: }
111: } else {
112: pageTextOutput = "";
113: }
114:
115: } catch (Exception ex) {
116: throw new IOException(
117: "Provider exception while rendering WikiViewer: "
118: + ex);
119: }
120:
121: writer.startElement("div", this );
122: // FIXME: Must not to be hardcoded
123: writer.writeAttribute("class", STYLE_CLASS, null);
124: writer.writeAttribute("id", getClientId(context), null);
125: writer.startElement("br", this );
126: writer.write(pageTextOutput);
127: writer.endElement("div");
128: } else {
129: logger.error("Wiki Viewer tag got null pageName");
130: }
131: }
132:
133: public Object saveState(FacesContext i_context) {
134: Object values[] = new Object[4];
135: values[0] = super .saveState(i_context);
136: values[1] = m_pageName;
137: values[2] = m_version;
138: values[3] = m_trimText;
139: return values;
140: }
141:
142: public void restoreState(FacesContext i_context, Object i_state) {
143: Object values[] = (Object[]) i_state;
144: super .restoreState(i_context, values[0]);
145: m_pageName = (String) values[1];
146: m_version = (Integer) values[2];
147: m_trimText = (Integer) values[3];
148: }
149: }
|