1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
public class Validateur extends DefaultHandler {
public Validateur() {
}
public void warning(SAXParseException ex)
throws SAXException {
printError("Avertissement", ex);
}
public void error(SAXParseException ex)
throws SAXException {
printError("Erreur", ex);
}
public void fatalError(SAXParseException ex)
throws SAXException {
printError("Erreur fatale", ex);
}
protected void printError(String type, SAXParseException ex) {
System.err.print("[");
System.err.print(type);
System.err.print("] ");
if (ex== null)
{
System.out.println("!!!");
}
String systemId = ex.getSystemId();
if (systemId != null)
{
int index = systemId.lastIndexOf('/');
if (index != -1)
systemId = systemId.substring(index + 1);
System.err.print(systemId);
}
System.err.print(" ligne: ");
System.err.println(ex.getLineNumber());
System.err.println(", colonne: ");
System.err.print(ex.getColumnNumber());
System.err.print(", description: ");
System.err.print(ex.getMessage());
System.err.println();
System.err.flush();
System.out.println("fichier non conforme");
}
public static void main(String argv[]) throws org.xml.sax.SAXException,javax.xml.parsers.ParserConfigurationException {
Validateur counter = new Validateur();
PrintWriter out = new PrintWriter(System.out);
XMLReader parser = null;
parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
parser.setContentHandler(counter);
parser.setErrorHandler(counter);
parser.setFeature("http://xml.org/sax/features/validation",true);
parser.setFeature("http://xml.org/sax/features/namespaces",true);
try {
parser.parse(argv[0]);
} catch (SAXParseException e) {
} catch (Exception e) {
System.err.println("erreur: Erreur de traitement -"
+e.getMessage());
}
}
} |
Partager