1
package com.thaiopensource.relaxng.util;
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;
20
import java.io.IOException;
21
import java.util.ArrayList;
22
import java.util.List;
26
* Ant task to validate XML files using RELAX NG or other schema languages.
29
public class JingTask extends Task {
31
private File schemaFile;
33
private final List filesets = new ArrayList();
34
private PropertyMapBuilder properties = new PropertyMapBuilder();
35
private boolean failOnError = true;
36
private SchemaReader schemaReader = null;
38
private class LogErrorHandler extends ErrorHandlerImpl {
39
int logLevel = Project.MSG_ERR;
41
public void warning(SAXParseException e) throws SAXParseException {
42
logLevel = Project.MSG_WARN;
46
public void error(SAXParseException e) {
47
logLevel = Project.MSG_ERR;
51
public void printException(Throwable e) {
52
logLevel = Project.MSG_ERR;
53
super.printException(e);
56
public void print(String message) {
57
log(message, logLevel);
62
RngProperty.CHECK_ID_IDREF.add(properties);
65
public void execute() throws BuildException {
66
if (schemaFile == null)
67
throw new BuildException("There must be an rngFile or schemaFile attribute",
69
if (src == null && filesets.size() == 0)
70
throw new BuildException("There must be a file attribute or a fileset child element",
73
ErrorHandlerImpl eh = new LogErrorHandler();
75
boolean hadError = false;
78
ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader);
79
if (!driver.loadSchema(ValidationDriver.fileInputSource(schemaFile)))
83
if (!driver.validate(ValidationDriver.fileInputSource(src)))
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]))))
98
catch (SAXException e) {
100
eh.printException(e);
102
catch (IOException e) {
104
eh.printException(e);
106
if (hadError && failOnError)
107
throw new BuildException("Validation failed, messages should have been provided.", getLocation());
111
* Handles the <code>rngfile</code> attribute.
113
* @param rngFilename the attribute value
115
public void setRngfile(String rngFilename) {
116
schemaFile = getProject().resolveFile(rngFilename);
120
* Handles the <code>schemafile</code> attribute.
122
* @param schemaFilename the attribute value
124
public void setSchemafile(String schemaFilename) {
125
schemaFile = getProject().resolveFile(schemaFilename);
128
public void setFile(File file) {
133
* Handles the <code>checkid</code> attribute.
135
* @param checkid the attribute value converted to a boolean
137
public void setCheckid(boolean checkid) {
138
properties.put(RngProperty.CHECK_ID_IDREF,
139
checkid ? Flag.PRESENT : null);
143
* Handles the <code>compactsyntax</code> attribute.
145
* @param compactsyntax the attribute value converted to a boolean
147
public void setCompactsyntax(boolean compactsyntax) {
148
schemaReader = compactsyntax ? CompactSchemaReader.getInstance() : null;
152
* Handles the <code>feasible</code> attribute.
154
* @param feasible the attribute value converted to a boolean
156
public void setFeasible(boolean feasible) {
157
properties.put(RngProperty.FEASIBLE, feasible ? Flag.PRESENT : null);
161
* Handles the phase attribute.
163
* @param phase the attribute value
165
public void setPhase(String phase) {
166
properties.put(SchematronProperty.PHASE, phase);
170
* Handles the <code>failonerror</code> attribute.
172
* @param failOnError the attribute value converted to a boolean
174
public void setFailonerror(boolean failOnError) {
175
this.failOnError = failOnError;
178
public void addFileset(FileSet set) {