AbstractAttribute xref

View Javadoc

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