1 package net.sf.invicta.dumper; 2 3 import java.util.Iterator ; 4 import java.util.StringTokenizer ; 5 6 import net.sf.invicta.InvictaException; 7 import net.sf.invicta.Logger; 8 import net.sf.invicta.api.InvictaComponent; 9 import net.sf.invicta.template.TemplateHelper; 10 11 12 16 public class AntDumper extends InvictaBasicDumper { 17 18 protected final static String ANT_FILE_HEADER_TEMPLATE 20 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 21 "<project name=\"%{projectName}\" basedir=\"%{projectDir}\" default=\"help\">\n\n"; 22 23 protected final static String ANT_FILE_FOOTER_TEMPLATE = "\n\n</project>\n"; 25 26 29 public AntDumper() { 30 super(); 31 } 32 33 36 public String getName() { 37 return "ant"; 38 } 39 40 43 public String getDumpContent() throws InvictaException { 44 45 StringBuffer dump = new StringBuffer (); 46 47 TemplateHelper.format(ANT_FILE_HEADER_TEMPLATE, project, dump); 49 50 Logger.info("ANT dumping components: "); 51 for (Iterator iter = getProject().getComponents().iterator(); iter.hasNext();) { 53 InvictaComponent component = (InvictaComponent) iter.next(); 54 log(component.getName() + ", "); 55 56 String template = component.getTemplate(); 57 58 TemplateHelper.format(template, getProject(), component, dump); 60 } 61 log("\n"); 62 63 TemplateHelper.format(ANT_FILE_FOOTER_TEMPLATE, getProject(), dump); 65 return indentXML(dump.toString()); 66 } 67 68 71 protected String indentXML (String xml) { 72 73 StringBuffer result = new StringBuffer (); 77 int indent = 0; 78 StringTokenizer tok = new StringTokenizer (xml,"\n"); 79 while (tok.hasMoreTokens()) { 80 81 String nextTok = tok.nextToken().trim(); 82 83 if (nextTok.startsWith("</")) 84 indent--; 85 86 indent(result, indent); 87 result.append(nextTok); 88 result.append("\n"); 89 90 if (nextTok.startsWith("<") && 91 !nextTok.startsWith("<?") && 92 !nextTok.startsWith("<!") && 93 (nextTok.indexOf("</") == -1)) 94 indent++; 95 96 if (nextTok.endsWith("/>")) 97 indent--; 98 99 } 100 return result.toString(); 101 } 102 103 protected static void indent(StringBuffer buffer, int indent) { 104 while (indent-- > 0) { 105 buffer.append("\t"); 106 } 107 } 108 109 } 110 | Popular Tags |