1 23 24 package org.objectweb.fractal.adl.attributes; 25 26 import java.lang.reflect.Method ; 27 import java.util.Map ; 28 29 import org.objectweb.fractal.adl.ADLException; 30 import org.objectweb.fractal.adl.Definition; 31 import org.objectweb.fractal.adl.AbstractLoader; 32 import org.objectweb.fractal.adl.Node; 33 import org.objectweb.fractal.adl.components.Component; 34 import org.objectweb.fractal.adl.components.ComponentContainer; 35 import org.objectweb.fractal.adl.components.ComponentDefinition; 36 37 43 44 public class AttributeLoader extends AbstractLoader { 45 46 50 public Definition load (final String name, final Map context) 51 throws ADLException 52 { 53 Definition d = clientLoader.load(name, context); 54 boolean extend = false; 55 if (d instanceof ComponentDefinition) { 56 extend = ((ComponentDefinition)d).getExtends() != null; 57 } 58 checkNode(d, extend, context); 59 return d; 60 } 61 62 66 private void checkNode ( 67 final Object node, 68 final boolean extend, 69 final Map context) throws ADLException 70 { 71 if (node instanceof AttributesContainer) { 72 checkAttributesContainer((AttributesContainer)node, extend, context); 73 } 74 if (node instanceof ComponentContainer) { 75 Component[] comps = ((ComponentContainer)node).getComponents(); 76 for (int i = 0; i < comps.length; i++) { 77 checkNode(comps[i], extend, context); 78 } 79 } 80 } 81 82 private void checkAttributesContainer ( 83 final AttributesContainer container, 84 final boolean extend, 85 final Map context) throws ADLException 86 { 87 Attributes attrs = container.getAttributes(); 88 if (attrs != null) { 89 String signature = attrs.getSignature(); 90 if (signature == null) { 91 if (!extend) { 92 throw new ADLException("Signature missing", (Node)attrs); 93 } else { 94 return; 95 } 96 } 97 Class c; 98 try { 99 c = getClassLoader(context).loadClass(signature); 100 } catch (ClassNotFoundException e) { 101 throw new ADLException( 102 "Invalid signature '" + signature + "'", (Node)attrs, e); 103 } 104 105 Attribute[] attributes = attrs.getAttributes(); 106 for (int i = 0; i < attributes.length; ++i) { 107 String attrName = attributes[i].getName(); 108 String attrValue = attributes[i].getValue(); 109 if (attrName == null) { 110 throw new ADLException("Attribute name missing", (Node)attributes[i]); 111 } 112 if (attrValue == null) { 113 throw new ADLException("Attribute value missing", (Node)attributes[i]); 114 } 115 String getterName = 116 "get" + 117 Character.toUpperCase(attrName.charAt(0)) + 118 attrName.substring(1); 119 Method getter; 120 try { 121 getter = c.getMethod(getterName, new Class [0]); 122 } catch (Exception e) { 123 throw new ADLException("No such attribute", (Node)attributes[i], e); 124 } 125 Class attrType = getter.getReturnType(); 126 if (attrType.isPrimitive()) { 127 if (attrType.equals(Integer.TYPE)) { 128 try { 129 Integer.valueOf(attrValue); 130 } catch (NumberFormatException e) { 131 throw new ADLException( 132 "Bad integer value: " + attrValue, (Node)attributes[i]); 133 } 134 } else if (attrType.equals(Long.TYPE)) { 135 try { 136 Long.valueOf(attrValue); 137 } catch (NumberFormatException e) { 138 throw new ADLException( 139 "Bad long value: " + attrValue, (Node)attributes[i]); 140 } 141 } else if (attrType.equals(Float.TYPE)) { 142 try { 143 Float.valueOf(attrValue); 144 } catch (NumberFormatException e) { 145 throw new ADLException( 146 "Bad float value: " + attrValue, (Node)attributes[i]); 147 } 148 } else if (attrType.equals(Double.TYPE)) { 149 try { 150 Double.valueOf(attrValue); 151 } catch (NumberFormatException e) { 152 throw new ADLException( 153 "Bad double value: " + attrValue, (Node)attributes[i]); 154 } 155 } else if (attrType.equals(Byte.TYPE)) { 156 try { 157 Byte.valueOf(attrValue); 158 } catch (NumberFormatException e) { 159 throw new ADLException( 160 "Bad byte value: " + attrValue, (Node)attributes[i]); 161 } 162 } else if (attrType.equals(Character.TYPE)) { 163 if (attrValue.length() != 1) { 164 throw new ADLException( 165 "Bad char value: " + attrValue, (Node)attributes[i]); 166 } 167 } else if (attrType.equals(Short.TYPE)) { 168 try { 169 Short.valueOf(attrValue); 170 } catch (NumberFormatException e) { 171 throw new ADLException( 172 "Bad short value: " + attrValue, (Node)attributes[i]); 173 } 174 } else if (attrType.equals(Boolean.TYPE)) { 175 if (!attrValue.equals("true") && !attrValue.equals("false")) { 176 throw new ADLException( 177 "Bad boolean value: " + attrValue, (Node)attributes[i]); 178 } 179 } else { 180 throw new ADLException("Unexpected case", (Node)attributes[i]); 181 } 182 } else if (attrType != String .class) { 183 throw new ADLException( 184 "Unsupported attribute type: " + attrType, (Node)attributes[i]); 185 } 186 } 187 } 188 } 189 } 190 | Popular Tags |