1 package spoon.support.processing; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.lang.reflect.Array ; 6 import java.util.ArrayList ; 7 import java.util.Collection ; 8 import java.util.Map ; 9 import java.util.TreeMap ; 10 import java.util.Map.Entry; 11 12 import org.xml.sax.Attributes ; 13 import org.xml.sax.InputSource ; 14 import org.xml.sax.SAXException ; 15 import org.xml.sax.XMLReader ; 16 import org.xml.sax.helpers.DefaultHandler ; 17 import org.xml.sax.helpers.XMLReaderFactory ; 18 19 import spoon.processing.ProcessorProperties; 20 import spoon.reflect.Factory; 21 import spoon.reflect.reference.CtExecutableReference; 22 import spoon.reflect.reference.CtFieldReference; 23 import spoon.reflect.reference.CtPackageReference; 24 import spoon.reflect.reference.CtTypeReference; 25 26 30 public class XmlProcessorProperties implements ProcessorProperties { 31 32 35 public class Loader extends DefaultHandler { 36 boolean isValue = false; 37 38 String name; 39 40 Object value; 41 42 45 @SuppressWarnings ("unchecked") 46 @Override 47 public void characters(char[] ch, int start, int length) 48 throws SAXException { 49 if (isValue) { 50 if (value == null || !(value instanceof Collection )) 51 value = new ArrayList <Object >(); 52 ((Collection <Object >) value).add(new String (ch, start, length)); 53 } 54 } 55 56 59 @Override 60 public void endElement(String uri, String localName, String qName) 61 throws SAXException { 62 if (localName.equals("property")) { 63 props.put(name, value); 64 value = null; 65 } else if (localName.equals("value")) { 66 isValue = false; 67 } 68 } 69 70 73 @Override 74 public void startElement(String uri, String localName, String qName, 75 Attributes attributes) throws SAXException { 76 if (localName.equals("property")) { 77 name = attributes.getValue("name"); 78 if (attributes.getValue("value") != null) 79 value = attributes.getValue("value"); 80 } else if (localName.equals("value")) { 81 isValue = true; 82 } 83 } 84 } 85 86 @SuppressWarnings ("unchecked") 87 public static <T> T convert(Factory factory, Class <T> type, Object o) { 88 if (o == null) 89 return null; 90 if (type == boolean.class) 91 return (T) new Boolean (o.toString()); 92 if (type == byte.class) 93 return (T) new Byte (o.toString()); 94 if (type == char.class) 95 return (T) new Character (o.toString().charAt(0)); 96 if (type == double.class) 97 return (T) new Double (o.toString()); 98 if (type == float.class) 99 return (T) new Float (o.toString()); 100 if (type == int.class) 101 return (T) new Integer (o.toString()); 102 if (type == long.class) 103 return (T) new Long (o.toString()); 104 if (CtTypeReference.class.isAssignableFrom(type)) { 105 return (T) factory.Type().createReference(o.toString()); 106 } 107 if (CtExecutableReference.class.isAssignableFrom(type)) { 108 return (T) factory.Executable().createReference(o.toString()); 109 } 110 if (CtFieldReference.class.isAssignableFrom(type)) { 111 return (T) factory.Field().createReference(o.toString()); 112 } 113 if (CtPackageReference.class.isAssignableFrom(type)) { 114 return (T) factory.Package().createReference(o.toString()); 115 } 116 if (type.isEnum()) { 117 return (T) Enum.valueOf((Class <Enum >) type, o.toString()); 118 } 119 return (T) o.toString(); 120 } 121 122 @SuppressWarnings ("unchecked") 123 public static <T> T convertArray(Factory factory, Class <T> type, 124 Collection <Object > val) { 125 if (type.equals(boolean.class)) { 126 boolean[] ret = new boolean[val.size()]; 127 int i = 0; 128 for (Object o : val) { 129 ret[i++] = convert(factory, boolean.class, o); 130 } 131 return (T) ret; 132 } else if (type.equals(byte.class)) { 133 byte[] ret = new byte[val.size()]; 134 int i = 0; 135 for (Object o : val) { 136 ret[i++] = convert(factory, byte.class, o); 137 } 138 return (T) ret; 139 } else if (type.equals(char.class)) { 140 char[] ret = new char[val.size()]; 141 int i = 0; 142 for (Object o : val) { 143 ret[i++] = convert(factory, char.class, o); 144 } 145 return (T) ret; 146 } else if (type.equals(double.class)) { 147 double[] ret = new double[val.size()]; 148 int i = 0; 149 for (Object o : val) { 150 ret[i++] = convert(factory, double.class, o); 151 } 152 return (T) ret; 153 } else if (type.equals(float.class)) { 154 float[] ret = new float[val.size()]; 155 int i = 0; 156 for (Object o : val) { 157 ret[i++] = convert(factory, float.class, o); 158 } 159 return (T) ret; 160 } else if (type.equals(int.class)) { 161 int[] ret = new int[val.size()]; 162 int i = 0; 163 for (Object o : val) { 164 ret[i++] = convert(factory, int.class, o); 165 } 166 return (T) ret; 167 } else if (type.equals(long.class)) { 168 long[] ret = new long[val.size()]; 169 int i = 0; 170 for (Object o : val) { 171 ret[i++] = convert(factory, long.class, o); 172 } 173 return (T) ret; 174 } else if (type.equals(String .class)) { 175 String [] ret = new String [val.size()]; 176 int i = 0; 177 for (Object o : val) { 178 ret[i++] = convert(factory, String .class, o); 179 } 180 return (T) ret; 181 } else if (CtPackageReference.class.isAssignableFrom(type)) { 182 CtPackageReference[] ret = new CtPackageReference[val.size()]; 183 int i = 0; 184 for (Object o : val) { 185 ret[i++] = convert(factory, CtPackageReference.class, o); 186 } 187 return (T) ret; 188 } else if (CtTypeReference.class.isAssignableFrom(type)) { 189 CtTypeReference[] ret = new CtTypeReference[val.size()]; 190 int i = 0; 191 for (Object o : val) { 192 ret[i++] = convert(factory, CtTypeReference.class, o); 193 } 194 return (T) ret; 195 } else if (CtFieldReference.class.isAssignableFrom(type)) { 196 CtFieldReference[] ret = new CtFieldReference[val.size()]; 197 int i = 0; 198 for (Object o : val) { 199 ret[i++] = convert(factory, CtFieldReference.class, o); 200 } 201 return (T) ret; 202 } else if (CtExecutableReference.class.isAssignableFrom(type)) { 203 CtExecutableReference[] ret = new CtExecutableReference[val.size()]; 204 int i = 0; 205 for (Object o : val) { 206 ret[i++] = convert(factory, CtExecutableReference.class, o); 207 } 208 return (T) ret; 209 } else if (type.isEnum()) { 210 Collection <Enum <?>> ret = new ArrayList <Enum <?>>(); 211 for (Object o : val) { 212 ret.add((Enum ) convert(factory, type, o)); 213 } 214 return (T) ret.toArray((Enum []) Array.newInstance(type, 0)); 215 } 216 return null; 217 } 218 219 Factory factory; 220 221 String processorName; 222 223 private Map <String , Object > props = new TreeMap <String , Object >(); 224 225 public XmlProcessorProperties(Factory factory, String processorName) { 226 this.processorName = processorName; 227 this.factory = factory; 228 } 229 230 public XmlProcessorProperties(Factory factory, String processorName, 231 InputStream stream) throws IOException { 232 this.processorName = processorName; 233 this.factory = factory; 234 load(stream); 235 } 236 237 public void addProperty(String name, Object value) { 238 props.put(name, value); 239 } 240 241 @SuppressWarnings ("unchecked") 242 public <T> T get(Class <T> type, String name) { 243 if (!props.containsKey(name)) { 244 return null; 245 } 246 if (type.isArray()) { 247 return (T) convertArray(factory, type.getComponentType(), 248 (Collection <Object >) props.get(name)); 249 } 250 return (T) convert(factory, type, props.get(name)); 251 } 252 253 public String getProcessorName() { 254 return processorName; 255 } 256 257 private void load(InputStream stream) throws IOException { 258 if (stream == null) 259 return; 260 XMLReader xr; 261 try { 262 xr = XMLReaderFactory.createXMLReader(); 263 Loader handler = new Loader(); 264 xr.setContentHandler(handler); 265 xr.parse(new InputSource (stream)); 266 } catch (SAXException e) { 267 throw new IOException (e.getMessage()); 268 } 269 } 270 271 @Override 272 public String toString() { 273 StringBuffer buf = new StringBuffer (); 274 buf.append("Properties : \n"); 275 276 for (Entry<String , Object > ent : props.entrySet()) { 277 buf.append(ent.getKey()); 278 for (int i = ent.getKey().length(); i < 15; i++) 279 buf.append(" "); 280 buf.append(": " + ent.getValue() + "\n"); 281 } 282 return buf.toString(); 283 } 284 } 285 | Popular Tags |