KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ConfigGenerator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 import java.io.BufferedWriter JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.velocity.Template;
29 import org.apache.velocity.VelocityContext;
30 import org.apache.velocity.app.Velocity;
31 import org.jdom.Document;
32 import org.jdom.input.SAXBuilder;
33 import org.jdom.Element;
34 import org.jdom.Attribute;
35
36 /**
37  * @author <a HREF="joriskuipers@xs4all.nl">Joris Kuipers</a>
38  *
39  * This class can be used to generate a CruiseControl config.xml.
40  * It parses the given xml-file for &lt;project&gt;-elements, and then
41  * calls the Velocity-template "config.vm" to generate the xml-file.
42  * The list of &lt;project&gt; JDom-elements is put into the context
43  * as "projects".
44  */

45 public class ConfigGenerator {
46
47     public static void main(String JavaDoc[] args) {
48         try {
49             Velocity.init();
50
51             SAXBuilder builder = new SAXBuilder();
52             Document projectsDoc = builder.build(new File JavaDoc(args[0]));
53
54             ConfigGenerator configGenerator = new ConfigGenerator();
55
56             Element rootElement = projectsDoc.getRootElement();
57
58             rootElement = configGenerator.replaceWithDefaultValues(rootElement);
59
60
61             if(rootElement.getChild("testattributes").getAttribute("testMode").getValue().toString().equals("yes"))
62             {
63                 rootElement = configGenerator.replaceWithTestValues(rootElement);
64             }
65
66             VelocityContext context = new VelocityContext();
67             context.put("root", rootElement);
68             Writer JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(args[1]));
69             Template configTemplate = Velocity.getTemplate("config.vm");
70             configTemplate.merge(context, writer);
71             writer.close();
72         } catch (Exception JavaDoc e) {
73             System.err.println(e);
74             e.printStackTrace();
75         }
76     }
77
78     //Sets Project Attributes with the default values
79
public Element replaceWithDefaultValues(Element rootElement)
80     {
81         List JavaDoc projectAttributeList;
82         List JavaDoc defaultProjectAttributeList;
83         Attribute tempAttr;
84         Attribute tempProjAttr;
85         Attribute newAttr;
86         boolean attrExists = false;
87
88         defaultProjectAttributeList = rootElement.getChild("defaultproject").getAttributes();
89         for(int i=0; i<rootElement.getChildren("project").size(); i++)
90         {
91             projectAttributeList = ((Element)rootElement.getChildren("project").get(i)).getAttributes();
92             int projectAttributeListSize = projectAttributeList.size();
93             for(int j=0; j<defaultProjectAttributeList.size(); j++)
94             {
95                 tempAttr = (Attribute)defaultProjectAttributeList.get(j);
96                 for(int k=0; k<projectAttributeListSize; k++)
97                 {
98                     tempProjAttr = (Attribute)projectAttributeList.get(k);
99                     if(tempProjAttr.getName().equals(tempAttr.getName()))
100                     {
101                         attrExists = true;
102                         break;
103                     }
104                     attrExists = false;
105                 }
106                 if(!attrExists)
107                 {
108                     newAttr = new Attribute(tempAttr.getName(),tempAttr.getValue());
109                     projectAttributeList.add(newAttr);
110                 }
111
112
113             }
114         }
115         return rootElement;
116     }
117
118     //Sets Project Attributes with the test values if testMode="yes" in testattributes tag
119
public Element replaceWithTestValues(Element rootElement)
120     {
121         List JavaDoc projectAttributeList;
122         List JavaDoc testAttributesList;
123         Attribute tempAttr;
124         Attribute tempProjAttr;
125         Attribute newAttr;
126
127         testAttributesList = rootElement.getChild("testattributes").getAttributes();
128         for(int i=0; i<rootElement.getChildren("project").size(); i++)
129         {
130             projectAttributeList = ((Element)rootElement.getChildren("project").get(i)).getAttributes();
131             int projectAttributeListSize = projectAttributeList.size();
132             for(int j=0; j<testAttributesList.size(); j++)
133             {
134                 tempAttr = (Attribute)testAttributesList.get(j);
135                 for(int k=0; k<projectAttributeListSize; k++)
136                 {
137                     tempProjAttr = (Attribute)projectAttributeList.get(k);
138                     if(tempProjAttr.getName().equals(tempAttr.getName()))
139                     {
140                         newAttr = new Attribute(tempAttr.getName(),tempAttr.getValue());
141                         projectAttributeList.set(k,newAttr);
142                         break;
143                     }
144                 }
145             }
146         }
147         return rootElement;
148     }
149
150 }
151
Popular Tags