1 package org.ejen.ext; 22 23 import org.ejen.util.DOMUtil; 24 import java.lang.reflect.*; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.Document ; 28 import org.apache.xalan.extensions.ExpressionContext; 29 import org.apache.xml.utils.WrappedRuntimeException; 30 31 62 public class JavaClassToXML { 63 64 65 public static final String DIM_STR = "[]"; 66 67 70 protected JavaClassToXML() {} 71 72 173 public static Node process(ExpressionContext context, String className) { 174 className = org.ejen.util.XSLUtil.evaluate(context, className); 175 Document doc = org.ejen.util.XSLUtil.getContextDocument(context); 176 Class clazz; 177 178 try { 179 clazz = Class.forName(className); 180 } catch (Exception e) { 181 throw new WrappedRuntimeException("Invalid class name", e); 182 } 183 return process(doc, clazz); 184 } 185 186 194 protected static Node process(Document doc, Class c) { 195 try { 196 Element root = doc.createElement("class-definition"); 197 198 root.setAttribute("interface", (c.isInterface()) ? "true" : "false"); 199 String className = c.getName(); 200 int iLastDot = className.lastIndexOf('.'); 201 202 if (iLastDot != -1) { 203 DOMUtil.createCDATANode(doc, root, "package", 204 className.substring(0, iLastDot)); 205 className = className.substring(iLastDot + 1); 206 } 207 DOMUtil.createCDATANode(doc, root, "modifiers", 208 Modifier.toString(c.getModifiers())); 209 DOMUtil.createCDATANode(doc, root, "name", className); 210 Class sc = c.getSuperclass(); 211 212 if (sc != null) { 213 DOMUtil.createNode(doc, root, "extends").appendChild(process(doc, 214 sc)); 215 } 216 Class [] is = c.getInterfaces(); 217 218 if (is.length > 0) { 219 Node nis = DOMUtil.createNode(doc, root, "implements"); 220 221 for (int i = 0; i < is.length; i++) { 222 nis.appendChild(process(doc, is[i])); 223 } 224 } 225 Field[] fs = c.getDeclaredFields(); 226 227 for (int i = 0; i < fs.length; i++) { 228 Field f = fs[i]; 229 int mods = f.getModifiers(); 230 Node nf = DOMUtil.createNode(doc, root, "field"); 231 232 DOMUtil.createCDATANode(doc, nf, "modifiers", 233 Modifier.toString(mods)); 234 DOMUtil.createCDATANode(doc, nf, "type", f.getType().getName()); 235 DOMUtil.createCDATANode(doc, nf, "name", f.getName()); 236 if (Modifier.isStatic(mods) && Modifier.isPublic(mods)) { 237 DOMUtil.createCDATANode(doc, nf, "init", 238 f.get(null).toString()); 239 } 240 } 241 Constructor[] cos = c.getConstructors(); 242 243 for (int i = 0; i < cos.length; i++) { 244 Constructor co = cos[i]; 245 Node nco = DOMUtil.createNode(doc, root, "constructor"); 246 247 DOMUtil.createCDATANode(doc, nco, "modifiers", 248 Modifier.toString(co.getModifiers())); 249 Class [] ps = co.getParameterTypes(); 250 251 for (int j = 0; j < ps.length; j++) { 252 DOMUtil.createCDATANode(doc, nco, "parameter", 253 ps[j].getName()); 254 } 255 Class [] es = co.getExceptionTypes(); 256 257 for (int j = 0; j < es.length; j++) { 258 DOMUtil.createCDATANode(doc, nco, "exception", 259 es[j].getName()); 260 } 261 } 262 Method[] ms = c.getDeclaredMethods(); 263 264 for (int i = 0; i < ms.length; i++) { 265 Method m = ms[i]; 266 Node nm = DOMUtil.createNode(doc, root, "method"); 267 268 DOMUtil.createCDATANode(doc, nm, "modifiers", 269 Modifier.toString(m.getModifiers())); 270 DOMUtil.createCDATANode(doc, nm, "return-type", 271 m.getReturnType().getName()); 272 DOMUtil.createCDATANode(doc, nm, "name", m.getName()); 273 Class [] ps = m.getParameterTypes(); 274 275 for (int j = 0; j < ps.length; j++) { 276 DOMUtil.createCDATANode(doc, nm, "parameter", 277 ps[j].getName()); 278 } 279 Class [] es = m.getExceptionTypes(); 280 281 for (int j = 0; j < es.length; j++) { 282 DOMUtil.createCDATANode(doc, nm, "exception", 283 es[j].getName()); 284 } 285 } 286 Class [] cs = c.getDeclaredClasses(); 287 288 for (int i = 0; i < cs.length; i++) { 289 root.appendChild(process(doc, cs[i])); 290 } 291 return root; 292 } catch (WrappedRuntimeException e) { 293 throw e; 294 } catch (Exception e) { 295 throw new WrappedRuntimeException(e); 296 } 297 } 298 299 322 public static String stripType(ExpressionContext context, String type) { 323 type = stripTypeDims(context, type); 324 int iLastDot = type.lastIndexOf('.'); 325 326 return (iLastDot != -1) ? type.substring(iLastDot + 1) : type; 327 } 328 329 352 public static String stripTypeDims(ExpressionContext context, String type) { 353 type = org.ejen.util.XSLUtil.evaluate(context, type); 354 if (!type.startsWith("[")) { 355 return type; 356 } 357 char c = type.charAt(1); 358 359 switch (c) { 360 case 'B': 361 return "byte"; 362 363 case 'C': 364 return "char"; 365 366 case 'D': 367 return "double"; 368 369 case 'F': 370 return "float"; 371 372 case 'I': 373 return "int"; 374 375 case 'J': 376 return "long"; 377 378 case 'S': 379 return "short"; 380 381 case 'Z': 382 return "boolean"; 383 384 case 'L': 385 break; 386 387 default: 388 throw new RuntimeException ("Unknown type signature: " + c); 389 } 390 391 int b = 2; 392 int e = type.length() - 1; 393 394 while (b < e && type.charAt(b) == c) { 395 ++b; 396 } 397 return type.substring(b, e); 398 } 399 400 422 public static String dimsOfType(ExpressionContext context, String type) { 423 type = org.ejen.util.XSLUtil.evaluate(context, type); 424 if (!type.startsWith("[")) { 425 return ""; 426 } 427 StringBuffer dims = new StringBuffer (DIM_STR); 428 char c = type.charAt(1); 429 int l = type.length(); 430 431 for (int i = 2; i < l && type.charAt(i) == c; i++) { 432 dims.append(DIM_STR); 433 } 434 return dims.toString(); 435 } 436 437 459 public static String packageOfType(ExpressionContext context, String type) { 460 type = stripTypeDims(context, type); 461 int iLastDot = type.lastIndexOf('.'); 462 463 return (iLastDot != -1) ? type.substring(0, iLastDot) : ""; 464 } 465 } 466
| Popular Tags
|