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: UserDataElement.java,v 1.4 2002/02/01 13:04:32 jstrachan Exp $ 008 */ 009 010package org.dom4j.util; 011 012import java.io.IOException; 013import java.io.StringWriter; 014import java.io.PrintWriter; 015import java.util.ArrayList; 016import java.util.Collections; 017import java.util.HashMap; 018import java.util.Iterator; 019import java.util.List; 020import java.util.Map; 021import java.util.StringTokenizer; 022 023import org.dom4j.Attribute; 024import org.dom4j.CDATA; 025import org.dom4j.CharacterData; 026import org.dom4j.Comment; 027import org.dom4j.Document; 028import org.dom4j.DocumentFactory; 029import org.dom4j.Element; 030import org.dom4j.Entity; 031import org.dom4j.IllegalAddException; 032import org.dom4j.Node; 033import org.dom4j.Namespace; 034import org.dom4j.QName; 035import org.dom4j.ProcessingInstruction; 036import org.dom4j.Text; 037import org.dom4j.tree.DefaultElement; 038 039import org.xml.sax.Attributes; 040 041/** <p><code>UserDataElement</code> support the adornment of a user 042 * data object on an Element or Attribute instance such that the 043 * methods {@link #getData} {@link #setData(Object)} 044 * will get and set the values of a user data object. 045 * This can be useful for developers wishing to create XML trees and 046 * adorn the trees with user defined objects.</p> 047 * 048 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 049 * @version $Revision: 1.4 $ 050 */ 051public class UserDataElement extends DefaultElement { 052 053 /** The user data object */ 054 private Object data; 055 056 057 public UserDataElement(String name) { 058 super(name); 059 } 060 061 public UserDataElement(QName qname) { 062 super(qname); 063 } 064 065 public Object getData() { 066 return data; 067 } 068 069 public void setData(Object data) { 070 this.data = data; 071 } 072 073 public String toString() { 074 return super.toString() + " userData: " + data; 075 } 076 077 public Object clone() { 078 UserDataElement answer = (UserDataElement) super.clone(); 079 if ( answer != this ) { 080 answer.data = getCopyOfUserData(); 081 } 082 return answer; 083 } 084 085 // Implementation methods 086 //------------------------------------------------------------------------- 087 088 /** If a deep copy of user data is required whenever the clone() or createCopy() 089 * methods are called on this element then this method should return a clone 090 * of the user data 091 */ 092 protected Object getCopyOfUserData() { 093 return data; 094 } 095 096 protected Element createElement(String name) { 097 Element answer = getDocumentFactory().createElement(name); 098 answer.setData( getCopyOfUserData() ); 099 return answer; 100 } 101 102 protected Element createElement(QName qName) { 103 Element answer = getDocumentFactory().createElement(qName); 104 answer.setData( getCopyOfUserData() ); 105 return answer; 106 } 107} 108 109 110 111 112/* 113 * Redistribution and use of this software and associated documentation 114 * ("Software"), with or without modification, are permitted provided 115 * that the following conditions are met: 116 * 117 * 1. Redistributions of source code must retain copyright 118 * statements and notices. Redistributions must also contain a 119 * copy of this document. 120 * 121 * 2. Redistributions in binary form must reproduce the 122 * above copyright notice, this list of conditions and the 123 * following disclaimer in the documentation and/or other 124 * materials provided with the distribution. 125 * 126 * 3. The name "DOM4J" must not be used to endorse or promote 127 * products derived from this Software without prior written 128 * permission of MetaStuff, Ltd. For written permission, 129 * please contact dom4j-info@metastuff.com. 130 * 131 * 4. Products derived from this Software may not be called "DOM4J" 132 * nor may "DOM4J" appear in their names without prior written 133 * permission of MetaStuff, Ltd. DOM4J is a registered 134 * trademark of MetaStuff, Ltd. 135 * 136 * 5. Due credit should be given to the DOM4J Project 137 * (http://dom4j.org/). 138 * 139 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 140 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 141 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 142 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 143 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 144 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 145 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 146 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 147 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 148 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 149 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 150 * OF THE POSSIBILITY OF SUCH DAMAGE. 151 * 152 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 153 * 154 * $Id: UserDataElement.java,v 1.4 2002/02/01 13:04:32 jstrachan Exp $ 155 */