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: AbstractAttribute.java,v 1.19 2004/06/25 08:03:40 maartenc Exp $
8    */
9   
10  package org.dom4j.tree;
11  
12  import java.io.IOException;
13  import java.io.Writer;
14  
15  import org.dom4j.Attribute;
16  import org.dom4j.Element;
17  import org.dom4j.Namespace;
18  import org.dom4j.Node;
19  import org.dom4j.Visitor;
20  
21  /*** <p><code>AbstractNamespace</code> is an abstract base class for 
22    * tree implementors to use for implementation inheritence.</p>
23    *
24    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25    * @version $Revision: 1.19 $
26    */
27  public abstract class AbstractAttribute extends AbstractNode implements Attribute {
28  
29      public short getNodeType() {
30          return ATTRIBUTE_NODE;
31      }
32  
33      
34      public void setNamespace(Namespace namespace) {
35          throw new UnsupportedOperationException("This Attribute is read only and cannot be changed" );
36      }
37      
38      public String getText() {
39          return getValue();
40      }
41  
42      public void setText(String text) {
43          setValue(text);
44      }
45  
46      public void setValue(String value) {
47          throw new UnsupportedOperationException("This Attribute is read only and cannot be changed" );
48      }
49      
50      public Object getData() {
51          return getValue();
52      }
53      
54      public void setData(Object data) {
55          setValue( data == null ? null : data.toString() );
56      }
57      
58      public String toString() {
59          return super.toString() + " [Attribute: name " + getQualifiedName() 
60              + " value \"" + getValue() + "\"]";
61      }
62  
63      public String asXML() {
64          return getQualifiedName() + "=\"" + getValue() + "\"";
65      }
66      
67      public void write(Writer writer) throws IOException {
68          writer.write( getQualifiedName() );
69          writer.write( "=\"" );
70          writer.write( getValue() );
71          writer.write( "\"" );
72      }
73          
74      public void accept(Visitor visitor) {
75          visitor.visit(this);
76      }
77      
78      // QName methods
79      
80      public Namespace getNamespace() {
81          return getQName().getNamespace();
82      }
83      
84      public String getName() {
85          return getQName().getName();
86      }
87      
88      public String getNamespacePrefix() {
89          return getQName().getNamespacePrefix();
90      }
91  
92      public String getNamespaceURI() {
93          return getQName().getNamespaceURI();
94      }
95  
96      public String getQualifiedName() {
97          return getQName().getQualifiedName();
98      }
99      
100     public String getPath(Element context) {
101         StringBuffer result = new StringBuffer();
102 
103         Element parent = getParent();
104         if ((parent != null) && (parent != context)) {
105             result.append(parent.getPath(context));
106             result.append("/");
107         }
108         result.append("@");
109         
110         String uri = getNamespaceURI();
111         String prefix = getNamespacePrefix();
112         if (uri == null || uri.length() == 0 || prefix == null || prefix.length() == 0) {
113             result.append(getName());
114         } else {
115             result.append(getQualifiedName());
116         }
117 
118         return result.toString();
119     }
120     
121     public String getUniquePath(Element context) {
122         StringBuffer result = new StringBuffer();
123 
124         Element parent = getParent();
125         if ((parent != null) && (parent != context)) {
126             result.append(parent.getUniquePath(context));
127             result.append("/");
128         }
129         result.append("@");
130         
131         String uri = getNamespaceURI();
132         String prefix = getNamespacePrefix();
133         if (uri == null || uri.length() == 0 || prefix == null || prefix.length() == 0) {
134             result.append(getName());
135         } else {
136             result.append(getQualifiedName());
137         }
138 
139         return result.toString();
140     }
141 
142     protected Node createXPathResult(Element parent) {
143         return new DefaultAttribute(parent, getQName(), getValue());
144     }
145 }
146     
147  
148 
149 
150 
151 
152 /*
153  * Redistribution and use of this software and associated documentation
154  * ("Software"), with or without modification, are permitted provided
155  * that the following conditions are met:
156  *
157  * 1. Redistributions of source code must retain copyright
158  *    statements and notices.  Redistributions must also contain a
159  *    copy of this document.
160  *
161  * 2. Redistributions in binary form must reproduce the
162  *    above copyright notice, this list of conditions and the
163  *    following disclaimer in the documentation and/or other
164  *    materials provided with the distribution.
165  *
166  * 3. The name "DOM4J" must not be used to endorse or promote
167  *    products derived from this Software without prior written
168  *    permission of MetaStuff, Ltd.  For written permission,
169  *    please contact dom4j-info@metastuff.com.
170  *
171  * 4. Products derived from this Software may not be called "DOM4J"
172  *    nor may "DOM4J" appear in their names without prior written
173  *    permission of MetaStuff, Ltd. DOM4J is a registered
174  *    trademark of MetaStuff, Ltd.
175  *
176  * 5. Due credit should be given to the DOM4J Project - 
177  *    http://www.dom4j.org
178  *
179  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
180  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
181  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
182  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
183  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
184  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
185  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
186  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
187  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
188  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
189  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
190  * OF THE POSSIBILITY OF SUCH DAMAGE.
191  *
192  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
193  *
194  * $Id: AbstractAttribute.java,v 1.19 2004/06/25 08:03:40 maartenc Exp $
195  */