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: AbstractEntity.java,v 1.10 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.Element; 016import org.dom4j.Entity; 017import org.dom4j.Visitor; 018 019 020/** <p><code>AbstractEntity</code> is an abstract base class for 021 * tree implementors to use for implementation inheritence.</p> 022 * 023 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 024 * @version $Revision: 1.10 $ 025 */ 026public abstract class AbstractEntity extends AbstractNode implements Entity { 027 028 public AbstractEntity() { 029 } 030 031 public short getNodeType() { 032 return ENTITY_REFERENCE_NODE; 033 } 034 035 public String getPath(Element context) { 036 // From XPaths perspective, entities are included in text 037 Element parent = getParent(); 038 return ( parent != null && parent != context ) 039 ? parent.getPath( context ) + "/text()" 040 : "text()"; 041 } 042 043 public String getUniquePath(Element context) { 044 // From XPaths perspective, entities are included in text 045 Element parent = getParent(); 046 return ( parent != null && parent != context ) 047 ? parent.getUniquePath( context ) + "/text()" 048 : "text()"; 049 } 050 051 public String toString() { 052 return super.toString() + " [Entity: &" + getName() + ";]"; 053 } 054 055 public String getStringValue() { 056 return "&" + getName() + ";"; 057 } 058 059 public String asXML() { 060 return "&" + getName() + ";"; 061 } 062 063 public void write(Writer writer) throws IOException { 064 writer.write( "&" ); 065 writer.write( getName() ); 066 writer.write( ";" ); 067 } 068 069 public void accept(Visitor visitor) { 070 visitor.visit(this); 071 } 072 073} 074 075 076 077 078/* 079 * Redistribution and use of this software and associated documentation 080 * ("Software"), with or without modification, are permitted provided 081 * that the following conditions are met: 082 * 083 * 1. Redistributions of source code must retain copyright 084 * statements and notices. Redistributions must also contain a 085 * copy of this document. 086 * 087 * 2. Redistributions in binary form must reproduce the 088 * above copyright notice, this list of conditions and the 089 * following disclaimer in the documentation and/or other 090 * materials provided with the distribution. 091 * 092 * 3. The name "DOM4J" must not be used to endorse or promote 093 * products derived from this Software without prior written 094 * permission of MetaStuff, Ltd. For written permission, 095 * please contact dom4j-info@metastuff.com. 096 * 097 * 4. Products derived from this Software may not be called "DOM4J" 098 * nor may "DOM4J" appear in their names without prior written 099 * permission of MetaStuff, Ltd. DOM4J is a registered 100 * trademark of MetaStuff, Ltd. 101 * 102 * 5. Due credit should be given to the DOM4J Project 103 * (http://dom4j.org/). 104 * 105 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 106 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 107 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 108 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 109 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 110 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 111 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 112 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 113 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 114 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 115 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 116 * OF THE POSSIBILITY OF SUCH DAMAGE. 117 * 118 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 119 * 120 * $Id: AbstractEntity.java,v 1.10 2001/07/25 10:51:11 jstrachan Exp $ 121 */