View Javadoc

1   /*
2    *  Java HTML Tidy - JTidy
3    *  HTML parser and pretty printer
4    *
5    *  Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
6    *  Institute of Technology, Institut National de Recherche en
7    *  Informatique et en Automatique, Keio University). All Rights
8    *  Reserved.
9    *
10   *  Contributing Author(s):
11   *
12   *     Dave Raggett <dsr@w3.org>
13   *     Andy Quick <ac.quick@sympatico.ca> (translation to Java)
14   *     Gary L Peskin <garyp@firstech.com> (Java development)
15   *     Sami Lempinen <sami@lempinen.net> (release management)
16   *     Fabrizio Giustina <fgiust at users.sourceforge.net>
17   *
18   *  The contributing author(s) would like to thank all those who
19   *  helped with testing, bug fixes, and patience.  This wouldn't
20   *  have been possible without all of you.
21   *
22   *  COPYRIGHT NOTICE:
23   * 
24   *  This software and documentation is provided "as is," and
25   *  the copyright holders and contributing author(s) make no
26   *  representations or warranties, express or implied, including
27   *  but not limited to, warranties of merchantability or fitness
28   *  for any particular purpose or that the use of the software or
29   *  documentation will not infringe any third party patents,
30   *  copyrights, trademarks or other rights. 
31   *
32   *  The copyright holders and contributing author(s) will not be
33   *  liable for any direct, indirect, special or consequential damages
34   *  arising out of any use of the software or documentation, even if
35   *  advised of the possibility of such damage.
36   *
37   *  Permission is hereby granted to use, copy, modify, and distribute
38   *  this source code, or portions hereof, documentation and executables,
39   *  for any purpose, without fee, subject to the following restrictions:
40   *
41   *  1. The origin of this source code must not be misrepresented.
42   *  2. Altered versions must be plainly marked as such and must
43   *     not be misrepresented as being the original source.
44   *  3. This Copyright notice may not be removed or altered from any
45   *     source or altered source distribution.
46   * 
47   *  The copyright holders and contributing author(s) specifically
48   *  permit, without fee, and encourage the use of this source code
49   *  as a component for supporting the Hypertext Markup Language in
50   *  commercial products. If you use this source code in a product,
51   *  acknowledgment is not required but would be appreciated.
52   *
53   */
54  package org.w3c.tidy;
55  
56  import org.w3c.dom.Attr;
57  import org.w3c.dom.DOMException;
58  import org.w3c.dom.TypeInfo;
59  
60  
61  /**
62   * DOMElementImpl.
63   * @author Dave Raggett <a href="mailto:dsr@w3.org">dsr@w3.org </a>
64   * @author Andy Quick <a href="mailto:ac.quick@sympatico.ca">ac.quick@sympatico.ca </a> (translation to Java)
65   * @author Fabrizio Giustina
66   * @version $Revision: 779 $ ($Author: fgiust $)
67   */
68  public class DOMElementImpl extends DOMNodeImpl implements org.w3c.dom.Element
69  {
70  
71      /**
72       * Instantiates a new DOM element.
73       * @param adaptee Tidy Node.
74       */
75      protected DOMElementImpl(Node adaptee)
76      {
77          super(adaptee);
78      }
79  
80      /**
81       * @see org.w3c.dom.Node#getNodeType
82       */
83      public short getNodeType()
84      {
85          return org.w3c.dom.Node.ELEMENT_NODE;
86      }
87  
88      /**
89       * @see org.w3c.dom.Element#getTagName
90       */
91      public String getTagName()
92      {
93          return super.getNodeName();
94      }
95  
96      /**
97       * @see org.w3c.dom.Element#getAttribute(java.lang.String)
98       */
99      public String getAttribute(String name)
100     {
101         if (this.adaptee == null)
102         {
103             return null;
104         }
105 
106         AttVal att = this.adaptee.attributes;
107         while (att != null)
108         {
109             if (att.attribute.equals(name))
110             {
111                 break;
112             }
113             att = att.next;
114         }
115         if (att != null)
116         {
117             return att.value;
118         }
119 
120         return "";
121     }
122 
123     /**
124      * @see org.w3c.dom.Element#setAttribute(java.lang.String, java.lang.String)
125      */
126     public void setAttribute(String name, String value) throws DOMException
127     {
128         if (this.adaptee == null)
129         {
130             return;
131         }
132 
133         AttVal att = this.adaptee.attributes;
134         while (att != null)
135         {
136             if (att.attribute.equals(name))
137             {
138                 break;
139             }
140             att = att.next;
141         }
142         if (att != null)
143         {
144             att.value = value;
145         }
146         else
147         {
148             att = new AttVal(null, null, '"', name, value);
149             att.dict = AttributeTable.getDefaultAttributeTable().findAttribute(att);
150             if (this.adaptee.attributes == null)
151             {
152                 this.adaptee.attributes = att;
153             }
154             else
155             {
156                 att.next = this.adaptee.attributes;
157                 this.adaptee.attributes = att;
158             }
159         }
160     }
161 
162     /**
163      * @see org.w3c.dom.Element#removeAttribute(java.lang.String)
164      */
165     public void removeAttribute(String name) throws DOMException
166     {
167         if (this.adaptee == null)
168         {
169             return;
170         }
171 
172         AttVal att = this.adaptee.attributes;
173         AttVal pre = null;
174         while (att != null)
175         {
176             if (att.attribute.equals(name))
177             {
178                 break;
179             }
180             pre = att;
181             att = att.next;
182         }
183         if (att != null)
184         {
185             if (pre == null)
186             {
187                 this.adaptee.attributes = att.next;
188             }
189             else
190             {
191                 pre.next = att.next;
192             }
193         }
194     }
195 
196     /**
197      * @see org.w3c.dom.Element#getAttributeNode(java.lang.String)
198      */
199     public org.w3c.dom.Attr getAttributeNode(String name)
200     {
201         if (this.adaptee == null)
202         {
203             return null;
204         }
205 
206         AttVal att = this.adaptee.attributes;
207         while (att != null)
208         {
209             if (att.attribute.equals(name))
210             {
211                 break;
212             }
213             att = att.next;
214         }
215         if (att != null)
216         {
217             return att.getAdapter();
218         }
219 
220         return null;
221     }
222 
223     /**
224      * @see org.w3c.dom.Element#setAttributeNode(org.w3c.dom.Attr)
225      */
226     public org.w3c.dom.Attr setAttributeNode(org.w3c.dom.Attr newAttr) throws DOMException
227     {
228         if (newAttr == null)
229         {
230             return null;
231         }
232         if (!(newAttr instanceof DOMAttrImpl))
233         {
234             throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "newAttr not instanceof DOMAttrImpl");
235         }
236 
237         DOMAttrImpl newatt = (DOMAttrImpl) newAttr;
238         String name = newatt.avAdaptee.attribute;
239         org.w3c.dom.Attr result = null;
240 
241         AttVal att = this.adaptee.attributes;
242         while (att != null)
243         {
244             if (att.attribute.equals(name))
245             {
246                 break;
247             }
248             att = att.next;
249         }
250         if (att != null)
251         {
252             result = att.getAdapter();
253             att.adapter = newAttr;
254         }
255         else
256         {
257             if (this.adaptee.attributes == null)
258             {
259                 this.adaptee.attributes = newatt.avAdaptee;
260             }
261             else
262             {
263                 newatt.avAdaptee.next = this.adaptee.attributes;
264                 this.adaptee.attributes = newatt.avAdaptee;
265             }
266         }
267         return result;
268     }
269 
270     /**
271      * @see org.w3c.dom.Element#removeAttributeNode(org.w3c.dom.Attr)
272      */
273     public org.w3c.dom.Attr removeAttributeNode(org.w3c.dom.Attr oldAttr) throws DOMException
274     {
275         if (oldAttr == null)
276         {
277             return null;
278         }
279 
280         org.w3c.dom.Attr result = null;
281         AttVal att = this.adaptee.attributes;
282         AttVal pre = null;
283         while (att != null)
284         {
285             if (att.getAdapter() == oldAttr)
286             {
287                 break;
288             }
289             pre = att;
290             att = att.next;
291         }
292         if (att != null)
293         {
294             if (pre == null)
295             {
296                 this.adaptee.attributes = att.next;
297             }
298             else
299             {
300                 pre.next = att.next;
301             }
302             result = oldAttr;
303         }
304         else
305         {
306             throw new DOMException(DOMException.NOT_FOUND_ERR, "oldAttr not found");
307         }
308         return result;
309     }
310 
311     /**
312      * @see org.w3c.dom.Element#getElementsByTagName(java.lang.String)
313      */
314     public org.w3c.dom.NodeList getElementsByTagName(String name)
315     {
316         return new DOMNodeListByTagNameImpl(this.adaptee, name);
317     }
318 
319     /**
320      * @todo DOM level 2 getOwnerDocument() Not supported. Do nothing.
321      * @see org.w3c.dom.Element#normalize
322      */
323     public void normalize()
324     {
325         // do nothing
326     }
327 
328     /**
329      * @todo DOM level 2 getAttributeNS() Not supported. Throws NOT_SUPPORTED_ERR.
330      * @see org.w3c.dom.Element#getAttributeNS(java.lang.String, java.lang.String)
331      */
332     public String getAttributeNS(String namespaceURI, String localName)
333     {
334         // DOMException - NOT_SUPPORTED_ERR: May be raised if the implementation does not support the feature "XML" and
335         // the language exposed through the Document does not support XML Namespaces (such as HTML 4.01).
336         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
337     }
338 
339     /**
340      * @todo DOM level 2 setAttributeNS() Not supported. Throws NOT_SUPPORTED_ERR.
341      * @see org.w3c.dom.Element#setAttributeNS(java.lang.String, java.lang.String, java.lang.String)
342      */
343     public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws org.w3c.dom.DOMException
344     {
345         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
346     }
347 
348     /**
349      * @todo DOM level 2 removeAttributeNS() Not supported. Throws NOT_SUPPORTED_ERR.
350      * @see org.w3c.dom.Element#removeAttributeNS(java.lang.String, java.lang.String)
351      */
352     public void removeAttributeNS(String namespaceURI, String localName) throws org.w3c.dom.DOMException
353     {
354         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
355     }
356 
357     /**
358      * @todo DOM level 2 getAttributeNodeNS() Not supported. Throws NOT_SUPPORTED_ERR.
359      * @see org.w3c.dom.Element#getAttributeNodeNS(java.lang.String, java.lang.String)
360      */
361     public org.w3c.dom.Attr getAttributeNodeNS(String namespaceURI, String localName)
362     {
363         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
364     }
365 
366     /**
367      * @todo DOM level 2 setAttributeNodeNS() Not supported. Throws NOT_SUPPORTED_ERR.
368      * @see org.w3c.dom.Element#setAttributeNodeNS(org.w3c.dom.Attr)
369      */
370     public org.w3c.dom.Attr setAttributeNodeNS(org.w3c.dom.Attr newAttr) throws org.w3c.dom.DOMException
371     {
372         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
373     }
374 
375     /**
376      * @todo DOM level 2 getElementsByTagNameNS() Not supported. Throws NOT_SUPPORTED_ERR.
377      * @see org.w3c.dom.Element#getElementsByTagNameNS(java.lang.String, java.lang.String)
378      */
379     public org.w3c.dom.NodeList getElementsByTagNameNS(String namespaceURI, String localName)
380     {
381         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
382     }
383 
384     /**
385      * @todo DOM level 2 hasAttribute() Not supported. Returns false.
386      * @see org.w3c.dom.Element#hasAttribute(java.lang.String)
387      */
388     public boolean hasAttribute(String name)
389     {
390         return false;
391     }
392 
393     /**
394      * @todo DOM level 2 hasAttribute() Not supported. Returns false.
395      * @see org.w3c.dom.Element#hasAttributeNS(java.lang.String, java.lang.String)
396      */
397     public boolean hasAttributeNS(String namespaceURI, String localName)
398     {
399         return false;
400     }
401 
402     /**
403      * @todo DOM level 3 getSchemaTypeInfo() Not supported. Returns null.
404      * @see org.w3c.dom.Element#getSchemaTypeInfo()
405      */
406     public TypeInfo getSchemaTypeInfo()
407     {
408         return null;
409     }
410 
411     /**
412      * @todo DOM level 3 setIdAttribute() Not supported. Throws NOT_SUPPORTED_ERR.
413      * @see org.w3c.dom.Element#setIdAttribute(java.lang.String, boolean)
414      */
415     public void setIdAttribute(String name, boolean isId) throws DOMException
416     {
417         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
418     }
419 
420     /**
421      * @todo DOM level 3 setIdAttributeNode() Not supported. Throws NOT_SUPPORTED_ERR.
422      * @see org.w3c.dom.Element#setIdAttributeNode(org.w3c.dom.Attr, boolean)
423      */
424     public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException
425     {
426         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
427     }
428 
429     /**
430      * @todo DOM level 3 setIdAttributeNS() Not supported. Throws NOT_SUPPORTED_ERR.
431      * @see org.w3c.dom.Element#setIdAttributeNS(java.lang.String, java.lang.String, boolean)
432      */
433     public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException
434     {
435         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
436     }
437 }