1 9 package com.vladium.emma.report.html.doc; 10 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import com.vladium.util.Strings; 16 17 21 public 22 abstract class AttributeSet implements IContent 23 { 24 26 public static AttributeSet create () 27 { 28 return new AttributeSetImpl (); 29 } 30 31 33 public abstract boolean isEmpty (); 34 35 37 public abstract AttributeSet set (Attribute attr, String value); 38 public abstract AttributeSet set (Attribute attr, int value); 39 40 42 44 45 AttributeSet () {} 46 47 49 50 private static final class AttributeSetImpl extends AttributeSet 51 { 52 public void emit (final HTMLWriter out) 53 { 54 boolean first = true; 55 for (Iterator a = m_attrMap.entrySet ().iterator (); a.hasNext (); ) 56 { 57 final Map.Entry entry = (Map.Entry ) a.next (); 58 59 final Attribute attr = (Attribute) entry.getKey (); 60 final String value = entry.getValue ().toString (); 61 62 if (first) 63 first = false; 64 else 65 out.write (' '); 66 67 out.write (attr.getName ()); 68 out.write ("=\""); 69 70 if ((m_buf != null) && (m_buf.length () <= MAX_BUF_LENGTH)) 71 m_buf.setLength (0); 72 else 73 m_buf = new StringBuffer (); 74 75 Strings.HTMLEscape (value, m_buf); 76 out.write (m_buf.toString ()); 77 78 out.write ('\"'); 79 } 80 } 81 82 public boolean isEmpty () 83 { 84 return m_attrMap.isEmpty (); 85 } 86 87 88 public AttributeSet set (final Attribute attr, final String value) { 90 m_attrMap.put (attr, value); 91 92 return this; 93 } 94 95 public AttributeSet set (final Attribute attr, final int value) 96 { 97 m_attrMap.put (attr, new Integer (value)); 99 return this; 100 } 101 102 103 AttributeSetImpl () 104 { 105 m_attrMap = new HashMap (); 106 } 107 108 private final Map m_attrMap; private StringBuffer m_buf; 112 private static final int MAX_BUF_LENGTH = 4 * 1024; 113 114 } 116 } | Popular Tags |