KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > html > HTMLElement


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.html;
12
13 import java.util.Hashtable JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 /**
19  * This class represents an HTML element. An HTML element has a name, a
20  * collection of attributes, and content
21  */

22 public class HTMLElement {
23
24     // the name of the element
25
private String JavaDoc elementName;
26
27     // a collection of attributes that belong to this element (possibly empty)
28
private Map JavaDoc elementAttributes;
29
30     // a collection of other HTMLElements or Strings contained inside this
31
// element
32
private Vector JavaDoc elementContent;
33
34     public HTMLElement(String JavaDoc name) {
35         this.elementName = name;
36         this.elementAttributes = new Hashtable JavaDoc();
37         this.elementContent = new Vector JavaDoc();
38     }
39
40     public HTMLElement(String JavaDoc name, Map JavaDoc attributes, Vector JavaDoc content) {
41         this.elementName = name;
42         this.elementAttributes = attributes;
43         this.elementContent = content;
44     }
45
46     /**
47      * Add an attribute with the given name and value to this HTMLElement
48      *
49      * @param attributeName
50      * @param attributeValue
51      */

52     public void addAttribute(String JavaDoc attributeName, String JavaDoc attributeValue) {
53         if(attributeName != null && attributeValue != null)
54             getElementAttributes().put(attributeName, attributeValue);
55     }
56
57     /**
58      * Add content to this element. The content should be in the form of
59      * another HTMLElement, or a String
60      */

61     public void addContent(Object JavaDoc content) {
62         getElementContent().add(content);
63     }
64
65     /**
66      * Get the attributes associated with this element
67      *
68      * @return Returns the elementAttributes.
69      */

70     public Map JavaDoc getElementAttributes() {
71         if (elementAttributes == null)
72             elementAttributes = new Hashtable JavaDoc();
73
74         return elementAttributes;
75     }
76
77     /**
78      * Set the attributes associated with this element
79      *
80      * @param elementAttributes
81      * The elementAttributes to set.
82      */

83     public void setElementAttributes(Map JavaDoc elementAttributes) {
84         this.elementAttributes = elementAttributes;
85     }
86
87     /**
88      * Get this element's content
89      *
90      * @return Returns the elementContent.
91      */

92     public Vector JavaDoc getElementContent() {
93         if (elementContent == null)
94             elementContent = new Vector JavaDoc();
95
96         return elementContent;
97     }
98
99     /**
100      * Set this element's content
101      *
102      * @param elementContent
103      * The elementContent to set.
104      */

105     public void setElementContent(Vector JavaDoc elementContent) {
106         this.elementContent = elementContent;
107     }
108
109     /**
110      * Get the name of this element
111      *
112      * @return Returns the elementName.
113      */

114     public String JavaDoc getElementName() {
115         return elementName;
116     }
117
118     /**
119      * Set the name of this element
120      *
121      * @param elementName
122      * The elementName to set.
123      */

124     public void setElementName(String JavaDoc elementName) {
125         this.elementName = elementName;
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see java.lang.Object#toString()
132      */

133     public String JavaDoc toString() {
134         StringBuffer JavaDoc element = new StringBuffer JavaDoc();
135
136         // add the start tag and attributes
137
element.append(
138             HTMLUtil.createHTMLStartTag(
139                 getElementName(),
140                 getElementAttributes(),
141                 false));
142
143         // include the element's content
144
for (Iterator JavaDoc it = getElementContent().iterator(); it.hasNext();) {
145             Object JavaDoc content = it.next();
146             element.append(content);
147         }
148
149         // include an end tag
150
element.append(HTMLUtil.createHTMLEndTag(getElementName(), false));
151         return element.toString();
152     }
153 }
154
Popular Tags