KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > collection > XmlSequence


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element.collection;
25
26 import java.util.Iterator JavaDoc;
27
28 import org.riotfamily.common.xml.XmlUtils;
29 import org.springframework.util.Assert;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 public class XmlSequence extends ListEditor {
34
35     private String JavaDoc name;
36
37     private Node JavaDoc node;
38
39     public void setName(String JavaDoc name) {
40         this.name = name;
41     }
42
43     protected void afterBindingSet() {
44         if (name == null) {
45             name = getEditorBinding().getProperty();
46         }
47     }
48
49     /**
50      *
51      */

52     public void setValue(Object JavaDoc value) {
53         if (value != null) {
54             Assert.isInstanceOf(Node JavaDoc.class, value,
55                     "Value must implement the org.w3c.Node interface");
56
57             node = (Node JavaDoc) value;
58             NodeList JavaDoc childNodes = node.getChildNodes();
59             for (int i = 0; i < childNodes.getLength(); i++) {
60                 Node JavaDoc child = childNodes.item(i);
61                 ListItem item = addItem();
62                 item.getElement().setValue(child);
63             }
64         }
65     }
66
67     public Object JavaDoc getValue() {
68         if (node != null) {
69             XmlUtils.removeAllChildNodes(node);
70         }
71         else {
72             node = XmlUtils.createDocument().createElement(name);
73         }
74         Iterator JavaDoc it = getItems().getElements().iterator();
75         while (it.hasNext()) {
76             ListItem item = (ListItem) it.next();
77             if (item.getElement().getValue() != null) {
78                 Object JavaDoc value = item.getElement().getValue();
79                 Assert.isInstanceOf(Node JavaDoc.class, value,
80                         "Value must implement the org.w3c.Node interface");
81
82                 XmlUtils.importAndAppend((Node JavaDoc) value, node);
83             }
84         }
85         return node;
86     }
87
88     protected void validate() {
89     }
90
91 }
92
Popular Tags