1 17 18 package org.objectweb.jac.aspects.export; 19 20 import gnu.regexp.RE; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Writer ; 27 import java.lang.StringBuffer ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import org.apache.log4j.Logger; 36 import org.objectweb.jac.aspects.naming.NamingAC; 37 import org.objectweb.jac.aspects.persistence.ValueConverter; 38 import org.objectweb.jac.core.ACManager; 39 import org.objectweb.jac.core.MethodPointcut; 40 import org.objectweb.jac.core.NameRepository; 41 import org.objectweb.jac.core.Wrappee; 42 import org.objectweb.jac.core.rtti.ClassItem; 43 import org.objectweb.jac.core.rtti.ClassRepository; 44 import org.objectweb.jac.core.rtti.CollectionItem; 45 import org.objectweb.jac.core.rtti.FieldItem; 46 import org.objectweb.jac.util.Strings; 47 48 public class Exporter { 49 static Logger logger = Logger.getLogger("export"); 50 51 Collection roots; 52 Collection allow; 53 Collection deny; 54 55 RE[] allowRegexps; 56 RE[] denyRegexps; 57 58 Map toExport = new HashMap (); Set exported = new HashSet (); 61 62 NameRepository nr; 63 64 67 public Exporter(String [] roots, String [] allow, String [] deny) { 68 this(Arrays.asList(roots),Arrays.asList(allow),Arrays.asList(deny)); 69 } 70 71 80 public Exporter(Collection roots, Collection allow, Collection deny) { 81 nr = (NameRepository)NameRepository.get(); 82 this.roots = roots; 83 this.allow = allow; 84 Iterator i = allow.iterator(); 85 int count = 0; 86 allowRegexps = new RE[allow.size()]; 87 while (i.hasNext()) { 88 String s = (String )i.next(); 89 try { 90 allowRegexps[count] = MethodPointcut.buildRegexp(s); 91 } catch (Exception e) { 92 logger.error("Failed to build regexp for "+s,e); 93 } 94 } 95 this.deny = deny; 96 i = deny.iterator(); 97 count = 0; 98 denyRegexps = new RE[deny.size()]; 99 while (i.hasNext()) { 100 String s = (String )i.next(); 101 try { 102 denyRegexps[count] = MethodPointcut.buildRegexp(s); 103 } catch (Exception e) { 104 logger.error("Failed to build regexp for "+s,e); 105 } 106 } 107 } 108 109 112 public void export(File file) throws IOException { 113 FileOutputStream out = new FileOutputStream (file); 114 try { 115 export(out); 116 } finally { 117 out.close(); 118 } 119 } 120 121 124 public void export(OutputStream outStream) throws IOException { 125 export(outStream,"UTF-8"); 126 } 127 128 131 public void export(OutputStream outStream, String encoding) throws IOException { 132 Writer out = new OutputStreamWriter (outStream,encoding); 133 out.write("<?xml version=\"1.0\" encoding=\""+encoding+"\"?>\n"); 134 out.write("<export>\n"); 135 int count = 0; 136 Iterator i = NameRepository.getObjects(roots).iterator(); 137 while (i.hasNext()) { 138 Object o = i.next(); 139 String name = nr.getName(o); 140 export(out,o,name,name); 141 count++; 142 } 143 while (!toExport.isEmpty()) { 144 i = new HashMap (toExport).entrySet().iterator(); 145 while (i.hasNext()) { 146 Map.Entry entry = (Map.Entry )i.next(); 147 Object o = entry.getKey(); 148 String opath = (String )entry.getValue(); 149 String name = nr.getName(o); 150 export(out,o,name,opath); 151 count++; 152 } 153 } 154 logger.info(count+" objects exported"); 155 Map counters = 156 ((NamingAC)ACManager.getACM().getACFromFullName("naming")).getNameCounters(); 157 i = counters.entrySet().iterator(); 158 while(i.hasNext()) { 159 Map.Entry entry = (Map.Entry )i.next(); 160 out.write("<nameCounter>\n"); 161 out.write(" <name>"+entry.getKey()+"</name>\n"); 162 out.write(" <counter>"+entry.getValue()+"</counter>\n"); 163 out.write("</nameCounter>\n"); 164 } 165 logger.info("Name counters exported"); 166 out.write("</export>\n"); 167 out.flush(); 168 } 169 170 static public String escapeChar(char c) { 171 return (c == '<') ? "<" : 172 (c == '>') ? ">" : 173 (c == '\'') ? "'" : 174 (c == '\"') ? """ : 175 (c == '&') ? "&" : 176 (c == ']') ? "]" : 177 null; 178 } 179 180 static public String escapeString(String s) { 181 StringBuffer buf = new StringBuffer (s.length()*2); 182 for (int i=0; i<s.length(); i++) { 183 char c = s.charAt(i); 184 String escaped = escapeChar(c); 185 if (escaped==null) 186 buf.append(c); 187 else 188 buf.append(escaped); 189 } 190 return buf.toString(); 191 } 192 193 201 protected boolean allowExport(ClassItem cli) { 202 for (int i=0; i<denyRegexps.length;i++) { 203 RE regexp = denyRegexps[i]; 204 if (regexp!=null && cli.isSubClassOf(regexp)) 205 return false; 206 } 207 208 for (int i=0; i<allowRegexps.length;i++) { 209 RE regexp = allowRegexps[i]; 210 if (regexp!=null && cli.isSubClassOf(regexp)) 211 return true; 212 } 213 214 return false; 215 } 216 217 224 public void export(Writer out, Object o, String name ,String opath) throws IOException { 225 ClassItem cl = ClassRepository.get().getClass(o); 226 if (name==null) { 227 logger.error("Skipping unamed object "+o+" "+cl.getName()+" at "+opath); 228 new Exception ().printStackTrace(); 229 toExport.remove(o); 230 return; 231 } 232 if (exported.contains(o)) { 233 logger.debug("Skipping already exported "+name+" "+cl.getName()); 234 toExport.remove(o); 235 return; 236 } 237 if (!allowExport(cl)) { 238 logger.debug("Skipping not allowed "+name+" "+cl.getName()); 239 toExport.remove(o); 240 return; 241 } 242 logger.debug("Exporting "+name+" "+cl.getName()); 243 exported.add(o); 244 out.write("<object name=\""+name+"\" class=\""+cl.getName()+"\">\n"); 245 Iterator i = cl.getAllFields().iterator(); 246 while (i.hasNext()) { 247 FieldItem field = (FieldItem)i.next(); 248 if (!field.isCalculated() && !field.isTransient() && 249 !field.isStatic() && !field.isTransient()) 250 { 251 String newPath = opath+"."+field.getName(); 252 try { 253 if (field instanceof CollectionItem) { 254 CollectionItem collection = (CollectionItem)field; 255 ClassItem componentType = collection.getComponentType(); 256 if (allowExport(componentType)) { 257 out.write(" <field name=\""+field.getName()+"\">\n"); 258 if (collection.isMap()) { 259 out.write(" <map>\n"); 260 Iterator j = ((Map )collection.getThroughAccessor(o)).entrySet().iterator(); 261 while (j.hasNext()) { 262 Map.Entry entry = (Map.Entry )j.next(); 263 out.write(" <entry><key>"); 264 writeValue(out,entry.getKey(),newPath+"[key]"); 265 out.write("</key><value>"); 266 writeValue(out,entry.getValue(),newPath+"["+entry.getKey()+"]"); 267 out.write("</value></entry>\n"); 268 } 269 out.write(" </map>\n"); 270 } else { 271 if (collection.isList()) 272 out.write(" <list>\n"); 273 else if (collection.isSet()) 274 out.write(" <set>\n"); 275 Iterator j = ((Collection )collection.getThroughAccessor(o)).iterator(); 276 int count = 0; 277 while (j.hasNext()) { 278 Object item = j.next(); 279 out.write(" "); 280 writeValue(out,item,newPath+"["+count+"]"); 281 out.write("\n"); 282 count++; 283 } 284 if (collection.isList()) 285 out.write(" </list>\n"); 286 else if (collection.isSet()) 287 out.write(" </set>\n"); 288 } 289 out.write(" </field>\n"); 290 } 291 } else { 292 if (allowExport(field.getTypeItem())) { 293 out.write(" <field name=\""+field.getName()+"\">\n"); 294 out.write(" "); 295 writeValue(out,field.getThroughAccessor(o),newPath); 296 out.write("\n"); 297 out.write(" </field>\n"); 298 } 299 } 300 } catch (Exception e) { 301 logger.error("Failed to export "+name+"."+field); 302 } 303 } 304 } 305 out.write("</object>\n"); 306 } 307 308 protected void writeValue(Writer out, Object value, String opath) throws IOException { 309 if (value instanceof Wrappee) { 310 if (value!=null) { 311 if (!exported.contains(value)) { 312 toExport.put(value,opath); 313 logger.debug("toExport "+opath+" -> "+value+" "+nr.getName(value)); 314 } 315 out.write("<reference>"+nr.getName(value)+"</reference>"); 316 } else { 317 out.write("<reference>null</reference>"); 318 } 319 } else { 320 if (value!=null) { 321 out.write("<primitive_value>"+ 322 escapeString(Strings.slashify(ValueConverter.objectToString(null,value)))+ 323 "</primitive_value>"); 324 } else { 325 out.write("<primitive_value>null</primitive_value>"); 326 } 327 } 328 } 329 } 330 | Popular Tags |