00001
00025
package org.objectweb.cjdbc.common.xml;
00026
00027
import java.io.BufferedReader;
00028
import java.io.File;
00029
import java.io.FileReader;
00030
import java.io.IOException;
00031
import java.io.InputStream;
00032
import java.io.StringReader;
00033
import java.util.ArrayList;
00034
00035
import org.objectweb.cjdbc.common.i18n.Translate;
00036
import org.objectweb.cjdbc.controller.core.ControllerConstants;
00037
import org.xml.sax.ErrorHandler;
00038
import org.xml.sax.InputSource;
00039
import org.xml.sax.SAXException;
00040
import org.xml.sax.SAXParseException;
00041
import org.xml.sax.XMLReader;
00042
import org.xml.sax.ext.LexicalHandler;
00043
import org.xml.sax.helpers.DefaultHandler;
00044
import org.xml.sax.helpers.XMLReaderFactory;
00045
00051 public class XmlValidator extends DefaultHandler
00052 implements
00053 ErrorHandler,
00054 LexicalHandler
00055 {
00056
00058 private XMLReader
parser;
00059 private String
pathToDtd;
00060 private boolean isXmlValid =
false;
00061 private boolean isDtdValid =
false;
00062 private String
xmlContent;
00063 private ArrayList
errors;
00064 private ArrayList
warnings;
00065
00072 public static void main(String args[])
throws Exception
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 =
ControllerConstants.C_JDBC_DTD_FILE;
00082
if(args.length == 2)
00083 dtdName = args[1];
00084
else
00085 System.out.println(
"Using default DTD:"+
ControllerConstants.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:" + dtd.getName());
00111
00112
00113
XmlValidator validator =
new XmlValidator(dtd.getAbsolutePath(),
new FileReader(xmlFile));
00114
00115
00116
if (!validator.
isDtdValid())
00117 System.out.println(
"[FAILED:Dtd is not valid]");
00118
else if (!validator.
isXmlValid())
00119 System.out.println(
"[FAILED:xml is not valid]");
00120
else if (validator.
isXmlValid())
00121 System.out.println(
"[OK]");
00122
00123
if (validator.
getLastException() != null)
00124 {
00125 ArrayList
errors = validator.
getExceptions();
00126
for (
int i = 0; i < errors.size(); i++)
00127 System.out.println(
"\t(parsing error):"
00128 + ((Exception) errors.get(i)).getMessage());
00129 }
00130 }
00131
00138 public XmlValidator(String pathToDtd, String xml)
00139 {
00140
validate(pathToDtd, xml);
00141 }
00142
00146 public XmlValidator(String pathToDtd, FileReader file)
throws IOException
00147 {
00148
00149 BufferedReader in =
new BufferedReader(file);
00150 StringBuffer xml =
new StringBuffer();
00151 String line;
00152
do
00153 {
00154 line = in.readLine();
00155
if (line != null)
00156 xml.append(line.trim());
00157 }
00158
while (line != null);
00159
xmlContent = xml.toString();
00160
validate(
pathToDtd,
xmlContent);
00161 }
00162
00168 public String
getXmlContent()
00169 {
00170
return xmlContent;
00171 }
00172
00179 public void validate(String pathToDtd, String xml)
00180 {
00181 System.setProperty(
"org.xml.sax.driver",
00182
"org.apache.crimson.parser.XMLReaderImpl");
00183
errors =
new ArrayList();
00184
warnings =
new ArrayList();
00185
try
00186 {
00187
00188
this.pathToDtd = pathToDtd;
00189
00190
parser = XMLReaderFactory.createXMLReader();
00191
00192
parser.setFeature(
"http://xml.org/sax/features/validation",
true);
00193
00194
parser.setErrorHandler(
this);
00195
00196
parser.setContentHandler(
this);
00197
parser.setProperty(
"http://xml.org/sax/properties/lexical-handler",
this);
00198
00199
parser.setEntityResolver(
this);
00200 InputSource input =
new InputSource(
new StringReader(xml));
00201
parser.parse(input);
00202 }
00203
catch (Exception e)
00204 {
00205
00206
00207
addError(e);
00208
isXmlValid =
false;
00209 }
00210 }
00211
00220 public InputSource
resolveEntity(String publicId, String systemId)
00221
throws SAXException
00222 {
00223
00224 File dtd =
new File(
pathToDtd);
00225
if (dtd.exists())
00226 {
00227
try
00228 {
00229 FileReader reader =
new FileReader(dtd);
00230
return new InputSource(reader);
00231 }
00232
catch (Exception e)
00233 {
00234 }
00235 }
00236
00237 InputStream stream =
XmlValidator.class
00238 .getResourceAsStream(
"/" +
pathToDtd);
00239
if (stream == null)
00240 {
00241 SAXException sax =
new SAXException(
Translate.get(
00242
"virtualdatabase.xml.dtd.not.found",
pathToDtd));
00243
addError(sax);
00244
throw sax;
00245 }
00246
00247
return new InputSource(stream);
00248 }
00249
00253 public void error(SAXParseException exception)
throws SAXException
00254 {
00255
addError(exception);
00256 }
00257
00261 public void fatalError(SAXParseException exception)
throws SAXException
00262 {
00263
addError(exception);
00264 }
00265
00269 public void warning(SAXParseException exception)
throws SAXException
00270 {
00271
warnings.add(exception);
00272 }
00273
00277 public void endDocument() throws SAXException
00278 {
00279
if (
errors.size() == 0)
00280
this.isXmlValid =
true;
00281 }
00282
00286 public boolean isValid()
00287 {
00288
return isXmlValid &&
isDtdValid;
00289 }
00290
00296 public Exception
getLastException()
00297 {
00298
if (
errors.size() == 0)
00299
return null;
00300
else
00301
return (Exception)
errors.get(
errors.size() - 1);
00302 }
00303
00309 public ArrayList
getExceptions()
00310 {
00311
return errors;
00312 }
00313
00317 public void comment(
char[] ch,
int start,
int length)
throws SAXException
00318 {
00319 }
00320
00324 public void endCDATA() throws SAXException
00325 {
00326 }
00327
00331 public void endDTD() throws SAXException
00332 {
00333
if (
errors.size() == 0)
00334 {
00335
isDtdValid =
true;
00336 }
00337
else
00338 {
00339
isDtdValid =
false;
00340 }
00341 }
00342
00346 public void endEntity(String name)
throws SAXException
00347 {
00348 }
00349
00353 public void startCDATA() throws SAXException
00354 {
00355 }
00356
00361 public void startDTD(String name, String publicId, String systemId)
00362
throws SAXException
00363 {
00364 }
00365
00369 public void startEntity(String name)
throws SAXException
00370 {
00371 }
00372
00376 public boolean isDtdValid()
00377 {
00378
return isDtdValid;
00379 }
00380
00384 public void setDtdValid(
boolean isDtdValid)
00385 {
00386
this.isDtdValid = isDtdValid;
00387 }
00388
00392 public boolean isXmlValid()
00393 {
00394
return isXmlValid;
00395 }
00396
00400 public void setXmlValid(
boolean isXmlValid)
00401 {
00402
this.isXmlValid = isXmlValid;
00403 }
00404
00405 private void addError(Exception e)
00406 {
00407
errors.add(e);
00408 }
00409
00413 public ArrayList
getWarnings()
00414 {
00415
return warnings;
00416 }
00417 }