1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
54: public abstract class DomHTMLElement
55: extends DomElement
56: implements HTMLElement
57: {
58:
59: protected DomHTMLElement(DomHTMLDocument owner, String namespaceURI,
60: String name)
61: {
62: super(owner, namespaceURI, name);
63: }
64:
65:
69: protected String getHTMLAttribute(String name)
70: {
71: if (hasAttributes())
72: {
73: NamedNodeMap attrs = getAttributes();
74: int len = attrs.getLength();
75: for (int i = 0; i < len; i++)
76: {
77: Node attr = attrs.item(i);
78: String attrName = attr.getLocalName();
79: if (attrName == null)
80: {
81: attrName = attr.getNodeName();
82: }
83: if (attrName.equalsIgnoreCase(name))
84: {
85: return attr.getNodeValue();
86: }
87: }
88: }
89: return "";
90: }
91:
92: protected int getIntHTMLAttribute(String name)
93: {
94: String value = getHTMLAttribute(name);
95: if (value == null)
96: {
97: return -1;
98: }
99: try
100: {
101: return Integer.parseInt(value);
102: }
103: catch (NumberFormatException e)
104: {
105: return -1;
106: }
107: }
108:
109: protected boolean getBooleanHTMLAttribute(String name)
110: {
111: String value = getHTMLAttribute(name);
112: return value != null;
113: }
114:
115:
119: protected void setHTMLAttribute(String name, String value)
120: {
121: Node attr;
122: NamedNodeMap attrs = getAttributes();
123: int len = attrs.getLength();
124: for (int i = 0; i < len; i++)
125: {
126: attr = attrs.item(i);
127: String attrName = attr.getLocalName();
128: if (attrName == null)
129: {
130: attrName = attr.getNodeName();
131: }
132: if (attrName.equalsIgnoreCase(name))
133: {
134: if (value != null)
135: {
136: attr.setNodeValue(value);
137: }
138: else
139: {
140: attrs.removeNamedItem(attr.getNodeName());
141: }
142: return;
143: }
144: }
145: if (value != null)
146: {
147:
148: DomHTMLDocument doc = (DomHTMLDocument) getOwnerDocument();
149:
150: attr = doc.createAttribute(name);
151: attr.setNodeValue(value);
152: }
153: }
154:
155: protected void setIntHTMLAttribute(String name, int value)
156: {
157: setHTMLAttribute(name, Integer.toString(value));
158: }
159:
160: protected void setBooleanHTMLAttribute(String name, boolean value)
161: {
162: setHTMLAttribute(name, value ? name : null);
163: }
164:
165:
168: protected Node getParentElement(String name)
169: {
170: for (Node parent = getParentNode(); parent != null;
171: parent = parent.getParentNode())
172: {
173: String parentName = parent.getLocalName();
174: if (parentName == null)
175: {
176: parentName = parent.getNodeName();
177: }
178: if (name.equalsIgnoreCase(parentName))
179: {
180: return parent;
181: }
182: }
183: return null;
184: }
185:
186:
189: protected Node getChildElement(String name)
190: {
191: for (Node child = getFirstChild(); child != null;
192: child = child.getNextSibling())
193: {
194: String childName = child.getLocalName();
195: if (childName == null)
196: {
197: childName = child.getLocalName();
198: }
199: if (name.equalsIgnoreCase(childName))
200: {
201: return child;
202: }
203: }
204: return null;
205: }
206:
207:
211: protected int getIndex()
212: {
213: int index = 0;
214: Node parent = getParentNode();
215: if (parent != null)
216: {
217: for (Node ctx = parent.getFirstChild(); ctx != null;
218: ctx = ctx.getNextSibling())
219: {
220: if (ctx == this)
221: {
222: return index;
223: }
224: index++;
225: }
226: }
227: throw new DomDOMException(DOMException.NOT_FOUND_ERR);
228: }
229:
230: protected void dispatchUIEvent(String name)
231: {
232: UIEvent event = new DomEvent.DomUIEvent(name);
233: dispatchEvent(event);
234: }
235:
236: public String getId()
237: {
238: return getHTMLAttribute("id");
239: }
240:
241: public void setId(String id)
242: {
243: setHTMLAttribute("id", id);
244: }
245:
246: public String getTitle()
247: {
248: return getHTMLAttribute("title");
249: }
250:
251: public void setTitle(String title)
252: {
253: setHTMLAttribute("title", title);
254: }
255:
256: public String getLang()
257: {
258: return getHTMLAttribute("lang");
259: }
260:
261: public void setLang(String lang)
262: {
263: setHTMLAttribute("lang", lang);
264: }
265:
266: public String getDir()
267: {
268: return getHTMLAttribute("dir");
269: }
270:
271: public void setDir(String dir)
272: {
273: setHTMLAttribute("dir", dir);
274: }
275:
276: public String getClassName()
277: {
278: return getHTMLAttribute("class");
279: }
280:
281: public void setClassName(String className)
282: {
283: setHTMLAttribute("class", className);
284: }
285:
286: }