KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > relaxng > util > JingTask


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 JavaDoc;
17 import org.xml.sax.SAXParseException JavaDoc;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Vector JavaDoc;
22
23
24 /**
25  * Ant task to validate XML files using RELAX NG or other schema languages.
26  */

27
28 public class JingTask extends Task {
29
30   private File JavaDoc schemaFile;
31   private File JavaDoc src;
32   private final Vector JavaDoc filesets = new Vector JavaDoc();
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 JavaDoc e) throws SAXParseException JavaDoc {
41       logLevel = Project.MSG_WARN;
42       super.warning(e);
43     }
44
45     public void error(SAXParseException JavaDoc e) {
46       logLevel = Project.MSG_ERR;
47       super.error(e);
48     }
49
50     public void printException(Throwable JavaDoc e) {
51       logLevel = Project.MSG_ERR;
52       super.printException(e);
53     }
54
55     public void print(String JavaDoc 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 JavaDoc dir = fs.getDir(project);
89       String JavaDoc[] srcs = ds.getIncludedFiles();
90       for (int j = 0; j < srcs.length; j++) {
91         if (!driver.validate(ValidationDriver.fileInputSource(new File JavaDoc(dir, srcs[j]))))
92           hadError = true;
93       }
94     }
95       }
96     }
97     catch (SAXException JavaDoc e) {
98       hadError = true;
99       eh.printException(e);
100     }
101     catch (IOException JavaDoc 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   /**
110    * Handles the <code>rngfile</code> attribute.
111    *
112    * @param rngFilename the attribute value
113    */

114   public void setRngfile(String JavaDoc rngFilename) {
115     schemaFile = project.resolveFile(rngFilename);
116   }
117
118   /**
119    * Handles the <code>schemafile</code> attribute.
120    *
121    * @param schemaFilename the attribute value
122    */

123   public void setSchemafile(String JavaDoc schemaFilename) {
124     schemaFile = project.resolveFile(schemaFilename);
125   }
126
127   public void setFile(File JavaDoc file) {
128     this.src = file;
129   }
130
131   /**
132    * Handles the <code>checkid</code> attribute.
133    *
134    * @param checkid the attribute value converted to a boolean
135    */

136   public void setCheckid(boolean checkid) {
137     properties.put(RngProperty.CHECK_ID_IDREF,
138                    checkid ? Flag.PRESENT : null);
139   }
140
141   /**
142    * Handles the <code>compactsyntax</code> attribute.
143    *
144    * @param compactsyntax the attribute value converted to a boolean
145    */

146   public void setCompactsyntax(boolean compactsyntax) {
147     schemaReader = compactsyntax ? CompactSchemaReader.getInstance() : null;
148   }
149
150   /**
151    * Handles the <code>feasible</code> attribute.
152    *
153    * @param feasible the attribute value converted to a boolean
154    */

155   public void setFeasible(boolean feasible) {
156     properties.put(RngProperty.FEASIBLE, feasible ? Flag.PRESENT : null);
157   }
158
159   /**
160    * Handles the phase attribute.
161    *
162    * @param phase the attribute value
163    */

164   public void setPhase(String JavaDoc phase) {
165     SchematronProperty.PHASE.put(properties, phase);
166   }
167
168   /**
169    * Handles the <code>failonerror</code> attribute.
170    *
171    * @param failOnError the attribute value converted to a boolean
172    */

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