1 23 package com.tc.jrexx.set; 24 25 import java.util.*; 26 import java.lang.reflect.*; 27 28 31 public abstract class XML implements java.io.Serializable { 32 public String toXML(String tag) { 33 final StringBuffer buffer = new StringBuffer (); 34 buffer.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"); 35 buffer.append("\n<").append(tag).append('>'); 36 final StringTokenizer strTkzr = new StringTokenizer(this.toInnerXML(),"\n"); 37 for (int i=strTkzr.countTokens(); i>0; --i) { 38 buffer.append("\n ").append(strTkzr.nextToken()); 39 } 40 buffer.append("\n</").append(tag).append('>'); 41 return buffer.toString(); 42 } 43 44 public String toXML() { 45 final StringBuffer buffer = new StringBuffer (); 46 buffer.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"); 47 buffer.append("\n").append(this.toInnerXML()); 48 return buffer.toString(); 49 } 50 51 protected String toInnerXML() { 52 try { 53 final StringBuffer buffer = new StringBuffer (); 54 55 final HashSet methodNames = new HashSet(); 56 java.lang.reflect.Method [] methods = this.getClass().getMethods(); 57 for (int i=0; i<methods.length; ++i) { 58 60 String name = methods[i].getName(); 61 if (name.length()<=3) continue; 62 if (name.startsWith("get")==false) continue; 63 if (Character.toUpperCase(name.charAt(3))!=name.charAt(3)) continue; 64 65 68 name = name.substring(3); 69 methodNames.add(name); 70 methodNames.add(Character.toLowerCase(name.charAt(0))+name.substring(1)); 71 methodNames.add(name.toUpperCase()); 72 methodNames.add(name.toLowerCase()); 73 } 74 75 final Field[] fields = this.getClass().getFields(); 76 for (int i=0; i<fields.length; ++i) { 77 if (Modifier.isStatic(fields[i].getModifiers())) continue; 78 79 String name = fields[i].getName(); 80 if (methodNames.contains(name)) continue; 81 82 Object value = fields[i].get(this); 83 if (value!=null) { 84 if (value instanceof XML) { 85 buffer.append("\n<").append(name).append(">"); 86 StringTokenizer strTkzr = new StringTokenizer(((XML)value).toInnerXML(),"\n"); 87 while (strTkzr.hasMoreTokens()) buffer.append("\n ").append(strTkzr.nextToken()); 88 buffer.append("\n</").append(name).append(">"); 89 } else { 90 if (value.getClass().isArray()) { 91 Object [] array = (Object [])value; 92 for (int t=0; t<array.length; ++t) { 93 buffer.append("\n<").append(name).append(">"); 94 if (array[t] instanceof XML) { 95 StringTokenizer strTkzr = new StringTokenizer(((XML)array[t]).toInnerXML(),"\n"); 96 while (strTkzr.hasMoreTokens()) 97 buffer.append("\n ").append(strTkzr.nextToken()); 98 } else { 99 buffer.append("\n ").append(array[t]); 100 } 101 buffer.append("\n</").append(name).append(">"); 102 } 103 } else { 104 buffer.append("\n<").append(name).append(">"); 105 buffer.append("\n ").append(value); 106 buffer.append("\n</").append(name).append(">"); 107 } 108 } 109 } 110 } 111 112 113 for (int i=0; i<methods.length; ++i) { 114 String name = methods[i].getName(); 115 name = name.substring(3); 116 if (methodNames.contains(name)==false) continue; 117 Object value = methods[i].invoke(this,null); 118 if (value!=null) { 119 if (value instanceof XML) { 120 buffer.append("\n<").append(name).append(">"); 121 StringTokenizer strTkzr = new StringTokenizer(((XML)value).toInnerXML(),"\n"); 122 while (strTkzr.hasMoreTokens()) buffer.append("\n ").append(strTkzr.nextToken()); 123 buffer.append("\n</").append(name).append(">"); 124 } else { 125 if (value.getClass().isArray()) { 126 Object [] array = (Object [])value; 127 for (int t=0; t<array.length; ++t) { 128 buffer.append("\n<").append(name).append(">"); 129 if (array[t] instanceof XML) { 130 StringTokenizer strTkzr = new StringTokenizer(((XML)array[t]).toInnerXML(),"\n"); 131 while (strTkzr.hasMoreTokens()) 132 buffer.append("\n ").append(strTkzr.nextToken()); 133 } else { 134 buffer.append("\n ").append(array[t]); 135 } 136 buffer.append("\n</").append(name).append(">"); 137 } 138 } else { 139 buffer.append("\n<").append(name).append(">"); 140 buffer.append("\n ").append(value); 141 buffer.append("\n</").append(name).append(">"); 142 } 143 } 144 } 145 } 146 147 return buffer.toString(); 148 } catch(InvocationTargetException e) { 149 throw new RuntimeException (e.getMessage()); 150 } catch(IllegalAccessException e) { 151 throw new RuntimeException (e.getMessage()); 152 } 153 } 154 155 156 public String toString() { 157 final StringBuffer buffer = new StringBuffer (); 158 buffer.append(this.getClass().getName()); 159 buffer.append("\n").append(this.toInnerString()); 160 return buffer.toString(); 161 } 162 163 protected String toInnerString() { 164 try { 165 final StringBuffer buffer = new StringBuffer (); 166 167 final HashSet methodNames = new HashSet(); 168 java.lang.reflect.Method [] methods = this.getClass().getMethods(); 169 for (int i=0; i<methods.length; ++i) { 170 172 String name = methods[i].getName(); 173 if (name.length()<=3) continue; 174 if (name.startsWith("get")==false) continue; 175 if (Character.toUpperCase(name.charAt(3))!=name.charAt(3)) continue; 176 177 180 name = name.substring(3); 181 methodNames.add(name); 182 methodNames.add(Character.toLowerCase(name.charAt(0))+name.substring(1)); 183 methodNames.add(name.toUpperCase()); 184 methodNames.add(name.toLowerCase()); 185 } 186 187 final Field[] fields = this.getClass().getFields(); 188 for (int i=0; i<fields.length; ++i) { 189 if (Modifier.isStatic(fields[i].getModifiers())) continue; 190 191 String name = fields[i].getName(); 192 if (methodNames.contains(name)) continue; 193 194 Object value = fields[i].get(this); 195 if (value!=null) { 196 if (value instanceof XML) { 197 buffer.append("\n").append(name); 198 StringTokenizer strTkzr = new StringTokenizer(((XML)value).toInnerString(),"\n"); 199 while (strTkzr.hasMoreTokens()) buffer.append("\n ").append(strTkzr.nextToken()); 200 } else { 201 if (value.getClass().isArray()) { 202 Object [] array = (Object [])value; 203 for (int t=0; t<array.length; ++t) { 204 buffer.append("\n").append(name); 205 if (array[t] instanceof XML) { 206 StringTokenizer strTkzr = new StringTokenizer(((XML)array[t]).toInnerString(),"\n"); 207 while (strTkzr.hasMoreTokens()) 208 buffer.append("\n ").append(strTkzr.nextToken()); 209 } else { 210 buffer.append("\n ").append(array[t]); 211 } 212 } 213 } else { 214 buffer.append("\n").append(name).append(": ").append(value); 215 } 216 } 217 } 218 } 219 220 221 for (int i=0; i<methods.length; ++i) { 222 String name = methods[i].getName(); 223 name = name.substring(3); 224 if (methodNames.contains(name)==false) continue; 225 Object value = methods[i].invoke(this,null); 226 if (value!=null) { 227 if (value instanceof XML) { 228 buffer.append("\n").append(name); 229 StringTokenizer strTkzr = new StringTokenizer(((XML)value).toInnerString(),"\n"); 230 while (strTkzr.hasMoreTokens()) buffer.append("\n ").append(strTkzr.nextToken()); 231 } else { 232 if (value.getClass().isArray()) { 233 Object [] array = (Object [])value; 234 for (int t=0; t<array.length; ++t) { 235 buffer.append("\n").append(name); 236 if (array[t] instanceof XML) { 237 StringTokenizer strTkzr = new StringTokenizer(((XML)array[t]).toInnerString(),"\n"); 238 while (strTkzr.hasMoreTokens()) 239 buffer.append("\n ").append(strTkzr.nextToken()); 240 } else { 241 buffer.append("\n ").append(array[t]); 242 } 243 } 244 } else { 245 buffer.append("\n").append(name).append(": ").append(value); 246 } 247 } 248 } 249 } 250 251 return buffer.toString(); 252 } catch(InvocationTargetException e) { 253 throw new RuntimeException (e.getMessage()); 254 } catch(IllegalAccessException e) { 255 throw new RuntimeException (e.getMessage()); 256 } 257 } 258 } 259 | Popular Tags |