001: package ru.emdev.EmForge.wiki;
002:
003: import java.io.IOException;
004: import java.io.StringReader;
005:
006: import javax.faces.component.StateHolder;
007: import javax.faces.component.UIComponent;
008: import javax.faces.component.UIComponentBase;
009: import javax.faces.context.FacesContext;
010: import javax.faces.convert.Converter;
011: import javax.faces.el.ValueBinding;
012:
013: import org.apache.commons.lang.StringUtils;
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016:
017: import ru.emdev.EmForge.util.Helper;
018:
019: import com.ecyrd.jspwiki.WikiContext;
020: import com.ecyrd.jspwiki.WikiEngine;
021: import com.ecyrd.jspwiki.WikiPage;
022: import com.ecyrd.jspwiki.parser.JSPWikiMarkupParser;
023: import com.ecyrd.jspwiki.parser.MarkupParser;
024: import com.ecyrd.jspwiki.render.WikiRenderer;
025:
026: /** Converter to convert from wiki formatted text to XHTML
027: *
028: * This class implements standard JSF Converter interface, so, may be used as
029: * converter in JSF Tags (for example h:outputText tags)
030: *
031: * It supports only converting from wiki to XHTML, back convertion is not supported yet
032: */
033: public class WikiTextToXHTMLConverter extends UIComponentBase implements
034: Converter, StateHolder {
035: /**
036: *
037: */
038: private static final long serialVersionUID = 1L;
039:
040: protected final Log logger = LogFactory.getLog(getClass());
041:
042: private WikiEngine m_wikiEngine;
043: private String m_wikiName = null;
044:
045: public void setWikiName(String i_wikiName) {
046: m_wikiName = i_wikiName;
047: }
048:
049: public String getWikiName() {
050: if (m_wikiName != null) {
051: return m_wikiName;
052: }
053:
054: ValueBinding vb = getValueBinding("wikiName");
055: String v = vb != null ? (String) vb.getValue(getFacesContext())
056: : null;
057: return v;
058: }
059:
060: private WikiEngine getWikiEngine(FacesContext context)
061: throws Exception {
062: if (m_wikiEngine != null) {
063: return m_wikiEngine;
064: } else {
065: return Helper.getWikiEngine(context);
066: }
067: }
068:
069: public WikiTextToXHTMLConverter() {
070:
071: }
072:
073: public WikiTextToXHTMLConverter(WikiEngine i_wikiEngine,
074: String i_wikiName) {
075: m_wikiName = i_wikiName;
076: m_wikiEngine = i_wikiEngine;
077: }
078:
079: /** Back convertions - is not currently supported
080: *
081: * It is not supported because it is not required
082: */
083: public Object getAsObject(FacesContext context,
084: UIComponent component, String value) {
085: return value;
086: }
087:
088: /** Convert from wiki formatted text to XHTML
089: *
090: * @param value - wiki formatted text
091: * @return XHTML formatted text
092: */
093: public String getAsString(FacesContext context,
094: UIComponent component, Object value) {
095: String result = "";
096: try {
097: // get wiki engine via Spring now
098: WikiEngine wikiEngine = getWikiEngine(context);
099: WikiPage pageInfo = null;
100: String wikiText = null;
101:
102: if (value instanceof WikiPage) {
103:
104: pageInfo = (WikiPage) value;
105: wikiText = wikiEngine.getPureText(pageInfo);
106:
107: } else if (value instanceof String) {
108:
109: if (StringUtils.isNotEmpty(getWikiName())) {
110:
111: pageInfo = wikiEngine.getPage(getWikiName());
112:
113: // probably a wiki page hasn't been created yet, so
114: // create temporary page info
115: if (pageInfo == null) {
116: pageInfo = new WikiPage(wikiEngine,
117: getWikiName());
118: }
119:
120: } else {
121: // just in case
122: pageInfo = new WikiPage(wikiEngine, "DummyPage");
123: }
124:
125: wikiText = (String) value;
126: }
127:
128: result = convert(wikiEngine, pageInfo, wikiText);
129:
130: } catch (Exception ex) {
131: logger.error("Cannot convert wiki text:", ex);
132: return null;
133: }
134: return result;
135: }
136:
137: /**
138: *
139: * @param i_wikiEngine
140: * @param i_pageInfo (the page where a text should be shown )
141: * @param i_wikiText
142: * @return
143: * @throws IOException
144: */
145: private String convert(WikiEngine i_wikiEngine,
146: WikiPage i_pageInfo, String i_wikiText) throws IOException {
147:
148: assert i_wikiEngine != null;
149:
150: // check for null
151: if (StringUtils.isEmpty(i_wikiText)) {
152: return "";
153: }
154:
155: WikiContext wikiContext = new WikiContext(i_wikiEngine,
156: i_pageInfo);
157:
158: i_wikiText = new ProcessInstanceLinkFilter()
159: .makeLinksToInstanceProcess(
160: ProcessInstanceLinkFilter.PROCESS_LINK_MARK,
161: i_wikiText);
162:
163: MarkupParser parser = new JSPWikiMarkupParser(wikiContext,
164: new StringReader(i_wikiText));
165: WikiRenderer renderer = new EmForgeRendererImpl(wikiContext,
166: parser.parse());
167: return renderer.getString();
168:
169: }
170:
171: private boolean m_transient = false;
172:
173: public boolean isTransient() {
174: return m_transient;
175: }
176:
177: public void setTransient(boolean newTransientValue) {
178: m_transient = newTransientValue;
179: }
180:
181: public Object saveState(FacesContext context) {
182: Object values[] = new Object[1];
183: values[0] = m_wikiName;
184: return values;
185: }
186:
187: public void restoreState(FacesContext context, Object state) {
188: Object values[] = (Object[]) state;
189: m_wikiName = (String) values[0];
190: }
191:
192: /**
193: * @todo clarify correct value for it
194: */
195: @Override
196: public String getFamily() {
197: return "EMFORGE_FAMILY";
198: }
199:
200: }
|