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.util.value; 018 019import java.util.Map; 020 021import org.apache.wicket.util.string.AppendingStringBuffer; 022import org.apache.wicket.util.string.Strings; 023 024/** 025 * Map of values, extending {@link ValueMap} with methods for generating (HTML) markup attributes. 026 * 027 * @author Eelco Hillenius 028 * @since 1.2.6 029 * 030 * @see Strings#escapeMarkup(CharSequence) 031 */ 032public final class AttributeMap extends ValueMap 033{ 034 private static final long serialVersionUID = 1L; 035 036 /** 037 * Constructs an empty <code>AttributeMap</code>. 038 */ 039 public AttributeMap() 040 { 041 super(); 042 } 043 044 /** 045 * Copy constructor. 046 * 047 * @param map 048 * a <code>Map</code> to be copied 049 */ 050 public AttributeMap(final Map<String, Object> map) 051 { 052 super(map); 053 } 054 055 /** 056 * Put a boolean attribute, removing it if {@code value} is false or using the key as value otherwise, 057 * i.e. {@code value="value"}. 058 * 059 * @param key 060 * key of attribute 061 * @param value 062 * @return previous value 063 */ 064 public boolean putAttribute(final String key, final boolean value) 065 { 066 Object previous = get(key); 067 if (value) 068 { 069 put(key, key); 070 } 071 else 072 { 073 remove(key); 074 } 075 return key.equals(previous); 076 } 077 078 /** 079 * Put a string attribute, removing it if the string is empty (nor null). 080 * 081 * @param key 082 * key of attribute 083 * @param value 084 * @return previous value 085 */ 086 public String putAttribute(String key, CharSequence value) 087 { 088 if (Strings.isEmpty(value)) 089 { 090 return (String)remove(key); 091 } 092 else 093 { 094 return (String)put(key, value); 095 } 096 } 097 098 /** 099 * Representation as encoded markup attributes. 100 * 101 * @see Strings#escapeMarkup(CharSequence) 102 */ 103 public String toString() 104 { 105 return toCharSequence().toString(); 106 } 107 108 /** 109 * Representation as encoded markup attributes. 110 * 111 * @see Strings#escapeMarkup(CharSequence) 112 */ 113 public CharSequence toCharSequence() 114 { 115 final AppendingStringBuffer buffer = new AppendingStringBuffer(); 116 117 for (String key : keySet()) 118 { 119 if (key != null) { 120 buffer.append(' '); 121 buffer.append(Strings.escapeMarkup(key)); 122 123 CharSequence value = getCharSequence(key); 124 if (value != null) { 125 buffer.append("=\""); 126 buffer.append(Strings.escapeMarkup(value)); 127 buffer.append('"'); 128 } 129 } 130 } 131 132 return buffer; 133 } 134}