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: HTMLDocument.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 com.vladium.util.IConstants;
12
13// ----------------------------------------------------------------------------
14/**
15 * @author Vlad Roubtsov, (C) 2003
16 */
17public
18final class HTMLDocument extends IElement.Factory.ElementImpl
19{
20    // public: ................................................................
21
22
23    public HTMLDocument ()
24    {
25        this (null, null);
26    }
27
28    public HTMLDocument (final String title, final String encoding)
29    {
30        super (Tag.HTML, AttributeSet.create ());
31
32        super.add (m_head = IElement.Factory.create (Tag.HEAD));
33        super.add (m_body = IElement.Factory.create (Tag.BODY));
34
35        // specify encoding META before anything else:
36        if ((encoding != null) && (encoding.length () != 0))
37        {
38            final ISimpleElement meta = ISimpleElement.Factory.create (Tag.META);
39
40            meta.getAttributes ()
41            .set (Attribute.HTTP_EQUIV, "Content-Type")
42            .set (Attribute.CONTENT, "text/html; charset=" + encoding);
43
44            m_head.add (meta);
45        }
46
47        if (title != null)
48        {
49            // TODO: escape
50            //getAttributes ().set (Attribute.TITLE, title);
51
52            final IElement titleElement = IElement.Factory.create (Tag.TITLE).setText (title, false);
53            m_head.add (titleElement);
54        }
55
56        m_title = title;
57    }
58
59    public String getTitle ()
60    {
61        return m_title;
62    }
63
64    public IElement getHead ()
65    {
66        return m_head;
67    }
68
69    public IElement getBody ()
70    {
71        return m_body;
72    }
73
74    public IContent getHeader ()
75    {
76        return m_header;
77    }
78
79    public IContent getFooter ()
80    {
81        return m_footer;
82    }
83
84
85    public void setHeader (final IContent header)
86    {
87        if (header != null) m_header = header;
88    }
89
90    public void setFooter (final IContent footer)
91    {
92        if (footer != null) m_footer = footer;
93    }
94
95    /**
96     * Overridden to ensure header/footer appear first/last in the body.
97     */
98    public void emit (HTMLWriter out)
99    {
100        if (m_header != null) m_body.add (0, m_header);
101        if (m_footer != null) m_body.add (m_body.size (), m_footer);
102
103        super.emit(out);
104    }
105
106    /**
107     * Overridden to add to the doc body.
108     */
109    public IElementList add (final IContent content)
110    {
111        m_body.add (content);
112
113        return this;
114    }
115
116    public void addStyle (final String css)
117    {
118        if (css != null)
119        {
120            final IElement style = IElement.Factory.create (Tag.STYLE);
121            style.getAttributes ().set (Attribute.TYPE, "text/css");
122
123            final StringBuffer def = new StringBuffer ("<!--");
124            def.append (IConstants.EOL);
125
126            style.setText (css, false);
127
128            def.append (IConstants.EOL);
129            def.append ("-->");
130
131            m_head.add (style);
132        }
133    }
134
135    /**
136     * Adds a &lt;LINK&gt; to the head.
137     */
138    public void addLINK (final String type, final String href)
139    {
140        final ISimpleElement link = ISimpleElement.Factory.create (Tag.LINK);
141
142        // TODO: add REL="STYLESHEET"
143
144        link.getAttributes ().set (Attribute.TYPE, type); // TODO: escape
145        link.getAttributes ().set (Attribute.HREF, href); // TODO: escape
146        link.getAttributes ().set (Attribute.SRC, href); // TODO: escape
147
148        m_head.add (link);
149    }
150
151    public void addH (final int level, final String text, final String classID)
152    {
153        final Tag Hl = Tag.Hs [level];
154
155        final IElement h = IElement.Factory.create (Hl);
156        h.setText (text, true);
157        h.setClass (classID);
158
159        add (h);
160    }
161
162    public void addH (final int level, final IContent text, final String classID)
163    {
164        final Tag Hl = Tag.Hs [level];
165
166        final IElement h = IElement.Factory.create (Hl);
167        h.add (text);
168        h.setClass (classID);
169
170        add (h);
171    }
172
173    public void addHR (final int size)
174    {
175        final IElement hr = IElement.Factory.create (Tag.HR);
176        hr.getAttributes ().set (Attribute.SIZE, size);
177
178        add (hr);
179    }
180
181    public void addEmptyP ()
182    {
183        add (IElement.Factory.create (Tag.P));
184    }
185
186    // protected: .............................................................
187
188    // package: ...............................................................
189
190    // private: ...............................................................
191
192
193    private final String m_title;
194    private final IElement m_head;
195    private final IElement m_body;
196
197    private IContent m_header, m_footer;
198
199} // end of class
200// ----------------------------------------------------------------------------