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; 018 019import java.io.Serializable; 020import java.util.Collections; 021import java.util.Set; 022import java.util.TreeSet; 023import java.util.regex.Pattern; 024 025import org.apache.wicket.behavior.AttributeAppender; 026import org.apache.wicket.util.string.Strings; 027 028/** 029 * An AttributeModifier specialized in managing the <em>CSS class</em> 030 * attribute 031 */ 032public abstract class ClassAttributeModifier extends AttributeAppender 033{ 034 private static final Pattern SPLITTER = Pattern.compile("\\s+"); 035 036 /** 037 * Constructor. 038 */ 039 public ClassAttributeModifier() 040 { 041 super("class", null, " "); 042 } 043 044 @Override 045 protected Serializable newValue(String currentValue, String appendValue) 046 { 047 String[] classes; 048 if (Strings.isEmpty(currentValue)) 049 { 050 classes = new String[0]; 051 } 052 else 053 { 054 classes = SPLITTER.split(currentValue.trim()); 055 } 056 Set<String> oldClasses = new TreeSet<>(); 057 Collections.addAll(oldClasses, classes); 058 059 Set<String> newClasses = update(oldClasses); 060 061 String separator = getSeparator(); 062 StringBuilder result = new StringBuilder(); 063 for (String cls : newClasses) 064 { 065 if (result.length() > 0) 066 { 067 result.append(separator); 068 } 069 result.append(cls); 070 } 071 return result.length() > 0 ? result.toString() : VALUELESS_ATTRIBUTE_REMOVE; 072 } 073 074 /** 075 * Callback to update the CSS class values for a tag. 076 * 077 * @param oldClasses 078 * A set with the old class values 079 * @return A set with the new class values 080 */ 081 protected abstract Set<String> update(Set<String> oldClasses); 082}