1 28 package org.jibx.binding.ant; 29 30 import java.io.File ; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 34 import org.apache.tools.ant.AntClassLoader; 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.DirectoryScanner; 37 import org.apache.tools.ant.Task; 38 import org.apache.tools.ant.types.FileSet; 39 import org.apache.tools.ant.types.Path; 40 import org.jibx.binding.Compile; 41 import org.jibx.runtime.JiBXException; 42 43 48 public class CompileTask extends Task { 49 private boolean m_load; 50 private boolean m_verbose; 51 private Path m_classpath; 52 private List m_fileSet; 53 private List m_bindingFileSet; 54 private String m_bindingFile; 55 56 61 public void init() throws BuildException { 62 super.init(); 66 67 m_fileSet = new ArrayList (); 72 m_bindingFileSet = new ArrayList (); 73 } 74 75 80 private String [] getPaths() { 81 String [] pathArray = null; 82 if (m_classpath != null) { 83 pathArray = m_classpath.list(); 89 } else { 90 pathArray = new String [m_fileSet.size()]; 95 96 for(int i = 0; i < m_fileSet.size(); i++) { 97 FileSet fileSet = (FileSet)m_fileSet.get(i); 98 File directory = fileSet.getDir(project); 99 pathArray[i] = directory.getAbsolutePath(); 100 } 101 } 102 103 try { 105 System.setProperty("java.class.path", 106 ((AntClassLoader)CompileTask.class.getClassLoader()).getClasspath()); 107 } catch (Exception e) { 108 System.err.println("Failed setting classpath from Ant task"); 109 } 110 111 112 if (m_verbose) { 116 log("Using the following paths:"); 117 for (int i = 0; i < pathArray.length; i++) { 118 log(" " + pathArray[i]); 119 } 120 } 121 122 return pathArray; 123 } 124 125 129 private String [] getBindings() { 130 String [] bindings = null; 131 if (m_bindingFileSet.size() == 0) { 132 bindings = new String [] { m_bindingFile }; 137 } else { 138 ArrayList paths = new ArrayList (); 142 for(int i = 0; i < m_bindingFileSet.size(); i++){ 143 FileSet bPath = (FileSet)m_bindingFileSet.get(i); 144 145 DirectoryScanner dirScn = bPath.getDirectoryScanner(project); 146 String [] bndingFiles = dirScn.getIncludedFiles(); 147 148 for(int x = 0; x < bndingFiles.length; x++){ 149 String fullPath = dirScn.getBasedir() 150 + System.getProperty("file.separator") 151 + bndingFiles[x]; 152 paths.add(fullPath); 153 } 154 } 155 156 bindings = (String [])paths.toArray(new String [paths.size()]); 160 } 161 162 if (m_verbose) { 167 log("Using the following binding paths:"); 168 for (int i = 0; i < bindings.length; i++) { 169 log(" " + bindings[i]); 170 } 171 } 172 173 return bindings; 174 } 175 176 188 private void validateRequiredFields() throws BuildException { 189 if (m_bindingFileSet.isEmpty() && m_bindingFile == null) { 193 throw new BuildException("Either the binding attribute or at " 194 + "least one bindingset nested element " 195 + "must be defined."); 196 } 197 198 if (!m_bindingFileSet.isEmpty() && m_bindingFile != null) { 203 throw new BuildException("You cannot specify both a binding attribute and a " 204 + "bindingset nested element."); 205 } 206 207 if(m_classpath == null && m_fileSet.isEmpty()) { 211 throw new BuildException("You must specify either a classpath " 212 + "or at least one nested classpathset element."); 213 } 214 215 if (m_classpath != null && !m_fileSet.isEmpty()) { 219 throw new BuildException("You cannot specify both a classpath and a " 220 + "classpathset nested element."); 221 } 222 } 223 224 231 public void execute() throws BuildException{ 232 try{ 233 validateRequiredFields(); 234 235 String [] pathArr = getPaths(); 236 String [] bindings = getBindings(); 237 238 Compile compiler = new Compile(); 239 240 compiler.setLoad(m_load); 245 246 compiler.setVerbose(m_verbose); 247 248 compiler.compile(pathArr, bindings); 249 250 } catch(JiBXException jEx) { 251 jEx.printStackTrace(); 252 throw new BuildException("JiBXException in JiBX binding compilation", jEx); 253 } 254 } 255 256 260 public void addClassPathSet(FileSet fSet){ 261 m_fileSet.add(fSet); 262 } 263 264 267 public Path getClasspath() { 268 return m_classpath; 269 } 270 271 276 public void setClasspath(Path classpath) { 277 if (m_classpath == null) { 278 m_classpath = classpath; 279 } 280 else { 281 m_classpath.append(classpath); 282 } 283 } 284 285 290 public Path createClasspath() { 291 if (m_classpath == null) { 292 m_classpath = new Path(project); 293 } 294 295 return m_classpath; 296 } 297 298 301 public void setBinding(String file) { 302 m_bindingFile = file; 303 } 304 305 309 public void addBindingFileSet(FileSet bfSet) { 310 m_bindingFileSet.add(bfSet); 311 } 312 313 316 public void setLoad(boolean bool) { 317 m_load = bool; 318 } 319 320 323 public void setVerbose(boolean bool) { 324 m_verbose = bool; 325 } 326 } 327 | Popular Tags |