• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2  *
3  * This program and the accompanying materials are made available under
4  * the terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  *
7  * $Id: AttributeSet.java,v 1.1.1.1 2004/05/09 16:57:41 vlad_r Exp $
8  */
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 // ----------------------------------------------------------------------------
18 /**
19  * @author Vlad Roubtsov, (C) 2003
20  */
21 public
22 abstract class AttributeSet implements IContent
23 {
24     // public: ................................................................
25 
create()26     public static AttributeSet create ()
27     {
28         return new AttributeSetImpl ();
29     }
30 
31     // ACCESSORS:
32 
isEmpty()33     public abstract boolean isEmpty ();
34 
35     // MUTATORS:
36 
set(Attribute attr, String value)37     public abstract AttributeSet set (Attribute attr, String value);
set(Attribute attr, int value)38     public abstract AttributeSet set (Attribute attr, int value);
39 
40     // protected: .............................................................
41 
42     // package: ...............................................................
43 
44 
AttributeSet()45     AttributeSet () {}
46 
47     // private: ...............................................................
48 
49 
50     private static final class AttributeSetImpl extends AttributeSet
51     {
emit(final HTMLWriter out)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 
isEmpty()82         public boolean isEmpty ()
83         {
84             return m_attrMap.isEmpty ();
85         }
86 
87 
set(final Attribute attr, final String value)88         public AttributeSet set (final Attribute attr, final String value) // null removes?
89         {
90             m_attrMap.put (attr, value);
91 
92             return this;
93         }
94 
set(final Attribute attr, final int value)95         public AttributeSet set (final Attribute attr, final int value)
96         {
97             m_attrMap.put (attr, new Integer (value)); // TODO: use int factory here
98 
99             return this;
100         }
101 
102 
AttributeSetImpl()103         AttributeSetImpl ()
104         {
105             m_attrMap = new HashMap ();
106         }
107 
108         // TODO: consider lazy-initing this
109         private final Map /* Attribute->String|Integer */ m_attrMap; // never null
110         private StringBuffer m_buf; // reused by emit()
111 
112         private static final int MAX_BUF_LENGTH = 4 * 1024;
113 
114     } // end of nested class
115 
116 } // end of class
117 // ----------------------------------------------------------------------------