[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui ...

Peter Muir peter at bleepbleep.org.uk
Wed Apr 11 11:53:48 EDT 2007


  User: pmuir   
  Date: 07/04/11 11:53:48

  Modified:    examples/wiki/src/main/org/jboss/seam/wiki/core/ui     
                        UIWikiFormattedText.java
  Added:       examples/wiki/src/main/org/jboss/seam/wiki/core/ui     
                        UIPlugin.java UIStyle.java
                        WikiFormattedTextHandler.java
  Removed:     examples/wiki/src/main/org/jboss/seam/wiki/core/ui     
                        WikiFormattedTextComponentHandler.java
  Log:
  Fix wiki plugin jsf lifecycle stuff
  
  Revision  Changes    Path
  1.15      +7 -89     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.14
  retrieving revision 1.15
  diff -u -b -r1.14 -r1.15
  --- UIWikiFormattedText.java	10 Apr 2007 08:08:11 -0000	1.14
  +++ UIWikiFormattedText.java	11 Apr 2007 15:53:48 -0000	1.15
  @@ -1,27 +1,19 @@
   package org.jboss.seam.wiki.core.ui;
   
  -import java.util.*;
  -import java.util.regex.Pattern;
  -import java.util.regex.Matcher;
  -import java.io.*;
  +import java.io.IOException;
  +import java.util.List;
   
   import javax.faces.component.UIOutput;
  -import javax.faces.component.UIComponent;
   import javax.faces.context.FacesContext;
  -import javax.faces.context.ResponseWriter;
   
  +import org.jboss.seam.Component;
   import org.jboss.seam.contexts.Contexts;
  -import org.jboss.seam.util.Resources;
  -import org.jboss.seam.ui.JSF;
  -import org.jboss.seam.wiki.core.model.File;
  +import org.jboss.seam.core.Conversation;
   import org.jboss.seam.wiki.core.engine.WikiLink;
  -import org.jboss.seam.wiki.core.engine.WikiTextRenderer;
   import org.jboss.seam.wiki.core.engine.WikiTextParser;
  -import org.jboss.seam.wiki.core.action.prefs.WikiPreferences;
  +import org.jboss.seam.wiki.core.engine.WikiTextRenderer;
  +import org.jboss.seam.wiki.core.model.File;
   import org.jboss.seam.wiki.util.WikiUtil;
  -import org.jboss.seam.Component;
  -import org.jboss.seam.core.Expressions;
  -import org.jboss.seam.core.Conversation;
   
   public class UIWikiFormattedText extends UIOutput {
   
  @@ -37,14 +29,6 @@
           return COMPONENT_FAMILY;
       }
   
  -    public boolean getRendersChildren() {
  -        return true;
  -    }
  -
  -    public void encodeChildren(FacesContext facesContext) throws IOException {
  -        // Already done by WikiTextRenderer
  -    }
  -
       public void encodeBegin(FacesContext facesContext) throws IOException {
           if (!isRendered() || getValue() == null) return;
   
  @@ -114,73 +98,7 @@
                   }
   
                   public String renderMacro(String macroName) {
  -                    if (macroName == null || macroName.length() == 0) return "";
  -
  -                    // Try to get the CSS for it
  -                    WikiPreferences wikiPrefs = (WikiPreferences) Component.getInstance("wikiPreferences");
  -                    String includeViewCSS = "/themes/" + wikiPrefs.getThemeName() + "/css/" + macroName + ".css";
  -
  -                    // Prepare all the writers for rendering
  -                    ResponseWriter originalResponseWriter = getFacesContext().getResponseWriter();
  -                    StringWriter stringWriter = new StringWriter();
  -                    ResponseWriter tempResponseWriter = originalResponseWriter.cloneWithWriter(stringWriter);
  -                    getFacesContext().setResponseWriter(tempResponseWriter);
  -
  -                    StringBuilder output = new StringBuilder();
  -
  -                    try {
  -                        // Render CSS
  -                        InputStream is = Resources.getResourceAsStream(includeViewCSS);
  -                        if (is != null) {
  -                            output.append("<style type=\"text/css\">\n");
  -
  -                            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  -                            StringBuilder css = new StringBuilder();
  -                            String line;
  -                            while ((line = reader.readLine()) != null) {
  -                                css.append(line);
  -                                css.append("\n");
  -                            }
  -                            is.close();
  -
  -                            // Resolve any EL value binding expression present in CSS text
  -                            StringBuffer resolvedCSS = new StringBuffer(css.length());
  -                            Matcher matcher =
  -                                    Pattern.compile(
  -                                            "#" + Pattern.quote("{") + "(.*)" + Pattern.quote("}")
  -                                    ).matcher(css);
  -
  -                            // Replace with [Link Text=>Page Name] or replace with BROKENLINK "page name"
  -                            while (matcher.find()) {
  -                                Expressions.ValueBinding valueMethod = Expressions.instance().createValueBinding("#{" + matcher.group(1) + "}");
  -                                String result = (String) valueMethod.getValue();
  -                                if (result != null) {
  -                                    matcher.appendReplacement(resolvedCSS, result);
  -                                } else {
  -                                    matcher.appendReplacement(resolvedCSS, "");
  -                                }
  -                            }
  -                            matcher.appendTail(resolvedCSS);
  -                            output.append(resolvedCSS);
  -
  -                            output.append("</style>\n");
  -                        }
  -
  -                        // Render the actual child component - the plugin XHTML
  -                        UIComponent pluginChild = findComponent(macroName);
  -                        if (pluginChild == null) return ""; // Swallow it
  -                        pluginChild.encodeBegin(getFacesContext());
  -                        JSF.renderChildren(getFacesContext(), pluginChild);
  -                        pluginChild.encodeEnd(getFacesContext());
  -
  -                        output.append(stringWriter.getBuffer().toString());
  -
  -                    } catch (Exception ex) {
  -                        throw new RuntimeException(ex);
  -                    } finally {
  -                        getFacesContext().setResponseWriter(originalResponseWriter);
  -                    }
  -                    return output.toString();
  +                    return "";
                   }
   
                   public void setAttachmentLinks(List<WikiLink> attachmentLinks) {
  
  
  
  1.1      date: 2007/04/11 15:53:48;  author: pmuir;  state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/UIPlugin.java
  
  Index: UIPlugin.java
  ===================================================================
  package org.jboss.seam.wiki.core.ui;
  
  import javax.faces.component.UINamingContainer;
  
  public class UIPlugin extends UINamingContainer
  {
     
     public static final String COMPONENT_FAMILY = "org.jboss.seam.wiki.core.ui.UIPlugin";
  
     @Override
     public String getFamily()
     {
        return COMPONENT_FAMILY;
     }
     
  }
  
  
  
  1.1      date: 2007/04/11 15:53:48;  author: pmuir;  state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/UIStyle.java
  
  Index: UIStyle.java
  ===================================================================
  package org.jboss.seam.wiki.core.ui;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.URL;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import javax.faces.component.UIComponentBase;
  import javax.faces.context.FacesContext;
  import javax.faces.context.ResponseWriter;
  
  import org.jboss.seam.core.Expressions;
  
  public class UIStyle extends UIComponentBase
  {
  
     public static final String COMPONENT_FAMILY = "org.jboss.seam.wiki.ui.UIStyle";
  
     private URL path;
  
     @Override
     public String getFamily()
     {
        return COMPONENT_FAMILY;
     }
  
     public URL getPath()
     {
        return path;
     }
  
     public void setPath(URL path)
     {
        this.path = path;
     }
  
     // Would be nicer to use a A4J HeaderResourceRenderer for this, but
     // need to rework EL binding stuff if so.
     @Override
     public void encodeBegin(FacesContext context) throws IOException
     {
        ResponseWriter output = context.getResponseWriter();
        // Render CSS
        InputStream is = path.openStream();
        if (is != null)
        {
           output.append("<style type=\"text/css\">\n");
  
           BufferedReader reader = new BufferedReader(new InputStreamReader(is));
           StringBuilder css = new StringBuilder();
           String line;
           while ((line = reader.readLine()) != null)
           {
              css.append(line);
              css.append("\n");
           }
           is.close();
  
           // Resolve any EL value binding expression present in CSS text
           StringBuffer resolvedCSS = new StringBuffer(css.length());
           Matcher matcher = Pattern.compile("#" + Pattern.quote("{") + "(.*)" + Pattern.quote("}"))
                    .matcher(css);
  
           // Replace with [Link Text=>Page Name] or replace with BROKENLINK "page
           // name"
           while (matcher.find())
           {
              Expressions.ValueBinding valueMethod = Expressions.instance().createValueBinding(
                       "#{" + matcher.group(1) + "}");
              String result = (String) valueMethod.getValue();
              if (result != null)
              {
                 matcher.appendReplacement(resolvedCSS, result);
              }
              else
              {
                 matcher.appendReplacement(resolvedCSS, "");
              }
           }
           matcher.appendTail(resolvedCSS);
           output.append(resolvedCSS);
  
           output.append("</style>\n");
        }
     }
     
     @Override
     public Object saveState(FacesContext arg0)
     {
        Object[] state = new Object[2];
        state[0] = super.saveState(arg0);
        state[1] = path;
        return state;
     }
     
     @Override
     public void restoreState(FacesContext arg0, Object arg1)
     {
        Object[] state = (Object[]) arg1;
        super.restoreState(arg0, state[0]);
        path = (URL) state[1];
     }
  
  }
  
  
  
  1.1      date: 2007/04/11 15:53:48;  author: pmuir;  state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/WikiFormattedTextHandler.java
  
  Index: WikiFormattedTextHandler.java
  ===================================================================
  package org.jboss.seam.wiki.core.ui;
  
  import java.io.IOException;
  import java.net.URL;
  import java.util.HashSet;
  import java.util.Set;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import javax.el.ELException;
  import javax.el.VariableMapper;
  import javax.faces.FacesException;
  import javax.faces.component.UIComponent;
  
  import org.jboss.seam.Component;
  import org.jboss.seam.contexts.Contexts;
  import org.jboss.seam.util.Resources;
  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.TagAttribute;
  import com.sun.facelets.tag.TagConfig;
  import com.sun.facelets.tag.TagHandler;
  import com.sun.facelets.tag.jsf.ComponentSupport;
  
  public class WikiFormattedTextHandler extends TagHandler
  {
  
     public static final String REGEX_MACRO = Pattern.quote("[") + "<=([a-zA-Z0-9]+)"
              + Pattern.quote("]");
  
     private TagAttribute value;
  
     private Set<String> includedMacros;
  
     public WikiFormattedTextHandler(TagConfig config)
     {
        super(config);
        this.value = this.getRequiredAttribute("value");
     }
  
     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException,
              FaceletException, ELException
     {
        includedMacros = new HashSet<String>();
        String unparsed = value.getValue(ctx);
        Matcher matcher = Pattern.compile(REGEX_MACRO).matcher(unparsed);
        int start = 0;
        this.nextHandler.apply(ctx, null);
        while (matcher.find())
        {
           if (ComponentSupport.isNew(parent))
           {
              // Include the string before the match
              UIWikiFormattedText wikiFormattedText = new UIWikiFormattedText();
              String text = unparsed.substring(start, matcher.start());
              start = matcher.end();
              wikiFormattedText.setValue(text);
              parent.getChildren().add(wikiFormattedText);
           }
           // Include the plugin
           String macroName = matcher.group(1);
           buildMacro(macroName, ctx, parent);
        }
        if (ComponentSupport.isNew(parent))
        {
           // Then include the end of the string
           UIWikiFormattedText endText = new UIWikiFormattedText();
           endText.setValue(unparsed.substring(start));
           parent.getChildren().add(endText);
        }
     }
  
     private void include(URL path, FaceletContext ctx, UIComponent parent) throws IOException, FaceletException,
              FacesException, ELException, IOException
     {
        // Cribbed from facelets
        VariableMapper orig = ctx.getVariableMapper();
        ctx.setVariableMapper(new VariableMapperWrapper(orig));
        try
        {
           ctx.includeFacelet(parent, path);
        }
        finally
        {
           ctx.setVariableMapper(orig);
        }
     }
  
     private void buildMacro(String macroName, FaceletContext ctx, UIComponent parent) throws IOException, FaceletException, FacesException, ELException, IOException
     {
        if (macroName == null || macroName.length() == 0 || includedMacros.contains(macroName))
        {
           return;
        }
  
        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);
        }
        createPreferencesEditor(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
        String pluginPreferenceName = macroName + "Preferences";
        Boolean showPluginPreferences = (Boolean) Component.getInstance("showPluginPreferences");
        Object existingEditor = Contexts.getConversationContext()
                 .get(pluginPreferenceName + "Editor");
        if (showPluginPreferences != null && showPluginPreferences && existingEditor == null)
        {
           PluginPreferenceEditor pluginPreferenceEditor = new PluginPreferenceEditor(
                    pluginPreferenceName);
           PluginPreferenceEditor.FlushObserver observer = (PluginPreferenceEditor.FlushObserver) Component
                    .getInstance("pluginPreferenceEditorFlushObserver");
           if (pluginPreferenceEditor.getPreferenceValues().size() > 0)
           {
              Contexts.getConversationContext().set(pluginPreferenceName + "Editor",
                       pluginPreferenceEditor);
              observer.addPluginPreferenceEditor(pluginPreferenceEditor);
           }
        }
        else if (showPluginPreferences == null || !showPluginPreferences)
        {
           Contexts.getConversationContext().set(pluginPreferenceName + "Editor", null);
        }
  
     }
     
     private void addCss(String macroName, UIComponent parent) 
     {
        // Try to get the CSS for it
        WikiPreferences wikiPrefs = (WikiPreferences) Component.getInstance("wikiPreferences");
        URL css = Resources.getResource("/themes/" + wikiPrefs.getThemeName() + "/css/" + macroName + ".css");
        if (css != null)
        {
           UIStyle style = new UIStyle();
           style.setPath(css);
           parent.getChildren().add(style);
        }
     }
  
  }
  
  



More information about the jboss-cvs-commits mailing list