summaryrefslogtreecommitdiff
path: root/core/java12/com/vladium/emma/report/html/doc/IElement.java
blob: a1e3ae0dfba666f2372abd20a1e2ff8a79ecf82a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: IElement.java,v 1.1.1.1 2004/05/09 16:57:41 vlad_r Exp $
 */
package com.vladium.emma.report.html.doc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

// ----------------------------------------------------------------------------
/**
 * @author Vlad Roubtsov, (C) 2003
 */
public
interface IElement extends ISimpleElement, IElementList
{
    // public: ................................................................
    
    IElement setText (String text, boolean nbsp); // size() is 0 after this
    
    abstract class Factory
    {
        public static IElement create (final Tag tag)
        {
            return new ElementImpl (tag, AttributeSet.create ());
        }
        
        public static IElement create (final Tag tag, final AttributeSet attrs)
        {
            return new ElementImpl (tag, attrs);
        }
        
        // TODO: should this extend ElementList?
        static class ElementImpl extends ISimpleElement.Factory.SimpleElementImpl
                                         implements IElement
        {
            public String toString ()
            {
                return "<" + m_tag.getName () + ">";
            }
            
            public void emit (final HTMLWriter out)
            {
                final String tagName = m_tag.getName ();
                
                out.write ('<');
                out.write (tagName);
                
                if (! m_attrs.isEmpty ())
                {
                    out.write (' ');
                    m_attrs.emit (out);
                }
                
                out.write ('>');
                
                for (Iterator c = m_contents.iterator (); c.hasNext (); )
                {
                    final IContent content = (IContent) c.next ();
                    content.emit (out);
                }
                
                out.write ("</");
                out.write (tagName);
                out.write ('>');
                if (DEBUG_HTML) out.eol (); // using ENABLED as DEBUG here
            }
                        
            public IElementList add (final IContent content)
            {
                if (content != null)
                {
                    m_contents.add (content);
                }
                
                return this;
            }
            
            public IElementList add (final int index, final IContent content)
            {
                if (content != null)
                {
                    m_contents.add (index, content);
                }
                
                return this;
            }
            
            public int size ()
            {
                return m_contents.size ();
            }
            
            public IElement setText (final String text, final boolean nbsp)
            {
                if (text != null)
                {
                    m_contents.clear ();
                    m_contents.add (new Text (text, nbsp));
                }
                
                return this;
            }
            
            ElementImpl (final Tag tag, final AttributeSet attrs)
            {
                super (tag, attrs);
                
                m_contents = new ArrayList ();
            }

            
            protected final List /* Content */ m_contents;
            
            private static final boolean DEBUG_HTML = false;
                    
        } // end of nested class

    } // end of nested class 
    
} // end of interface
// ----------------------------------------------------------------------------