KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > xml > XmlValidator


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s):
23  */

24
25 package org.objectweb.cjdbc.common.xml;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.StringReader JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 import org.objectweb.cjdbc.common.i18n.Translate;
36 import org.objectweb.cjdbc.common.util.Constants;
37 import org.xml.sax.ErrorHandler JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.SAXParseException JavaDoc;
41 import org.xml.sax.XMLReader JavaDoc;
42 import org.xml.sax.ext.LexicalHandler JavaDoc;
43 import org.xml.sax.helpers.DefaultHandler JavaDoc;
44 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
45
46 /**
47  * Validate a document and its DTD.
48  *
49  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
50  */

51 public class XmlValidator extends DefaultHandler JavaDoc
52     implements
53       ErrorHandler JavaDoc,
54       LexicalHandler JavaDoc
55 {
56
57   /** XML parser. */
58   private XMLReader JavaDoc parser;
59   private String JavaDoc pathToDtd;
60   private boolean isXmlValid = false;
61   private boolean isDtdValid = false;
62   private String JavaDoc xmlContent;
63   private ArrayList JavaDoc errors;
64   private ArrayList JavaDoc warnings;
65
66   /**
67    * Allow to use the xml validator as an external program
68    *
69    * @param args the xmlfile and the dtd file
70    * @throws Exception if fails
71    */

72   public static void main(String JavaDoc[] args) throws Exception JavaDoc
73   {
74     if (args.length < 1 || args.length > 2)
75     {
76       System.out.println("usage: XmlValidator [xmlFile] ([dtd]) ");
77       System.exit(0);
78     }
79
80     String JavaDoc fileName = args[0];
81     String JavaDoc dtdName = Constants.C_JDBC_DTD_FILE;
82     if (args.length == 2)
83       dtdName = args[1];
84     else
85       System.out.println("Using default DTD:" + Constants.C_JDBC_DTD_FILE);
86
87     File JavaDoc dtd = null;
88     dtd = new File JavaDoc(ClassLoader.getSystemResource(dtdName).getFile());
89     File JavaDoc xmlFile = null;
90     try
91     {
92       xmlFile = new File JavaDoc(ClassLoader.getSystemResource(fileName).getFile());
93     }
94     catch (RuntimeException JavaDoc e)
95     {
96       xmlFile = new File JavaDoc(fileName);
97     }
98
99     if (!dtd.exists())
100     {
101       System.out.println("Cannot find specified dtd");
102       System.exit(1);
103     }
104     if (!xmlFile.exists())
105     {
106       System.out.println("Cannot find specified xml file");
107       System.exit(1);
108     }
109
110     System.out.println("Validating:\tFile:" + xmlFile.getName() + " with dtd:"
111         + dtd.getName());
112
113     // Validate xml and dtd
114
XmlValidator validator = new XmlValidator(dtd.getAbsolutePath(),
115         new FileReader JavaDoc(xmlFile));
116
117     // Display Results
118
if (!validator.isDtdValid())
119       System.out.println("[FAILED:Dtd is not valid]");
120     else if (!validator.isXmlValid())
121       System.out.println("[FAILED:xml is not valid]");
122     else if (validator.isXmlValid())
123       System.out.println("[OK]");
124
125     if (validator.getLastException() != null)
126     {
127       ArrayList JavaDoc errors = validator.getExceptions();
128       for (int i = 0; i < errors.size(); i++)
129         System.out.println("\t(parsing error):"
130             + ((Exception JavaDoc) errors.get(i)).getMessage());
131     }
132   }
133
134   /**
135    * Check the given dtd, and the given xml are valid.
136    *
137    * @param pathToDtd path to dtd
138    * @param xml source to parse as a string
139    */

140   public XmlValidator(String JavaDoc pathToDtd, String JavaDoc xml)
141   {
142     validate(pathToDtd, xml);
143   }
144
145   /**
146    * @see #XmlValidator(String pathToDtd,String xml)
147    */

148   public XmlValidator(String JavaDoc pathToDtd, FileReader JavaDoc file) throws IOException JavaDoc
149   {
150     // Read the file
151
BufferedReader JavaDoc in = new BufferedReader JavaDoc(file);
152     StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
153     String JavaDoc line;
154     do
155     {
156       line = in.readLine();
157       if (line != null)
158         xml.append(line.trim());
159     }
160     while (line != null);
161     xmlContent = xml.toString();
162     validate(pathToDtd, xmlContent);
163   }
164
165   /**
166    * get the xml that was formatted
167    *
168    * @return xml
169    */

170   public String JavaDoc getXmlContent()
171   {
172     return xmlContent;
173   }
174
175   /**
176    * Starts the verification of the xml document AND the dtd
177    *
178    * @param pathToDtd path
179    * @param xml content
180    */

181   public void validate(String JavaDoc pathToDtd, String JavaDoc xml)
182   {
183     System.setProperty("org.xml.sax.driver",
184         "org.apache.crimson.parser.XMLReaderImpl");
185     errors = new ArrayList JavaDoc();
186     warnings = new ArrayList JavaDoc();
187     try
188     {
189       // Store dtd reference
190
this.pathToDtd = pathToDtd;
191       // Instantiate a new parser
192
parser = XMLReaderFactory.createXMLReader();
193       // Activate validation
194
parser.setFeature("http://xml.org/sax/features/validation", true);
195       // Install error handler
196
parser.setErrorHandler(this);
197       // Install document handler
198
parser.setContentHandler(this);
199       parser.setProperty("http://xml.org/sax/properties/lexical-handler", this);
200       // Install local entity resolver
201
parser.setEntityResolver(this);
202       InputSource JavaDoc input = new InputSource JavaDoc(new StringReader JavaDoc(xml));
203       parser.parse(input);
204     }
205     catch (Exception JavaDoc e)
206     {
207       //throw new Exception("Xml document can not be validated.");
208
//e.printStackTrace();
209
addError(e);
210       isXmlValid = false;
211     }
212   }
213
214   /**
215    * Allows to parse the document with a local copy of the DTD whatever the
216    * original <code>DOCTYPE</code> found. Warning, this method is called only
217    * if the XML document contains a <code>DOCTYPE</code>.
218    *
219    * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
220    * java.lang.String)
221    */

222   public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
223       throws SAXException JavaDoc
224   {
225
226     File JavaDoc dtd = new File JavaDoc(pathToDtd);
227     if (dtd.exists())
228     {
229       try
230       {
231         FileReader JavaDoc reader = new FileReader JavaDoc(dtd);
232         return new InputSource JavaDoc(reader);
233       }
234       catch (Exception JavaDoc e)
235       { //impossible
236
}
237     }
238
239     InputStream JavaDoc stream = XmlValidator.class
240         .getResourceAsStream("/" + pathToDtd);
241     if (stream == null)
242     {
243       SAXException JavaDoc sax = new SAXException JavaDoc(Translate.get(
244           "virtualdatabase.xml.dtd.not.found", pathToDtd));
245       addError(sax);
246       throw sax;
247     }
248
249     return new InputSource JavaDoc(stream);
250   }
251
252   /**
253    * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
254    */

255   public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc
256   {
257     addError(exception);
258   }
259
260   /**
261    * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
262    */

263   public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc
264   {
265     addError(exception);
266   }
267
268   /**
269    * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
270    */

271   public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc
272   {
273     warnings.add(exception);
274   }
275
276   /**
277    * @see org.xml.sax.ContentHandler#endDocument()
278    */

279   public void endDocument() throws SAXException JavaDoc
280   {
281     if (errors.size() == 0)
282       this.isXmlValid = true;
283   }
284
285   /**
286    * @return Returns the isXmlValid.
287    */

288   public boolean isValid()
289   {
290     return isXmlValid && isDtdValid;
291   }
292
293   /**
294    * Return the last cause of parsing failure
295    *
296    * @return exception, null if no exception
297    */

298   public Exception JavaDoc getLastException()
299   {
300     if (errors.size() == 0)
301       return null;
302     else
303       return (Exception JavaDoc) errors.get(errors.size() - 1);
304   }
305
306   /**
307    * Retrieve an <code>ArrayList</code> of all parsing exceptions
308    *
309    * @return an <code>ArrayList</code> of <code>Exception</code>
310    */

311   public ArrayList JavaDoc getExceptions()
312   {
313     return errors;
314   }
315
316   /**
317    * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
318    */

319   public void comment(char[] ch, int start, int length) throws SAXException JavaDoc
320   {
321   }
322
323   /**
324    * @see org.xml.sax.ext.LexicalHandler#endCDATA()
325    */

326   public void endCDATA() throws SAXException JavaDoc
327   {
328   }
329
330   /**
331    * @see org.xml.sax.ext.LexicalHandler#endDTD()
332    */

333   public void endDTD() throws SAXException JavaDoc
334   {
335     if (errors.size() == 0)
336     {
337       isDtdValid = true;
338     }
339     else
340     {
341       isDtdValid = false;
342     }
343   }
344
345   /**
346    * @see org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
347    */

348   public void endEntity(String JavaDoc name) throws SAXException JavaDoc
349   {
350   }
351
352   /**
353    * @see org.xml.sax.ext.LexicalHandler#startCDATA()
354    */

355   public void startCDATA() throws SAXException JavaDoc
356   {
357   }
358
359   /**
360    * @see org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String,
361    * java.lang.String, java.lang.String)
362    */

363   public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
364       throws SAXException JavaDoc
365   {
366   }
367
368   /**
369    * @see org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
370    */

371   public void startEntity(String JavaDoc name) throws SAXException JavaDoc
372   {
373   }
374
375   /**
376    * @return Returns the isDtdValid.
377    */

378   public boolean isDtdValid()
379   {
380     return isDtdValid;
381   }
382
383   /**
384    * @param isDtdValid The isDtdValid to set.
385    */

386   public void setDtdValid(boolean isDtdValid)
387   {
388     this.isDtdValid = isDtdValid;
389   }
390
391   /**
392    * @return Returns the isXmlValid.
393    */

394   public boolean isXmlValid()
395   {
396     return isXmlValid;
397   }
398
399   /**
400    * @param isXmlValid The isXmlValid to set.
401    */

402   public void setXmlValid(boolean isXmlValid)
403   {
404     this.isXmlValid = isXmlValid;
405   }
406
407   private void addError(Exception JavaDoc e)
408   {
409     errors.add(e);
410   }
411
412   /**
413    * @return Returns the warnings.
414    */

415   public ArrayList JavaDoc getWarnings()
416   {
417     return warnings;
418   }
419 }
Popular Tags