KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > dumper > AntDumper


1 package net.sf.invicta.dumper;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.StringTokenizer JavaDoc;
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 /**
13  * The main dumper of the Invicta project. Generates an ANT's build.xml
14  * file according to the project definition.
15  **/

16 public class AntDumper extends InvictaBasicDumper {
17
18     // Template of the header of the generated build.xml file.
19
protected final static String JavaDoc 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     // Template of the footer of the generated build.xml file
24
protected final static String JavaDoc ANT_FILE_FOOTER_TEMPLATE = "\n\n</project>\n";
25     
26     /**
27      * Constructor for AntDumper.
28      */

29     public AntDumper() {
30         super();
31     }
32
33     /**
34      *
35      */

36     public String JavaDoc getName() {
37         return "ant";
38     }
39     
40     /**
41      * Returns the content of the generated build.xml.
42      */

43     public String JavaDoc getDumpContent() throws InvictaException {
44
45         StringBuffer JavaDoc dump = new StringBuffer JavaDoc();
46     
47         // Format the header of the new ANT build.xml file.
48
TemplateHelper.format(ANT_FILE_HEADER_TEMPLATE, project, dump);
49                          
50         Logger.info("ANT dumping components: ");
51         // Go over all components and format their templates
52
for (Iterator JavaDoc iter = getProject().getComponents().iterator(); iter.hasNext();) {
53             InvictaComponent component = (InvictaComponent) iter.next();
54             log(component.getName() + ", ");
55                             
56             String JavaDoc template = component.getTemplate();
57             
58             // Format the template of the current component (add to the dump content)
59
TemplateHelper.format(template, getProject(), component, dump);
60         }
61         log("\n");
62     
63         // Format the footer of the new ant build file.
64
TemplateHelper.format(ANT_FILE_FOOTER_TEMPLATE, getProject(), dump);
65         return indentXML(dump.toString());
66     }
67     
68     /**
69      * Indent the given XML content (format, organize tabs).
70      */

71     protected String JavaDoc indentXML (String JavaDoc xml) {
72         
73         // For each line starting with '<' (not '<?' or '<!') increase indent
74
// only if the line ends with '>' (not '/>' or '->').
75
//ident all lines as well with the same indentation
76
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
77         int indent = 0;
78         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(xml,"\n");
79         while (tok.hasMoreTokens()) {
80         
81             String JavaDoc 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 JavaDoc buffer, int indent) {
104         while (indent-- > 0) {
105             buffer.append("\t");
106         }
107     }
108
109 }
110
Popular Tags