KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*--
2
3  $Id: TestIndexed.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 import java.util.List JavaDoc;
64 import java.beans.*;
65 import java.lang.reflect.*;
66     
67 public class TestIndexed implements java.io.Serializable JavaDoc {
68     private String JavaDoc name;
69     private List JavaDoc toppings = new ArrayList();
70     private int[] measurements = new int[]{36, 24, 38};
71
72     public String JavaDoc getName() {
73         return name;
74     }
75
76     public void setName(String JavaDoc name) {
77         this.name = name;
78     }
79
80     public String JavaDoc getTopping(int i) {
81         return (String JavaDoc) toppings.get(i);
82     }
83
84     public void setTopping(int i, String JavaDoc topping) {
85         while (i >= toppings.size()) {
86             toppings.add(null);
87         }
88         toppings.set(i, topping);
89     }
90
91     public String JavaDoc[] getTopping() {
92         String JavaDoc[] a = new String JavaDoc[toppings.size()];
93         for (int i = 0; i < toppings.size(); ++i) {
94             a[i] = (String JavaDoc) toppings.get(i);
95         }
96         return a;
97     }
98
99     public void setTopping(String JavaDoc[] x) {
100         if (x != null)
101             toppings = Arrays.asList(x);
102     }
103
104     public int[] getMeasurements() {
105         return measurements;
106     }
107
108     public void setMeasurements(int[] measurements) {
109         this.measurements = measurements;
110     }
111
112     public String JavaDoc toString() {
113         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
114         buf.append("TestIndexed[name='" + name + "'");
115         for (int i = 0; i < toppings.size(); ++i) {
116             buf.append(", topping=" + toppings.get(i));
117         }
118         buf.append(", measurements=");
119         for (int i = 0; i < toppings.size(); ++i) {
120             buf.append(" " + measurements[i]);
121         }
122         buf.append("]");
123         return buf.toString();
124     }
125
126     public static void main(String JavaDoc[] args) throws java.beans.IntrospectionException JavaDoc, java.io.IOException JavaDoc, BeanMapperException {
127         BeanMapper mapper = new BeanMapper();
128         mapper.setBeanPackage("org.jdom.contrib.beans");
129
130         TestIndexed pizza = new TestIndexed();
131         pizza.setName("Abominable");
132         pizza.setTopping(0, "Anchovies");
133         pizza.setTopping(1, "Steak Tartare");
134         pizza.setTopping(2, "Raw Eggs");
135
136         BeanInfo info = Introspector.getBeanInfo(pizza.getClass());
137         PropertyDescriptor[] ps = info.getPropertyDescriptors();
138         for (int i = 0; i < ps.length; ++i) {
139             if (ps[i] instanceof IndexedPropertyDescriptor) {
140                 IndexedPropertyDescriptor p = (IndexedPropertyDescriptor) ps[i];
141                 System.out.println("Indexed property " + p.getName() + " " + p.getShortDescription());
142                 System.out.println("Type = " + p.getPropertyType());
143                 System.out.println("Getter = " + p.getReadMethod());
144                 System.out.println("Indexed Getter = " + p.getIndexedReadMethod());
145             }
146         }
147
148         System.out.println("===== Testing toDocument()");
149         Document doc = mapper.toDocument(pizza);
150         XMLOutputter o = new XMLOutputter(Format.getPrettyFormat());
151         o.output(doc, System.out);
152         System.out.println();
153
154         // test jdom->bean
155
System.out.println("===== Testing toBean()");
156         TestIndexed test2 = (TestIndexed) mapper.toBean(doc);
157         System.out.println(test2);
158
159         int[] test = new int[10];
160         Class JavaDoc a1 = test.getClass();
161         Class JavaDoc a2 = a1.getSuperclass();
162         System.out.println("classes: " + a1 + " " + a2);
163     }
164 }
165
Popular Tags