You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2011/01/15 11:50:09 UTC

svn commit: r1059291 - in /wicket/trunk/wicket-core/src/main/java/org/apache/wicket: Component.java MarkupContainer.java markup/html/panel/DefaultMarkupSourcingStrategy.java

Author: jdonnerstag
Date: Sat Jan 15 10:50:09 2011
New Revision: 1059291

URL: http://svn.apache.org/viewvc?rev=1059291&view=rev
Log:
extracted a default implementation which also avoids the null checks.
Issue: WICKET-3314

Added:
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/DefaultMarkupSourcingStrategy.java
Modified:
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java

Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java?rev=1059291&r1=1059290&r2=1059291&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java Sat Jan 15 10:50:09 2011
@@ -44,6 +44,7 @@ import org.apache.wicket.markup.WicketTa
 import org.apache.wicket.markup.html.IHeaderContributor;
 import org.apache.wicket.markup.html.IHeaderResponse;
 import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
+import org.apache.wicket.markup.html.panel.DefaultMarkupSourcingStrategy;
 import org.apache.wicket.markup.html.panel.IMarkupSourcingStrategy;
 import org.apache.wicket.model.IComponentAssignedModel;
 import org.apache.wicket.model.IComponentInheritedModel;
@@ -462,6 +463,12 @@ public abstract class Component
 	private transient IMarkupFragment markup;
 
 	/**
+	 * Will be re-created instead of persisted when session is replicated. Markup sourcing strategy
+	 * are typically stateless (but don't have to).
+	 */
+	private transient IMarkupSourcingStrategy markupSourcingStrategy;
+
+	/**
 	 * The object that holds the component state.
 	 * <p>
 	 * What's stored here depends on what attributes are set on component. Data can contains
@@ -2490,16 +2497,9 @@ public abstract class Component
 			// Render the body only if open-body-close. Do not render if open-close.
 			if (tag.isOpen())
 			{
-				// Render the body
-				IMarkupSourcingStrategy provider = getMarkupSourcingStrategy();
-				if (provider != null)
-				{
-					provider.onComponentTagBody(this, markupStream, tag);
-				}
-				else
-				{
-					onComponentTagBody(markupStream, tag);
-				}
+				// Render the body. The default strategy will simply call the component's
+				// onComponentTagBody() implementation.
+				getMarkupSourcingStrategy().onComponentTagBody(this, markupStream, tag);
 			}
 
 			// Render close tag
@@ -2569,30 +2569,32 @@ public abstract class Component
 		}
 	}
 
-	// Will be re-created instead of persisted when session is replicated.
-	// Markup sourcing strategy are meant to be stateless.
-	private transient IMarkupSourcingStrategy markupSourcingStrategy;
-
 	/**
 	 * Get the markup sourcing strategy for the component. If null,
-	 * {@link #newMarkupSourcingStrategy()} will be called. A return value of null indicates that no
-	 * specific strategy is attached to the Component, which is perfectly ok.
+	 * {@link #newMarkupSourcingStrategy()} will be called.
 	 * 
-	 * @return Markup sourcing strategy or null if no strategy is registered
+	 * @return Markup sourcing strategy
 	 */
 	protected final IMarkupSourcingStrategy getMarkupSourcingStrategy()
 	{
 		if (markupSourcingStrategy == null)
 		{
 			markupSourcingStrategy = newMarkupSourcingStrategy();
+
+			// If not strategy by provided, than we use a default one.
+			if (markupSourcingStrategy == null)
+			{
+				markupSourcingStrategy = DefaultMarkupSourcingStrategy.get();
+			}
 		}
 		return markupSourcingStrategy;
 	}
 
 	/**
-	 * If {@link #getMarkupSourcingStrategy()} return null, this method will be called. By default
-	 * it returns null, which means that no markup strategy is attached to the component.
-	 * 
+	 * If {@link #getMarkupSourcingStrategy()} returns null, this method will be called. By default
+	 * it returns null, which means that a default markup strategy will be attached to the
+	 * component.
+	 * <p>
 	 * Please note that markup source strategies are not persisted. Instead they get re-created by
 	 * calling this method again. That's ok since markup sourcing strategies usually do not maintain
 	 * a state.
@@ -2631,13 +2633,11 @@ public abstract class Component
 			// Allow component to contribute
 			if (response.wasRendered(this) == false)
 			{
+				// Let the component contribute something to the header
 				renderHead(response);
 
-				IMarkupSourcingStrategy provider = getMarkupSourcingStrategy();
-				if (provider != null)
-				{
-					provider.renderHead(this, container);
-				}
+				// Make sure the markup source strategy has been considered as well.
+				getMarkupSourcingStrategy().renderHead(this, container);
 
 				response.markRendered(this);
 			}
@@ -3777,11 +3777,8 @@ public abstract class Component
 			tag.put("wicketpath", path);
 		}
 
-		IMarkupSourcingStrategy provider = getMarkupSourcingStrategy();
-		if (provider != null)
-		{
-			provider.onComponentTag(this, tag);
-		}
+		// The markup sourcing strategy may also want to work on the tag
+		getMarkupSourcingStrategy().onComponentTag(this, tag);
 	}
 
 	/**

Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java?rev=1059291&r1=1059290&r2=1059291&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java Sat Jan 15 10:50:09 2011
@@ -35,7 +35,6 @@ import org.apache.wicket.markup.MarkupTy
 import org.apache.wicket.markup.RawMarkup;
 import org.apache.wicket.markup.WicketTag;
 import org.apache.wicket.markup.html.border.Border;
-import org.apache.wicket.markup.html.panel.IMarkupSourcingStrategy;
 import org.apache.wicket.markup.resolver.ComponentResolvers;
 import org.apache.wicket.model.IComponentInheritedModel;
 import org.apache.wicket.model.IModel;
@@ -453,63 +452,8 @@ public abstract class MarkupContainer ex
 	 */
 	public IMarkupFragment getMarkup(final Component child)
 	{
-		IMarkupSourcingStrategy provider = getMarkupSourcingStrategy();
-		if (provider != null)
-		{
-			IMarkupFragment markup = provider.getMarkup(this, child);
-			if (markup != null)
-			{
-				return markup;
-			}
-		}
-
-		// Get the markup for the container
-		IMarkupFragment markup = getMarkup();
-		if (markup == null)
-		{
-			return null;
-		}
-
-		if (child == null)
-		{
-			return markup;
-		}
-
-		// Find the child's markup
-		markup = markup.find(child.getId());
-		if (markup != null)
-		{
-			return markup;
-		}
-
-		// This is to make migration for Items from 1.4 to 1.5 more easy
-		if (Character.isDigit(child.getId().charAt(0)))
-		{
-			String id = child.getId();
-			boolean miss = false;
-			for (int i = 1; i < id.length(); i++)
-			{
-				if (Character.isDigit(id.charAt(i)) == false)
-				{
-					miss = true;
-					break;
-				}
-			}
-
-			if (miss == false)
-			{
-				// The LoopItems markup is equal to the Loops markup
-				markup = getMarkup();
-
-				if (log.isWarnEnabled())
-				{
-					log.warn("1.4 to 1.5 migration issue: your item component should be derived from AbstractItem. Item=" +
-						child.toString());
-				}
-			}
-		}
-
-		return markup;
+		// Delegate request to attached markup sourcing strategy.
+		return getMarkupSourcingStrategy().getMarkup(this, child);
 	}
 
 	/**

Added: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/DefaultMarkupSourcingStrategy.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/DefaultMarkupSourcingStrategy.java?rev=1059291&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/DefaultMarkupSourcingStrategy.java (added)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/panel/DefaultMarkupSourcingStrategy.java Sat Jan 15 10:50:09 2011
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.panel;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.IMarkupFragment;
+import org.apache.wicket.markup.MarkupStream;
+import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This is a no-op sourcing strategy.
+ * 
+ * @author Juergen Donnerstag
+ */
+public final class DefaultMarkupSourcingStrategy implements IMarkupSourcingStrategy
+{
+	/** Log for reporting. */
+	private static final Logger log = LoggerFactory.getLogger(DefaultMarkupSourcingStrategy.class);
+
+	private static DefaultMarkupSourcingStrategy instance = new DefaultMarkupSourcingStrategy();
+
+	/**
+	 * 
+	 * @return A singleton of the strategy
+	 */
+	public final static DefaultMarkupSourcingStrategy get()
+	{
+		return instance;
+	}
+
+	/**
+	 * Construct. Please use {@link #get()} instead.
+	 */
+	private DefaultMarkupSourcingStrategy()
+	{
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void onComponentTag(final Component component, final ComponentTag tag)
+	{
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void onComponentTagBody(final Component component, final MarkupStream markupStream,
+		final ComponentTag openTag)
+	{
+		component.onComponentTagBody(markupStream, openTag);
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public IMarkupFragment getMarkup(final MarkupContainer container, final Component child)
+	{
+		// If the sourcing strategy did not provide one, than ask the component.
+		// Get the markup for the container
+		IMarkupFragment markup = container.getMarkup();
+		if (markup == null)
+		{
+			return null;
+		}
+
+		if (child == null)
+		{
+			return markup;
+		}
+
+		// Find the child's markup
+		markup = markup.find(child.getId());
+		if (markup != null)
+		{
+			return markup;
+		}
+
+		// This is to make migration for Items from 1.4 to 1.5 more easy
+		if (Character.isDigit(child.getId().charAt(0)))
+		{
+			String id = child.getId();
+			boolean miss = false;
+			for (int i = 1; i < id.length(); i++)
+			{
+				if (Character.isDigit(id.charAt(i)) == false)
+				{
+					miss = true;
+					break;
+				}
+			}
+
+			if (miss == false)
+			{
+				// The LoopItems markup is equal to the Loops markup
+				markup = container.getMarkup();
+
+				if (log.isWarnEnabled())
+				{
+					log.warn("1.4 to 1.5 migration issue: your item component should be derived from AbstractItem. Item=" +
+						child.toString());
+				}
+			}
+		}
+
+		return markup;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void renderHead(final Component component, HtmlHeaderContainer container)
+	{
+	}
+}