1 package com.genimen.djeneric.tools.generator.util; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.util.ArrayList ; 6 import java.util.Enumeration ; 7 import java.util.Iterator ; 8 import java.util.Properties ; 9 10 import javax.xml.parsers.DocumentBuilder ; 11 import javax.xml.parsers.DocumentBuilderFactory ; 12 import javax.xml.transform.OutputKeys ; 13 import javax.xml.transform.Transformer ; 14 import javax.xml.transform.TransformerFactory ; 15 import javax.xml.transform.dom.DOMSource ; 16 import javax.xml.transform.stream.StreamResult ; 17 18 import org.w3c.dom.Document ; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.NodeList ; 21 22 import com.genimen.djeneric.util.DjLogger; 23 24 public class ProjectDocumentHandler 25 { 26 private Document _xmlDoc = null; 27 28 public final static String PROJECT_ELEMENT = "project"; 29 public final static String PROJECT_OUTPUT_DIR = "dir"; 30 public final static String PROJECT_OVERWRITE_ACTION = "overwriteaction"; 31 public final static String PROJECT_OPENAFTERGENERATE = "openaftergenerate"; 32 33 public final static String TEMPLATESET_ELEMENT = "templateset"; 34 public final static String TEMPLATESET_NAME = "name"; 35 36 public final static String TEMPLATESETTINGS_ELEMENT = "templatesettings"; 37 public final static String TEMPLATESETTINGS_NAME = "name"; 38 public final static String TEMPLATESETTINGS_ENABLED = "enabled"; 39 40 public ProjectDocumentHandler() throws Exception 41 { 42 } 43 44 public ProjectDocumentHandler(String xmlString) throws Exception 45 { 46 ByteArrayInputStream sr = new ByteArrayInputStream (xmlString.getBytes("UTF8")); 47 48 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 49 _xmlDoc = dfactory.newDocumentBuilder().parse(sr); 50 } 51 52 public String [] getTemplateSetIds() 53 { 54 ArrayList result = new ArrayList (); 55 56 Element project = (Element ) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0); 57 58 if (project != null) 59 { 60 NodeList templateSets = project.getElementsByTagName(TEMPLATESET_ELEMENT); 61 for (int t = 0; t < templateSets.getLength(); t++) 62 { 63 if (templateSets.item(t) instanceof Element ) 64 { 65 Element templateSet = (Element ) (templateSets.item(t)); 66 result.add(templateSet.getAttribute(TEMPLATESET_NAME)); 67 } 68 } 69 } 70 return (String []) result.toArray(new String [0]); 71 } 72 73 public String [] getDisabledTemplates() 74 { 75 ArrayList result = new ArrayList (); 76 77 Element project = (Element ) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0); 78 79 if (project != null) 80 { 81 NodeList templateSets = project.getElementsByTagName(TEMPLATESETTINGS_ELEMENT); 82 for (int t = 0; t < templateSets.getLength(); t++) 83 { 84 if (templateSets.item(t) instanceof Element ) 85 { 86 Element templateSet = (Element ) (templateSets.item(t)); 87 if (templateSet.getAttribute(TEMPLATESETTINGS_ENABLED).equals("false")) result.add(templateSet 88 .getAttribute(TEMPLATESETTINGS_NAME)); 89 } 90 } 91 } 92 return (String []) result.toArray(new String [0]); 93 } 94 95 public String getOutputPath() 96 { 97 Element project = (Element ) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0); 98 99 if (project != null) 100 { 101 String result = project.getAttribute(PROJECT_OUTPUT_DIR); 102 if (result != null) return result; 103 } 104 return ""; 105 } 106 107 public int getOverwriteAction() 108 { 109 Element project = (Element ) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0); 110 111 if (project != null) 112 { 113 String result = project.getAttribute(PROJECT_OVERWRITE_ACTION); 114 try 115 { 116 if (result != null) return Integer.parseInt(result); 117 } 118 catch (Exception x) 119 { 120 DjLogger.log(x); 121 } 122 } 123 return ProjectProperties.OVERWRITE_ASK; 124 } 125 126 public boolean getOpenAfterGenerate() 127 { 128 Element project = (Element ) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0); 129 130 if (project != null) 131 { 132 return "true".equals(project.getAttribute(PROJECT_OPENAFTERGENERATE)); 133 } 134 return false; 135 } 136 137 public String createXmlString(ProjectProperties projProps) throws Exception 138 { 139 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 140 DocumentBuilder builder = factory.newDocumentBuilder(); 141 Document doc = builder.newDocument(); 142 143 Element root = doc.createElement(PROJECT_ELEMENT); 144 root.setAttribute(PROJECT_OUTPUT_DIR, projProps.getOutputPath()); 145 root.setAttribute(PROJECT_OVERWRITE_ACTION, "" + projProps.getOverwriteAction()); 146 root.setAttribute(PROJECT_OPENAFTERGENERATE, "" + projProps.isOpenAfterGenerate()); 147 148 doc.appendChild(root); 149 150 Iterator it = projProps.getTemplateSetIterator(); 151 while (it.hasNext()) 152 { 153 Element setId = doc.createElement(TEMPLATESET_ELEMENT); 154 setId.setAttribute(TEMPLATESET_NAME, it.next().toString()); 155 156 root.appendChild(setId); 157 } 158 159 Properties props = projProps.getTemplateProperties(); 160 Enumeration enum1 = props.keys(); 161 while (enum1.hasMoreElements()) 162 { 163 String key = enum1.nextElement().toString(); 164 if (key.endsWith(ProjectProperties.TEMPLATE_ENABLED_KEY)) 165 { 166 boolean enabled = props.getProperty(key).equals("true"); 167 if (!enabled) 168 { 169 int idx = key.indexOf("."); 170 String name = key.substring(0, idx); 171 172 Element disabled = doc.createElement(TEMPLATESETTINGS_ELEMENT); 173 disabled.setAttribute(TEMPLATESETTINGS_NAME, name); 174 disabled.setAttribute(TEMPLATESETTINGS_ENABLED, "false"); 175 root.appendChild(disabled); 176 } 177 } 178 } 179 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 180 181 Transformer serializer = TransformerFactory.newInstance().newTransformer(); 182 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 183 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 184 serializer.transform(new DOMSource (doc), new StreamResult (baos)); 185 186 return baos.toString(); 187 } 188 189 public void load(ProjectProperties projectProperties) 190 { 191 projectProperties.setOutputPath(getOutputPath()); 192 String [] ids = getTemplateSetIds(); 193 for (int i = 0; i < ids.length; i++) 194 projectProperties.addTemplate(ids[i]); 195 ids = getDisabledTemplates(); 196 197 for (int i = 0; i < ids.length; i++) 198 { 199 projectProperties.enableTemplate(ids[i], false); 200 } 201 projectProperties.setOverwriteAction(getOverwriteAction()); 202 projectProperties.setOpenAfterGenerate(getOpenAfterGenerate()); 203 projectProperties.setModified(false); 204 } 205 } | Popular Tags |