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.DOMException;
57  import org.w3c.dom.TypeInfo;
58  
59  
60  /**
61   * Tidy implementation of org.w3c.dom.DOMAttrImpl.
62   * @author Dave Raggett <a href="mailto:dsr@w3.org">dsr@w3.org </a>
63   * @author Andy Quick <a href="mailto:ac.quick@sympatico.ca">ac.quick@sympatico.ca </a> (translation to Java)
64   * @author Fabrizio Giustina
65   * @version $Revision: 779 $ ($Author: fgiust $)
66   */
67  public class DOMAttrImpl extends DOMNodeImpl implements org.w3c.dom.Attr, Cloneable
68  {
69  
70      /**
71       * wrapped org.w3c.tidy.AttVal.
72       */
73      protected AttVal avAdaptee;
74  
75      /**
76       * instantiates a new DOMAttrImpl which wraps the given AttVal.
77       * @param adaptee wrapped AttVal
78       */
79      protected DOMAttrImpl(AttVal adaptee)
80      {
81          super(null); // must override all methods of DOMNodeImpl
82          this.avAdaptee = adaptee;
83      }
84  
85      /**
86       * @see org.w3c.dom.Node#getNodeValue()
87       */
88      public String getNodeValue() throws DOMException
89      {
90          return getValue();
91      }
92  
93      /**
94       * @see org.w3c.dom.Node#setNodeValue(java.lang.String)
95       */
96      public void setNodeValue(String nodeValue) throws DOMException
97      {
98          setValue(nodeValue);
99      }
100 
101     /**
102      * @see org.w3c.dom.Node#getNodeName()
103      */
104     public String getNodeName()
105     {
106         return getName();
107     }
108 
109     /**
110      * @see org.w3c.dom.Node#getNodeType()
111      */
112     public short getNodeType()
113     {
114         return org.w3c.dom.Node.ATTRIBUTE_NODE;
115     }
116 
117     /**
118      * @see org.w3c.dom.Attr#getName
119      */
120     public String getName()
121     {
122         return avAdaptee.attribute;
123     }
124 
125     /**
126      * @see org.w3c.dom.Attr#getSpecified
127      */
128     public boolean getSpecified()
129     {
130         return avAdaptee.value != null;
131     }
132 
133     /**
134      * @see org.w3c.dom.Attr#getValue
135      */
136     public String getValue()
137     {
138         // Thanks to Brett Knights brett@knightsofthenet.com for this fix.
139         return (avAdaptee.value == null) ? avAdaptee.attribute : avAdaptee.value;
140     }
141 
142     /**
143      * @see org.w3c.dom.Attr#setValue(java.lang.String)
144      */
145     public void setValue(String value)
146     {
147         avAdaptee.value = value;
148     }
149 
150     /**
151      * @see org.w3c.dom.Node#getParentNode()
152      */
153     public org.w3c.dom.Node getParentNode()
154     {
155         // Attr.getParentNode() should always return null
156         // http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-637646024
157         return null;
158     }
159 
160     /**
161      * @todo DOM level 2 getChildNodes() Not implemented. Returns an empty NodeList.
162      * @see org.w3c.dom.Node#getChildNodes()
163      */
164     public org.w3c.dom.NodeList getChildNodes()
165     {
166         // Calling getChildNodes on a DOM Attr node does return the children of the Attr, which are the text and
167         // EntityReference nodes that make up the Attr's content.
168         return new DOMNodeListImpl(null);
169     }
170 
171     /**
172      * @todo DOM level 2 getFirstChild() Not implemented. Returns null.
173      * @see org.w3c.dom.Node#getFirstChild()
174      */
175     public org.w3c.dom.Node getFirstChild()
176     {
177         return null;
178     }
179 
180     /**
181      * @todo DOM level 2 getLastChild() Not implemented. Returns null.
182      * @see org.w3c.dom.Node#getLastChild()
183      */
184     public org.w3c.dom.Node getLastChild()
185     {
186         return null;
187     }
188 
189     /**
190      * @see org.w3c.dom.Node#getPreviousSibling()
191      */
192     public org.w3c.dom.Node getPreviousSibling()
193     {
194         // Attr.getPreviousSibling() should always return null
195         return null;
196     }
197 
198     /**
199      * @see org.w3c.dom.Node#getNextSibling()
200      */
201     public org.w3c.dom.Node getNextSibling()
202     {
203         // Attr.getNextSibling() should always return null
204         return null;
205     }
206 
207     /**
208      * @see org.w3c.dom.Node#getAttributes()
209      */
210     public org.w3c.dom.NamedNodeMap getAttributes()
211     {
212         return null;
213     }
214 
215     /**
216      * @todo DOM level 2 getOwnerDocument() Not implemented. Returns null.
217      * @see org.w3c.dom.Node#getOwnerDocument()
218      */
219     public org.w3c.dom.Document getOwnerDocument()
220     {
221         return null;
222     }
223 
224     /**
225      * Not supported.
226      * @see org.w3c.dom.Node#insertBefore(org.w3c.dom.Node, org.w3c.dom.Node)
227      */
228     public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild) throws DOMException
229     {
230         throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "Not supported");
231     }
232 
233     /**
234      * Not supported.
235      * @see org.w3c.dom.Node#replaceChild(org.w3c.dom.Node, org.w3c.dom.Node)
236      */
237     public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild, org.w3c.dom.Node oldChild) throws DOMException
238     {
239         throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "Not supported");
240     }
241 
242     /**
243      * Not supported.
244      * @see org.w3c.dom.Node#removeChild(org.w3c.dom.Node)
245      */
246     public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) throws DOMException
247     {
248         throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "Not supported");
249     }
250 
251     /**
252      * Not supported.
253      * @see org.w3c.dom.Node#appendChild(org.w3c.dom.Node)
254      */
255     public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild) throws DOMException
256     {
257         throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "Not supported");
258     }
259 
260     /**
261      * @see org.w3c.dom.Node#hasChildNodes()
262      */
263     public boolean hasChildNodes()
264     {
265         return false;
266     }
267 
268     /**
269      * @see org.w3c.dom.Node#cloneNode(boolean)
270      */
271     public org.w3c.dom.Node cloneNode(boolean deep)
272     {
273         // http://java.sun.com/j2se/1.5.0/docs/api/index.html?org/w3c/dom/Attr.html
274         // Cloning an Attr always clones its children, since they represent its value, no matter whether this is a deep
275         // clone or not.
276         return (org.w3c.dom.Node) clone();
277     }
278 
279     /**
280      * @todo DOM level 2 getOwnerElement() Not implemented. Returns null.
281      * @see org.w3c.dom.Attr#getOwnerElement()
282      */
283     public org.w3c.dom.Element getOwnerElement()
284     {
285         return null;
286     }
287 
288     /**
289      * @todo DOM level 3 getSchemaTypeInfo() Not implemented. Returns null.
290      * @see org.w3c.dom.Attr#getSchemaTypeInfo()
291      */
292     public TypeInfo getSchemaTypeInfo()
293     {
294         return null;
295     }
296 
297     /**
298      * @see org.w3c.dom.Attr#isId()
299      */
300     public boolean isId()
301     {
302         return "id".equals(this.avAdaptee.getAttribute());
303     }
304 
305     /**
306      * @see java.lang.Object#clone()
307      */
308     protected Object clone()
309     {
310         DOMAttrImpl clone;
311         try
312         {
313             clone = (DOMAttrImpl) super.clone();
314         }
315         catch (CloneNotSupportedException e)
316         {
317             // should never happen
318             throw new RuntimeException("Clone not supported");
319         }
320         clone.avAdaptee = (AttVal) this.avAdaptee.clone();
321         return clone;
322     }
323 }