1 23 24 31 package org.enhydra.ant.taskdefs; 32 33 import java.io.File ; 34 import java.io.FileNotFoundException ; 35 import java.io.IOException ; 36 import java.util.Properties ; 37 import javax.xml.transform.OutputKeys ; 38 import org.apache.tools.ant.BuildException; 39 import org.apache.tools.ant.Task; 40 import org.enhydra.dods.xml.XmlUtil; 41 import org.enhydra.dods.xml.XmlUtilException; 42 43 46 public class DomlValidateTask extends Task { 47 48 private String doml; 49 private String projRoot; 50 private String template; 51 private String force; 52 53 56 public DomlValidateTask() {} 57 58 62 public void setDoml(String doml) { 63 this.doml = doml; 64 } 65 66 70 public void setProjRoot(String projRoot) { 71 this.projRoot = projRoot; 72 } 73 74 78 public void setTemplate(String temp) { 79 this.template = temp; 80 } 81 82 86 public void setForce(String force) { 87 this.force = force; 88 } 89 90 public void execute() throws BuildException { 91 try { 92 if (doml == null) { 93 doml = getDomlFile(); 94 } 95 if (projRoot == null) { 96 projRoot = getProjectRoot(); 97 } 98 if (template == null) { 99 template = "standard"; 100 } 101 copyDoml(); 102 } catch (Exception e) { 103 e.printStackTrace(); 104 } 105 } 106 107 public void copyDoml() throws FileNotFoundException , IOException { 108 File domlFile = new File (doml); 109 110 if (!domlFile.exists()) { 111 throw new FileNotFoundException (doml + " file not found"); 112 } 113 File projFile = new File (projRoot); 114 115 if (!projFile.exists()) { 116 throw new FileNotFoundException (projRoot + " not found"); 117 } 118 try { 119 if (!force.equals("true")) { int dotIndex = doml.indexOf("."); 121 String update = projRoot 122 + doml.substring(doml.lastIndexOf(File.separator), 123 doml.lastIndexOf(".")) + ".uptodate"; 124 File updateFile = new File (update); 125 126 if (domlFile.lastModified() < updateFile.lastModified()) { 127 return; 128 } 129 } 130 } catch (StringIndexOutOfBoundsException ex) {} 131 Properties prop = new Properties (); 132 133 prop.setProperty(OutputKeys.DOCTYPE_SYSTEM, 134 "file:///" + System.getProperty("DODS_HOME") + File.separator 135 + "build" + File.separator + "dtd" + File.separator + "doml.dtd"); 136 prop.setProperty(OutputKeys.VERSION, "1.0"); 137 prop.setProperty(OutputKeys.ENCODING, "UTF-8"); 138 prop.setProperty(OutputKeys.STANDALONE, "no"); 139 try { 140 XmlUtil xmlUtil = new XmlUtil(doml); 141 142 xmlUtil.setSingleAttribute("/doml/database@templateset", null, 143 template); 144 xmlUtil.store(projRoot + File.separator + domlFile.getName(), prop); 145 } catch (XmlUtilException e) { 146 e.printStackTrace(); 147 } 148 } 149 150 154 public static String getProjectRoot() { 155 String projectRoot = null; 156 157 projectRoot = System.getProperty("PROJECT_ROOT"); 158 if (projectRoot == null) { 159 projectRoot = "."; 160 } 161 return projectRoot; 162 } 163 164 168 public static String getDomlFile() { 169 String strDoml = null; 170 171 strDoml = System.getProperty("DOML_FILE"); 172 String domlFile = null; 173 174 if (strDoml == null) { 175 File current = new File ("."); 176 String [] files = current.list(); 177 178 out: 179 for (int i = 0; i < files.length; i++) { 180 if (files[i].toLowerCase().endsWith(".doml")) { 181 domlFile = files[i]; 182 break out; 183 } else { 184 domlFile = null; 185 } 186 } 187 strDoml = domlFile; 188 } 189 return strDoml; 190 } 191 192 public static void main(String [] args) { 193 DomlValidateTask validate = new DomlValidateTask(); 194 195 try { 196 validate.copyDoml(); 197 } catch (Exception e) { 198 e.printStackTrace(); 199 } 200 } 201 } 202 | Popular Tags |