Author: jdonnerstag
Date: Sat Jan  3 14:04:54 2009
New Revision: 731096

URL: http://svn.apache.org/viewvc?rev=731096&view=rev
Log:
fixed wicket-1192:  move static factory methods off of HeaderContributer and 
onto related classes

Added:
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/CSSPackageResource.java
Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/HeaderContributor.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/JavascriptPackageResource.java
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java?rev=731096&r1=731095&r2=731096&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java 
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java Sat Jan  
3 14:04:54 2009
@@ -29,7 +29,6 @@
 import org.apache.wicket.authorization.AuthorizationException;
 import org.apache.wicket.authorization.IAuthorizationStrategy;
 import org.apache.wicket.authorization.UnauthorizedActionException;
-import org.apache.wicket.behavior.HeaderContributor;
 import org.apache.wicket.behavior.IBehavior;
 import org.apache.wicket.feedback.FeedbackMessage;
 import org.apache.wicket.feedback.IFeedback;
@@ -37,7 +36,9 @@
 import org.apache.wicket.markup.MarkupException;
 import org.apache.wicket.markup.MarkupStream;
 import org.apache.wicket.markup.WicketTag;
+import org.apache.wicket.markup.html.CSSPackageResource;
 import org.apache.wicket.markup.html.IHeaderContributor;
+import org.apache.wicket.markup.html.JavascriptPackageResource;
 import org.apache.wicket.markup.html.PackageResource;
 import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
 import org.apache.wicket.model.IComponentAssignedModel;
@@ -933,12 +934,12 @@
                {
                        if (PackageResource.exists(this.getClass(), name + 
".css", getLocale(), getStyle()))
                        {
-                               add(HeaderContributor.forCss(this.getClass(), 
name + ".css"));
+                               
add(CSSPackageResource.getHeaderContribution(this.getClass(), name + ".css"));
                        }
 
                        if (PackageResource.exists(this.getClass(), name + 
".js", getLocale(), getStyle()))
                        {
-                               
add(HeaderContributor.forJavaScript(this.getClass(), name + ".js"));
+                               
add(JavascriptPackageResource.getHeaderContribution(this.getClass(), name + 
".js"));
                        }
                }
        }

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/HeaderContributor.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/HeaderContributor.java?rev=731096&r1=731095&r2=731096&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/HeaderContributor.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/HeaderContributor.java
 Sat Jan  3 14:04:54 2009
@@ -16,12 +16,10 @@
  */
 package org.apache.wicket.behavior;
 
-import org.apache.wicket.RequestCycle;
 import org.apache.wicket.ResourceReference;
+import org.apache.wicket.markup.html.CSSPackageResource;
 import org.apache.wicket.markup.html.IHeaderContributor;
-import org.apache.wicket.markup.html.IHeaderResponse;
-import org.apache.wicket.markup.html.resources.CompressedResourceReference;
-import org.apache.wicket.markup.html.resources.JavascriptResourceReference;
+import org.apache.wicket.markup.html.JavascriptPackageResource;
 
 /**
  * A {...@link org.apache.wicket.behavior.AbstractHeaderContributor} behavior 
that is specialized on
@@ -51,18 +49,12 @@
         * @param path
         *            The path
         * @return the new header contributor instance
+        * @deprecated please use CSSPackageResource.getHeaderContribution() 
instead
         */
+       @Deprecated
        public static final HeaderContributor forCss(final Class<?> scope, 
final String path)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               response.renderCSSReference(new 
CompressedResourceReference(scope, path));
-                       }
-               });
+               return CSSPackageResource.getHeaderContribution(scope, path);
        }
 
        /**
@@ -77,19 +69,13 @@
         * @param media
         *            The media type for this CSS ("print", "screen", etc.)
         * @return the new header contributor instance
+        * @deprecated please use CSSPackageResource.getHeaderContribution() 
instead
         */
+       @Deprecated
        public static final HeaderContributor forCss(final Class<?> scope, 
final String path,
                final String media)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               response.renderCSSReference(new 
CompressedResourceReference(scope, path), media);
-                       }
-               });
+               return CSSPackageResource.getHeaderContribution(scope, path, 
media);
        }
 
        /**
@@ -99,18 +85,12 @@
         * @param reference
         * 
         * @return the new header contributor instance
+        * @deprecated please use CSSPackageResource.getHeaderContribution() 
instead
         */
+       @Deprecated
        public static final HeaderContributor forCss(final ResourceReference 
reference)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               response.renderCSSReference(reference);
-                       }
-               });
+               return CSSPackageResource.getHeaderContribution(reference);
        }
 
        /**
@@ -121,19 +101,13 @@
         * @param media
         *            The media type for this CSS ("print", "screen", etc.)
         * @return the new header contributor instance
+        * @deprecated please use CSSPackageResource.getHeaderContribution() 
instead
         */
+       @Deprecated
        public static final HeaderContributor forCss(final ResourceReference 
reference,
                final String media)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               response.renderCSSReference(reference, media);
-                       }
-               });
+               return CSSPackageResource.getHeaderContribution(reference, 
media);
        }
 
        /**
@@ -149,18 +123,12 @@
         * @param location
         *            The location of the css file.
         * @return the new header contributor instance
+        * @deprecated please use CSSPackageResource.getHeaderContribution() 
instead
         */
+       @Deprecated
        public static final HeaderContributor forCss(final String location)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               
response.renderCSSReference(returnRelativePath(location));
-                       }
-               });
+               return CSSPackageResource.getHeaderContribution(location);
        }
 
        /**
@@ -178,18 +146,12 @@
         * @param media
         *            The media type for this CSS ("print", "screen", etc.)
         * @return the new header contributor instance
+        * @deprecated please use CSSPackageResource.getHeaderContribution() 
instead
         */
+       @Deprecated
        public static final HeaderContributor forCss(final String location, 
final String media)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               
response.renderCSSReference(returnRelativePath(location), media);
-                       }
-               });
+               return CSSPackageResource.getHeaderContribution(location, 
media);
        }
 
        /**
@@ -202,18 +164,12 @@
         * @param path
         *            The path
         * @return the new header contributor instance
+        * @deprecated please use 
JavascriptPackageResource.getHeaderContribution() instead
         */
+       @Deprecated
        public static final HeaderContributor forJavaScript(final Class<?> 
scope, final String path)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               response.renderJavascriptReference(new 
JavascriptResourceReference(scope, path));
-                       }
-               });
+               return JavascriptPackageResource.getHeaderContribution(scope, 
path);
        }
 
        /**
@@ -223,18 +179,12 @@
         * @param reference
         * 
         * @return the new header contributor instance
+        * @deprecated please use 
JavascriptPackageResource.getHeaderContribution() instead
         */
+       @Deprecated
        public static final HeaderContributor forJavaScript(final 
ResourceReference reference)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               response.renderJavascriptReference(reference);
-                       }
-               });
+               return 
JavascriptPackageResource.getHeaderContribution(reference);
        }
 
        /**
@@ -250,37 +200,12 @@
         * @param location
         *            The location of the java script file.
         * @return the new header contributor instance
+        * @deprecated please use 
JavascriptPackageResource.getHeaderContribution() instead
         */
+       @Deprecated
        public static final HeaderContributor forJavaScript(final String 
location)
        {
-               return new HeaderContributor(new IHeaderContributor()
-               {
-                       private static final long serialVersionUID = 1L;
-
-                       public void renderHead(IHeaderResponse response)
-                       {
-                               
response.renderJavascriptReference(returnRelativePath(location));
-                       }
-               });
-       }
-
-       // Adds ../ links to make the location relative to the root of the 
webapp,
-       // provided it's not a fully-qualified URL.
-       private static final String returnRelativePath(String location)
-       {
-               // WICKET-59 allow external URLs, WICKET-612 allow absolute 
URLs.
-               if (location.startsWith("http://";) || 
location.startsWith("https://";) ||
-                       location.startsWith("/"))
-               {
-                       return location;
-               }
-               else
-               {
-                       return RequestCycle.get()
-                               .getProcessor()
-                               .getRequestCodingStrategy()
-                               .rewriteStaticRelativeUrl(location);
-               }
+               return 
JavascriptPackageResource.getHeaderContribution(location);
        }
 
        /**

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/CSSPackageResource.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/CSSPackageResource.java?rev=731096&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/CSSPackageResource.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/CSSPackageResource.java
 Sat Jan  3 14:04:54 2009
@@ -0,0 +1,224 @@
+/*
+ * 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;
+
+import java.util.Locale;
+
+import org.apache.wicket.RequestCycle;
+import org.apache.wicket.ResourceReference;
+import org.apache.wicket.behavior.HeaderContributor;
+import org.apache.wicket.markup.html.resources.CompressedResourceReference;
+
+
+/**
+ * Package resource for CSS files. It is not different than PackageResource 
except that it provide
+ * utility methods to create proper HTML header contributions for CSS files
+ * 
+ * @author Juergen Donnerstag
+ */
+public class CSSPackageResource extends PackageResource
+{
+       private static final long serialVersionUID = 1L;;
+
+       /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor that references
+        * a CSS file that lives in a package.
+        * 
+        * @param scope
+        *            The scope of the package resource (typically the class of 
the caller, or a class
+        *            that lives in the package where the resource lives).
+        * @param path
+        *            The path
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
Class<?> scope,
+               final String path)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               response.renderCSSReference(new 
CompressedResourceReference(scope, path));
+                       }
+               });
+       }
+
+       /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor that references
+        * a CSS file that lives in a package.
+        * 
+        * @param scope
+        *            The scope of the package resource (typically the class of 
the caller, or a class
+        *            that lives in the package where the resource lives).
+        * @param path
+        *            The path
+        * @param media
+        *            The media type for this CSS ("print", "screen", etc.)
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
Class<?> scope,
+               final String path, final String media)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               response.renderCSSReference(new 
CompressedResourceReference(scope, path), media);
+                       }
+               });
+       }
+
+       /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor that references
+        * a CSS file that lives in a package.
+        * 
+        * @param reference
+        * 
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
ResourceReference reference)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               response.renderCSSReference(reference);
+                       }
+               });
+       }
+
+       /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor that references
+        * a CSS file that lives in a package.
+        * 
+        * @param reference
+        * @param media
+        *            The media type for this CSS ("print", "screen", etc.)
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
ResourceReference reference,
+               final String media)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               response.renderCSSReference(reference, media);
+                       }
+               });
+       }
+
+       /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor referencing a
+        * CSS file using one of the following schemes:
+        * <ul>
+        * <li>Starts with http:// or https:// for an external reference.</li>
+        * <li>Starts with "/" for an absolute reference that Wicket will not 
rewrite.</li>
+        * <li>Starts with anything else, which Wicket will automatically 
prepend to make relative to
+        * the context root of your web-app.</li>
+        * </ul>
+        * 
+        * @param location
+        *            The location of the css file.
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
String location)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               
response.renderCSSReference(returnRelativePath(location));
+                       }
+               });
+       }
+
+       /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor referencing a
+        * CSS file using one of the following schemes:
+        * <ul>
+        * <li>Starts with http:// or https:// for an external reference.</li>
+        * <li>Starts with "/" for an absolute reference that Wicket will not 
rewrite.</li>
+        * <li>Starts with anything else, which Wicket will automatically 
prepend to make relative to
+        * the context root of your web-app.</li>
+        * </ul>
+        * 
+        * @param location
+        *            The location of the css.
+        * @param media
+        *            The media type for this CSS ("print", "screen", etc.)
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
String location,
+               final String media)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               
response.renderCSSReference(returnRelativePath(location), media);
+                       }
+               });
+       }
+
+       /**
+        * 
+        * @param location
+        * @return relative path
+        */
+       private static final String returnRelativePath(String location)
+       {
+               // WICKET-59 allow external URLs, WICKET-612 allow absolute 
URLs.
+               if (location.startsWith("http://";) || 
location.startsWith("https://";) ||
+                       location.startsWith("/"))
+               {
+                       return location;
+               }
+               else
+               {
+                       return RequestCycle.get()
+                               .getProcessor()
+                               .getRequestCodingStrategy()
+                               .rewriteStaticRelativeUrl(location);
+               }
+       }
+
+       /**
+        * Creates a new javascript package resource.
+        * 
+        * @param scope
+        * @param path
+        * @param locale
+        * @param style
+        */
+       protected CSSPackageResource(Class<?> scope, String path, Locale 
locale, String style)
+       {
+               super(scope, path, locale, style);
+       }
+}

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/JavascriptPackageResource.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/JavascriptPackageResource.java?rev=731096&r1=731095&r2=731096&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/JavascriptPackageResource.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/JavascriptPackageResource.java
 Sat Jan  3 14:04:54 2009
@@ -24,6 +24,10 @@
 import java.util.Locale;
 
 import org.apache.wicket.Application;
+import org.apache.wicket.RequestCycle;
+import org.apache.wicket.ResourceReference;
+import org.apache.wicket.behavior.HeaderContributor;
+import org.apache.wicket.markup.html.resources.JavascriptResourceReference;
 import org.apache.wicket.util.io.Streams;
 import org.apache.wicket.util.resource.IResourceStream;
 import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
@@ -46,6 +50,100 @@
        private static final Logger log = 
LoggerFactory.getLogger(JavascriptPackageResource.class);
 
        /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor that references
+        * a java script file that lives in a package.
+        * 
+        * @param scope
+        *            The scope of the package resource (typically the class of 
the caller, or a class
+        *            that lives in the package where the resource lives).
+        * @param path
+        *            The path
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
Class<?> scope, final String path)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               response.renderJavascriptReference(new 
JavascriptResourceReference(scope, path));
+                       }
+               });
+       }
+
+       /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor that references
+        * a java script file that lives in a package.
+        * 
+        * @param reference
+        * 
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
ResourceReference reference)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               response.renderJavascriptReference(reference);
+                       }
+               });
+       }
+
+       /**
+        * Returns a new instance of {...@link HeaderContributor} with a header 
contributor referencing a
+        * java script file using one of the following schemes:
+        * <ul>
+        * <li>Starts with http:// or https:// for an external reference.</li>
+        * <li>Starts with "/" for an absolute reference that Wicket will not 
rewrite.</li>
+        * <li>Starts with anything else, which Wicket will automatically 
prepend to make relative to
+        * the context root of your web-app.</li>
+        * </ul>
+        * 
+        * @param location
+        *            The location of the java script file.
+        * @return the new header contributor instance
+        */
+       public static final HeaderContributor getHeaderContribution(final 
String location)
+       {
+               return new HeaderContributor(new IHeaderContributor()
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       public void renderHead(IHeaderResponse response)
+                       {
+                               
response.renderJavascriptReference(returnRelativePath(location));
+                       }
+               });
+       }
+
+       /**
+        * 
+        * @param location
+        * @return relative path
+        */
+       private static final String returnRelativePath(String location)
+       {
+               // WICKET-59 allow external URLs, WICKET-612 allow absolute 
URLs.
+               if (location.startsWith("http://";) || 
location.startsWith("https://";) ||
+                       location.startsWith("/"))
+               {
+                       return location;
+               }
+               else
+               {
+                       return RequestCycle.get()
+                               .getProcessor()
+                               .getRequestCodingStrategy()
+                               .rewriteStaticRelativeUrl(location);
+               }
+       }
+
+       /**
         * Resource Stream that caches the stripped content.
         * 
         * @author Matej Knopp

Modified: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java?rev=731096&r1=731095&r2=731096&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java
 (original)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java
 Sat Jan  3 14:04:54 2009
@@ -16,7 +16,7 @@
  */
 package org.apache.wicket.markup.html.header;
 
-import org.apache.wicket.behavior.HeaderContributor;
+import org.apache.wicket.markup.html.CSSPackageResource;
 import org.apache.wicket.markup.html.WebPage;
 
 /**
@@ -32,6 +32,8 @@
        public MyPage2()
        {
                // And it is not problem if historically it has been added. 
Wicket will render it just once
-               add(HeaderContributor.forCss(this.getClass(), 
this.getClass().getSimpleName() + ".css"));
+               add(CSSPackageResource.getHeaderContribution(this.getClass(), 
this.getClass()
+                       .getSimpleName() +
+                       ".css"));
        }
 }


Reply via email to