Author: jdonnerstag
Date: Sun Jan  4 03:53:14 2009
New Revision: 731223

URL: http://svn.apache.org/viewvc?rev=731223&view=rev
Log:
applied wicket-918: allow for pluggable javascript compression algorithms

Added:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/DefaultJavascriptCompressor.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/IJavascriptCompressor.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/NoOpJavascriptCompressor.java
Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/JavascriptPackageResource.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java?rev=731223&r1=731222&r2=731223&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java 
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java Sun 
Jan  4 03:53:14 2009
@@ -31,6 +31,7 @@
 import org.apache.wicket.application.IComponentInstantiationListener;
 import org.apache.wicket.application.IComponentOnAfterRenderListener;
 import org.apache.wicket.application.IComponentOnBeforeRenderListener;
+import org.apache.wicket.javascript.DefaultJavascriptCompressor;
 import org.apache.wicket.markup.IMarkupCache;
 import org.apache.wicket.markup.html.IHeaderContributor;
 import org.apache.wicket.markup.html.IHeaderResponse;
@@ -283,7 +284,7 @@
                         */
                        public void onInstantiation(final Component component)
                        {
-                               final Class<? extends Component> cl = (Class<? 
extends Component>)component.getClass();
+                               final Class<? extends Component> cl = 
component.getClass();
                                // If component instantiation is not authorized
                                if 
(!Session.get().getAuthorizationStrategy().isInstantiationAuthorized(cl))
                                {
@@ -346,7 +347,7 @@
                        getExceptionSettings().setUnexpectedExceptionDisplay(
                                IExceptionSettings.SHOW_EXCEPTION_PAGE);
                        getDebugSettings().setAjaxDebugModeEnabled(true);
-                       
getResourceSettings().setStripJavascriptCommentsAndWhitespace(false);
+                       getResourceSettings().setJavascriptCompressor(null);
                }
                else if (DEPLOYMENT.equalsIgnoreCase(configurationType))
                {
@@ -356,7 +357,7 @@
                        getExceptionSettings().setUnexpectedExceptionDisplay(
                                IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE);
                        getDebugSettings().setAjaxDebugModeEnabled(false);
-                       
getResourceSettings().setStripJavascriptCommentsAndWhitespace(true);
+                       getResourceSettings().setJavascriptCompressor(new 
DefaultJavascriptCompressor());
                }
                else
                {
@@ -1021,8 +1022,8 @@
        }
 
        /**
-        * Adds an {...@link IComponentOnBeforeRenderListener}. This method 
should typically only be
-        * called during application startup; it is not thread safe.
+        * Adds an {...@link IComponentOnBeforeRenderListener}. This method 
should typically only be called
+        * during application startup; it is not thread safe.
         * 
         * @param listener
         */
@@ -1076,8 +1077,8 @@
        }
 
        /**
-        * Adds an {...@link IComponentOnBeforeRenderListener}. This method 
should typically only be
-        * called during application startup; it is not thread safe.
+        * Adds an {...@link IComponentOnBeforeRenderListener}. This method 
should typically only be called
+        * during application startup; it is not thread safe.
         * 
         * @param listener
         */

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/DefaultJavascriptCompressor.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/DefaultJavascriptCompressor.java?rev=731223&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/DefaultJavascriptCompressor.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/DefaultJavascriptCompressor.java
 Sun Jan  4 03:53:14 2009
@@ -0,0 +1,35 @@
+/*
+ * 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.javascript;
+
+import org.apache.wicket.util.string.JavascriptStripper;
+
+/**
+ * Wicket default implementation of a javascript compressor
+ * 
+ * @author Juergen Donnerstag
+ */
+public class DefaultJavascriptCompressor implements IJavascriptCompressor
+{
+       /**
+        * @see 
org.apache.wicket.javascript.IJavascriptCompressor#compress(java.lang.String)
+        */
+       public String compress(String original)
+       {
+               return JavascriptStripper.stripCommentsAndWhitespace(original);
+       }
+}

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/IJavascriptCompressor.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/IJavascriptCompressor.java?rev=731223&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/IJavascriptCompressor.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/IJavascriptCompressor.java
 Sun Jan  4 03:53:14 2009
@@ -0,0 +1,33 @@
+/*
+ * 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.javascript;
+
+/**
+ * Allow for different implementation of a javascript compressor
+ * 
+ * @author Juergen Donnerstag
+ */
+public interface IJavascriptCompressor
+{
+       /**
+        * Remove comments and whitespaces from the javascript
+        * 
+        * @param original
+        * @return compressed Javascript
+        */
+       public String compress(String original);
+}

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/NoOpJavascriptCompressor.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/NoOpJavascriptCompressor.java?rev=731223&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/NoOpJavascriptCompressor.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/javascript/NoOpJavascriptCompressor.java
 Sun Jan  4 03:53:14 2009
@@ -0,0 +1,33 @@
+/*
+ * 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.javascript;
+
+/**
+ * A no-op javascript compressor
+ * 
+ * @author Juergen Donnerstag
+ */
+public class NoOpJavascriptCompressor implements IJavascriptCompressor
+{
+       /**
+        * @see 
org.apache.wicket.javascript.IJavascriptCompressor#compress(java.lang.String)
+        */
+       public String compress(String original)
+       {
+               return original;
+       }
+}

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=731223&r1=731222&r2=731223&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
 Sun Jan  4 03:53:14 2009
@@ -27,6 +27,7 @@
 import org.apache.wicket.RequestCycle;
 import org.apache.wicket.ResourceReference;
 import org.apache.wicket.behavior.HeaderContributor;
+import org.apache.wicket.javascript.IJavascriptCompressor;
 import org.apache.wicket.markup.html.resources.JavascriptResourceReference;
 import org.apache.wicket.util.io.Streams;
 import org.apache.wicket.util.resource.IResourceStream;
@@ -60,7 +61,8 @@
         *            The path
         * @return the new header contributor instance
         */
-       public static final HeaderContributor getHeaderContribution(final 
Class<?> scope, final String path)
+       public static final HeaderContributor getHeaderContribution(final 
Class<?> scope,
+               final String path)
        {
                return new HeaderContributor(new IHeaderContributor()
                {
@@ -303,6 +305,7 @@
                        {
                                try
                                {
+                                       // @TODO remove in 1.5
                                        if (Application.get()
                                                .getResourceSettings()
                                                
.getStripJavascriptCommentsAndWhitespace())
@@ -310,11 +313,18 @@
                                                String s = new String(input, 
"UTF-8");
                                                return 
JavascriptStripper.stripCommentsAndWhitespace(s).getBytes("UTF-8");
                                        }
-                                       else
+
+                                       IJavascriptCompressor compressor = 
Application.get()
+                                               .getResourceSettings()
+                                               .getJavascriptCompressor();
+                                       if (compressor != null)
                                        {
-                                               // don't strip the comments, 
just return original input
-                                               return input;
+                                               String s = new String(input, 
"UTF-8");
+                                               return 
compressor.compress(s).getBytes("UTF-8");
                                        }
+
+                                       // don't strip the comments, just 
return original input
+                                       return input;
                                }
                                catch (Exception e)
                                {

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java?rev=731223&r1=731222&r2=731223&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java
 Sun Jan  4 03:53:14 2009
@@ -20,6 +20,7 @@
 
 import org.apache.wicket.IResourceFactory;
 import org.apache.wicket.Localizer;
+import org.apache.wicket.javascript.IJavascriptCompressor;
 import org.apache.wicket.markup.html.IPackageResourceGuard;
 import org.apache.wicket.markup.html.JavascriptPackageResource;
 import org.apache.wicket.markup.html.PackageResourceGuard;
@@ -279,16 +280,40 @@
         * through {...@link JavascriptPackageResource}
         * 
         * @param value
+        * @deprecated please us {...@link 
#setJavascriptCompressor(IJavascriptCompressor)} instead. Will
+        *             be removed in 1.5
         */
+       @Deprecated
        void setStripJavascriptCommentsAndWhitespace(boolean value);
 
        /**
         * @return whether the comments and whitespace characters will be 
stripped from resources served
         *         through {...@link JavascriptPackageResource}
+        * @deprecated please use {...@link #getJavascriptCompressor()} 
instead. Will be removed in 1.5
         */
+       @Deprecated
        boolean getStripJavascriptCommentsAndWhitespace();
 
        /**
+        * Set the javascript compressor implemententation use e.g. by 
{...@link JavascriptPackageResource}
+        * . A typical implementation will remove comments and whitespace. But 
a no-op implementation is
+        * available as well.
+        * 
+        * @param compressor
+        *            The implementation to be used
+        * @return The old value
+        */
+       IJavascriptCompressor setJavascriptCompressor(IJavascriptCompressor 
compressor);
+
+       /**
+        * Get the javascript compressor to remove comments and whitespace 
characters from javascripts
+        * 
+        * @return whether the comments and whitespace characters will be 
stripped from resources served
+        *         through {...@link JavascriptPackageResource}. Null is a 
valid value.
+        */
+       IJavascriptCompressor getJavascriptCompressor();
+
+       /**
         * Sets whether Wicket should add last modified time as a parameter to 
resource reference URL
         * (can help with browsers too aggressively caching certain resources).
         * 

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java?rev=731223&r1=731222&r2=731223&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java 
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java 
Sun Jan  4 03:53:14 2009
@@ -35,6 +35,7 @@
 import org.apache.wicket.authorization.IAuthorizationStrategy;
 import 
org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;
 import org.apache.wicket.authorization.UnauthorizedInstantiationException;
+import org.apache.wicket.javascript.IJavascriptCompressor;
 import org.apache.wicket.markup.IMarkupCache;
 import org.apache.wicket.markup.IMarkupParserFactory;
 import org.apache.wicket.markup.MarkupCache;
@@ -301,9 +302,14 @@
        private boolean requestLoggerEnabled;
 
        /**
-        * Whether the comments and whitespace will be stripped from javascript 
resources
+        * Whether the comments and whitespace will be stripped from javascript 
resources.
+        * 
+        * @TODO Remove in 1.5
         */
-       private boolean stripJavascriptCommentsAndWhitespace;
+       private boolean stripJavascriptCommentsAndWhitespace = false;
+
+       /** The Javascript compressor */
+       private IJavascriptCompressor javascriptCompressor;
 
        /**
         * Whether the container's class name should be printed to response (in 
a html comment).
@@ -1251,16 +1257,32 @@
        }
 
        /**
+        * For backwards compatibility reasons, if the return value is true, 
wicket's default javascript
+        * compressor will be used no matter which one was configured via
+        * {...@link #setJavascriptCompressor(IJavascriptCompressor)}.
+        * 
         * @see 
org.apache.wicket.settings.IResourceSettings#getStripJavascriptCommentsAndWhitespace()
+        * 
+        * @deprecated please us {...@link 
#setJavascriptCompressor(IJavascriptCompressor)} instead. Will
+        *             be removed in 1.5
         */
+       @Deprecated
        public boolean getStripJavascriptCommentsAndWhitespace()
        {
                return stripJavascriptCommentsAndWhitespace;
        }
 
        /**
+        * For backwards compatibility reasons, if the return value is true, 
wicket's default javascript
+        * compressor will be used no matter which one was configured via
+        * {...@link #setJavascriptCompressor(IJavascriptCompressor)}.
+        * 
         * @see 
org.apache.wicket.settings.IResourceSettings#setStripJavascriptCommentsAndWhitespace(boolean)
+        * 
+        * @deprecated please us {...@link 
#setJavascriptCompressor(IJavascriptCompressor)} instead. Will
+        *             be removed in 1.5
         */
+       @Deprecated
        public void setStripJavascriptCommentsAndWhitespace(boolean value)
        {
                stripJavascriptCommentsAndWhitespace = value;
@@ -1439,5 +1461,21 @@
                this.outputComponentPath = outputComponentPath;
        }
 
+       /**
+        * @see 
org.apache.wicket.settings.IResourceSettings#getJavascriptCompressor()
+        */
+       public IJavascriptCompressor getJavascriptCompressor()
+       {
+               return javascriptCompressor;
+       }
 
+       /**
+        * @see 
org.apache.wicket.settings.IResourceSettings#setJavascriptCompressor(org.apache.wicket.javascript.IJavascriptCompressor)
+        */
+       public IJavascriptCompressor 
setJavascriptCompressor(IJavascriptCompressor compressor)
+       {
+               IJavascriptCompressor old = javascriptCompressor;
+               javascriptCompressor = compressor;
+               return old;
+       }
 }


Reply via email to