KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom > AttrImpl


1 /**
2  * org/ozone-db/xml/dom/AttrImpl.java
3  *
4  * The contents of this file are subject to the OpenXML Public
5  * License Version 1.0; you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at
7  * http://www.openxml.org/license.html
8  *
9  * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
10  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
11  * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
12  * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
13  * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
14  * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
15  *
16  * The Initial Developer of this code under the License is Assaf Arkin.
17  * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
18  * All Rights Reserved.
19  */

20
21 /**
22  * Changes for Persistent DOM running with ozone are
23  * Copyright 1999 by SMB GmbH. All rights reserved.
24  */

25
26 package org.ozoneDB.xml.dom;
27
28 import java.io.*;
29 import org.w3c.dom.*;
30 import org.w3c.dom.html.HTMLDocument;
31
32
33 /**
34  * Represents an attribute in an {@link org.w3c.dom.Element} node.
35  * <P>
36  * Attributes are not real nodes, they are not children in their parent element
37  * and the methods {@link AttrImpl#getParentNode}, {@link AttrImpl#getNextSibling}
38  * and {@link #getPreviousSibling} always return null.
39  * <P>
40  * Attributes in XML documents support children, but only of the type {@link
41  * org.w3c.dom.Text} and {@link org.w3c.dom.EntityReference}.
42  * <P>
43  * The specified value of an attribute indicates whether it's value has been
44  * changed since it was constructed with the default value. The specified value
45  * is not used when cloning an attribute or testing for equality.
46  * <P>
47  * To speed up implementation, all attributes are implemented as double-linked
48  * list using {@link NodeImpl#_parent}, {@link NodeImpl#_nextNode} and
49  * {@link NodeImpl#_prevNode}.
50  *
51  *
52  * @version $Revision: 1.2 $ $Date: 2003/11/20 23:18:42 $
53  * @author <a HREF="mailto:arkin@trendline.co.il">Assaf Arkin</a>
54  * @see org.w3c.dom.Attr
55  * @see NodeImpl
56  * @see ElementImpl
57  */

58 public final class AttrImpl extends NodeImpl implements AttrProxy, Externalizable {
59
60     final static long serialVersionUID = 1;
61
62
63     public Element getOwnerElement() {
64         return (Element)getParentNode();
65     }
66
67
68     public final short getNodeType() {
69         return ATTRIBUTE_NODE;
70     }
71
72
73     public final String JavaDoc getName() {
74         return getNodeName();
75     }
76
77
78     public final boolean getSpecified() {
79         return _specified;
80     }
81
82
83     public void setSpecified( boolean value ) {
84         _specified = value;
85     }
86
87
88     public String JavaDoc getValue() {
89         return getNodeValue();
90     }
91
92
93     public void setNodeValue( String JavaDoc value ) {
94         super.setNodeValue( value );
95         _specified = true;
96     }
97
98
99     public void setValue( String JavaDoc value ) {
100         super.setNodeValue( value );
101         _specified = true;
102     }
103
104     // public Node getParentNode() {
105
// // Must return null per DOM spec.
106
// return null;
107
// }
108
//
109
// public Node getPreviousSibling() {
110
// // Must return null per DOM spec.
111
// return null;
112
// }
113
//
114
// public Node getNextSibling() {
115
// // Must return null per DOM spec.
116
// return null;
117
// }
118

119
120     public String JavaDoc toString() {
121         String JavaDoc value;
122
123         value = getValue();
124         if (value.length() > 32) {
125             value = value.substring( 0, 32 ) + "..";
126         }
127         return "Attribute node: [" + getName() + "] [" + value + (_specified ? "] SPECIFIED" : "]");
128     }
129
130
131     protected boolean supportsChildern() {
132         return true;
133     }
134
135
136     public final Object JavaDoc clone() {
137         AttrProxy clone = null;
138         try {
139             clone = (AttrProxy)database().createObject( AttrImpl.class.getName() );
140             clone.init( _ownerDocument, getNodeName(), getNodeValue() );
141             cloneInto( clone, true );
142         } catch (Exception JavaDoc except) {
143             throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() );
144         }
145         return clone;
146     }
147
148
149     public final Node cloneNode( boolean deep ) {
150         AttrProxy clone = null;
151         try {
152             clone = (AttrProxy)database().createObject( AttrImpl.class.getName() );
153             clone.init( _ownerDocument, getNodeName(), getNodeValue() );
154             cloneInto( clone, deep );
155         } catch (Exception JavaDoc except) {
156             throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() );
157         }
158         return clone;
159     }
160
161
162     public synchronized void cloneInto( NodeProxy into, boolean deep ) {
163         super.cloneInto( into, deep );
164         ((AttrProxy)into).setSpecified( getSpecified() );
165     }
166
167
168     /**
169      * Assures that the children of an attribute are either {@link
170      * org.w3c.dom.Text} or {@link org.w3c.dom.EntityReference}.
171      *
172      * @see NodeImpl#castNewChild
173      */

174     protected Node castNewChild( Node newChild ) throws DOMException {
175
176         if (newChild == null) {
177             throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, "Child reference is null." );
178         }
179         if (!(newChild instanceof Text || newChild instanceof EntityReference)) {
180             throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR,
181                     "Child is not a compatible type for this node." );
182         }
183         return newChild;
184     }
185
186
187     /** */
188     public void writeExternal( ObjectOutput out ) throws IOException {
189         super.writeExternal( out );
190         out.writeBoolean( _specified );
191     }
192
193
194     /** */
195     public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException JavaDoc {
196         super.readExternal( in );
197         _specified = in.readBoolean();
198     }
199
200
201     /**
202      * Hidden constructor for attribute. Note that <TT>value</TT> is the DTD's
203      * default value for this attribute, it is not the value specified in the XML
204      * document. The value specified in the XML document should be passed by
205      * calling {@link #setValue} explicitly.
206      * <P>
207      * The attribute name is case sensitive for XML, but is case insensitive and
208      * all lower case for HTML documents. <TT>owner</TT> must point to a valid
209      * document object.
210      *
211      * @param owner The owner document
212      * @param name The attribute's name
213      * @param defValue The attribute's value as specified in the DTD or null
214      */

215     AttrImpl( DocumentImpl owner, String JavaDoc name, String JavaDoc defValue ) {
216         super( owner, owner instanceof HTMLDocument ? name.toLowerCase() : name, defValue, true );
217         // Make sure that all attribute names are converted to lower case
218
// for HTML documents.
219
// Specified is always false. If a value was specified, it was the
220
// default.
221
_specified = false;
222     }
223
224
225     public AttrImpl() {
226         super();
227         _specified = false;
228     }
229
230
231     public final void init( DocumentProxy owner, String JavaDoc name, String JavaDoc value ) {
232         super.init( owner, owner instanceof HTMLDocument ? name.toLowerCase() : name, value, true );
233     }
234
235     /**
236      * True if a value has been specified for this node in the document.
237      * This value is not used when cloning or testing for equality.
238      */

239     private boolean _specified;
240
241 }
242
Popular Tags