KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > conf > Element


1 /*****************************************************************************
2  * Copyright (C) The Apache Software Foundation. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * This software is published under the terms of the Apache Software License *
5  * version 1.1, a copy of which has been included with this distribution in *
6  * the LICENSE file. *
7  *****************************************************************************/

8  
9 package org.apache.slide.util.conf;
10
11 import java.util.*;
12
13 /**
14  */

15 public class Element {
16
17     private String JavaDoc name;
18     private Hashtable attributes;
19     private Vector children;
20     private String JavaDoc data;
21     private String JavaDoc comment;
22     private Element parent;
23     
24     public Element getParent() {
25         return parent;
26     }
27     
28     public String JavaDoc getName() {
29         return name;
30     }
31
32     public void setName(String JavaDoc s) {
33         name = new String JavaDoc(s);
34     }
35
36     public boolean hasAttributes() {
37         return !attributes.isEmpty();
38     }
39
40     public Enumeration getAttributeNames() {
41         return attributes.keys();
42     }
43
44     public String JavaDoc getAttributeValue(String JavaDoc s) {
45         return (String JavaDoc) attributes.get(s);
46     }
47
48     public void setAttribute(String JavaDoc s, String JavaDoc s1) {
49         attributes.put(s, s1);
50     }
51
52     public void removeAttribute(String JavaDoc s) {
53         attributes.remove(s);
54     }
55
56     public void clearAttributes() {
57         attributes.clear();
58     }
59
60     public Enumeration getChildren() {
61         return children.elements();
62     }
63
64     public Enumeration getChildren(String JavaDoc s) {
65         Vector vector = new Vector();
66         for(Enumeration enumeration = getChildren(); enumeration.hasMoreElements();) {
67             Element element = (Element) enumeration.nextElement();
68             if(element.getName().equals(s))
69                 vector.addElement(element);
70         }
71         return vector.elements();
72     }
73
74     public Element getChild(String JavaDoc name) {
75         Enumeration enumeration = getChildren(name);
76         if (!enumeration.hasMoreElements()) {
77             return null;
78         }
79         return (Element) enumeration.nextElement();
80     }
81
82     public boolean hasChildren() {
83         return !children.isEmpty();
84     }
85
86     public int countChildren() {
87         return children.size();
88     }
89
90     public void addChild(Element element)
91     throws NullPointerException JavaDoc {
92         if(element == null) {
93             throw new NullPointerException JavaDoc("Child cannot be null");
94         }
95         children.addElement(element);
96     }
97
98     public void clearChildren() {
99         children.removeAllElements();
100     }
101
102     public String JavaDoc getData() {
103         return data;
104     }
105
106     public void setData(Object JavaDoc s) {
107         data = new String JavaDoc(s.toString()).trim();
108     }
109
110     public void clearData() {
111         data = new String JavaDoc();
112     }
113
114     public String JavaDoc getComment() {
115         return comment;
116     }
117
118     public void setComment(String JavaDoc s) {
119         comment = new String JavaDoc(s);
120     }
121
122     public void clearComment() {
123         comment = new String JavaDoc();
124     }
125
126     public Element() {
127         this("", null);
128     }
129
130     public Element(String JavaDoc s, Element parent) {
131         this.parent = parent;
132         name = new String JavaDoc(s);
133         attributes = new Hashtable();
134         children = new Vector();
135         data = new String JavaDoc();
136         comment = new String JavaDoc();
137     }
138     
139     public String JavaDoc toString() {
140         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
141         buffer.append("Name=" + name);
142         buffer.append(" Data=" + data);
143         for (Enumeration e = getChildren(); e.hasMoreElements(); ) {
144             Element el = (Element) e.nextElement();
145             buffer.append(" " + el.toString());
146         }
147         return buffer.toString();
148     }
149 }
150
Popular Tags