Skip to main content
summaryrefslogtreecommitdiffstats
blob: d2babce9e54ab538c0f79bcf2c6217fa4cc05ca7 (plain) (blame)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.intro.impl.html;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

/**
 * This class represents an HTML element. An HTML element has a name, a
 * collection of attributes, and content
 */
public class HTMLElement {

	// the name of the element
	private String elementName;

	// a collection of attributes that belong to this element (possibly empty)
	private Map<String, String> elementAttributes;

	// a collection of other HTMLElements or Strings contained inside this
	// element
	private Vector<Object> elementContent;

	public HTMLElement(String name) {
		this.elementName = name;
		this.elementAttributes = new HashMap<>();
		this.elementContent = new Vector<>();
	}

	public HTMLElement(String name, Map<String, String> attributes, Vector<Object> content) {
		this.elementName = name;
		this.elementAttributes = attributes;
		this.elementContent = content;
	}

	/**
	 * Add an attribute with the given name and value to this HTMLElement
	 *
	 * @param attributeName
	 * @param attributeValue
	 */
	public void addAttribute(String attributeName, String attributeValue) {
		if(attributeName != null && attributeValue != null)
			getElementAttributes().put(attributeName, attributeValue);
	}

	/**
	 * Add content to this element. The content should be in the form of
	 * another HTMLElement, or a String
	 */
	public void addContent(Object content) {
		getElementContent().add(content);
	}

	/**
	 * Get the attributes associated with this element
	 *
	 * @return Returns the elementAttributes.
	 */
	public Map<String, String> getElementAttributes() {
		if (elementAttributes == null)
			elementAttributes = new HashMap<>();

		return elementAttributes;
	}

	/**
	 * Set the attributes associated with this element
	 *
	 * @param elementAttributes
	 *            The elementAttributes to set.
	 */
	public void setElementAttributes(Map<String, String> elementAttributes) {
		this.elementAttributes = elementAttributes;
	}

	/**
	 * Get this element's content
	 *
	 * @return Returns the elementContent.
	 */
	public Vector<Object> getElementContent() {
		if (elementContent == null)
			elementContent = new Vector<>();

		return elementContent;
	}

	/**
	 * Set this element's content
	 *
	 * @param elementContent
	 *            The elementContent to set.
	 */
	public void setElementContent(Vector<Object> elementContent) {
		this.elementContent = elementContent;
	}

	/**
	 * Get the name of this element
	 *
	 * @return Returns the elementName.
	 */
	public String getElementName() {
		return elementName;
	}

	/**
	 * Set the name of this element
	 *
	 * @param elementName
	 *            The elementName to set.
	 */
	public void setElementName(String elementName) {
		this.elementName = elementName;
	}

	@Override
	public String toString() {
		StringBuffer element = new StringBuffer();

		// add the start tag and attributes
		element.append(
			HTMLUtil.createHTMLStartTag(
				getElementName(),
				getElementAttributes(),
				false));

		// include the element's content
		for (Iterator it = getElementContent().iterator(); it.hasNext();) {
			Object content = it.next();
			element.append(content);
		}

		// include an end tag
		element.append(HTMLUtil.createHTMLEndTag(getElementName(), false));
		return element.toString();
	}
}

Back to the top