1 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 ; 64 import java.beans.*; 65 import java.lang.reflect.*; 66 67 public class TestIndexed implements java.io.Serializable { 68 private String name; 69 private List toppings = new ArrayList(); 70 private int[] measurements = new int[]{36, 24, 38}; 71 72 public String getName() { 73 return name; 74 } 75 76 public void setName(String name) { 77 this.name = name; 78 } 79 80 public String getTopping(int i) { 81 return (String ) toppings.get(i); 82 } 83 84 public void setTopping(int i, String topping) { 85 while (i >= toppings.size()) { 86 toppings.add(null); 87 } 88 toppings.set(i, topping); 89 } 90 91 public String [] getTopping() { 92 String [] a = new String [toppings.size()]; 93 for (int i = 0; i < toppings.size(); ++i) { 94 a[i] = (String ) toppings.get(i); 95 } 96 return a; 97 } 98 99 public void setTopping(String [] 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 toString() { 113 StringBuffer buf = new StringBuffer (); 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 [] args) throws java.beans.IntrospectionException , java.io.IOException , 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 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 a1 = test.getClass(); 161 Class a2 = a1.getSuperclass(); 162 System.out.println("classes: " + a1 + " " + a2); 163 } 164 } 165 | Popular Tags |