001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: AbstractAttribute.java,v 1.14 2001/07/25 10:51:11 jstrachan Exp $ 008 */ 009 010package org.dom4j.tree; 011 012import java.io.IOException; 013import java.io.Writer; 014 015import org.dom4j.Attribute; 016import org.dom4j.Element; 017import org.dom4j.Node; 018import org.dom4j.Namespace; 019import org.dom4j.Visitor; 020 021/** <p><code>AbstractNamespace</code> is an abstract base class for 022 * tree implementors to use for implementation inheritence.</p> 023 * 024 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 025 * @version $Revision: 1.14 $ 026 */ 027public abstract class AbstractAttribute extends AbstractNode implements Attribute { 028 029 public short getNodeType() { 030 return ATTRIBUTE_NODE; 031 } 032 033 034 public void setNamespace(Namespace namespace) { 035 throw new UnsupportedOperationException("This Attribute is read only and cannot be changed" ); 036 } 037 038 public String getText() { 039 return getValue(); 040 } 041 042 public void setText(String text) { 043 setValue(text); 044 } 045 046 public void setValue(String value) { 047 throw new UnsupportedOperationException("This Attribute is read only and cannot be changed" ); 048 } 049 050 public Object getData() { 051 return getValue(); 052 } 053 054 public void setData(Object data) { 055 setValue( data == null ? null : data.toString() ); 056 } 057 058 public String toString() { 059 return super.toString() + " [Attribute: name " + getQualifiedName() 060 + " value \"" + getValue() + "\"]"; 061 } 062 063 public String asXML() { 064 return getQualifiedName() + "=\"" + getValue() + "\""; 065 } 066 067 public void write(Writer writer) throws IOException { 068 writer.write( getQualifiedName() ); 069 writer.write( "=\"" ); 070 writer.write( getValue() ); 071 writer.write( "\"" ); 072 } 073 074 public void accept(Visitor visitor) { 075 visitor.visit(this); 076 } 077 078 // QName methods 079 080 public Namespace getNamespace() { 081 return getQName().getNamespace(); 082 } 083 084 public String getName() { 085 return getQName().getName(); 086 } 087 088 public String getNamespacePrefix() { 089 return getQName().getNamespacePrefix(); 090 } 091 092 public String getNamespaceURI() { 093 return getQName().getNamespaceURI(); 094 } 095 096 public String getQualifiedName() { 097 return getQName().getQualifiedName(); 098 } 099 100 public String getPath(Element context) { 101 Element parent = getParent(); 102 return ( parent != null && parent != context ) 103 ? parent.getPath( context ) + "/@" + getName() 104 : "@" + getName(); 105 } 106 107 public String getUniquePath(Element context) { 108 Element parent = getParent(); 109 return ( parent != null && parent != context ) 110 ? parent.getUniquePath( context ) + "/@" + getName() 111 : "@" + getName(); 112 } 113 114 protected Node createXPathResult(Element parent) { 115 return new DefaultAttribute(parent, getQName(), getValue()); 116 } 117} 118 119 120 121 122 123 124/* 125 * Redistribution and use of this software and associated documentation 126 * ("Software"), with or without modification, are permitted provided 127 * that the following conditions are met: 128 * 129 * 1. Redistributions of source code must retain copyright 130 * statements and notices. Redistributions must also contain a 131 * copy of this document. 132 * 133 * 2. Redistributions in binary form must reproduce the 134 * above copyright notice, this list of conditions and the 135 * following disclaimer in the documentation and/or other 136 * materials provided with the distribution. 137 * 138 * 3. The name "DOM4J" must not be used to endorse or promote 139 * products derived from this Software without prior written 140 * permission of MetaStuff, Ltd. For written permission, 141 * please contact dom4j-info@metastuff.com. 142 * 143 * 4. Products derived from this Software may not be called "DOM4J" 144 * nor may "DOM4J" appear in their names without prior written 145 * permission of MetaStuff, Ltd. DOM4J is a registered 146 * trademark of MetaStuff, Ltd. 147 * 148 * 5. Due credit should be given to the DOM4J Project 149 * (http://dom4j.org/). 150 * 151 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 152 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 153 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 154 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 155 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 156 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 157 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 158 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 159 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 160 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 161 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 162 * OF THE POSSIBILITY OF SUCH DAMAGE. 163 * 164 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 165 * 166 * $Id: AbstractAttribute.java,v 1.14 2001/07/25 10:51:11 jstrachan Exp $ 167 */