[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui ...
Christian Bauer
christian at hibernate.org
Wed Apr 18 11:34:28 EDT 2007
User: cbauer
Date: 07/04/18 11:34:28
Modified: examples/wiki/src/main/org/jboss/seam/wiki/core/ui
WikiFormattedTextHandler.java
UIWikiFormattedText.java
Log:
Disabled broken stuff (RichFaces trees, arithmetic EL) and finished comments plugin
Revision Changes Path
1.4 +179 -215 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/WikiFormattedTextHandler.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: WikiFormattedTextHandler.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/WikiFormattedTextHandler.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- WikiFormattedTextHandler.java 17 Apr 2007 15:16:54 -0000 1.3
+++ WikiFormattedTextHandler.java 18 Apr 2007 15:34:28 -0000 1.4
@@ -20,9 +20,7 @@
import org.jboss.seam.wiki.core.action.PluginPreferenceEditor;
import org.jboss.seam.wiki.core.action.prefs.WikiPreferences;
-
import com.sun.facelets.FaceletContext;
-import com.sun.facelets.FaceletException;
import com.sun.facelets.el.VariableMapperWrapper;
import com.sun.facelets.tag.MetaRuleset;
import com.sun.facelets.tag.MetaTagHandler;
@@ -30,22 +28,24 @@
import com.sun.facelets.tag.TagConfig;
import com.sun.facelets.tag.jsf.ComponentSupport;
-
-
-public class WikiFormattedTextHandler extends MetaTagHandler
-{
+/**
+ * Creates a UIWikiText JSF component and substitutes macro names in wiki
+ * text with real plugin includes/JSF components in the tree.
+ *
+ * @author Peter Muir
+ */
+public class WikiFormattedTextHandler extends MetaTagHandler {
private static final String MARK = "org.jboss.seam.wiki.core.ui.WikiFormattedTextHandler";
- public static final String REGEX_MACRO = Pattern.quote("[") + "<=([a-zA-Z0-9]+)"
- + Pattern.quote("]");
+ public static final String REGEX_MACRO =
+ Pattern.quote("[") + "<=([a-zA-Z0-9]+)" + Pattern.quote("]");
private TagAttribute valueAttribute;
private Set<String> includedMacros;
- public WikiFormattedTextHandler(TagConfig config)
- {
+ public WikiFormattedTextHandler(TagConfig config) {
super(config);
this.valueAttribute = this.getRequiredAttribute("value");
}
@@ -53,13 +53,11 @@
/*
* Main apply method called by facelets to create this component.
*/
- public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException
- {
+ public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException {
includedMacros = new HashSet<String>();
String id = ctx.generateUniqueId(this.tagId);
UIComponent cmp = findChildByTagId(parent, id);
- if (cmp == null)
- {
+ if (cmp == null) {
cmp = createComponent(ctx);
cmp.getAttributes().put(MARK, id);
@@ -72,8 +70,7 @@
/**
* Create the Component
*/
- private UIComponent createComponent(FaceletContext ctx)
- {
+ private UIComponent createComponent(FaceletContext ctx) {
UIWikiFormattedText wikiFormattedText = new UIWikiFormattedText();
setAttributes(ctx, wikiFormattedText);
return wikiFormattedText;
@@ -84,8 +81,7 @@
* is a package scoped class
*/
@Override
- protected void setAttributes(FaceletContext ctx, Object instance)
- {
+ protected void setAttributes(FaceletContext ctx, Object instance) {
UIComponent cmp = (UIComponent) instance;
setAttribute(ctx, cmp, "linkStyleClass");
setAttribute(ctx, cmp, "brokenLinkStyleClass");
@@ -93,25 +89,22 @@
setAttribute(ctx, cmp, "thumbnailLinkStyleClass");
}
- private void setAttribute(FaceletContext ctx, UIComponent cmp, String name)
- {
+ private void setAttribute(FaceletContext ctx, UIComponent cmp, String name) {
TagAttribute attribute = this.getAttribute(name);
cmp.getAttributes().put(name, attribute.getObject(ctx));
}
@Override
- protected MetaRuleset createMetaRuleset(Class type)
- {
+ protected MetaRuleset createMetaRuleset(Class type) {
return super.createMetaRuleset(type).ignoreAll();
}
-
/**
* We create the plugins as first-class components here.
- *
+ * <p/>
* The plugins need to be rendered in the correct place in the
* rendered wikitext. The plugin name is replaced with a placeholder.
- *
+ * <p/>
* To allow multiple use of the same plugin
* we replace the plugin name with a reference to the component.
* The clientId of the component is used as the reference. To
@@ -119,94 +112,87 @@
* a list on the parent UIWikiFormattedText component and it's
* position is used as the placeholder.
*/
- private void createPlugins (FaceletContext ctx, UIComponent parent)
- {
- if (parent instanceof UIWikiFormattedText)
- {
+ private void createPlugins(FaceletContext ctx, UIComponent parent) {
+ if (!(parent instanceof UIWikiFormattedText)) return;
UIWikiFormattedText wikiFormattedText = (UIWikiFormattedText) parent;
+
String unparsed = valueAttribute.getValue(ctx);
Matcher matcher = Pattern.compile(REGEX_MACRO).matcher(unparsed);
StringBuffer parsed = new StringBuffer();
- while (matcher.find())
- {
+ while (matcher.find()) {
+
// Include the plugin
String macroName = matcher.group(1);
- buildMacro(macroName, ctx, parent);
+ includePluginFacelet(macroName, ctx, parent);
// Get the placeholder to use
String placeHolder;
Object nextPlugin = parent.getAttributes().get(UIPlugin.NEXT_PLUGIN);
- if (nextPlugin != null)
- {
+ if (nextPlugin != null) {
placeHolder = wikiFormattedText.addPlugin(nextPlugin.toString());
parent.getAttributes().remove(UIPlugin.NEXT_PLUGIN);
- }
- else
- {
+ } else {
// Best guess based plugin renderer
- placeHolder = wikiFormattedText.addPlugin((parent.getChildren().get(parent.getChildCount() - 1).getClientId(ctx.getFacesContext())));
+ placeHolder = wikiFormattedText.addPlugin(
+ (parent.getChildren().get(parent.getChildCount() - 1)
+ .getClientId( ctx.getFacesContext() )
+ )
+ );
}
matcher.appendReplacement(parsed, " [<=" + placeHolder + "]");
}
matcher.appendTail(parsed);
wikiFormattedText.setValue(parsed.toString());
}
- }
- private void include(URL path, FaceletContext ctx, UIComponent parent)
- {
+ private void includePluginFacelet(String macroName, FaceletContext ctx, UIComponent parent) {
+ if (macroName == null || macroName.length() == 0 || includedMacros.contains(macroName)) return;
+
+ String includeView = "/plugins/" + macroName + "/plugin.xhtml";
+
+ // View can't include itself
+ String currentViewId = ctx.getFacesContext().getViewRoot().getViewId();
+ if (currentViewId.equals(includeView)) return;
+
+ // Try to get the XHTML document
+ URL includeViewURL = Resources.getResource(includeView);
+ if (includeViewURL == null) return;
+
+ includePluginCSS(macroName, parent);
+
// Cribbed from facelets
VariableMapper orig = ctx.getVariableMapper();
+ try {
ctx.setVariableMapper(new VariableMapperWrapper(orig));
- try
- {
- ctx.includeFacelet(parent, path);
- }
- catch (IOException e)
- {
+ ctx.includeFacelet(parent, includeViewURL);
+ } catch (IOException e) {
throw new RuntimeException(e);
- }
- finally
- {
+ } finally {
ctx.setVariableMapper(orig);
- }
+ includedMacros.add(macroName);
}
- private void buildMacro(String macroName, FaceletContext ctx, UIComponent parent)
- {
- if (macroName == null || macroName.length() == 0 || includedMacros.contains(macroName))
- {
- return;
+ createPreferencesEditor(macroName);
}
- addCss(macroName, parent);
-
- String includeView = "/plugins/" + macroName + "/plugin.xhtml";
- // View can't include itself
- String currentViewId = ctx.getFacesContext().getViewRoot().getViewId();
- if (currentViewId.equals(includeView))
- {
- return;
- }
- // Try to get the XHTML document
- URL includeViewURL = Resources.getResource(includeView);
- if (includeViewURL == null)
- {
- return;
- }
- try
- {
- include(includeViewURL, ctx, parent);
- }
- finally
- {
- includedMacros.add(macroName);
+ /*
+ * Add a CSS resource to the document head
+ */
+ private void includePluginCSS(String macroName, UIComponent cmp) {
+ // Try to get the CSS for it
+ WikiPreferences wikiPrefs = (WikiPreferences) Component.getInstance("wikiPreferences");
+ String css = "/themes/" + wikiPrefs.getThemeName() + "/css/" + macroName
+ + ".css";
+ if (Resources.getResource(css) != null) {
+ UILoadStyle style = new UILoadStyle();
+ style.setSrc(css);
+ cmp.getChildren().add(style);
+ // Clear these out in the next build phase
+ ComponentSupport.markForDeletion(style);
}
- createPreferencesEditor(macroName);
}
- private void createPreferencesEditor(String macroName)
- {
+ private void createPreferencesEditor(String macroName) {
// If this plugin has preferences and editing is enabled, instantiate a
// plugin preferences editor and put it in the PAGE context
@@ -214,44 +200,22 @@
Boolean showPluginPreferences = (Boolean) Component.getInstance("showPluginPreferences");
Object existingEditor = Contexts.getConversationContext()
.get(pluginPreferenceName + "Editor");
- if (showPluginPreferences != null && showPluginPreferences && existingEditor == null)
- {
+ if (showPluginPreferences != null && showPluginPreferences && existingEditor == null) {
PluginPreferenceEditor pluginPreferenceEditor = new PluginPreferenceEditor(
pluginPreferenceName);
PluginPreferenceEditor.FlushObserver observer = (PluginPreferenceEditor.FlushObserver) Component
.getInstance("pluginPreferenceEditorFlushObserver");
- if (pluginPreferenceEditor.getPreferenceValues().size() > 0)
- {
+ if (pluginPreferenceEditor.getPreferenceValues().size() > 0) {
Contexts.getConversationContext().set(pluginPreferenceName + "Editor",
pluginPreferenceEditor);
observer.addPluginPreferenceEditor(pluginPreferenceEditor);
}
- }
- else if (showPluginPreferences == null || !showPluginPreferences)
- {
+ } else if (showPluginPreferences == null || !showPluginPreferences) {
Contexts.getConversationContext().set(pluginPreferenceName + "Editor", null);
}
}
- /*
- * Add a CSS resource to the document head
- */
- private void addCss(String macroName, UIComponent cmp)
- {
- // Try to get the CSS for it
- WikiPreferences wikiPrefs = (WikiPreferences) Component.getInstance("wikiPreferences");
- String css = "/themes/" + wikiPrefs.getThemeName() + "/css/" + macroName
- + ".css";
- if (Resources.getResource(css) != null)
- {
- UILoadStyle style = new UILoadStyle();
- style.setSrc(css);
- cmp.getChildren().add(style);
- // Clear these out in the next build phase
- ComponentSupport.markForDeletion(style);
- }
- }
/*
* Support method to find the UIWikiFormattedText component created by
1.17 +148 -158 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/UIWikiFormattedText.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: UIWikiFormattedText.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/UIWikiFormattedText.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- UIWikiFormattedText.java 17 Apr 2007 15:16:54 -0000 1.16
+++ UIWikiFormattedText.java 18 Apr 2007 15:34:28 -0000 1.17
@@ -20,8 +20,7 @@
import org.jboss.seam.wiki.core.model.File;
import org.jboss.seam.wiki.util.WikiUtil;
-public class UIWikiFormattedText extends UIOutput
-{
+public class UIWikiFormattedText extends UIOutput {
private List<String> plugins;
@@ -29,44 +28,37 @@
public static final String COMPONENT_TYPE = "org.jboss.seam.wiki.core.ui.UIWikiFormattedText";
- public UIWikiFormattedText()
- {
+ public UIWikiFormattedText() {
super();
plugins = new ArrayList<String>();
}
@Override
- public String getFamily()
- {
+ public String getFamily() {
return COMPONENT_FAMILY;
}
@Override
- public boolean getRendersChildren()
- {
+ public boolean getRendersChildren() {
return true;
}
@Override
- public String getRendererType()
- {
+ public String getRendererType() {
return null;
}
@Override
- public void encodeBegin(FacesContext facesContext) throws IOException
- {
+ public void encodeBegin(FacesContext facesContext) throws IOException {
if (!isRendered() || getValue() == null) return;
// Use the WikiTextParser to resolve macros
WikiTextParser parser = new WikiTextParser((String) getValue(), false);
// Set a customized renderer for parser macro callbacks
- parser.setRenderer(new WikiTextRenderer()
- {
+ parser.setRenderer(new WikiTextRenderer() {
- public String renderInlineLink(WikiLink inlineLink)
- {
+ public String renderInlineLink(WikiLink inlineLink) {
return "<a href=\""
+ (inlineLink.isBroken() ? inlineLink.getUrl() : WikiUtil.renderURL(inlineLink
.getNode()))
@@ -76,8 +68,7 @@
+ inlineLink.getDescription() + "</a>";
}
- public String renderExternalLink(WikiLink externalLink)
- {
+ public String renderExternalLink(WikiLink externalLink) {
return "<a href=\""
+ externalLink.getUrl()
+ "\" class=\""
@@ -86,28 +77,25 @@
+ externalLink.getDescription() + "</a>";
}
- public String renderFileAttachmentLink(int attachmentNumber, WikiLink attachmentLink)
- {
+ public String renderFileAttachmentLink(int attachmentNumber, WikiLink attachmentLink) {
return "<a href=\"#attachment" + attachmentNumber + "\" class=\""
+ getAttributes().get("attachmentLinkStyleClass") + "\">"
+ attachmentLink.getDescription() + "[" + attachmentNumber + "]" + "</a>";
}
- public String renderThumbnailImageInlineLink(WikiLink inlineLink)
- {
+ public String renderThumbnailImageInlineLink(WikiLink inlineLink) {
File file = (File) inlineLink.getNode();
int thumbnailWidth;
// TODO: We could make these sizes customizable, maybe as attributes
// of the JSF tag
- switch (file.getImageMetaInfo().getThumbnail())
- {
- case 'S':
+ switch (file.getImageMetaInfo().getThumbnail()) {
+ case'S':
thumbnailWidth = 80;
break;
- case 'M':
+ case'M':
thumbnailWidth = 160;
break;
- case 'L':
+ case'L':
thumbnailWidth = 320;
break;
default:
@@ -127,38 +115,41 @@
+ thumbnailUrl + "\"/></a>";
}
- public String renderMacro(String macroName)
- {
+ public String renderMacro(String macroName) {
+ if (macroName == null || macroName.length() == 0) return "";
+ try {
+ new Integer(macroName);
+ } catch (NumberFormatException ex) {
+ // This is the name of a not-found plugin, otherwise
+ // we'd have a numeric client identifier of the
+ // included plugin component
+ return "";
+ }
UIComponent child = findComponent(plugins.get(new Integer(macroName)));
ResponseWriter originalResponseWriter = getFacesContext().getResponseWriter();
StringWriter stringWriter = new StringWriter();
ResponseWriter tempResponseWriter = originalResponseWriter
.cloneWithWriter(stringWriter);
getFacesContext().setResponseWriter(tempResponseWriter);
- try
- {
+ try {
JSF.renderChild(getFacesContext(), child);
}
- catch (Exception ex)
- {
+ catch (Exception ex) {
throw new RuntimeException(ex);
}
- finally
- {
+ finally {
getFacesContext().setResponseWriter(originalResponseWriter);
}
return stringWriter.getBuffer().toString();
}
- public void setAttachmentLinks(List<WikiLink> attachmentLinks)
- {
+ public void setAttachmentLinks(List<WikiLink> attachmentLinks) {
// Put attachments (wiki links...) into the event context for later
// rendering
Contexts.getEventContext().set("wikiTextAttachments", attachmentLinks);
}
- public void setExternalLinks(List<WikiLink> externalLinks)
- {
+ public void setExternalLinks(List<WikiLink> externalLinks) {
// Put external links (to targets not on this wiki) into the event
// context for later rendering
Contexts.getEventContext().set("wikiTextExternalLinks", externalLinks);
@@ -172,8 +163,7 @@
}
- protected String addPlugin(String clientId)
- {
+ protected String addPlugin(String clientId) {
plugins.add(clientId);
return (plugins.size() - 1) + "";
}
More information about the jboss-cvs-commits
mailing list