001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.resource; 018 019import java.util.ArrayList; 020import java.util.Arrays; 021import java.util.List; 022 023import org.apache.wicket.css.ICssCompressor; 024 025/** 026 * Used to apply several {@link ICssCompressor} to the CSS compression.<br> 027 * <br> 028 * Usage: 029 * 030 * <pre> 031 * CompositeCssCompressor compositeCssCompressor = new CompositeCssCompressor(); 032 * 033 * compositeCssCompressor.add(new MyCssCompressor()); 034 * compositeCssCompressor.add(new AnotherCssCompressor()); 035 * 036 * this.getResourceSettings().setCssCompressor(compositeCssCompressor); 037 * </pre> 038 * The compressors can also be given as constructor arguments. 039 * 040 * @since 6.20.0 041 * @author Tobias Soloschenko 042 */ 043public class CompositeCssCompressor implements IScopeAwareTextResourceProcessor, ICssCompressor 044{ 045 /* Compressors to compress the CSS content */ 046 private final List<ICssCompressor> compressors = new ArrayList<>(); 047 048 /** 049 * Initializes the composite CSS compressor with the given {@link ICssCompressor}(s) 050 * 051 * @param compressors 052 * The {@link ICssCompressor}(s) this composite CSS compressor is initialized with 053 */ 054 public CompositeCssCompressor(ICssCompressor... compressors) 055 { 056 this.compressors.addAll(Arrays.asList(compressors)); 057 } 058 059 /** 060 * Compresses the given original content in the order of compressors. If no compressor has been 061 * given the original content is going to be returned. 062 */ 063 @Override 064 public String process(String input, Class<?> scope, String name) 065 { 066 String compressed = input; 067 for (ICssCompressor compressor : compressors) 068 { 069 if (compressor instanceof IScopeAwareTextResourceProcessor) 070 { 071 IScopeAwareTextResourceProcessor processor = (IScopeAwareTextResourceProcessor)compressor; 072 compressed = processor.process(compressed, scope, name); 073 } 074 else 075 { 076 compressed = compressor.compress(compressed); 077 } 078 } 079 return compressed; 080 } 081 082 @Override 083 public String compress(String original) 084 { 085 throw new UnsupportedOperationException(CompositeCssCompressor.class.getSimpleName() + ".process() should be used instead!"); 086 } 087 088 /** 089 * Adds a ICssCompressor to the list of delegates. 090 * 091 * @return {@code this} instance, for chaining 092 */ 093 public CompositeCssCompressor add(ICssCompressor compressor) 094 { 095 compressors.add(compressor); 096 return this; 097 } 098}