KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > support > XmlElementWrapper


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.support;
25
26 import org.riotfamily.common.beans.ObjectWrapper;
27 import org.riotfamily.common.xml.XmlUtils;
28 import org.springframework.beans.AbstractPropertyAccessor;
29 import org.springframework.beans.BeansException;
30 import org.springframework.util.xml.DomUtils;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33
34 public class XmlElementWrapper extends AbstractPropertyAccessor
35         implements ObjectWrapper {
36
37     private static final String JavaDoc ATTRIBUTE_PREFIX = "@";
38
39     private static final String JavaDoc TEXT_VALUE = "text()";
40
41     public Element JavaDoc element;
42
43     private String JavaDoc elementName;
44
45
46     public XmlElementWrapper(String JavaDoc elementName) {
47         this.elementName = elementName;
48     }
49
50     public Class JavaDoc getObjectClass() {
51         return Element JavaDoc.class;
52     }
53
54     public void setObject(Object JavaDoc object) {
55         if (object != null) {
56             element = (Element JavaDoc) object;
57         }
58         else {
59             element = XmlUtils.createDocument().createElement(elementName);
60         }
61     }
62
63     public Object JavaDoc getObject() {
64         return element;
65     }
66
67     public Object JavaDoc getPropertyValue(String JavaDoc name) throws BeansException {
68         if (element == null) {
69             return null;
70         }
71         if (name.startsWith(ATTRIBUTE_PREFIX)) {
72             return element.getAttribute(name.substring(1));
73         }
74         if (name.equals(TEXT_VALUE)) {
75             return DomUtils.getTextValue(element);
76         }
77         Element JavaDoc child = XmlUtils.getFirstChildByTagName(element, name);
78         return child != null ? DomUtils.getTextValue(child) : null;
79     }
80
81     public void setPropertyValue(String JavaDoc name, Object JavaDoc value) throws BeansException {
82         if (element == null) {
83             element = XmlUtils.createDocument().createElement(elementName);
84         }
85         if (name.startsWith(ATTRIBUTE_PREFIX)) {
86             if (value != null) {
87                 element.setAttribute(name.substring(1), value.toString());
88             }
89             else {
90                 element.removeAttribute(name);
91             }
92         }
93         else if (name.equals(TEXT_VALUE)) {
94             XmlUtils.setTextValue(element, value.toString());
95         }
96         if (value instanceof Element JavaDoc) {
97             Element JavaDoc child = XmlUtils.getFirstChildByTagName(element, name);
98             XmlUtils.removeNode(child);
99             child = (Element JavaDoc) value;
100             Node JavaDoc node = element.getOwnerDocument().importNode(child, true);
101             element.appendChild(node);
102         }
103         else {
104             Element JavaDoc child = XmlUtils.getFirstChildByTagName(element, name);
105             if (value != null) {
106                 if (child == null) {
107                     child = element.getOwnerDocument().createElement(name);
108                     element.appendChild(child);
109                 }
110                 XmlUtils.setTextValue(child, (String JavaDoc) value);
111             }
112             else {
113                 XmlUtils.removeNode(child);
114             }
115         }
116     }
117
118     public boolean isReadableProperty(String JavaDoc propertyName) throws BeansException {
119         return true;
120     }
121
122     public boolean isWritableProperty(String JavaDoc propertyName) throws BeansException {
123         return true;
124     }
125
126 }
127
Popular Tags