View Javadoc

1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
6    *
7    * $Id: DOMAttribute.java,v 1.12 2004/06/25 08:03:34 maartenc Exp $
8    */
9   
10  package org.dom4j.dom;
11  
12  import org.dom4j.Element;
13  import org.dom4j.QName;
14  import org.dom4j.tree.DefaultAttribute;
15  import org.w3c.dom.DOMException;
16  import org.w3c.dom.Document;
17  import org.w3c.dom.NamedNodeMap;
18  import org.w3c.dom.NodeList;
19  
20  /*** <p><code>DOMAttribute</code> implements a doubly linked attribute which
21    * supports the W3C DOM API.</p>
22    *
23    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
24    * @version $Revision: 1.12 $
25    */
26  public class DOMAttribute extends DefaultAttribute implements org.w3c.dom.Attr {
27  
28      public DOMAttribute(QName qname) {
29          super(qname);
30      }
31  
32      public DOMAttribute(QName qname, String value) {
33          super(qname, value);
34      }
35  
36      public DOMAttribute(Element parent, QName qname, String value) {
37          super(parent, qname, value);
38      }
39  
40  
41      // org.w3c.dom.Node interface
42      //-------------------------------------------------------------------------
43      public boolean supports(String feature, String version) {
44          return DOMNodeHelper.supports(this, feature, version);
45      }
46  
47      public String getNamespaceURI() {
48          return getQName().getNamespaceURI();
49      }
50  
51      public String getPrefix() {
52          return getQName().getNamespacePrefix();
53      }
54  
55      public void setPrefix(String prefix) throws DOMException {
56          DOMNodeHelper.setPrefix(this, prefix);
57      }
58  
59      public String getLocalName() {
60          return getQName().getName();
61      }
62  
63      public String getNodeName() {
64          return getName();
65      }
66  
67      //already part of API
68      //
69      //public short getNodeType();
70  
71  
72  
73      public String getNodeValue() throws DOMException {
74          return DOMNodeHelper.getNodeValue(this);
75      }
76  
77      public void setNodeValue(String nodeValue) throws DOMException {
78          DOMNodeHelper.setNodeValue(this, nodeValue);
79      }
80  
81  
82      public org.w3c.dom.Node getParentNode() {
83          // Per http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-637646024
84          // and the NIST conformance tests, Attr.getParentNode() should always
85          // return null
86          return null;
87      }
88  
89      public NodeList getChildNodes() {
90          return DOMNodeHelper.getChildNodes(this);
91      }
92  
93      public org.w3c.dom.Node getFirstChild() {
94          return DOMNodeHelper.getFirstChild(this);
95      }
96  
97      public org.w3c.dom.Node getLastChild() {
98          return DOMNodeHelper.getLastChild(this);
99      }
100 
101     public org.w3c.dom.Node getPreviousSibling() {
102         return DOMNodeHelper.getPreviousSibling(this);
103     }
104 
105     public org.w3c.dom.Node getNextSibling() {
106         return DOMNodeHelper.getNextSibling(this);
107     }
108 
109     public NamedNodeMap getAttributes() {
110         return null;
111     }
112 
113     public Document getOwnerDocument() {
114         return DOMNodeHelper.getOwnerDocument(this);
115     }
116 
117     public org.w3c.dom.Node insertBefore(
118         org.w3c.dom.Node newChild,
119         org.w3c.dom.Node refChild
120     ) throws DOMException {
121         checkNewChildNode(newChild);
122         return DOMNodeHelper.insertBefore(this, newChild, refChild);
123     }
124 
125     public org.w3c.dom.Node replaceChild(
126         org.w3c.dom.Node newChild,
127         org.w3c.dom.Node oldChild
128     ) throws DOMException {
129         checkNewChildNode(newChild);
130         return DOMNodeHelper.replaceChild(this, newChild, oldChild);
131     }
132 
133     public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) throws DOMException {
134         return DOMNodeHelper.removeChild(this, oldChild);
135     }
136 
137     public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild) throws DOMException {
138         checkNewChildNode(newChild);
139         return DOMNodeHelper.appendChild(this, newChild);
140     }
141     
142     private void checkNewChildNode(org.w3c.dom.Node newChild) throws DOMException {
143         final int nodeType = newChild.getNodeType();
144         if (!(nodeType == org.w3c.dom.Node.TEXT_NODE ||
145               nodeType == org.w3c.dom.Node.ENTITY_REFERENCE_NODE)) {
146             throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
147                "Specified node cannot be a child of attribute");
148         }
149     }
150     
151 
152     public boolean hasChildNodes() {
153         return DOMNodeHelper.hasChildNodes(this);
154     }
155 
156     public org.w3c.dom.Node cloneNode(boolean deep) {
157         return DOMNodeHelper.cloneNode(this, deep);
158     }
159 
160     public void normalize() {
161         DOMNodeHelper.normalize(this);
162     }
163 
164     public boolean isSupported(String feature, String version) {
165         return DOMNodeHelper.isSupported(this, feature, version);
166     }
167 
168     public boolean hasAttributes() {
169         return DOMNodeHelper.hasAttributes(this);
170     }
171 
172 
173     // org.w3c.dom.Attr interface
174     //-------------------------------------------------------------------------
175 
176     //public String getName();
177 
178     public boolean getSpecified() {
179         return true;
180     }
181 
182     //public String getValue();
183 
184     //public void setValue(String value) throws DOMException;
185 
186     public org.w3c.dom.Element getOwnerElement() {
187         return DOMNodeHelper.asDOMElement( getParent() );
188     }
189 
190 }
191 
192 
193 
194 
195 /*
196  * Redistribution and use of this software and associated documentation
197  * ("Software"), with or without modification, are permitted provided
198  * that the following conditions are met:
199  *
200  * 1. Redistributions of source code must retain copyright
201  *    statements and notices.  Redistributions must also contain a
202  *    copy of this document.
203  *
204  * 2. Redistributions in binary form must reproduce the
205  *    above copyright notice, this list of conditions and the
206  *    following disclaimer in the documentation and/or other
207  *    materials provided with the distribution.
208  *
209  * 3. The name "DOM4J" must not be used to endorse or promote
210  *    products derived from this Software without prior written
211  *    permission of MetaStuff, Ltd.  For written permission,
212  *    please contact dom4j-info@metastuff.com.
213  *
214  * 4. Products derived from this Software may not be called "DOM4J"
215  *    nor may "DOM4J" appear in their names without prior written
216  *    permission of MetaStuff, Ltd. DOM4J is a registered
217  *    trademark of MetaStuff, Ltd.
218  *
219  * 5. Due credit should be given to the DOM4J Project - 
220  *    http://www.dom4j.org
221  *
222  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
223  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
224  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
225  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
226  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
227  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
228  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
229  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
231  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
232  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
233  * OF THE POSSIBILITY OF SUCH DAMAGE.
234  *
235  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
236  *
237  * $Id: DOMAttribute.java,v 1.12 2004/06/25 08:03:34 maartenc Exp $
238  */