1 package com.thaiopensource.relaxng.util; 2 3 import com.thaiopensource.util.PropertyMapBuilder; 4 import com.thaiopensource.validate.Flag; 5 import com.thaiopensource.validate.SchemaReader; 6 import com.thaiopensource.validate.ValidationDriver; 7 import com.thaiopensource.validate.schematron.SchematronProperty; 8 import com.thaiopensource.validate.rng.CompactSchemaReader; 9 import com.thaiopensource.validate.rng.RngProperty; 10 import com.thaiopensource.xml.sax.ErrorHandlerImpl; 11 import org.apache.tools.ant.BuildException; 12 import org.apache.tools.ant.DirectoryScanner; 13 import org.apache.tools.ant.Project; 14 import org.apache.tools.ant.Task; 15 import org.apache.tools.ant.types.FileSet; 16 import org.xml.sax.SAXException ; 17 import org.xml.sax.SAXParseException ; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Vector ; 22 23 24 27 28 public class JingTask extends Task { 29 30 private File schemaFile; 31 private File src; 32 private final Vector filesets = new Vector (); 33 private PropertyMapBuilder properties = new PropertyMapBuilder(); 34 private boolean failOnError = true; 35 private SchemaReader schemaReader = null; 36 37 private class LogErrorHandler extends ErrorHandlerImpl { 38 int logLevel = Project.MSG_ERR; 39 40 public void warning(SAXParseException e) throws SAXParseException { 41 logLevel = Project.MSG_WARN; 42 super.warning(e); 43 } 44 45 public void error(SAXParseException e) { 46 logLevel = Project.MSG_ERR; 47 super.error(e); 48 } 49 50 public void printException(Throwable e) { 51 logLevel = Project.MSG_ERR; 52 super.printException(e); 53 } 54 55 public void print(String message) { 56 log(message, logLevel); 57 } 58 } 59 60 public JingTask() { 61 RngProperty.CHECK_ID_IDREF.add(properties); 62 } 63 64 public void execute() throws BuildException { 65 if (schemaFile == null) 66 throw new BuildException("There must be an rngFile or schemaFile attribute", 67 location); 68 if (src == null && filesets.size() == 0) 69 throw new BuildException("There must be a file attribute or a fileset child element", 70 location); 71 72 ErrorHandlerImpl eh = new LogErrorHandler(); 73 74 boolean hadError = false; 75 76 try { 77 ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader); 78 if (!driver.loadSchema(ValidationDriver.fileInputSource(schemaFile))) 79 hadError = true; 80 else { 81 if (src != null) { 82 if (!driver.validate(ValidationDriver.fileInputSource(src))) 83 hadError = true; 84 } 85 for (int i = 0; i < filesets.size(); i++) { 86 FileSet fs = (FileSet)filesets.elementAt(i); 87 DirectoryScanner ds = fs.getDirectoryScanner(project); 88 File dir = fs.getDir(project); 89 String [] srcs = ds.getIncludedFiles(); 90 for (int j = 0; j < srcs.length; j++) { 91 if (!driver.validate(ValidationDriver.fileInputSource(new File (dir, srcs[j])))) 92 hadError = true; 93 } 94 } 95 } 96 } 97 catch (SAXException e) { 98 hadError = true; 99 eh.printException(e); 100 } 101 catch (IOException e) { 102 hadError = true; 103 eh.printException(e); 104 } 105 if (hadError && failOnError) 106 throw new BuildException("Validation failed, messages should have been provided.", location); 107 } 108 109 114 public void setRngfile(String rngFilename) { 115 schemaFile = project.resolveFile(rngFilename); 116 } 117 118 123 public void setSchemafile(String schemaFilename) { 124 schemaFile = project.resolveFile(schemaFilename); 125 } 126 127 public void setFile(File file) { 128 this.src = file; 129 } 130 131 136 public void setCheckid(boolean checkid) { 137 properties.put(RngProperty.CHECK_ID_IDREF, 138 checkid ? Flag.PRESENT : null); 139 } 140 141 146 public void setCompactsyntax(boolean compactsyntax) { 147 schemaReader = compactsyntax ? CompactSchemaReader.getInstance() : null; 148 } 149 150 155 public void setFeasible(boolean feasible) { 156 properties.put(RngProperty.FEASIBLE, feasible ? Flag.PRESENT : null); 157 } 158 159 164 public void setPhase(String phase) { 165 SchematronProperty.PHASE.put(properties, phase); 166 } 167 168 173 public void setFailonerror(boolean failOnError) { 174 this.failOnError = failOnError; 175 } 176 177 public void addFileset(FileSet set) { 178 filesets.addElement(set); 179 } 180 181 } 182 | Popular Tags |