Repository: wicket Updated Branches: refs/heads/master d39ebcd1b -> be3ed3d38
Compression - Allow multiple ICssCompressors and IJavaScriptCompressors - CssUrlReplacer Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/9752eac7 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/9752eac7 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/9752eac7 Branch: refs/heads/master Commit: 9752eac7fb63e83ffceb9969a1b4f6aa9305b5e7 Parents: f0340a3 Author: klopfdreh <klopfdreh@tobiass-mbp> Authored: Wed Feb 4 13:15:11 2015 +0100 Committer: klopfdreh <klopfdreh@tobiass-mbp> Committed: Wed Feb 4 13:15:11 2015 +0100 ---------------------------------------------------------------------- .../wicket/resource/CompositeCssCompressor.java | 82 +++++++++++ .../resource/CompositeJavaScriptCompressor.java | 79 +++++++++++ .../resource/CssUrlReplacementCompressor.java | 140 +++++++++++++++++++ 3 files changed, 301 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/9752eac7/wicket-core/src/main/java/org/apache/wicket/resource/CompositeCssCompressor.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/resource/CompositeCssCompressor.java b/wicket-core/src/main/java/org/apache/wicket/resource/CompositeCssCompressor.java new file mode 100644 index 0000000..7b3c6e8 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/resource/CompositeCssCompressor.java @@ -0,0 +1,82 @@ +/* + * 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.resource; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.wicket.css.ICssCompressor; + +/** + * Used to apply several {@link ICssCompressor} to the css compression.<br> + * <br> + * Usage: + * + * <pre> + * CompositeCssCompressor compositeCssCompressor = new CompositeCssCompressor(); + * + * compositeCssCompressor.getCompressors().add(new MyCssCompressor()); + * compositeCssCompressor.getCompressors().add(new AnotherCssCompressor()); + * + * this.getResourceSettings().setCssCompressor(compositeCssCompressor); + * </pre> + * @since 6.20.0 + * @author Tobias Soloschenko + * + */ +public class CompositeCssCompressor implements ICssCompressor +{ + + /* Compressors to compress the CSS content */ + private List<ICssCompressor> compressors; + + /** + * Compresses the given original content in the order of compressors. + */ + @Override + public String compress(String original) + { + if (compressors != null) + { + String compressed = original; + for (ICssCompressor compressor : compressors) + { + compressed = compressor.compress(compressed); + } + return compressed; + } + else + { + return original; + } + } + + /** + * Gets a list of {@link ICssCompressor} to be used for CSS compression. They are applied in the + * order of the List. + * + * @return A list of {@link ICssCompressor} to be used for CSS compression. + */ + public List<ICssCompressor> getCompressors() + { + if (compressors == null) + { + compressors = new ArrayList<>(); + } + return compressors; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/9752eac7/wicket-core/src/main/java/org/apache/wicket/resource/CompositeJavaScriptCompressor.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/resource/CompositeJavaScriptCompressor.java b/wicket-core/src/main/java/org/apache/wicket/resource/CompositeJavaScriptCompressor.java new file mode 100644 index 0000000..13d5efc --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/resource/CompositeJavaScriptCompressor.java @@ -0,0 +1,79 @@ +/* + * 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.resource; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.wicket.javascript.IJavaScriptCompressor; + +/** + * Used to apply several {@link IJavaScriptCompressor} to the javascript compression.<br> + * <br> + * Usage: + * + * <pre> + * CompositeJavaScriptCompressor compositeJavaScriptCompressor = new CompositeJavaScriptCompressor(); + * + * compositeJavaScriptCompressor.getCompressors().add(new MyJavaScriptCompressor()); + * compositeJavaScriptCompressor.getCompressors().add(new AnotherJavaScriptCompressor()); + * + * this.getResourceSettings().setJavaScriptCompressor(compositeJavaScriptCompressor); + * </pre> + * @since 6.20.0 + * @author Tobias Soloschenko + * + */ +public class CompositeJavaScriptCompressor implements IJavaScriptCompressor +{ + + /* Compressors to compress javascript content */ + private List<IJavaScriptCompressor> compressors; + + @Override + public String compress(String original) + { + if (compressors != null) + { + String compressed = original; + for (IJavaScriptCompressor compressor : compressors) + { + compressed = compressor.compress(compressed); + } + return compressed; + } + else + { + return original; + } + } + + /** + * Gets a list of {@link IJavaScriptCompressor} to be used for javascript compression. They are applied in the + * order of the List. + * + * @return A list of {@link IJavaScriptCompressor} to be used for javascript compression. + */ + public List<IJavaScriptCompressor> getCompressors() + { + if (compressors == null) + { + compressors = new ArrayList<>(); + } + return compressors; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/9752eac7/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacementCompressor.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacementCompressor.java b/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacementCompressor.java new file mode 100644 index 0000000..eac8d23 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacementCompressor.java @@ -0,0 +1,140 @@ +/* + * 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.resource; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.wicket.Application; +import org.apache.wicket.Component; +import org.apache.wicket.Page; +import org.apache.wicket.WicketRuntimeException; +import org.apache.wicket.application.IComponentInitializationListener; +import org.apache.wicket.css.ICssCompressor; +import org.apache.wicket.request.cycle.RequestCycle; +import org.apache.wicket.request.resource.PackageResourceReference; + +/** + * This compressor is used to replace url within css files with resources that belongs to their + * corresponding page classes. The compress method is not compressing any content, but replacing the URLs + * with Wicket representatives.<br> + * <br> + * Usage: + * + * <pre> + * this.getResourceSettings().setCssCompressor(new CssUrlReplacementCompressor(this)); + * </pre> + * + * @since 6.20.0 + * @author Tobias Soloschenko + * + */ +public class CssUrlReplacementCompressor implements ICssCompressor +{ + + // Holds the names of pages + private List<String> pageNames = new ArrayList<String>(); + + // The pattern to find URLs in CSS resources + private Pattern urlPattern = Pattern.compile("url\\(['|\"](.*)['|\"]\\)"); + + public CssUrlReplacementCompressor(Application application) + { + + // Create an instantiation listener which filters only pages. + application.getComponentInitializationListeners().add( + new IComponentInitializationListener() + { + + @Override + public void onInitialize(Component component) + { + if (Page.class.isAssignableFrom(component.getClass())) + { + CssUrlReplacementCompressor.this.pageNames.add(component.getClass().getName()); + } + } + }); + } + + /** + * Replaces the URLs of CSS resources with Wicket representatives. + */ + @SuppressWarnings("unchecked") + @Override + public String compress(String original) + { + Matcher matcher = this.urlPattern.matcher(original); + // Search for urls + while (matcher.find()) + { + for (String pageName : this.pageNames) + { + try + { + Class<Page> pageClass = (Class<Page>)Class.forName(pageName); + String url = matcher.group(1); + if (!url.contains("/")) + { + URL urlResource = pageClass.getResource(url); + // If the resource is not found for a page skip it + if (urlResource != null) + { + PackageResourceReference packageResourceReference = new PackageResourceReference( + pageClass, url); + String replacedUrl = RequestCycle.get() + .urlFor(packageResourceReference, null) + .toString(); + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append("url('"); + urlBuilder.append(replacedUrl); + urlBuilder.append("')"); + original = matcher.replaceFirst(urlBuilder.toString()); + } + } + } + catch (Exception e) + { + StringWriter stringWriter = this.printStack(e); + throw new WicketRuntimeException(stringWriter.toString()); + } + } + + } + return original; + } + + /** + * Prints the stack trace to a print writer + * + * @param exception + * the exception + * @return the string writer containing the stack trace + */ + private StringWriter printStack(Exception exception) + { + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + exception.printStackTrace(printWriter); + return stringWriter; + } +}
