[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui ...
Christian Bauer
christian at hibernate.org
Mon Mar 19 00:53:53 EDT 2007
User: cbauer
Date: 07/03/19 00:53:53
Modified: examples/wiki/src/main/org/jboss/seam/wiki/core/ui
WikiTextParser.java UIWikiFormattedText.java
Log:
Basic in-document plugin system working
Revision Changes Path
1.3 +13 -3 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/WikiTextParser.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: WikiTextParser.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/WikiTextParser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- WikiTextParser.java 8 Mar 2007 10:44:15 -0000 1.2
+++ WikiTextParser.java 19 Mar 2007 04:53:53 -0000 1.3
@@ -4,7 +4,6 @@
import org.jboss.seam.core.Expressions;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.wiki.core.model.File;
-import org.jboss.seam.wiki.core.links.WikiLinkResolver;
import antlr.TokenStream;
import java.util.Map;
@@ -12,7 +11,6 @@
import java.util.List;
import java.util.HashMap;
-
public class WikiTextParser extends SeamTextParser {
private String linkClass;
@@ -20,17 +18,22 @@
private String attachmentLinkClass;
private String inlineLinkClass;
+ private UIWikiFormattedText textComponent;
+
private List<WikiLink> attachments = new ArrayList<WikiLink>();
private List<WikiLink> externalLinks = new ArrayList<WikiLink>();
private Map<String, WikiLink> links = new HashMap<String, WikiLink>();
+ // TODO: Refactor to avoid callback to the UI component
public WikiTextParser(TokenStream tokenStream,
- String linkClass, String brokenLinkClass, String attachmentLinkClass, String inlineLinkClass) {
+ String linkClass, String brokenLinkClass, String attachmentLinkClass, String inlineLinkClass,
+ UIWikiFormattedText textComponent) {
super(tokenStream);
this.linkClass = linkClass;
this.brokenLinkClass = brokenLinkClass;
this.attachmentLinkClass = attachmentLinkClass;
this.inlineLinkClass = inlineLinkClass;
+ this.textComponent = textComponent;
}
protected String linkTag(String descriptionText, String linkText) {
@@ -96,6 +99,11 @@
+ "</a>";
}
+ protected String macroInclude(String macroName) {
+ // Filter out any dangerous characters
+ return textComponent.renderMacro(macroName.replaceAll("[^\\p{Alnum}]+", ""));
+ }
+
public List<WikiLink> getAttachments() {
return attachments;
}
@@ -103,4 +111,6 @@
public List<WikiLink> getExternalLinks() {
return externalLinks;
}
+
+
}
1.5 +72 -4 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.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- UIWikiFormattedText.java 9 Mar 2007 08:02:09 -0000 1.4
+++ UIWikiFormattedText.java 19 Mar 2007 04:53:53 -0000 1.5
@@ -3,26 +3,43 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
+import java.io.StringWriter;
+import java.net.URL;
+import java.util.*;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
-import javax.persistence.EntityManager;
+import javax.faces.context.ResponseWriter;
import org.jboss.seam.text.SeamTextLexer;
import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.util.Resources;
+import org.jboss.seam.ui.JSF;
import antlr.ANTLRException;
+import com.sun.facelets.Facelet;
+import com.sun.facelets.impl.DefaultFaceletFactory;
+import com.sun.facelets.impl.DefaultResourceResolver;
+import com.sun.facelets.compiler.SAXCompiler;
public class UIWikiFormattedText extends UIOutput {
+
public static final String COMPONENT_FAMILY = "org.jboss.seam.wiki.core.ui.WikiFormattedText";
+ private Set<String> includedViews = new HashSet<String>();
+
@Override
public String getFamily() {
return COMPONENT_FAMILY;
}
@Override
- public void encodeBegin(FacesContext context) throws IOException {
+ public boolean getRendersChildren() {
+ return true;
+ }
+
+ @Override
+ public void encodeBegin(FacesContext facesContext) throws IOException {
if (!isRendered() || getValue() == null) return;
Reader r = new StringReader((String) getValue());
@@ -36,7 +53,8 @@
getAttributes().get("linkStyleClass").toString(),
getAttributes().get("brokenLinkStyleClass").toString(),
getAttributes().get("attachmentLinkStyleClass").toString(),
- getAttributes().get("inlineLinkStyleClass").toString()
+ getAttributes().get("inlineLinkStyleClass").toString(),
+ this
);
try {
@@ -46,10 +64,60 @@
throw new RuntimeException(re);
}
- context.getResponseWriter().write(parser.toString());
+ facesContext.getResponseWriter().write(parser.toString());
// Put attachments (wiki links...) into the event context for later rendering
Contexts.getEventContext().set("wikiTextAttachments", parser.getAttachments());
}
+ String renderMacro(String macroName) {
+ if (macroName == null || macroName.length() == 0) return "";
+
+ String includeView = "/plugins/" + macroName + "/plugin.xhtml";
+
+ // TODO: Can only include once (otherwise we'd have to renumber child identifiers recursively...)
+ if (includedViews.contains(includeView)) return "[Can't use the same plugin twice!]";
+
+ // View can't include itself
+ FacesContext facesContext = getFacesContext();
+ if (facesContext.getViewRoot().getViewId().equals(includeView)) return "";
+
+ // Try to get the XHTML document
+ URL url = Resources.getResource(includeView);
+ if (url == null) return "";
+
+ ResponseWriter originalResponseWriter = facesContext.getResponseWriter();
+ StringWriter stringWriter = new StringWriter();
+ ResponseWriter tempResponseWriter = originalResponseWriter.cloneWithWriter(stringWriter);
+ facesContext.setResponseWriter(tempResponseWriter);
+
+ String output;
+ try {
+ Facelet f = new DefaultFaceletFactory(new SAXCompiler(), new DefaultResourceResolver()).getFacelet(url);
+
+ // TODO: I'm not sure this is good...
+ List storedChildren = new ArrayList(getChildren());
+ getChildren().clear();
+
+ // TODO: This is why I copy the list back and forth: apply() hammers the children
+ f.apply(facesContext, this);
+ JSF.renderChildren(facesContext, this);
+
+ // TODO: And back... it's definitely in the wrong order in the component tree but the ids look ok to me...
+ getChildren().addAll(storedChildren);
+
+ output = stringWriter.getBuffer().toString();
+ } catch (Exception ex) {
+ throw new RuntimeException(ex);
+ } finally {
+ includedViews.add(includeView);
+ facesContext.setResponseWriter(originalResponseWriter);
+ }
+ return output;
+ }
+
+
+ public void encodeChildren(FacesContext facesContext) throws IOException {
+ // Already done
+ }
}
More information about the jboss-cvs-commits
mailing list