Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

org.objectweb.cjdbc.common.xml.XmlValidator Class Reference

List of all members.

Public Member Functions

 XmlValidator (String pathToDtd, String xml)
 XmlValidator (String pathToDtd, FileReader file) throws IOException
String getXmlContent ()
void validate (String pathToDtd, String xml)
InputSource resolveEntity (String publicId, String systemId) throws SAXException
void error (SAXParseException exception) throws SAXException
void fatalError (SAXParseException exception) throws SAXException
void warning (SAXParseException exception) throws SAXException
void endDocument () throws SAXException
boolean isValid ()
Exception getLastException ()
ArrayList getExceptions ()
void comment (char[] ch, int start, int length) throws SAXException
void endCDATA () throws SAXException
void endDTD () throws SAXException
void endEntity (String name) throws SAXException
void startCDATA () throws SAXException
void startDTD (String name, String publicId, String systemId) throws SAXException
void startEntity (String name) throws SAXException
boolean isDtdValid ()
void setDtdValid (boolean isDtdValid)
boolean isXmlValid ()
void setXmlValid (boolean isXmlValid)
ArrayList getWarnings ()

Static Public Member Functions

void main (String args[]) throws Exception

Detailed Description

Validate a document and its DTD.

Author:
Nicolas Modrzyk

Definition at line 51 of file XmlValidator.java.


Constructor & Destructor Documentation

org.objectweb.cjdbc.common.xml.XmlValidator.XmlValidator String  pathToDtd,
String  xml
 

Check the given dtd, and the given xml are valid.

Parameters:
pathToDtd path to dtd
xml source to parse as a string

Definition at line 140 of file XmlValidator.java.

References org.objectweb.cjdbc.common.xml.XmlValidator.validate().

Referenced by org.objectweb.cjdbc.common.xml.XmlValidator.main().

00141   {
00142     validate(pathToDtd, xml);
00143   }

org.objectweb.cjdbc.common.xml.XmlValidator.XmlValidator String  pathToDtd,
FileReader  file
throws IOException
 

See also:
XmlValidator(String pathToDtd,String xml)

Definition at line 148 of file XmlValidator.java.

References org.objectweb.cjdbc.common.xml.XmlValidator.validate().

00149   {
00150     // Read the file
00151     BufferedReader in = new BufferedReader(file);
00152     StringBuffer xml = new StringBuffer();
00153     String line;
00154     do
00155     {
00156       line = in.readLine();
00157       if (line != null)
00158         xml.append(line.trim());
00159     }
00160     while (line != null);
00161     xmlContent = xml.toString();
00162     validate(pathToDtd, xmlContent);
00163   }


Member Function Documentation

void org.objectweb.cjdbc.common.xml.XmlValidator.comment char[]  ch,
int  start,
int  length
throws SAXException
 

See also:
org.xml.sax.ext.LexicalHandler#comment(char[], int, int)

Definition at line 319 of file XmlValidator.java.

00320   {
00321   }

void org.objectweb.cjdbc.common.xml.XmlValidator.endCDATA  )  throws SAXException
 

See also:
org.xml.sax.ext.LexicalHandler#endCDATA()

Definition at line 326 of file XmlValidator.java.

00327   {
00328   }

void org.objectweb.cjdbc.common.xml.XmlValidator.endDocument  )  throws SAXException
 

See also:
org.xml.sax.ContentHandler#endDocument()

Definition at line 279 of file XmlValidator.java.

References org.objectweb.cjdbc.common.xml.XmlValidator.isXmlValid.

00280   {
00281     if (errors.size() == 0)
00282       this.isXmlValid = true;
00283   }

void org.objectweb.cjdbc.common.xml.XmlValidator.endDTD  )  throws SAXException
 

See also:
org.xml.sax.ext.LexicalHandler#endDTD()

Definition at line 333 of file XmlValidator.java.

00334   {
00335     if (errors.size() == 0)
00336     {
00337       isDtdValid = true;
00338     }
00339     else
00340     {
00341       isDtdValid = false;
00342     }
00343   }

void org.objectweb.cjdbc.common.xml.XmlValidator.endEntity String  name  )  throws SAXException
 

See also:
org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)

Definition at line 348 of file XmlValidator.java.

00349   {
00350   }

void org.objectweb.cjdbc.common.xml.XmlValidator.error SAXParseException  exception  )  throws SAXException
 

See also:
org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)

Definition at line 255 of file XmlValidator.java.

Referenced by org.objectweb.cjdbc.controller.xml.DatabasesParser.endElement(), org.objectweb.cjdbc.controller.xml.ControllerParser.endElement(), org.objectweb.cjdbc.controller.xml.DatabasesParser.error(), org.objectweb.cjdbc.controller.xml.ControllerParser.error(), org.objectweb.cjdbc.controller.xml.DatabasesParser.fatalError(), org.objectweb.cjdbc.controller.xml.ControllerParser.fatalError(), org.objectweb.cjdbc.controller.xml.DatabasesParser.readXML(), org.objectweb.cjdbc.controller.xml.ControllerParser.readXML(), and org.objectweb.cjdbc.controller.xml.DatabasesParser.startElement().

00256   {
00257     addError(exception);
00258   }

void org.objectweb.cjdbc.common.xml.XmlValidator.fatalError SAXParseException  exception  )  throws SAXException
 

See also:
org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)

Definition at line 263 of file XmlValidator.java.

00264   {
00265     addError(exception);
00266   }

ArrayList org.objectweb.cjdbc.common.xml.XmlValidator.getExceptions  ) 
 

Retrieve an ArrayList of all parsing exceptions

Returns:
an ArrayList of Exception

Definition at line 311 of file XmlValidator.java.

Referenced by org.objectweb.cjdbc.common.xml.XmlValidator.main(), org.objectweb.cjdbc.controller.xml.DatabasesParser.readXML(), org.objectweb.cjdbc.controller.xml.ControllerParser.readXML(), org.objectweb.cjdbc.console.gui.threads.GuiParsingThread.run(), and org.objectweb.cjdbc.console.wizard.XmlWizard.validateDocument().

00312   {
00313     return errors;
00314   }

Exception org.objectweb.cjdbc.common.xml.XmlValidator.getLastException  ) 
 

Return the last cause of parsing failure

Returns:
exception, null if no exception

Definition at line 298 of file XmlValidator.java.

Referenced by org.objectweb.cjdbc.common.xml.XmlValidator.main().

00299   {
00300     if (errors.size() == 0)
00301       return null;
00302     else
00303       return (Exception) errors.get(errors.size() - 1);
00304   }

ArrayList org.objectweb.cjdbc.common.xml.XmlValidator.getWarnings  ) 
 

Returns:
Returns the warnings.

Definition at line 415 of file XmlValidator.java.

Referenced by org.objectweb.cjdbc.controller.xml.DatabasesParser.readXML(), org.objectweb.cjdbc.controller.xml.ControllerParser.readXML(), and org.objectweb.cjdbc.console.wizard.XmlWizard.validateDocument().

00416   {
00417     return warnings;
00418   }

String org.objectweb.cjdbc.common.xml.XmlValidator.getXmlContent  ) 
 

get the xml that was formatted

Returns:
xml

Definition at line 170 of file XmlValidator.java.

00171   {
00172     return xmlContent;
00173   }

boolean org.objectweb.cjdbc.common.xml.XmlValidator.isDtdValid  ) 
 

Returns:
Returns the isDtdValid.

Definition at line 378 of file XmlValidator.java.

00379   {
00380     return isDtdValid;
00381   }

boolean org.objectweb.cjdbc.common.xml.XmlValidator.isValid  ) 
 

Returns:
Returns the isXmlValid.

Definition at line 288 of file XmlValidator.java.

Referenced by org.objectweb.cjdbc.controller.xml.DatabasesParser.readXML(), and org.objectweb.cjdbc.controller.xml.ControllerParser.readXML().

00289   {
00290     return isXmlValid && isDtdValid;
00291   }

boolean org.objectweb.cjdbc.common.xml.XmlValidator.isXmlValid  ) 
 

Returns:
Returns the isXmlValid.

Definition at line 394 of file XmlValidator.java.

00395   {
00396     return isXmlValid;
00397   }

void org.objectweb.cjdbc.common.xml.XmlValidator.main String  args[]  )  throws Exception [static]
 

Allow to use the xml validator as an external program

Parameters:
args the xmlfile and the dtd file
Exceptions:
Exception if fails

Definition at line 72 of file XmlValidator.java.

References org.objectweb.cjdbc.common.xml.XmlValidator.getExceptions(), org.objectweb.cjdbc.common.xml.XmlValidator.getLastException(), org.objectweb.cjdbc.common.xml.XmlValidator.isDtdValid, org.objectweb.cjdbc.common.xml.XmlValidator.isXmlValid, and org.objectweb.cjdbc.common.xml.XmlValidator.XmlValidator().

00073   {
00074     if (args.length < 1 || args.length > 2)
00075     {
00076       System.out.println("usage: XmlValidator [xmlFile] ([dtd]) ");
00077       System.exit(0);
00078     }
00079 
00080     String fileName = args[0];
00081     String dtdName = Constants.C_JDBC_DTD_FILE;
00082     if (args.length == 2)
00083       dtdName = args[1];
00084     else
00085       System.out.println("Using default DTD:" + Constants.C_JDBC_DTD_FILE);
00086 
00087     File dtd = null;
00088     dtd = new File(ClassLoader.getSystemResource(dtdName).getFile());
00089     File xmlFile = null;
00090     try
00091     {
00092       xmlFile = new File(ClassLoader.getSystemResource(fileName).getFile());
00093     }
00094     catch (RuntimeException e)
00095     {
00096       xmlFile = new File(fileName);
00097     }
00098 
00099     if (!dtd.exists())
00100     {
00101       System.out.println("Cannot find specified dtd");
00102       System.exit(1);
00103     }
00104     if (!xmlFile.exists())
00105     {
00106       System.out.println("Cannot find specified xml file");
00107       System.exit(1);
00108     }
00109 
00110     System.out.println("Validating:\tFile:" + xmlFile.getName() + " with dtd:"
00111         + dtd.getName());
00112 
00113     // Validate xml and dtd
00114     XmlValidator validator = new XmlValidator(dtd.getAbsolutePath(),
00115         new FileReader(xmlFile));
00116 
00117     // Display Results
00118     if (!validator.isDtdValid())
00119       System.out.println("[FAILED:Dtd is not valid]");
00120     else if (!validator.isXmlValid())
00121       System.out.println("[FAILED:xml is not valid]");
00122     else if (validator.isXmlValid())
00123       System.out.println("[OK]");
00124 
00125     if (validator.getLastException() != null)
00126     {
00127       ArrayList errors = validator.getExceptions();
00128       for (int i = 0; i < errors.size(); i++)
00129         System.out.println("\t(parsing error):"
00130             + ((Exception) errors.get(i)).getMessage());
00131     }
00132   }

InputSource org.objectweb.cjdbc.common.xml.XmlValidator.resolveEntity String  publicId,
String  systemId
throws SAXException
 

Allows to parse the document with a local copy of the DTD whatever the original DOCTYPE found. Warning, this method is called only if the XML document contains a DOCTYPE.

See also:
org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)

Definition at line 222 of file XmlValidator.java.

00224   {
00225 
00226     File dtd = new File(pathToDtd);
00227     if (dtd.exists())
00228     {
00229       try
00230       {
00231         FileReader reader = new FileReader(dtd);
00232         return new InputSource(reader);
00233       }
00234       catch (Exception e)
00235       { //impossible
00236       }
00237     }
00238 
00239     InputStream stream = XmlValidator.class
00240         .getResourceAsStream("/" + pathToDtd);
00241     if (stream == null)
00242     {
00243       SAXException sax = new SAXException(Translate.get(
00244           "virtualdatabase.xml.dtd.not.found", pathToDtd));
00245       addError(sax);
00246       throw sax;
00247     }
00248 
00249     return new InputSource(stream);
00250   }

void org.objectweb.cjdbc.common.xml.XmlValidator.setDtdValid boolean  isDtdValid  ) 
 

Parameters:
isDtdValid The isDtdValid to set.

Definition at line 386 of file XmlValidator.java.

References org.objectweb.cjdbc.common.xml.XmlValidator.isDtdValid.

00387   {
00388     this.isDtdValid = isDtdValid;
00389   }

void org.objectweb.cjdbc.common.xml.XmlValidator.setXmlValid boolean  isXmlValid  ) 
 

Parameters:
isXmlValid The isXmlValid to set.

Definition at line 402 of file XmlValidator.java.

References org.objectweb.cjdbc.common.xml.XmlValidator.isXmlValid.

00403   {
00404     this.isXmlValid = isXmlValid;
00405   }

void org.objectweb.cjdbc.common.xml.XmlValidator.startCDATA  )  throws SAXException
 

See also:
org.xml.sax.ext.LexicalHandler#startCDATA()

Definition at line 355 of file XmlValidator.java.

00356   {
00357   }

void org.objectweb.cjdbc.common.xml.XmlValidator.startDTD String  name,
String  publicId,
String  systemId
throws SAXException
 

See also:
org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String, java.lang.String, java.lang.String)

Definition at line 363 of file XmlValidator.java.

00365   {
00366   }

void org.objectweb.cjdbc.common.xml.XmlValidator.startEntity String  name  )  throws SAXException
 

See also:
org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)

Definition at line 371 of file XmlValidator.java.

00372   {
00373   }

void org.objectweb.cjdbc.common.xml.XmlValidator.validate String  pathToDtd,
String  xml
 

Starts the verification of the xml document AND the dtd

Parameters:
pathToDtd path
xml content

Definition at line 181 of file XmlValidator.java.

References org.objectweb.cjdbc.common.xml.XmlValidator.pathToDtd.

Referenced by org.objectweb.cjdbc.common.xml.XmlValidator.XmlValidator().

00182   {
00183     System.setProperty("org.xml.sax.driver",
00184         "org.apache.crimson.parser.XMLReaderImpl");
00185     errors = new ArrayList();
00186     warnings = new ArrayList();
00187     try
00188     {
00189       // Store dtd reference
00190       this.pathToDtd = pathToDtd;
00191       // Instantiate a new parser
00192       parser = XMLReaderFactory.createXMLReader();
00193       // Activate validation
00194       parser.setFeature("http://xml.org/sax/features/validation", true);
00195       // Install error handler
00196       parser.setErrorHandler(this);
00197       // Install document handler
00198       parser.setContentHandler(this);
00199       parser.setProperty("http://xml.org/sax/properties/lexical-handler", this);
00200       // Install local entity resolver
00201       parser.setEntityResolver(this);
00202       InputSource input = new InputSource(new StringReader(xml));
00203       parser.parse(input);
00204     }
00205     catch (Exception e)
00206     {
00207       //throw new Exception("Xml document can not be validated.");
00208       //e.printStackTrace();
00209       addError(e);
00210       isXmlValid = false;
00211     }
00212   }

void org.objectweb.cjdbc.common.xml.XmlValidator.warning SAXParseException  exception  )  throws SAXException
 

See also:
org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)

Definition at line 271 of file XmlValidator.java.

00272   {
00273     warnings.add(exception);
00274   }


The documentation for this class was generated from the following file:
Generated on Mon Apr 11 22:02:14 2005 for C-JDBC by  doxygen 1.3.9.1