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

Christian Bauer christian at hibernate.org
Sat Dec 29 21:33:26 EST 2007


  User: cbauer  
  Date: 07/12/29 21:33:26

  Modified:    examples/wiki/src/main/org/jboss/seam/wiki/core/ui    
                        FeedServlet.java WikiFormattedTextHandler.java
                        Converters.java UIWikiFormattedText.java
  Log:
  Complete overhaul of the preferences system
  
  Revision  Changes    Path
  1.13      +32 -19    jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/FeedServlet.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: FeedServlet.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/FeedServlet.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -b -r1.12 -r1.13
  --- FeedServlet.java	19 Dec 2007 04:29:22 -0000	1.12
  +++ FeedServlet.java	30 Dec 2007 02:33:26 -0000	1.13
  @@ -9,12 +9,14 @@
   import com.sun.syndication.feed.synd.*;
   import com.sun.syndication.io.SyndFeedOutput;
   import org.jboss.seam.Component;
  +import org.jboss.seam.international.Messages;
   import org.jboss.seam.wiki.core.feeds.FeedDAO;
   import org.jboss.seam.wiki.core.model.Feed;
   import org.jboss.seam.wiki.core.model.FeedEntry;
   import org.jboss.seam.wiki.core.action.Authenticator;
   import org.jboss.seam.wiki.core.action.prefs.WikiPreferences;
   import org.jboss.seam.wiki.util.WikiUtil;
  +import org.jboss.seam.wiki.preferences.Preferences;
   
   import javax.servlet.ServletException;
   import javax.servlet.http.HttpServlet;
  @@ -67,6 +69,7 @@
   
           String pathInfo = request.getPathInfo();
           String feedId = request.getParameter("feedId");
  +        String tag = request.getParameter("tag");
   
           if (!feedTypes.containsKey(pathInfo)) return;
           SyndFeedType syndFeedType = feedTypes.get(pathInfo);
  @@ -103,7 +106,7 @@
                   }
               }
   
  -            SyndFeed syndFeed = createSyndFeed(request.getRequestURL().toString(), syndFeedType,  feed, currentAccessLevel);
  +            SyndFeed syndFeed = createSyndFeed(request.getRequestURL().toString(), syndFeedType,  feed, currentAccessLevel, tag);
   
               // Write feed to output
               response.setContentType(syndFeedType.contentType);
  @@ -125,14 +128,23 @@
       }
   
       public SyndFeed createSyndFeed(String baseURI, SyndFeedType syndFeedType, Feed feed, Integer currentAccessLevel) {
  +        return createSyndFeed(baseURI, syndFeedType, feed, currentAccessLevel, null);
  +    }
  +
  +    public SyndFeed createSyndFeed(String baseURI, SyndFeedType syndFeedType, Feed feed, Integer currentAccessLevel, String tag) {
   
  -        WikiPreferences prefs = (WikiPreferences)Component.getInstance("wikiPreferences");
  +        WikiPreferences prefs = (WikiPreferences) Preferences.getInstance("Wiki");
   
           // Create feed
           SyndFeed syndFeed = new SyndFeedImpl();
           syndFeed.setUri(baseURI + "?feedId=" + feed.getId());
           syndFeed.setFeedType(syndFeedType.feedType);
           syndFeed.setTitle(prefs.getFeedTitlePrefix() + feed.getTitle());
  +        if (tag != null) {
  +            syndFeed.setTitle(
  +                syndFeed.getTitle() + " (" + Messages.instance().get("lacewiki.label.tagDisplay.Tag") + " '" + tag + "')"
  +            );
  +        }
           syndFeed.setLink(WikiUtil.renderURL(feed.getDirectory()));
           syndFeed.setAuthor(feed.getAuthor());
           if (feed.getDescription() != null && feed.getDescription().length() >0)
  @@ -143,7 +155,9 @@
           List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
           SortedSet<FeedEntry> entries = feed.getFeedEntries();
           for (FeedEntry entry : entries) {
  -            if (entry.getReadAccessLevel() <= currentAccessLevel) {
  +            if (entry.getReadAccessLevel() > currentAccessLevel) continue;
  +            if (tag != null && !entry.isTagged(tag)) continue;
  +
                   SyndEntry syndEntry;
                   syndEntry = new SyndEntryImpl();
                   syndEntry.setTitle(entry.getTitle());
  @@ -161,7 +175,6 @@
   
                   syndEntries.add(syndEntry);
               }
  -        }
           syndFeed.setEntries(syndEntries);
   
           return syndFeed;
  
  
  
  1.18      +74 -92    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.17
  retrieving revision 1.18
  diff -u -b -r1.17 -r1.18
  --- WikiFormattedTextHandler.java	20 Dec 2007 18:35:54 -0000	1.17
  +++ WikiFormattedTextHandler.java	30 Dec 2007 02:33:26 -0000	1.18
  @@ -6,28 +6,8 @@
    */
   package org.jboss.seam.wiki.core.ui;
   
  -import java.io.IOException;
  -import java.net.URL;
  -import java.util.HashSet;
  -import java.util.Iterator;
  -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.log.Logging;
  -import org.jboss.seam.log.Log;
  -import org.jboss.seam.faces.ResourceLoader;
  -import org.jboss.seam.contexts.Contexts;
  -import org.jboss.seam.ui.component.UILoadStyle;
  -import org.jboss.seam.wiki.core.action.PluginPreferenceEditor;
  -import org.jboss.seam.wiki.core.action.prefs.WikiPreferences;
  -
  +import antlr.ANTLRException;
  +import antlr.RecognitionException;
   import com.sun.facelets.FaceletContext;
   import com.sun.facelets.el.VariableMapperWrapper;
   import com.sun.facelets.tag.MetaRuleset;
  @@ -35,6 +15,24 @@
   import com.sun.facelets.tag.TagAttribute;
   import com.sun.facelets.tag.TagConfig;
   import com.sun.facelets.tag.jsf.ComponentSupport;
  +import org.jboss.seam.contexts.Contexts;
  +import org.jboss.seam.faces.ResourceLoader;
  +import org.jboss.seam.log.Log;
  +import org.jboss.seam.log.Logging;
  +import org.jboss.seam.ui.component.UILoadStyle;
  +import org.jboss.seam.wiki.core.action.prefs.WikiPreferences;
  +import org.jboss.seam.wiki.core.engine.NullWikiTextRenderer;
  +import org.jboss.seam.wiki.core.engine.WikiMacro;
  +import org.jboss.seam.wiki.core.engine.WikiTextParser;
  +import org.jboss.seam.wiki.preferences.Preferences;
  +
  +import javax.el.ELException;
  +import javax.el.VariableMapper;
  +import javax.faces.FacesException;
  +import javax.faces.component.UIComponent;
  +import java.io.IOException;
  +import java.net.URL;
  +import java.util.Iterator;
   
   /**
    * Creates a UIWikiText JSF component and substitutes macro names in wiki
  @@ -49,13 +47,8 @@
   
       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("]");
  -
       private TagAttribute valueAttribute;
   
  -    private Set<String> includedMacros;
  -
       public WikiFormattedTextHandler(TagConfig config) {
           super(config);
           this.valueAttribute = this.getRequiredAttribute("value");
  @@ -65,7 +58,7 @@
       * Main apply method called by facelets to create this component.
       */
       public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException {
  -        includedMacros = new HashSet<String>();
  +        log.debug("<<< building wiki text components");
           String id = ctx.generateUniqueId(this.tagId);
           UIComponent cmp = findChildByTagId(parent, id);
           if (cmp == null) {
  @@ -130,56 +123,73 @@
        * @param ctx FaceletContext
        * @param parent Parent component
        */
  -    private void createPlugins(FaceletContext ctx, UIComponent parent) {
  +    private void createPlugins(final FaceletContext ctx, final UIComponent parent) {
           if (!(parent instanceof UIWikiFormattedText)) return;
  -        UIWikiFormattedText wikiFormattedText = (UIWikiFormattedText) parent;
  +        final UIWikiFormattedText wikiFormattedText = (UIWikiFormattedText) parent;
   
           String unparsed = valueAttribute.getValue(ctx);
   
  +        // Don't forget this, transporting the value to the handled component
  +        wikiFormattedText.setValue(unparsed);
  +
           if (getAttribute(UIWikiFormattedText.ATTR_ENABLE_PLUGINS) == null ||
               !getAttribute(UIWikiFormattedText.ATTR_ENABLE_PLUGINS).getBoolean(ctx)) {
  -            wikiFormattedText.setValue(unparsed);
  +            log.debug("plugin rendering disabled");
               return;
           }
   
  -        Matcher matcher = Pattern.compile(REGEX_MACRO).matcher(unparsed);
  -        StringBuffer parsed = new StringBuffer();
  -        while (matcher.find()) {
  -
  -            // Include the plugin
  -            String macroName = matcher.group(1);
  -
  -            URL faceletURL = getPluginURL(macroName, ctx);
  -            if (faceletURL != null) {
  -                includePluginCSS(macroName, parent);
  +        log.debug("<<< creating plugin components from wiki text macros");
  +
  +        WikiTextParser parser = new WikiTextParser(unparsed, true, false);
  +        parser.setRenderer(
  +            new NullWikiTextRenderer() {
  +                public String renderMacro(WikiMacro macro) {
  +                    log.debug("found macro: " + macro);
  +
  +                    URL faceletURL = getPluginURL(macro.getName(), ctx);
  +                    if (faceletURL == null) return null;
  +
  +                    log.debug("setting current macro in EVENT context");
  +                    Contexts.getEventContext().set(UIWikiFormattedText.CURRENT_MACRO_EVENT_VARIABLE, macro);
  +
  +                    includePluginCSS(macro.getName(), parent);
                   includePluginFacelet(faceletURL, ctx, parent);
  -                createPreferencesEditor(macroName);
  -                includedMacros.add(macroName);
   
  -                // Get the placeholder to use
  -                String placeHolder;
  -                Object nextPlugin = parent.getAttributes().get(UIPlugin.NEXT_PLUGIN);
  -                if (nextPlugin != null) {
  -                    placeHolder = wikiFormattedText.addPlugin(nextPlugin.toString());
  +                    // TODO: Need to understand this magic from Pete if we want to make sub-clientIds for plugins
  +                    Object nextPluginId = parent.getAttributes().get(UIPlugin.NEXT_PLUGIN);
  +                    if (nextPluginId != null) {
  +                        macro.setClientId(nextPluginId.toString());
  +                        wikiFormattedText.addPluginMacro(macro.getPosition(), macro);
                       parent.getAttributes().remove(UIPlugin.NEXT_PLUGIN);
                   } else {
                       // Best guess based plugin renderer
  -                    placeHolder = wikiFormattedText.addPlugin(
  -                        (parent.getChildren().get(parent.getChildCount() - 1).getClientId( ctx.getFacesContext() )
  -                        )
  -                    );
  +                        String pluginId =
  +                            parent.getChildren().get( parent.getChildCount()-1 )
  +                                   .getClientId( ctx.getFacesContext() );
  +                        macro.setClientId(pluginId);
  +                        wikiFormattedText.addPluginMacro(macro.getPosition(), macro);
                   }
  -                matcher.appendReplacement(parsed, " [<=" + placeHolder + "]");
  -            } else {
  -                matcher.appendReplacement(parsed, " [<=" + macroName + "]");
  +
  +                    log.debug("including plugin CSS and facelet template at URL: " + faceletURL);
  +
  +                    return null;
               }
           }
  -        matcher.appendTail(parsed);
  -        wikiFormattedText.setValue(parsed.toString());
  +        );
  +
  +        try {
  +            parser.parse();
  +        } catch (RecognitionException rex) {
  +            // Swallow parsing errors, we don't really care here...
  +        } catch (ANTLRException ex) {
  +            // All other errors are fatal;
  +            throw new RuntimeException(ex);
  +        }
       }
   
       private URL getPluginURL(String macroName, FaceletContext ctx) {
  -        if (macroName == null || macroName.length() == 0 || includedMacros.contains(macroName)) return null;
  +        //if (macroName == null || macroName.length() == 0 || includedMacros.contains(macroName)) return null;
  +        if (macroName == null || macroName.length() == 0) return null;
   
           String includeView = "/plugins/" + macroName + "/plugin.xhtml";
   
  @@ -210,7 +220,7 @@
       */
       private void includePluginCSS(String macroName, UIComponent cmp) {
           // Try to get the CSS for it
  -        WikiPreferences wikiPrefs = (WikiPreferences) Component.getInstance("wikiPreferences");
  +        WikiPreferences wikiPrefs = (WikiPreferences) Preferences.getInstance("Wiki");
           String css = "/themes/" + wikiPrefs.getThemeName() + "/css/" + macroName + ".css";
           if (ResourceLoader.instance().getResource(css) != null) {
               // TODO: For Pete to fix, UILoadStyle doesn't load the CSS anymore
  @@ -223,34 +233,6 @@
       }
   
       /*
  -     * If this plugin has preferences and editing is enabled, instantiate a
  -     * plugin preferences editor and put it in the conversation context
  -     */
  -    private void createPreferencesEditor(String macroName) {
  -
  -        String pluginPreferenceName = macroName + "Preferences";
  -        Boolean showPluginPreferences = (Boolean) Contexts.getPageContext().get("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) {
  -                log.debug("Creating plugin preference editor for: " + pluginPreferenceName);
  -                Contexts.getConversationContext().set(pluginPreferenceName + "Editor", pluginPreferenceEditor);
  -                observer.addPluginPreferenceEditor(pluginPreferenceEditor);
  -            }
  -        } else if (showPluginPreferences == null || !showPluginPreferences) {
  -            log.debug("Disabling plugin preference editor for: " + pluginPreferenceName);
  -            Contexts.getConversationContext().set(pluginPreferenceName + "Editor", null);
  -        }
  -
  -    }
  -
  -    /*
       * Support method to find the UIWikiFormattedText component created by
       * this tag on a previous tree build
       */
  
  
  
  1.14      +1 -1      jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/Converters.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Converters.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/Converters.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -b -r1.13 -r1.14
  --- Converters.java	19 Dec 2007 04:29:22 -0000	1.13
  +++ Converters.java	30 Dec 2007 02:33:26 -0000	1.14
  @@ -33,7 +33,6 @@
           return new String[]{"NULL","January","February","March","April","May","June","July","August","September","October","November","December"};
       }
   
  -
       @Name("importerConverter")
       @org.jboss.seam.annotations.faces.Converter(forClass = Importer.class)
       public static class ImporterConverter implements Converter, Serializable {
  @@ -102,6 +101,7 @@
           }
       }
   
  +
       @Name("treeNodeAdapter")
       public static class TreeNodeAdapter {
   
  
  
  
  1.34      +28 -20    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.33
  retrieving revision 1.34
  diff -u -b -r1.33 -r1.34
  --- UIWikiFormattedText.java	20 Dec 2007 18:35:54 -0000	1.33
  +++ UIWikiFormattedText.java	30 Dec 2007 02:33:26 -0000	1.34
  @@ -9,15 +9,13 @@
   import antlr.ANTLRException;
   import antlr.RecognitionException;
   import org.jboss.seam.Component;
  +import org.jboss.seam.core.Events;
   import org.jboss.seam.contexts.Contexts;
   import org.jboss.seam.log.Log;
   import org.jboss.seam.log.Logging;
   import org.jboss.seam.ui.util.JSF;
   import org.jboss.seam.ui.validator.FormattedTextValidator;
  -import org.jboss.seam.wiki.core.engine.DefaultWikiTextRenderer;
  -import org.jboss.seam.wiki.core.engine.WikiLink;
  -import org.jboss.seam.wiki.core.engine.WikiLinkResolver;
  -import org.jboss.seam.wiki.core.engine.WikiTextParser;
  +import org.jboss.seam.wiki.core.engine.*;
   import org.jboss.seam.wiki.core.model.WikiFile;
   import org.jboss.seam.wiki.core.model.WikiUploadImage;
   import org.jboss.seam.wiki.util.WikiUtil;
  @@ -28,8 +26,9 @@
   import javax.faces.context.ResponseWriter;
   import java.io.IOException;
   import java.io.StringWriter;
  -import java.util.ArrayList;
  +import java.util.HashMap;
   import java.util.List;
  +import java.util.Map;
   
   /**
    * Uses WikiTextParser and WikiLinkResolver to render Seam Text markup with wiki links.
  @@ -43,6 +42,9 @@
   
       Log log = Logging.getLog(UIWikiFormattedText.class);
   
  +    public static final String CURRENT_MACRO_EVENT_VARIABLE         = "currentMacro";
  +    public static final String CURRENT_MACRO_EVENT_VARIABLE_SET     = "Macro.render.";
  +
       public static final String ATTR_LINK_STYLE_CLASS                = "linkStyleClass";
       public static final String ATTR_BROKEN_LINK_STYLE_CLASS         = "brokenLinkStyleClass";
       public static final String ATTR_ATTACHMENT_LINK_STYLE_CLASS     = "attachmentLinkStyleClass";
  @@ -53,7 +55,7 @@
       public static final String ATTR_CURRENT_AREA_NUMBER             = "currentAreaNumber";
       public static final String ATTR_ENABLE_PLUGINS                  = "enablePlugins";
   
  -    private List<String> plugins;
  +    private Map<Integer, WikiMacro> pluginMacros;
   
       public static final String COMPONENT_FAMILY = "org.jboss.seam.wiki.core.ui.UIWikiFormattedText";
   
  @@ -61,7 +63,7 @@
   
       public UIWikiFormattedText() {
           super();
  -        plugins = new ArrayList<String>();
  +        pluginMacros = new HashMap<Integer, WikiMacro>();
       }
   
       @Override
  @@ -84,7 +86,7 @@
           if (!isRendered() || getValue() == null) return;
   
           // Use the WikiTextParser to resolve macros
  -        WikiTextParser parser = new WikiTextParser((String) getValue(), false, true);
  +        WikiTextParser parser = new WikiTextParser((String) getValue(), true, true);
   
           // Resolve the base document and directory we are resolving against
           final WikiFile baseFile = (WikiFile)getAttributes().get(ATTR_LINK_BASE_FILE);
  @@ -165,23 +167,29 @@
                   }
               }
   
  -            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
  +            public String renderMacro(WikiMacro macro) {
  +
  +                WikiMacro pluginMacro = pluginMacros.get(macro.getPosition());
  +                if (pluginMacro == null) {
  +                    log.debug("macro is not a plugin, skipping: " + macro);
                       return "";
                   }
  -                UIComponent child = findComponent(plugins.get(new Integer(macroName)));
  +
  +                log.debug("preparing plugin rendering for macro: " + macro);
  +                UIComponent child = findComponent( pluginMacros.get(macro.getPosition()).getClientId() );
  +                log.debug("JSF child client identifier: " + child.getClientId(getFacesContext()));
                   ResponseWriter originalResponseWriter = getFacesContext().getResponseWriter();
                   StringWriter stringWriter = new StringWriter();
                   ResponseWriter tempResponseWriter = originalResponseWriter
                           .cloneWithWriter(stringWriter);
                   getFacesContext().setResponseWriter(tempResponseWriter);
  +
  +                log.debug("setting current macro in EVENT context");
  +                Contexts.getEventContext().set(CURRENT_MACRO_EVENT_VARIABLE, macro);
  +                Events.instance().raiseEvent(CURRENT_MACRO_EVENT_VARIABLE_SET+macro.getName());
  +
                   try {
  +                    log.debug("rendering plugin macro: " + macro);
                       JSF.renderChild(getFacesContext(), child);
                   }
                   catch (Exception ex) {
  @@ -227,6 +235,7 @@
           parser.setRenderer(new WikiFormattedTextRenderer());
   
           try {
  +            log.debug(">>> rendering wiki text");
               parser.parse();
   
           } catch (RecognitionException rex) {
  @@ -241,9 +250,8 @@
   
       }
   
  -    protected String addPlugin(String clientId) {
  -        plugins.add(clientId);
  -        return (plugins.size() - 1) + "";
  +    protected void addPluginMacro(Integer position, WikiMacro macro) {
  +        pluginMacros.put(position, macro);
       }
   
   }
  
  
  



More information about the jboss-cvs-commits mailing list