1 22 import java.io.BufferedWriter ; 23 import java.io.File ; 24 import java.io.FileWriter ; 25 import java.io.Writer ; 26 import java.util.List ; 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 45 public class ConfigGenerator { 46 47 public static void main(String [] args) { 48 try { 49 Velocity.init(); 50 51 SAXBuilder builder = new SAXBuilder(); 52 Document projectsDoc = builder.build(new File (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 writer = new BufferedWriter (new FileWriter (args[1])); 69 Template configTemplate = Velocity.getTemplate("config.vm"); 70 configTemplate.merge(context, writer); 71 writer.close(); 72 } catch (Exception e) { 73 System.err.println(e); 74 e.printStackTrace(); 75 } 76 } 77 78 public Element replaceWithDefaultValues(Element rootElement) 80 { 81 List projectAttributeList; 82 List 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 public Element replaceWithTestValues(Element rootElement) 120 { 121 List projectAttributeList; 122 List 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 |