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