Coverage Report - org.x4o.plugin.ant.eld.doc.EldDocWriterTask
 
Classes in this File Line Coverage Branch Coverage Complexity
EldDocWriterTask
100%
55/55
100%
12/12
2.5
 
 1  
 /*
 2  
  * Copyright (c) 2004-2012, Willem Cazander
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without modification, are permitted provided
 6  
  * that the following conditions are met:
 7  
  * 
 8  
  * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
 9  
  *   following disclaimer.
 10  
  * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 11  
  *   the following disclaimer in the documentation and/or other materials provided with the distribution.
 12  
  * 
 13  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 14  
  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 15  
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 16  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 17  
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 18  
  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 19  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 20  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 21  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 22  
  */
 23  
 
 24  
 package org.x4o.plugin.ant.eld.doc;
 25  
 
 26  
 import java.io.File;
 27  
 
 28  
 import org.apache.tools.ant.BuildException;
 29  
 import org.apache.tools.ant.Project;
 30  
 import org.apache.tools.ant.Task;
 31  
 import org.x4o.xml.core.X4OParserSupportException;
 32  
 import org.x4o.xml.core.config.X4OLanguageClassLoader;
 33  
 import org.x4o.xml.eld.doc.X4OLanguageEldDocWriter;
 34  
 
 35  
 /**
 36  
  * EldDocWriterTask creates schema for language.
 37  
  * 
 38  
  * @author Willem Cazander
 39  
  * @version 1.0 Aug 24, 2012
 40  
  */
 41  10
 public class EldDocWriterTask extends Task {
 42  
 
 43  10
         private String supportclass = null;
 44  10
         private String destdir = null;
 45  10
         private boolean verbose = false;
 46  10
         private boolean failonerror = true;
 47  
         
 48  
         /**
 49  
          * Executes the x4o eld schema task.
 50  
          * @see org.apache.tools.ant.Task#execute()
 51  
          */
 52  
         @Override
 53  
         public void execute() throws BuildException {
 54  
                 try {
 55  10
                         executeTask();
 56  7
                 } catch (BuildException e) {
 57  7
                         if (isFailonerror()) {
 58  6
                                 throw e;
 59  
                         } else {
 60  1
                                 log(e.getMessage(), Project.MSG_WARN);
 61  
                         }
 62  3
                 }
 63  4
         }
 64  
         
 65  
         private void executeTask() throws BuildException {
 66  10
                 if (getSupportclass()==null) {
 67  2
                         throw new BuildException("supportclass attribute is not set.");
 68  
                 }
 69  8
                 if (getDestdir()==null) {
 70  1
                         throw new BuildException("basePath attribute is not set.");
 71  
                 }
 72  7
                 if (isVerbose()) {
 73  1
                         log("Execute task from: "+getLocation());
 74  1
                         log("destdir:"+getDestdir());
 75  1
                         log("supportclass:"+getSupportclass());
 76  1
                         log("verbose:"+isVerbose());
 77  1
                         log("failonerror:"+isFailonerror());
 78  
                 }
 79  7
                 File basePathFile = new File(getDestdir());
 80  7
                 if (basePathFile.exists()==false) {
 81  1
                         throw new BuildException("destdir does not exists: "+basePathFile);
 82  
                 }
 83  6
                 Class<?> parserSupport = null;
 84  
                 try {
 85  6
                         parserSupport = X4OLanguageClassLoader.loadClass(getSupportclass());
 86  2
                 } catch (ClassNotFoundException e) {
 87  2
                         throw new BuildException("Could not load class: "+getSupportclass(),e);
 88  4
                 }
 89  
                 
 90  
                 // Config and start schema writer
 91  4
                 X4OLanguageEldDocWriter writer = new X4OLanguageEldDocWriter();
 92  4
                 writer.setBasePath(basePathFile);
 93  4
                 writer.setLanguageParserSupport(parserSupport);
 94  
                 try {
 95  4
                         if (isVerbose()) {
 96  1
                                 log("Starting writing.");
 97  
                         }
 98  4
                         long startTime = System.currentTimeMillis();
 99  4
                         writer.execute();
 100  3
                         long stopTime = System.currentTimeMillis();
 101  3
                         log("Done writing elddoc in "+(stopTime-startTime)+" ms.");
 102  1
                 } catch (X4OParserSupportException e) {
 103  1
                         throw new BuildException(e);
 104  3
                 }
 105  3
         }
 106  
 
 107  
         /**
 108  
          * @return the supportclass
 109  
          */
 110  
         public String getSupportclass() {
 111  19
                 return supportclass;
 112  
         }
 113  
 
 114  
         /**
 115  
          * @param supportclass the supportclass to set
 116  
          */
 117  
         public void setSupportclass(String supportclass) {
 118  8
                 this.supportclass = supportclass;
 119  8
         }
 120  
 
 121  
         /**
 122  
          * @return the destdir
 123  
          */
 124  
         public String getDestdir() {
 125  16
                 return destdir;
 126  
         }
 127  
 
 128  
         /**
 129  
          * @param destdir the destdir to set
 130  
          */
 131  
         public void setDestdir(String destdir) {
 132  8
                 this.destdir = destdir;
 133  8
         }
 134  
 
 135  
         /**
 136  
          * @return the verbose
 137  
          */
 138  
         public boolean isVerbose() {
 139  12
                 return verbose;
 140  
         }
 141  
 
 142  
         /**
 143  
          * @param verbose the verbose to set
 144  
          */
 145  
         public void setVerbose(boolean verbose) {
 146  1
                 this.verbose = verbose;
 147  1
         }
 148  
 
 149  
         /**
 150  
          * @return the failonerror
 151  
          */
 152  
         public boolean isFailonerror() {
 153  8
                 return failonerror;
 154  
         }
 155  
 
 156  
         /**
 157  
          * @param failonerror the failonerror to set
 158  
          */
 159  
         public void setFailonerror(boolean failonerror) {
 160  1
                 this.failonerror = failonerror;
 161  1
         }
 162  
 }