001: package com.technoetic.xplanner.tags;
002:
003: import java.lang.reflect.Constructor;
004: import java.util.HashMap;
005: import java.util.Properties;
006: import java.util.Enumeration;
007: import javax.servlet.jsp.JspException;
008: import javax.servlet.jsp.tagext.TagSupport;
009:
010: import org.apache.commons.beanutils.PropertyUtils;
011: import org.apache.log4j.Logger;
012: import org.apache.struts.util.RequestUtils;
013:
014: import com.technoetic.xplanner.XPlannerProperties;
015: import com.technoetic.xplanner.DomainSpecificPropertiesFactory;
016: import com.technoetic.xplanner.db.hibernate.HibernateHelper;
017: import com.technoetic.xplanner.util.LogUtil;
018: import com.technoetic.xplanner.wiki.SchemeHandler;
019: import com.technoetic.xplanner.wiki.SimpleSchemeHandler;
020: import com.technoetic.xplanner.wiki.TwikiFormat;
021: import com.technoetic.xplanner.wiki.WikiFormat;
022:
023: public class TwikiTag extends TagSupport {
024: private static Logger LOG = LogUtil.getLogger();
025: private String name;
026: private String property;
027: private static WikiFormat formatter;
028: private HashMap schemeHandlers;
029:
030: final private String prefix = "twiki.scheme.";
031: final private String handlerSuffix = ".handler";
032: final private String wikiKey = prefix + "wiki";
033:
034: public int doEndTag() throws JspException {
035: try {
036: Object obj = RequestUtils.lookup(pageContext, name, null);
037: String content = (String) PropertyUtils.getProperty(obj,
038: property);
039: Properties properties = new DomainSpecificPropertiesFactory(
040: HibernateHelper.getSessionFactory(),
041: XPlannerProperties.getProperties())
042: .createPropertiesFor(obj);
043: WikiFormat formatter = getFormatter(properties);
044: if (content != null) {
045: formatter.setProperties(properties);
046: pageContext.getOut().print(
047: formatter.format(content.toString()));
048: }
049: } catch (Exception ex) {
050: throw new JspException(ex);
051: }
052: return EVAL_PAGE;
053: }
054:
055: public void setName(String name) {
056: this .name = name;
057: }
058:
059: public String getName() {
060: return name;
061: }
062:
063: public void setProperty(String property) {
064: this .property = property;
065: }
066:
067: public String getProperty() {
068: return property;
069: }
070:
071: private synchronized WikiFormat getFormatter(Properties properties) {
072: if (formatter == null) {
073: // Read formatter class name from properties
074: if (properties.getProperty("wiki.format") != null) {
075: try {
076: formatter = (WikiFormat) Class.forName(
077: properties.getProperty("wiki.format"))
078: .newInstance();
079: } catch (Exception e) {
080: LOG
081: .error(
082: "Cannot instantiate wiki format, using default: ",
083: e);
084: // Fall back to default
085: formatter = new TwikiFormat();
086: }
087: } else {
088: // Fall back to default
089: formatter = new TwikiFormat();
090: }
091: }
092: formatter.setProperties(properties);
093: if (schemeHandlers == null || schemeHandlers.isEmpty()) {
094: schemeHandlers = loadSchemeHandlers(properties);
095: } else {
096: final String translation = properties.getProperty(wikiKey);
097: schemeHandlers.put(wikiKey.substring(prefix.length()),
098: new SimpleSchemeHandler(translation));
099: }
100: formatter = new TwikiFormat(schemeHandlers);
101: return formatter;
102: }
103:
104: private HashMap loadSchemeHandlers(Properties properties) {
105: HashMap schemeHandlers = new HashMap();
106: Enumeration keys = properties.keys();
107: while (keys.hasMoreElements()) {
108: String key = (String) keys.nextElement();
109: if (key.startsWith(prefix)) {
110: final String translation = properties.getProperty(key);
111: if (key.endsWith(handlerSuffix)
112: && !key.equals(prefix
113: + handlerSuffix.substring(1))) {
114: try {
115: String className = translation;
116: String argument = null;
117: int argumentOffset = translation.indexOf(";");
118: if (argumentOffset != -1) {
119: className = translation.substring(0,
120: argumentOffset);
121: argument = translation
122: .substring(argumentOffset + 1);
123: }
124: Class handlerClass = Class.forName(className);
125: SchemeHandler handler = null;
126: try {
127: Constructor argConstructor = handlerClass
128: .getConstructor(new Class[] { String.class });
129: if (argConstructor != null) {
130: handler = (SchemeHandler) argConstructor
131: .newInstance(new Object[] { argument });
132: }
133: } catch (Exception e) {
134: handler = (SchemeHandler) handlerClass
135: .newInstance();
136: }
137: String handlerKey = key.substring(prefix
138: .length(), key.length()
139: - handlerSuffix.length());
140: schemeHandlers.put(handlerKey, handler);
141: } catch (Exception e) {
142: LOG.error("error", e);
143: }
144: } else {
145: schemeHandlers.put(key.substring(prefix.length()),
146: new SimpleSchemeHandler(translation));
147: }
148: }
149: }
150: return schemeHandlers;
151: }
152: }
|