La classe ConsommateurSAX proposée
à droite se veut un exemple de classe Java
capable de consommer des documents selon une approche
SAX.
|
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import org.xml.sax.*;
import java.io.*;
public class ConsommateurSAX extends DefaultHandler {
public static void main(String [] args) {
if (args.length == 0) {
System.err.println
("Usage: java AfficheurDOM fichierXML...");
} else {
for (String s : args) {
ConsommateurSAX cs = new ConsommateurSAX();
cs.consommer (s);
}
}
}
private static final String ENCODAGE = "ISO-8859-1";
private static final String ENTÊTE_XML = "<?xml " +
"version='1.0' " +
"encoding='" +
ENCODAGE +
"'?>";
private int niveau;
private Writer écrivain;
public ConsommateurSAX() {
niveau = 0;
try {
écrivain = new OutputStreamWriter(System.out, ENCODAGE);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private String générerIndentation() {
final int NB_ESPACES_NIVEAU = 3;
String ind = "";
for (int i = 0; i < niveau * NB_ESPACES_NIVEAU; ++i) {
ind = ind + " ";
}
return ind;
}
private void émettre(String s) throws SAXException {
try {
écrivain.write(s);
} catch (IOException e) {
throw new SAXException("Erreur d'entrée/ sortie: ", e);
}
}
private void nouvelleLigne() throws SAXException {
émettre(System.getProperty("line.separator"));
}
public void consommer(String nomFich) {
try {
écrivain.émettre("Décodage du fichier " + nomFich + "\n");
écrivain.flush();
SAXParserFactory fabrique = SAXParserFactory.newInstance();
SAXParser analysteSAX = fabrique .newSAXParser();
analysteSAX.parse(new File(nomFich), this);
} catch (SAXException sxe) {
Exception ex = (sxe.getException() != null)?
sxe.getException() : sxe;
ex.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void startDocument() throws SAXException {
émettre(ENTêTE_XML);
nouvelleLigne();
}
public void endDocument() throws SAXException {
try {
nouvelleLigne();
écrivain.flush();
} catch (IOException e) {
throw new SAXException("Erreur d'entrée/ sortie: ", e);
}
}
public void startElement(
String namespaceURI, String nomSimple,
String nomQualifié, Attributes attrs) throws SAXException {
émettre(générerIndentation());
++niveau;
String nomélément = nomSimple;
if ("".equals(nomélément)) { // pas de namespace
nomélément = nomQualifié;
}
émettre ("<" + nomélément);
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
String nomAttribut = attrs.getLocalName(i);
if ("".equals(nomAttribut)) {
nomAttribut = attrs.getQName(i);
}
émettre(" " + nomAttribut + "=\"" +
attrs.getValue (i)+ "\"");
}
}
émettre(">");
nouvelleLigne();
}
public void endElement(
String namespaceURI, String nomSimple,
String nomQualifié) throws SAXException {
--niveau;
émettre(générerIndentation() + "</" + nomQualifié + ">");
nouvelleLigne();
}
public void characters
(char buf[], int offset, int len) throws SAXException {
String s = new String(buf, offset, len).trim();
if (! "".equals(s)) {
émettre(générerIndentation() + s);
nouvelleLigne();
}
}
}
|