1 41 package org.jfree.xml.factory.objects; 42 43 import java.util.ArrayList ; 44 import java.util.Collection ; 45 import java.util.Iterator ; 46 47 import org.jfree.util.Log; 48 49 55 public class CollectionObjectDescription extends AbstractObjectDescription { 56 57 64 public CollectionObjectDescription(final Class c) { 65 super(c); 66 if (!Collection .class.isAssignableFrom(c)) { 67 throw new ClassCastException ("The given class is no Collection instance"); 68 } 69 } 70 71 78 private int parseParameterName(final String name) { 79 try { 80 return Integer.parseInt(name); 81 } 82 catch (Exception e) { 83 return -1; 84 } 85 } 86 87 95 public Class getParameterDefinition(final String name) { 96 if (name.equals("size")) { 97 return Integer.TYPE; 98 } 99 final int par = parseParameterName(name); 100 if (par < 0) { 101 return null; 102 } 103 return Object .class; 104 } 105 106 111 public Iterator getParameterNames() { 112 final Integer size = (Integer ) getParameter("size"); 113 if (size == null) { 114 return getDefinedParameterNames(); 115 } 116 else { 117 final ArrayList l = new ArrayList (); 118 l.add("size"); 119 for (int i = 0; i < size.intValue(); i++) { 120 l.add(String.valueOf(i)); 121 } 122 return l.iterator(); 123 } 124 } 125 126 131 public Object createObject() { 132 try { 133 final Collection l = (Collection ) getObjectClass().newInstance(); 134 int counter = 0; 135 while (getParameterDefinition(String.valueOf(counter)) != null) { 136 final Object value = getParameter(String.valueOf(counter)); 137 if (value == null) { 138 break; 139 } 140 141 l.add(value); 142 counter += 1; 143 } 144 return l; 145 } 146 catch (Exception ie) { 147 Log.warn("Unable to instantiate Object", ie); 148 return null; 149 } 150 } 151 152 160 public void setParameterFromObject(final Object o) throws ObjectFactoryException { 161 if (o == null) { 162 throw new NullPointerException ("Given object is null"); 163 } 164 final Class c = getObjectClass(); 165 if (!c.isInstance(o)) { 166 throw new ObjectFactoryException("Object is no instance of " + c + "(is " 167 + o.getClass() + ")"); 168 } 169 170 final Collection l = (Collection ) o; 171 final Iterator it = l.iterator(); 172 int counter = 0; 173 while (it.hasNext()) { 174 final Object ob = it.next(); 175 setParameter(String.valueOf(counter), ob); 176 counter++; 177 } 178 } 179 } 180 | Popular Tags |