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: IElement.java,v 1.1.1.1 2004/05/09 16:57:41 vlad_r Exp $
8 */
9package com.vladium.emma.report.html.doc;
10
11import java.util.ArrayList;
12import java.util.Iterator;
13import java.util.List;
14
15// ----------------------------------------------------------------------------
16/**
17 * @author Vlad Roubtsov, (C) 2003
18 */
19public
20interface IElement extends ISimpleElement, IElementList
21{
22    // public: ................................................................
23
24    IElement setText (String text, boolean nbsp); // size() is 0 after this
25
26    abstract class Factory
27    {
28        public static IElement create (final Tag tag)
29        {
30            return new ElementImpl (tag, AttributeSet.create ());
31        }
32
33        public static IElement create (final Tag tag, final AttributeSet attrs)
34        {
35            return new ElementImpl (tag, attrs);
36        }
37
38        // TODO: should this extend ElementList?
39        static class ElementImpl extends ISimpleElement.Factory.SimpleElementImpl
40                                         implements IElement
41        {
42            public String toString ()
43            {
44                return "<" + m_tag.getName () + ">";
45            }
46
47            public void emit (final HTMLWriter out)
48            {
49                final String tagName = m_tag.getName ();
50
51                out.write ('<');
52                out.write (tagName);
53
54                if (! m_attrs.isEmpty ())
55                {
56                    out.write (' ');
57                    m_attrs.emit (out);
58                }
59
60                out.write ('>');
61
62                for (Iterator c = m_contents.iterator (); c.hasNext (); )
63                {
64                    final IContent content = (IContent) c.next ();
65                    content.emit (out);
66                }
67
68                out.write ("</");
69                out.write (tagName);
70                out.write ('>');
71                if (DEBUG_HTML) out.eol (); // using ENABLED as DEBUG here
72            }
73
74            public IElementList add (final IContent content)
75            {
76                if (content != null)
77                {
78                    m_contents.add (content);
79                }
80
81                return this;
82            }
83
84            public IElementList add (final int index, final IContent content)
85            {
86                if (content != null)
87                {
88                    m_contents.add (index, content);
89                }
90
91                return this;
92            }
93
94            public int size ()
95            {
96                return m_contents.size ();
97            }
98
99            public IElement setText (final String text, final boolean nbsp)
100            {
101                if (text != null)
102                {
103                    m_contents.clear ();
104                    m_contents.add (new Text (text, nbsp));
105                }
106
107                return this;
108            }
109
110            ElementImpl (final Tag tag, final AttributeSet attrs)
111            {
112                super (tag, attrs);
113
114                m_contents = new ArrayList ();
115            }
116
117
118            protected final List /* Content */ m_contents;
119
120            private static final boolean DEBUG_HTML = false;
121
122        } // end of nested class
123
124    } // end of nested class
125
126} // end of interface
127// ----------------------------------------------------------------------------