| 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.prop.rng.RngProperty; |
| 8 |
import com.thaiopensource.validate.prop.schematron.SchematronProperty; |
| 9 |
import com.thaiopensource.validate.rng.CompactSchemaReader; |
| 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.ArrayList; |
| 22 |
import java.util.List; |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Ant task to validate XML files using RELAX NG or other schema languages. |
| 27 |
*/ |
| 28 |
|
| 29 |
public class JingTask extends Task { |
| 30 |
|
| 31 |
private File schemaFile; |
| 32 |
private File src; |
| 33 |
private final List filesets = new ArrayList(); |
| 34 |
private PropertyMapBuilder properties = new PropertyMapBuilder(); |
| 35 |
private boolean failOnError = true; |
| 36 |
private SchemaReader schemaReader = null; |
| 37 |
|
| 38 |
private class LogErrorHandler extends ErrorHandlerImpl { |
| 39 |
int logLevel = Project.MSG_ERR; |
| 40 |
|
| 41 |
public void warning(SAXParseException e) throws SAXParseException { |
| 42 |
logLevel = Project.MSG_WARN; |
| 43 |
super.warning(e); |
| 44 |
} |
| 45 |
|
| 46 |
public void error(SAXParseException e) { |
| 47 |
logLevel = Project.MSG_ERR; |
| 48 |
super.error(e); |
| 49 |
} |
| 50 |
|
| 51 |
public void printException(Throwable e) { |
| 52 |
logLevel = Project.MSG_ERR; |
| 53 |
super.printException(e); |
| 54 |
} |
| 55 |
|
| 56 |
public void print(String message) { |
| 57 |
log(message, logLevel); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public JingTask() { |
| 62 |
RngProperty.CHECK_ID_IDREF.add(properties); |
| 63 |
} |
| 64 |
|
| 65 |
public void execute() throws BuildException { |
| 66 |
if (schemaFile == null) |
| 67 |
throw new BuildException("There must be an rngFile or schemaFile attribute", |
| 68 |
getLocation()); |
| 69 |
if (src == null && filesets.size() == 0) |
| 70 |
throw new BuildException("There must be a file attribute or a fileset child element", |
| 71 |
getLocation()); |
| 72 |
|
| 73 |
ErrorHandlerImpl eh = new LogErrorHandler(); |
| 74 |
|
| 75 |
boolean hadError = false; |
| 76 |
|
| 77 |
try { |
| 78 |
ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader); |
| 79 |
if (!driver.loadSchema(ValidationDriver.fileInputSource(schemaFile))) |
| 80 |
hadError = true; |
| 81 |
else { |
| 82 |
if (src != null) { |
| 83 |
if (!driver.validate(ValidationDriver.fileInputSource(src))) |
| 84 |
hadError = true; |
| 85 |
} |
| 86 |
for (int i = 0; i < filesets.size(); i++) { |
| 87 |
FileSet fs = (FileSet)filesets.get(i); |
| 88 |
DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
| 89 |
File dir = fs.getDir(getProject()); |
| 90 |
String[] srcs = ds.getIncludedFiles(); |
| 91 |
for (int j = 0; j < srcs.length; j++) { |
| 92 |
if (!driver.validate(ValidationDriver.fileInputSource(new File(dir, srcs[j])))) |
| 93 |
hadError = true; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
catch (SAXException e) { |
| 99 |
hadError = true; |
| 100 |
eh.printException(e); |
| 101 |
} |
| 102 |
catch (IOException e) { |
| 103 |
hadError = true; |
| 104 |
eh.printException(e); |
| 105 |
} |
| 106 |
if (hadError && failOnError) |
| 107 |
throw new BuildException("Validation failed, messages should have been provided.", getLocation()); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Handles the <code>rngfile</code> attribute. |
| 112 |
* |
| 113 |
* @param rngFilename the attribute value |
| 114 |
*/ |
| 115 |
public void setRngfile(String rngFilename) { |
| 116 |
schemaFile = getProject().resolveFile(rngFilename); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Handles the <code>schemafile</code> attribute. |
| 121 |
* |
| 122 |
* @param schemaFilename the attribute value |
| 123 |
*/ |
| 124 |
public void setSchemafile(String schemaFilename) { |
| 125 |
schemaFile = getProject().resolveFile(schemaFilename); |
| 126 |
} |
| 127 |
|
| 128 |
public void setFile(File file) { |
| 129 |
this.src = file; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Handles the <code>checkid</code> attribute. |
| 134 |
* |
| 135 |
* @param checkid the attribute value converted to a boolean |
| 136 |
*/ |
| 137 |
public void setCheckid(boolean checkid) { |
| 138 |
properties.put(RngProperty.CHECK_ID_IDREF, |
| 139 |
checkid ? Flag.PRESENT : null); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Handles the <code>compactsyntax</code> attribute. |
| 144 |
* |
| 145 |
* @param compactsyntax the attribute value converted to a boolean |
| 146 |
*/ |
| 147 |
public void setCompactsyntax(boolean compactsyntax) { |
| 148 |
schemaReader = compactsyntax ? CompactSchemaReader.getInstance() : null; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Handles the <code>feasible</code> attribute. |
| 153 |
* |
| 154 |
* @param feasible the attribute value converted to a boolean |
| 155 |
*/ |
| 156 |
public void setFeasible(boolean feasible) { |
| 157 |
properties.put(RngProperty.FEASIBLE, feasible ? Flag.PRESENT : null); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Handles the phase attribute. |
| 162 |
* |
| 163 |
* @param phase the attribute value |
| 164 |
*/ |
| 165 |
public void setPhase(String phase) { |
| 166 |
properties.put(SchematronProperty.PHASE, phase); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Handles the <code>failonerror</code> attribute. |
| 171 |
* |
| 172 |
* @param failOnError the attribute value converted to a boolean |
| 173 |
*/ |
| 174 |
public void setFailonerror(boolean failOnError) { |
| 175 |
this.failOnError = failOnError; |
| 176 |
} |
| 177 |
|
| 178 |
public void addFileset(FileSet set) { |
| 179 |
filesets.add(set); |
| 180 |
} |
| 181 |
|
| 182 |
} |