KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > report > html > HTMLElement


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.diagnostics.report.html;
24
25 import java.io.File JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33
34 /**
35  * Basic implementation of an element.
36  */

37 public class HTMLElement implements Element {
38     
39     /**
40      * The name of this element.
41      */

42     private String JavaDoc name = null;
43     
44     /**
45      * The children of this element which are not attributes.
46      */

47     private List JavaDoc<HTMLComponent> children = new LinkedList JavaDoc<HTMLComponent>();
48     
49     /**
50      * The attributes for this element.
51      */

52     private List JavaDoc<Attribute> attributes = new LinkedList JavaDoc<Attribute>();
53     
54     private static final char TAG_MARKER_BEGIN = '<';
55     private static final char TAG_MARKER_END = '>';
56     private static final String JavaDoc SLASH_BEGIN="/>";
57     private static final String JavaDoc SLASH_END="</";
58
59     /**
60      * Make a new element, specifying the name.
61      * @param name The name of the element.
62      */

63     public HTMLElement(String JavaDoc name) {
64         setName(name);
65     }
66
67     /**
68      * @param child The node to add.
69      * @return This element.
70      */

71     public void add(HTMLComponent child) {
72         if (child == null) {
73             throw new NullPointerException JavaDoc("Child node is null.");
74         }
75         if (child == this) {
76             throw new IllegalArgumentException JavaDoc("Attempt to add a node " +
77                     "to itself. Node is " + toString() + ".");
78         }
79         if (child instanceof Attribute) {
80             attributes.add((Attribute) child);
81         } else {
82             children.add(child);
83         }
84     }
85     
86
87     /**
88      */

89     public Element addElement(String JavaDoc name) {
90         if (name == null) {
91             throw new NullPointerException JavaDoc("Element name is null.");
92         }
93         Element newElement = new HTMLElement(name);
94         add(newElement);
95         return newElement;
96     }
97
98     /**
99      */

100     public Text addComment(String JavaDoc content) {
101         HTMLComment comment = new HTMLComment(content);
102         add(comment);
103         return comment;
104        
105     }
106
107     /**
108      */

109     public Text addText(String JavaDoc text) {
110         if(text != null) {
111             Text textNode = new HTMLText(text);
112             add(textNode);
113             return textNode;
114         }
115         return null;
116     }
117
118     /**
119      */

120     public void addText(Iterator JavaDoc<String JavaDoc> textValues) {
121         while(textValues.hasNext()) {
122             Text textNode = new HTMLText(textValues.next());
123             add(textNode);
124         }
125     }
126     public Attribute addAttribute(String JavaDoc id, String JavaDoc value) {
127         Attribute att = new HTMLAttribute(id, value);
128         add(att);
129         return att;
130     }
131     /**
132      */

133     public List JavaDoc<Element> getElements() {
134         List JavaDoc<Element> retval = new LinkedList JavaDoc<Element>();
135         for (HTMLComponent node : children) {
136             if (node instanceof Element) {
137                 retval.add((Element) node);
138             }
139         } // Loop over children.
140
return retval;
141     }
142
143
144     /**
145      */

146     public List JavaDoc<Element> getElements(String JavaDoc name) {
147         List JavaDoc<Element> list = new LinkedList JavaDoc<Element>();
148         for (HTMLComponent node : children) {
149             if (node instanceof Element) {
150                 Element element = (Element) node;
151                 if (element.getName().equalsIgnoreCase(name)) {
152                     list.add((Element) node);
153                 }
154             }
155         } // Loop over children.
156
return list;
157     }
158
159     /**
160      */

161     public List JavaDoc<Text> getComments() {
162         List JavaDoc<Text> list = new LinkedList JavaDoc<Text>();
163         for (HTMLComponent node : children) {
164             if (node instanceof HTMLComment) {
165                 list.add((HTMLComment) node);
166             }
167         } // Loop over children.
168
return list;
169     }
170     
171     /**
172      */

173     public List JavaDoc<Text> getTexts() {
174         List JavaDoc<Text> retval = new LinkedList JavaDoc<Text>();
175         for (HTMLComponent node : children) {
176             if (node instanceof Text) {
177                 retval.add((Text) node);
178             }
179         } // Loop over children.
180
return retval;
181     }
182
183     /**
184      */

185     public List JavaDoc<Attribute> getAttributes() {
186         List JavaDoc<Attribute> retval = new LinkedList JavaDoc<Attribute>();
187         retval.addAll(attributes);
188         return retval;
189     }
190
191
192     /**
193      */

194     public List JavaDoc<Attribute> getAttributes(String JavaDoc name) {
195         List JavaDoc<Attribute> retval = new LinkedList JavaDoc<Attribute>();
196         for (HTMLComponent node : getAttributes()) {
197             if (node instanceof Attribute) {
198                 Attribute att = (Attribute) node;
199                 if (att.getName().equalsIgnoreCase(name)) {
200                     retval.add((Attribute) node);
201                 }
202             }
203         } // Loop over children.
204
return retval;
205     }
206
207     /**
208      */

209     public List JavaDoc<HTMLComponent> children() {
210         List JavaDoc<HTMLComponent> childrenList = new LinkedList JavaDoc<HTMLComponent>();
211         childrenList.addAll(children);
212         childrenList.addAll(attributes);
213         return childrenList;
214     }
215
216
217     /**
218      * @param child The node to delete.
219      */

220     public void delete(HTMLComponent child) {
221         if (child instanceof Attribute) {
222             attributes.remove(child);
223         } else {
224             children.remove(child);
225         }
226     }
227     
228
229     /**
230      * @see java.lang.Object#toString()
231      */

232     public String JavaDoc toString() {
233         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
234       
235         String JavaDoc eName = Escape.getInstance().encodeEntities(name, " \t\r\n");
236         buf.append(TAG_MARKER_BEGIN).append(eName);
237         for (Attribute att : attributes) {
238             buf.append(" ").append(att.toString());
239         } // Loop over attributes.
240
if (children.size() == 0) {
241             buf.append(SLASH_BEGIN);
242         } else {
243             buf.append(TAG_MARKER_END);
244             for (HTMLComponent node : children) {
245                 buf.append(node.toString());
246             } // Loop over children.
247
buf.append(SLASH_END).append(eName).append(TAG_MARKER_END);
248         }
249         return buf.toString();
250     }
251
252
253     /**
254      */

255     public void write(Writer JavaDoc output) throws IOException JavaDoc {
256         output.append(toString());
257     output.flush();
258     }
259
260
261     /**
262      */

263     public void write(File JavaDoc file) throws IOException JavaDoc {
264         FileWriter JavaDoc fileWriter = new FileWriter JavaDoc(file);
265         write(fileWriter);
266         fileWriter.close();
267     }
268
269
270     /**
271      */

272     public String JavaDoc getName() {
273         return name;
274     }
275
276
277     /**
278      */

279     public void setName(String JavaDoc name) {
280         if (name == null) {
281             throw new NullPointerException JavaDoc("Element name is null.");
282         }
283         if (name.length() == 0) {
284             throw new IllegalArgumentException JavaDoc("Element name is empty.");
285         }
286         this.name = name;
287     }
288
289
290     /**
291      * Get all children of this element which are of a specified class.
292      * @param type The class of the children to obtain.
293      * @return All children of the specified class.
294      */

295     public <T extends HTMLComponent> List JavaDoc<T> get(Class JavaDoc<T> type) {
296         List JavaDoc<T> list = new LinkedList JavaDoc<T>();
297         for (HTMLComponent child : children()) {
298             if (type.isInstance(child)) {
299                 list.add((T) child);
300             }
301         }
302         return list;
303     }
304  }
305
Popular Tags