KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > OzoneElementImpl


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: OzoneElementImpl.java,v 1.2 2003/11/20 23:18:41 per_nyfelt Exp $
8
package org.ozoneDB.xml.dom4j.o3impl;
9
10 import org.dom4j.*;
11 import org.ozoneDB.OzoneCompatible;
12 import org.ozoneDB.OzoneInterface;
13 import org.ozoneDB.OzoneRemote;
14 import org.ozoneDB.OzoneRemoteException;
15 import org.ozoneDB.xml.dom4j.OzoneBranch;
16 import org.ozoneDB.xml.dom4j.OzoneDocumentFactory;
17 import org.ozoneDB.xml.dom4j.OzoneElement;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  *
24  * @author Per Nyfelt
25  */

26
27 public class OzoneElementImpl extends DefaultElement implements OzoneElement, OzoneCompatible {
28
29     final static long serialVersionUID = 1L;
30
31     private void init() {
32     }
33
34     public OzoneElementImpl(String JavaDoc name) {
35         super(name);
36         init();
37     }
38
39     public static OzoneElement create(OzoneInterface db, String JavaDoc name) {
40         System.out.println("[OzoneElementImpl] Creating new object");
41         return (OzoneElement) db.createObject(OzoneElementImpl.class,
42                 new Class JavaDoc[] {String JavaDoc.class},
43                 new Object JavaDoc[]{name});
44     }
45
46     public OzoneElementImpl(QName qname) {
47         super(qname);
48         init();
49     }
50
51     public static OzoneElement create(OzoneInterface db, QName qname) {
52         return (OzoneElement) db.createObject(OzoneElementImpl.class,
53                 new Class JavaDoc[] {QName.class},
54                 new Object JavaDoc[]{qname});
55     }
56
57     public OzoneElementImpl(QName qname, int attributeCount) {
58         super(qname, attributeCount);
59         init();
60     }
61
62     public static OzoneElement create(OzoneInterface db, QName qname, int attributeCount) {
63         final Class JavaDoc[] signature = new Class JavaDoc[] {QName.class, int.class};
64         return (OzoneElement) db.createObject(OzoneElementImpl.class,
65                 signature,
66                 new Object JavaDoc[]{qname, new Integer JavaDoc(attributeCount)});
67     }
68
69     public OzoneElementImpl(String JavaDoc name, Namespace namespace) {
70         super(name, namespace);
71         init();
72     }
73
74     public static OzoneElement create(OzoneInterface db, String JavaDoc name, Namespace namespace) {
75         final Class JavaDoc[] signature = new Class JavaDoc[] {String JavaDoc.class, Namespace.class};
76         return (OzoneElement) db.createObject(OzoneElementImpl.class,
77                 signature,
78                 new Object JavaDoc[]{name, namespace});
79     }
80
81     public DocumentFactory getDocumentFactory() {
82         throw new OzoneRemoteException("getDocumentFactory will not work in Ozone");
83     }
84
85     protected NodeFactory getNodeFactory() {
86         try {
87             // todo: might be faster to return super.getNodeFactory() if it is instanceof OzoneCompatible first
88
// before falling back to object for name look-up
89
return (NodeFactory) database().objectForName(OzoneDocumentFactory.OBJECT_NAME);
90         } catch (Exception JavaDoc e) {
91             throw new OzoneRemoteException("NodeFctory not registered" + e.toString());
92         }
93     }
94
95     protected XPathFactory getXPathFactory() {
96         try {
97             // todo: might be faster to return super.getXPathFactory() if it is instanceof OzoneCompatible first
98
// before falling back to object for name look-up
99
return (XPathFactory) database().objectForName(OzoneDocumentFactory.OBJECT_NAME);
100         } catch (Exception JavaDoc e) {
101             throw new OzoneRemoteException("XPathFactory not registered" + e.toString());
102         }
103     }
104
105     public Element addAttribute(String JavaDoc name, String JavaDoc value) {
106         // adding a null value is equivalent to removing the attribute
107
Attribute attribute = attribute(name);
108         //System.out.println("[" + this.getClass().getName() + "] addAttribute(" + name + ", " + value + ") , attribute = " + attribute);
109
if (value != null) {
110             if (attribute == null) {
111                 //System.out.println("creating and adding attribute");
112
//Attribute att = getDocumentFactory().createAttribute(this, name, value);
113
Attribute att = getNodeFactory().createAttribute(null, name, value);
114                 System.out.println("Attribute created: " + att + ", parent=" + att.getParent());
115                 add(att);
116             } else if (attribute.isReadOnly()) {
117                 System.out.println("removing attribute");
118                 remove(attribute);
119                 //System.out.println("creating and adding attribute");
120
//add(getDocumentFactory().createAttribute(this, name, value));
121
add(getDocumentFactory().createAttribute(null, name, value));
122             } else {
123                 System.out.println("Setting attribute value");
124                 attribute.setValue(value);
125             }
126         } else if (attribute != null) {
127             remove(attribute);
128         }
129         return this;
130
131     }
132
133     public Element addAttribute(QName qName, String JavaDoc value) {
134         // adding a null value is equivalent to removing the attribute
135
Attribute attribute = attribute(qName);
136         if (value != null) {
137             if (attribute == null) {
138                 //add(getDocumentFactory().createAttribute(this, qName, value));
139
add(getNodeFactory().createAttribute(null, qName, value));
140             } else if (attribute.isReadOnly()) {
141                 remove(attribute);
142                 //add(getDocumentFactory().createAttribute(this, qName, value));
143
add(getNodeFactory().createAttribute(null, qName, value));
144             } else {
145                 attribute.setValue(value);
146             }
147         } else if (attribute != null) {
148             remove(attribute);
149         }
150         return this;
151     }
152
153     public List JavaDoc attributes() {
154         return new O3ContentListFacade((OzoneBranch) self(), attributeList());
155     }
156
157     public List JavaDoc content() {
158         List JavaDoc backingList = contentList();
159         return new O3ContentListFacade((OzoneBranch) self(), backingList);
160     }
161
162     /** Called when an invalid node has been added.
163      * Throws an {@link org.dom4j.IllegalAddException}.
164      */

165     public void invalidNodeTypeAddException(Node node) {
166         super.invalidNodeTypeAddException(node);
167     }
168
169     /** Called when the given List content has been removed so
170      * each node should have its parent and document relationships
171      * cleared
172      */

173     public void contentRemoved() {
174         super.contentRemoved();
175     }
176
177     /** @return the text value of the given content object
178      * as text which returns the text value of CDATA, Entity or Text nodes
179      */

180     public String JavaDoc getContentAsText(Object JavaDoc content) {
181         return super.getContentAsText(content);
182     }
183
184     /** @return the XPath defined string-value of the given content object
185      */

186     public String JavaDoc getContentAsStringValue(Object JavaDoc content) {
187         return super.getContentAsStringValue(content);
188     }
189
190     /** @return the ID of the given <code>Element</code>
191      */

192     public String JavaDoc elementID(Element element) {
193         return super.elementID(element);
194     }
195
196     /** A Factory Method pattern which creates
197      * a List implementation used to store content
198      */

199     public List JavaDoc createContentList() {
200         return super.createContentList();
201     }
202
203     /** A Factory Method pattern which creates
204      * a List implementation used to store content
205      */

206     public List JavaDoc createContentList(int size) {
207         return super.createContentList(size);
208     }
209
210     /** A Factory Method pattern which creates
211      * a BackedList implementation used to store results of
212      * a filtered content query. */

213     public BackedList createResultList() {
214         return super.createResultList();
215     }
216
217     /** A Factory Method pattern which creates
218      * a BackedList implementation which contains a single result
219      */

220     public List JavaDoc createSingleResultList(Object JavaDoc result) {
221         return super.createSingleResultList(result);
222     }
223
224     /** A Factory Method pattern which creates an empty
225      * a BackedList implementation
226      */

227     public List JavaDoc createEmptyList() {
228         return super.createEmptyList();
229     }
230
231     public void childRemoved(Node node) {
232         super.childRemoved(node);
233     }
234
235     public void childAdded(Node node) {
236         super.childAdded(node);
237     }
238
239     public boolean removeNode(Node node) {
240         return super.removeNode(node);
241     }
242
243     public void addNode(Node node) {
244         super.addNode(node);
245     }
246
247     public List JavaDoc contentList() {
248         return super.contentList();
249     }
250
251     public void addNode(int index, Node node) {
252         super.addNode(index, node);
253     }
254
255
256     /**
257      * An element clone is identitical to its original except for two things
258      * 1. it has no document
259      * 2. it has no parent
260      * @return a clone of the Element as its own unique persiatant object
261      */

262     public Object JavaDoc clone() {
263         try {
264             Element answer = (Element) database().copyObject(self());
265             answer.setDocument(null);
266             answer.setParent(null);
267             // now we need to continue to clone our way down, otherwise the content
268
// of the clone will refer to the same as the original
269
answer.setContent(null);
270             answer.setAttributes(null);
271             answer.appendAttributes((Element) self());
272             answer.appendContent((Element) self());
273             return answer;
274         } catch (Exception JavaDoc e) {
275             e.printStackTrace();
276             return null;
277         }
278     }
279
280     public void onCreate() {
281     }
282
283     public void onActivate() {
284     }
285
286     public void onPassivate() {
287     }
288
289     public void onDelete() {
290         OzoneInterface db = database();
291 // QName qname = getQName();
292
// if (qname != null && qname instanceof OzoneRemote) {
293
// db.deleteObject((OzoneRemote)qname);
294
// }
295
List JavaDoc contentList = content();
296         OzoneRemote content;
297         for (Iterator JavaDoc it = contentList.iterator(); it.hasNext();) {
298             Object JavaDoc obj = it.next();
299             System.out.println("deleting " + obj.getClass());
300             content = (OzoneRemote) obj;
301             db.deleteObject(content);
302         }
303     }
304 }
305
Popular Tags