KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > ElementImpl


1 /*
2  GNU LESSER GENERAL PUBLIC LICENSE
3  Copyright (C) 2006 The Lobo Project
4
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19  Contact info: xamjadmin@users.sourceforge.net
20  */

21 /*
22  * Created on Oct 29, 2005
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import java.util.*;
27
28 import org.lobobrowser.util.*;
29 import org.w3c.dom.Attr JavaDoc;
30 import org.w3c.dom.DOMException JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.NamedNodeMap JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35 import org.w3c.dom.TypeInfo JavaDoc;
36 import org.w3c.dom.Text JavaDoc;
37 import org.w3c.dom.Comment JavaDoc;
38
39 public class ElementImpl extends NodeImpl implements Element JavaDoc {
40     private final String JavaDoc name;
41
42     public ElementImpl(String JavaDoc name) {
43         super();
44         this.name = name;
45     }
46
47     protected Map JavaDoc attributes;
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.xamjwg.html.domimpl.NodeImpl#getattributes()
53      */

54     public NamedNodeMap JavaDoc getAttributes() {
55         synchronized(this) {
56             Map JavaDoc attrs = this.attributes;
57             if (attrs == null) {
58                 attrs = new HashMap();
59                 this.attributes = attrs;
60             }
61             return new NamedNodeMapImpl(this, this.attributes);
62         }
63     }
64
65     public boolean hasAttributes() {
66         synchronized(this) {
67             Map JavaDoc attrs = this.attributes;
68             return attrs == null ? false : !attrs.isEmpty();
69         }
70     }
71
72     public boolean equalAttributes(Node JavaDoc arg) {
73         if (arg instanceof ElementImpl) {
74             synchronized(this) {
75                 Map JavaDoc attrs1 = this.attributes;
76                 if (attrs1 == null) {
77                     attrs1 = Collections.EMPTY_MAP;
78                 }
79                 Map JavaDoc attrs2 = ((ElementImpl) arg).attributes;
80                 if (attrs2 == null) {
81                     attrs2 = Collections.EMPTY_MAP;
82                 }
83                 return Objects.equals(attrs1, attrs2);
84             }
85         } else {
86             return false;
87         }
88     }
89
90     private String JavaDoc id;
91
92     public String JavaDoc getId() {
93         return this.id;
94     }
95
96     public void setId(String JavaDoc id) {
97         this.setAttribute("id", id);
98     }
99
100     //private String title;
101

102     public String JavaDoc getTitle() {
103         return this.getAttribute("title");
104     }
105
106     public void setTitle(String JavaDoc title) {
107         this.setAttribute("title", title);
108     }
109
110     public String JavaDoc getLang() {
111         return this.getAttribute("lang");
112     }
113
114     public void setLang(String JavaDoc lang) {
115         this.setAttribute("lang", lang);
116     }
117
118     public String JavaDoc getDir() {
119         return this.getAttribute("dir");
120     }
121
122     public void setDir(String JavaDoc dir) {
123         this.setAttribute("dir", dir);
124     }
125
126     public final String JavaDoc getAttribute(String JavaDoc name) {
127         String JavaDoc normalName = this.normalizeAttributeName(name);
128         synchronized(this) {
129             Map JavaDoc attributes = this.attributes;
130             return attributes == null ? null : (String JavaDoc) attributes
131                     .get(normalName);
132         }
133     }
134
135     private Attr JavaDoc getAttr(String JavaDoc normalName, String JavaDoc value) {
136         // TODO: "specified" attributes
137
return new AttrImpl(normalName, value, true, this, "id"
138                 .equals(normalName));
139     }
140
141     public Attr JavaDoc getAttributeNode(String JavaDoc name) {
142         String JavaDoc normalName = this.normalizeAttributeName(name);
143         synchronized(this) {
144             Map JavaDoc attributes = this.attributes;
145             String JavaDoc value = attributes == null ? null : (String JavaDoc) attributes
146                     .get(normalName);
147             return value == null ? null : this.getAttr(normalName, value);
148         }
149     }
150
151     public Attr JavaDoc getAttributeNodeNS(String JavaDoc namespaceURI, String JavaDoc localName)
152             throws DOMException JavaDoc {
153         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
154                 "Namespaces not supported");
155     }
156
157     public String JavaDoc getAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName)
158             throws DOMException JavaDoc {
159         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
160                 "Namespaces not supported");
161     }
162
163     protected static boolean isTagName(Node JavaDoc node, String JavaDoc name) {
164         return node.getNodeName().equalsIgnoreCase(name);
165     }
166
167     public NodeList JavaDoc getElementsByTagName(String JavaDoc name) {
168         boolean matchesAll = "*".equals(name);
169         List JavaDoc descendents = new LinkedList();
170         synchronized(this.treeLock) {
171             ArrayList nl = this.nodeList;
172             if(nl != null) {
173                 Iterator i = nl.iterator();
174                 while (i.hasNext()) {
175                     Object JavaDoc child = i.next();
176                     if (child instanceof Element JavaDoc) {
177                         Element JavaDoc childElement = (Element JavaDoc) child;
178                         if (matchesAll || isTagName(childElement, name)) {
179                             descendents.add(child);
180                         }
181                         NodeList JavaDoc sublist = childElement.getElementsByTagName(name);
182                         int length = sublist.getLength();
183                         for (int idx = 0; idx < length; idx++) {
184                             descendents.add(sublist.item(idx));
185                         }
186                     }
187                 }
188             }
189         }
190         return new NodeListImpl(descendents);
191     }
192
193     public NodeList JavaDoc getElementsByTagNameNS(String JavaDoc namespaceURI, String JavaDoc localName)
194             throws DOMException JavaDoc {
195         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
196                 "Namespaces not supported");
197     }
198
199     public TypeInfo JavaDoc getSchemaTypeInfo() {
200         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
201                 "Namespaces not supported");
202     }
203
204     public String JavaDoc getTagName() {
205         return this.getNodeName();
206     }
207
208     public boolean hasAttribute(String JavaDoc name) {
209         String JavaDoc normalName = this.normalizeAttributeName(name);
210         synchronized(this) {
211             Map JavaDoc attributes = this.attributes;
212             return attributes == null ? false : attributes
213                     .containsKey(normalName);
214         }
215     }
216
217     public boolean hasAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName)
218             throws DOMException JavaDoc {
219         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
220                 "Namespaces not supported");
221     }
222
223     public void removeAttribute(String JavaDoc name) throws DOMException JavaDoc {
224         String JavaDoc normalName = this.normalizeAttributeName(name);
225         synchronized(this) {
226             Map JavaDoc attributes = this.attributes;
227             if (attributes == null) {
228                 return;
229             }
230             attributes.remove(normalName);
231         }
232     }
233
234     public Attr JavaDoc removeAttributeNode(Attr JavaDoc oldAttr) throws DOMException JavaDoc {
235         String JavaDoc normalName = this.normalizeAttributeName(oldAttr.getName());
236         synchronized(this) {
237             Map JavaDoc attributes = this.attributes;
238             if (attributes == null) {
239                 return null;
240             }
241             String JavaDoc oldValue = (String JavaDoc) attributes.remove(normalName);
242             // TODO: "specified" attributes
243
return oldValue == null ? null : this.getAttr(normalName, oldValue);
244         }
245     }
246
247     public void removeAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName)
248             throws DOMException JavaDoc {
249         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
250                 "Namespaces not supported");
251     }
252
253     protected void assignAttributeField(String JavaDoc normalName, String JavaDoc value) {
254         // Note: overriders assume that processing here is only done after
255
// checking attribute names, i.e. they may not call the super
256
// implementation if an attribute is already taken care of.
257
boolean isName = false;
258         if ("id".equals(normalName) || (isName = "name".equals(normalName))) {
259             // Note that the value of name is used
260
// as an ID, but the value of ID is not
261
// used as a name.
262
String JavaDoc oldId = this.id;
263             this.id = value;
264             HTMLDocumentImpl document = (HTMLDocumentImpl) this.document;
265             if (document != null) {
266                 if (oldId != null) {
267                     document.removeElementById(oldId);
268                 }
269                 document.setElementById(value, this);
270                 if(isName) {
271                     String JavaDoc oldName = this.getAttribute("name");
272                     if(oldName != null) {
273                         document.removeNamedItem(oldName);
274                     }
275                     document.setNamedItem(value, this);
276                 }
277             }
278         }
279     }
280
281     protected final String JavaDoc normalizeAttributeName(String JavaDoc name) {
282         return name.toLowerCase();
283     }
284
285     public void setAttribute(String JavaDoc name, String JavaDoc value) throws DOMException JavaDoc {
286         String JavaDoc normalName = this.normalizeAttributeName(name);
287         synchronized(this) {
288             Map JavaDoc attribs = this.attributes;
289             if (attribs == null) {
290                 attribs = new HashMap(2);
291                 this.attributes = attribs;
292             }
293             attribs.put(normalName, value);
294         }
295         this.assignAttributeField(normalName, value);
296     }
297
298     /**
299      * Fast method to set attributes. It is not thread safe.
300      * Calling thread should hold a treeLock.
301      */

302     public void setAttributeImpl(String JavaDoc name, String JavaDoc value) throws DOMException JavaDoc {
303         String JavaDoc normalName = this.normalizeAttributeName(name);
304         Map JavaDoc attribs = this.attributes;
305         if (attribs == null) {
306             attribs = new HashMap(2);
307             this.attributes = attribs;
308         }
309         this.assignAttributeField(normalName, value);
310         attribs.put(normalName, value);
311     }
312
313     public Attr JavaDoc setAttributeNode(Attr JavaDoc newAttr) throws DOMException JavaDoc {
314         String JavaDoc normalName = this.normalizeAttributeName(newAttr.getName());
315         String JavaDoc value = newAttr.getValue();
316         synchronized(this) {
317             if (this.attributes == null) {
318                 this.attributes = new HashMap();
319             }
320             this.attributes.put(normalName, value);
321             // this.setIdAttribute(normalName, newAttr.isId());
322
}
323         this.assignAttributeField(normalName, value);
324         return newAttr;
325     }
326
327     public Attr JavaDoc setAttributeNodeNS(Attr JavaDoc newAttr) throws DOMException JavaDoc {
328         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
329                 "Namespaces not supported");
330     }
331
332     public void setAttributeNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName,
333             String JavaDoc value) throws DOMException JavaDoc {
334         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
335                 "Namespaces not supported");
336     }
337
338     public void setIdAttribute(String JavaDoc name, boolean isId) throws DOMException JavaDoc {
339         String JavaDoc normalName = this.normalizeAttributeName(name);
340         if (!"id".equals(normalName)) {
341             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
342                     "IdAttribute can't be anything other than ID");
343         }
344     }
345
346     public void setIdAttributeNode(Attr JavaDoc idAttr, boolean isId)
347             throws DOMException JavaDoc {
348         String JavaDoc normalName = this.normalizeAttributeName(idAttr.getName());
349         if (!"id".equals(normalName)) {
350             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
351                     "IdAttribute can't be anything other than ID");
352         }
353     }
354
355     public void setIdAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName,
356             boolean isId) throws DOMException JavaDoc {
357         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
358                 "Namespaces not supported");
359     }
360
361     /*
362      * (non-Javadoc)
363      *
364      * @see org.xamjwg.html.domimpl.NodeImpl#getLocalName()
365      */

366     public String JavaDoc getLocalName() {
367         return this.getNodeName();
368     }
369
370     /*
371      * (non-Javadoc)
372      *
373      * @see org.xamjwg.html.domimpl.NodeImpl#getNodeName()
374      */

375     public String JavaDoc getNodeName() {
376         return this.name;
377     }
378
379     /*
380      * (non-Javadoc)
381      *
382      * @see org.xamjwg.html.domimpl.NodeImpl#getNodeType()
383      */

384     public short getNodeType() {
385         return Node.ELEMENT_NODE;
386     }
387
388     /*
389      * (non-Javadoc)
390      *
391      * @see org.xamjwg.html.domimpl.NodeImpl#getNodeValue()
392      */

393     public String JavaDoc getNodeValue() throws DOMException JavaDoc {
394         return null;
395     }
396
397     /*
398      * (non-Javadoc)
399      *
400      * @see org.xamjwg.html.domimpl.NodeImpl#setNodeValue(java.lang.String)
401      */

402     public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc {
403         // nop
404
}
405
406     /**
407      * Gets inner text of the element, possibly including text in comments.
408      * This can be used to get Javascript code out of a SCRIPT element.
409      *
410      * @param includeComment
411      */

412     protected String JavaDoc getRawInnerText(boolean includeComment) {
413         synchronized(this.treeLock) {
414             ArrayList nl = this.nodeList;
415             if(nl != null) {
416                 Iterator i = nl.iterator();
417                 StringBuffer JavaDoc sb = null;
418                 while (i.hasNext()) {
419                     Object JavaDoc node = i.next();
420                     if (node instanceof Text JavaDoc) {
421                         Text JavaDoc tn = (Text JavaDoc) node;
422                         String JavaDoc txt = tn.getNodeValue();
423                         if (!"".equals(txt)) {
424                             if (sb == null) {
425                                 sb = new StringBuffer JavaDoc();
426                             }
427                             sb.append(txt);
428                         }
429                     } else if (node instanceof ElementImpl) {
430                         ElementImpl en = (ElementImpl) node;
431                         String JavaDoc txt = en.getRawInnerText(includeComment);
432                         if (!"".equals(txt)) {
433                             if (sb == null) {
434                                 sb = new StringBuffer JavaDoc();
435                             }
436                             sb.append(txt);
437                         }
438                     } else if (includeComment && node instanceof Comment JavaDoc) {
439                         Comment JavaDoc cn = (Comment JavaDoc) node;
440                         String JavaDoc txt = cn.getNodeValue();
441                         if (!"".equals(txt)) {
442                             if (sb == null) {
443                                 sb = new StringBuffer JavaDoc();
444                             }
445                             sb.append(txt);
446                         }
447                     }
448                 }
449                 return sb == null ? "" : sb.toString();
450             }
451             else {
452                 return "";
453             }
454         }
455     }
456
457     public String JavaDoc toString() {
458         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
459         sb.append(this.getNodeName());
460         sb.append(" [");
461         NamedNodeMap JavaDoc attribs = this.getAttributes();
462         int length = attribs.getLength();
463         for (int i = 0; i < length; i++) {
464             Attr JavaDoc attr = (Attr JavaDoc) attribs.item(i);
465             sb.append(attr.getNodeName());
466             sb.append('=');
467             sb.append(attr.getNodeValue());
468             if (i + 1 < length) {
469                 sb.append(',');
470             }
471         }
472         sb.append("]");
473         return sb.toString();
474     }
475
476     /**
477      * Attempts to convert the subtree starting at this point to a close text
478      * representation. BR elements are converted to line breaks, and so forth.
479      */

480     public String JavaDoc getInnerText() {
481         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
482         synchronized(this.treeLock) {
483             this.appendInnerTextImpl(buffer);
484         }
485         return buffer.toString();
486     }
487     
488     public void setInnerText(String JavaDoc newText) {
489         org.w3c.dom.Document JavaDoc document = this.document;
490         if(document == null) {
491             this.warn("setInnerText(): Element " + this + " does not belong to a document.");
492             return;
493         }
494         synchronized(this.treeLock) {
495             ArrayList nl = this.nodeList;
496             if (nl != null) {
497                 nl.clear();
498             }
499         }
500         // Create node and call appendChild outside of synchronized block.
501
Node JavaDoc textNode = document.createTextNode(newText);
502         this.appendChild(textNode);
503     }
504
505     protected void appendInnerTextImpl(StringBuffer JavaDoc buffer) {
506         ArrayList nl = this.nodeList;
507         if (nl == null) {
508             return;
509         }
510         int size = nl.size();
511         if (size == 0) {
512             return;
513         }
514         for (int i = 0; i < size; i++) {
515             Node JavaDoc child = (Node JavaDoc) nl.get(i);
516             if (child instanceof ElementImpl) {
517                 ((ElementImpl) child).appendInnerTextImpl(buffer);
518             }
519             if(child instanceof Comment JavaDoc) {
520                 // skip
521
}
522             else if (child instanceof Text JavaDoc) {
523                 buffer.append(((Text JavaDoc) child).getTextContent());
524             }
525         }
526     }
527
528     protected Node JavaDoc createSimilarNode() {
529         HTMLDocumentImpl doc = (HTMLDocumentImpl) this.document;
530         return doc == null ? null : doc.createElement(this.getTagName());
531     }
532 }
533
Popular Tags