// SAXParserDemo.java
//
// Parse avec l'Api SAX un document XML
// Transformation XML --> HTML
// +traitement des attributs
// Document pour l'ENSICAEN
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
public class SAXParserDemo extends DefaultHandler {
private StringBuffer str;
private String anneeDisque = null;
public static void main (String argv[])
throws IOException {
if (argv.length != 1) {
System.err.println
("Usage: SAXParserDemo fichierSource > fichierCible");
System.exit (1);
}
DefaultHandler handler = new SAXParserDemo();
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(argv[0]), handler );
} catch (Throwable t) {
t.printStackTrace ();
System.exit (2);
}
System.exit (0);
}
public SAXParserDemo() {
this.str = new StringBuffer();
}
public void error(SAXParseException e)
throws SAXParseException {
throw e;
}
public void startDocument ()
throws SAXException {
this.cat("\n");
this.cat("\n");
this.cat("\n");
this.cat("
\n");
this.cat("Conversions XML -> XHTML\n");
this.cat("\n");
this.cat("\n");
this.cat("\n");
}
public void endDocument ()
throws SAXException {
this.cat("");
this.cat("");
System.out.println(this.str.toString());
}
public void startElement (String namespaceURI,
String simpleName,
String qualifiedName,
Attributes attrs)
throws SAXException {
String name = null;
// System.err.println("namespaceURI: " + namespaceURI + ", simpleName: " + ", qualifiedName: " + qualifiedName);
if (namespaceURI.length() == 0) {
name = qualifiedName;
} else {
name = simpleName;
}
if ("groupes".equals(name)) {
this.cat("| Photo | Nom |
\n");
}
if ("groupe".equals(name)) {
String img = "img_disco/" + attrs.getValue("img");
String url = attrs.getValue("url");
this.cat(";) | ");
}
if ("musiciens".equals(name)) {
this.cat("| Nom | Instruments | Biographie | ");
}
if ("musicien".equals(name)) {
this.cat("| ");
}
if ("instruments".equals(name)) {
this.cat(" | ");
}
if ("bio".equals(name)) {
String att = null;
this.cat(" | ");
att = attrs.getValue("naissance");
if (! "-".equals(att)) {
this.cat(att);
}
att = attrs.getValue("mort");
if (! "-".equals(att)) {
this.cat(" - ");
this.cat(att);
}
this.cat(" | ");
}
if ("disques".equals(name)) {
this.cat("| Photo | Titre, année | ");
}
if ("disque".equals(name)) {
String img = "img_disco/" + attrs.getValue("img");
this.anneeDisque = attrs.getValue("annee");
this.cat(";) | ");
}
}
public void endElement (String namespaceURI,
String simpleName,
String qualifiedName)
throws SAXException {
String name = "";
if (namespaceURI.length() == 0) {
name = qualifiedName;
} else {
name = simpleName;
}
if ("groupes".equals(name)) {
this.cat(" | ");
}
if ("groupe".equals(name)) {
this.cat(" ");
}
if ("musiciens".equals(name)) {
this.cat(" ");
}
if ("musicien".equals(name)) {
this.cat(" |
");
}
if ("instruments".equals(name)) {
this.cat("");
}
if ("disques".equals(name)) {
this.cat("
");
}
if ("disque".equals(name)) {
this.cat(", ");
this.cat(this.anneeDisque);
this.cat("");
}
}
public void characters (char buf [], int offset, int len)
throws SAXException {
// String(byte[] bytes, int offset, int length)
this.cat(new String(buf, offset, len));
}
private void cat(String str) {
this.str.append(str);
}
}