KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdom > contrib > beans > TestBean


1 /*--
2
3  $Id: TestBean.java,v 1.3 2004/02/17 02:29:57 jhunter Exp $
4
5  Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
6  All rights reserved.
7
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11
12  1. Redistributions of source code must retain the above copyright
13     notice, this list of conditions, and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright
16     notice, this list of conditions, and the disclaimer that follows
17     these conditions in the documentation and/or other materials
18     provided with the distribution.
19
20  3. The name "JDOM" must not be used to endorse or promote products
21     derived from this software without prior written permission. For
22     written permission, please contact <request_AT_jdom_DOT_org>.
23
24  4. Products derived from this software may not be called "JDOM", nor
25     may "JDOM" appear in their name, without prior written permission
26     from the JDOM Project Management <request_AT_jdom_DOT_org>.
27
28  In addition, we request (but do not require) that you include in the
29  end-user documentation provided with the redistribution and/or in the
30  software itself an acknowledgement equivalent to the following:
31      "This product includes software developed by the
32       JDOM Project (http://www.jdom.org/)."
33  Alternatively, the acknowledgment may be graphical using the logos
34  available at http://www.jdom.org/images/logos.
35
36  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  SUCH DAMAGE.
48
49  This software consists of voluntary contributions made by many
50  individuals on behalf of the JDOM Project and was originally
51  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52  Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53  on the JDOM Project, please see <http://www.jdom.org/>.
54
55  */

56
57  package org.jdom.contrib.beans;
58
59 import org.jdom.*;
60 import org.jdom.output.*;
61 import org.jdom.input.*;
62 import java.util.*;
63
64 public class TestBean implements java.io.Serializable JavaDoc {
65     private String JavaDoc name;
66     private int age;
67     private Date birthdate;
68     private TestBean friend;
69
70     public String JavaDoc getName() {
71     return name;
72     }
73     public int getAge() {
74     return age;
75     }
76     public Date getBirthdate() {
77     return birthdate;
78     }
79     public TestBean getFriend() {
80     return friend;
81     }
82
83     public void setName(String JavaDoc name) {
84     this.name = name;
85     }
86     public void setAge(int age) {
87     this.age = age;
88     }
89     public void setBirthdate(Date birthdate) {
90     this.birthdate = birthdate;
91     }
92     public void setFriend(TestBean friend) {
93     this.friend = friend;
94     }
95
96     public String JavaDoc toString() {
97     return "TestBean[name='" + name + "', age=" + age + ", birthdate=" + birthdate + ", friend=" + friend + "]";
98     }
99
100
101     // Test
102

103     public static void main(String JavaDoc[] args) throws java.beans.IntrospectionException JavaDoc, java.io.IOException JavaDoc {
104
105     try {
106         BeanMapper mapper = new BeanMapper();
107         mapper.addMapping("birthdate", "dob"); // element mapping
108
mapper.addMapping("age", "dob", "age"); // attribute mapping
109

110         mapper.setBeanPackage("org.jdom.contrib.beans");
111     
112         // test bean->jdom
113

114         TestBean alex = new TestBean();
115         alex.setName("Alex");
116         alex.setAge(31);
117         alex.setBirthdate(new Date(69, 7, 8));
118
119         TestBean amy = new TestBean();
120         amy.setName("Amy");
121         amy.setAge(25);
122         amy.setBirthdate(new Date(75, 4, 1));
123
124         alex.setFriend(amy);
125         
126         Document doc = mapper.toDocument(alex);
127         XMLOutputter o = new XMLOutputter(Format.getPrettyFormat());
128         o.output(doc, System.out);
129         System.out.println();
130
131         // test jdom->bean
132
TestBean test2 = (TestBean)mapper.toBean(doc);
133         System.out.println(test2);
134     }
135     catch (BeanMapperException e) {
136         e.printStackTrace();
137     }
138     }
139     
140 }
141
Popular Tags