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: UserDataElement.java,v 1.10 2004/08/04 18:22:40 maartenc Exp $
8 */
9
10 package org.dom4j.util;
11
12 import org.dom4j.Element;
13 import org.dom4j.QName;
14 import org.dom4j.tree.DefaultElement;
15
16 /*** <p><code>UserDataElement</code> support the adornment of a user
17 * data object on an Element or Attribute instance such that the
18 * methods {@link #getData} {@link #setData(Object)}
19 * will get and set the values of a user data object.
20 * This can be useful for developers wishing to create XML trees and
21 * adorn the trees with user defined objects.</p>
22 *
23 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
24 * @version $Revision: 1.10 $
25 */
26 public class UserDataElement extends DefaultElement {
27
28 // private static final DocumentFactory DOCUMENT_FACTORY = UserDataDocumentFactory.getInstance();
29
30 /*** The user data object */
31 private Object data;
32
33
34 public UserDataElement(String name) {
35 super(name);
36 }
37
38 public UserDataElement(QName qname) {
39 super(qname);
40 }
41
42 public Object getData() {
43 return data;
44 }
45
46 public void setData(Object data) {
47 this.data = data;
48 }
49
50 public String toString() {
51 return super.toString() + " userData: " + data;
52 }
53
54 public Object clone() {
55 UserDataElement answer = (UserDataElement) super.clone();
56 if ( answer != this ) {
57 answer.data = getCopyOfUserData();
58 }
59 return answer;
60 }
61
62 // Implementation methods
63 //-------------------------------------------------------------------------
64
65 /*** If a deep copy of user data is required whenever the clone() or createCopy()
66 * methods are called on this element then this method should return a clone
67 * of the user data
68 */
69 protected Object getCopyOfUserData() {
70 return data;
71 }
72
73 protected Element createElement(String name) {
74 Element answer = getDocumentFactory().createElement(name);
75 answer.setData( getCopyOfUserData() );
76 return answer;
77 }
78
79 protected Element createElement(QName qName) {
80 Element answer = getDocumentFactory().createElement(qName);
81 answer.setData( getCopyOfUserData() );
82 return answer;
83 }
84
85 // protected DocumentFactory getDocumentFactory() {
86 // return DOCUMENT_FACTORY;
87 // }
88 }
89
90
91
92
93 /*
94 * Redistribution and use of this software and associated documentation
95 * ("Software"), with or without modification, are permitted provided
96 * that the following conditions are met:
97 *
98 * 1. Redistributions of source code must retain copyright
99 * statements and notices. Redistributions must also contain a
100 * copy of this document.
101 *
102 * 2. Redistributions in binary form must reproduce the
103 * above copyright notice, this list of conditions and the
104 * following disclaimer in the documentation and/or other
105 * materials provided with the distribution.
106 *
107 * 3. The name "DOM4J" must not be used to endorse or promote
108 * products derived from this Software without prior written
109 * permission of MetaStuff, Ltd. For written permission,
110 * please contact dom4j-info@metastuff.com.
111 *
112 * 4. Products derived from this Software may not be called "DOM4J"
113 * nor may "DOM4J" appear in their names without prior written
114 * permission of MetaStuff, Ltd. DOM4J is a registered
115 * trademark of MetaStuff, Ltd.
116 *
117 * 5. Due credit should be given to the DOM4J Project -
118 * http://www.dom4j.org
119 *
120 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
121 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
122 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
123 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
124 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
125 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
126 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
127 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
128 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
129 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
130 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
131 * OF THE POSSIBILITY OF SUCH DAMAGE.
132 *
133 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
134 *
135 * $Id: UserDataElement.java,v 1.10 2004/08/04 18:22:40 maartenc Exp $
136 */