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: AbstractEntity.java,v 1.13 2004/06/25 08:03:41 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.Element;
16 import org.dom4j.Entity;
17 import org.dom4j.Visitor;
18
19
20 /*** <p><code>AbstractEntity</code> is an abstract base class for
21 * tree implementors to use for implementation inheritence.</p>
22 *
23 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
24 * @version $Revision: 1.13 $
25 */
26 public abstract class AbstractEntity extends AbstractNode implements Entity {
27
28 public AbstractEntity() {
29 }
30
31 public short getNodeType() {
32 return ENTITY_REFERENCE_NODE;
33 }
34
35 public String getPath(Element context) {
36 // From XPaths perspective, entities are included in text
37 Element parent = getParent();
38 return ( parent != null && parent != context )
39 ? parent.getPath( context ) + "/text()"
40 : "text()";
41 }
42
43 public String getUniquePath(Element context) {
44 // From XPaths perspective, entities are included in text
45 Element parent = getParent();
46 return ( parent != null && parent != context )
47 ? parent.getUniquePath( context ) + "/text()"
48 : "text()";
49 }
50
51 public String toString() {
52 return super.toString() + " [Entity: &" + getName() + ";]";
53 }
54
55 public String getStringValue() {
56 return "&" + getName() + ";";
57 }
58
59 public String asXML() {
60 return "&" + getName() + ";";
61 }
62
63 public void write(Writer writer) throws IOException {
64 writer.write( "&" );
65 writer.write( getName() );
66 writer.write( ";" );
67 }
68
69 public void accept(Visitor visitor) {
70 visitor.visit(this);
71 }
72
73 }
74
75
76
77
78 /*
79 * Redistribution and use of this software and associated documentation
80 * ("Software"), with or without modification, are permitted provided
81 * that the following conditions are met:
82 *
83 * 1. Redistributions of source code must retain copyright
84 * statements and notices. Redistributions must also contain a
85 * copy of this document.
86 *
87 * 2. Redistributions in binary form must reproduce the
88 * above copyright notice, this list of conditions and the
89 * following disclaimer in the documentation and/or other
90 * materials provided with the distribution.
91 *
92 * 3. The name "DOM4J" must not be used to endorse or promote
93 * products derived from this Software without prior written
94 * permission of MetaStuff, Ltd. For written permission,
95 * please contact dom4j-info@metastuff.com.
96 *
97 * 4. Products derived from this Software may not be called "DOM4J"
98 * nor may "DOM4J" appear in their names without prior written
99 * 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://www.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-2004 (C) MetaStuff, Ltd. All Rights Reserved.
119 *
120 * $Id: AbstractEntity.java,v 1.13 2004/06/25 08:03:41 maartenc Exp $
121 */