Transformation XML --> HTML

Programme

// Analyse5.java

import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class Analyse5 extends DefaultHandler {
  static private FileWriter dest;
  static private int premiereReplique = 0 ;
  public static void main (String argv[])
  throws IOException {
    if (argv.length != 2) {
      System.err.println
         ("Usage: Analyse5 fichierSource fichierCible");
      System.exit (1);
    }
    File fichierDest = new File(argv[1]);
    dest = new FileWriter(fichierDest);
    DefaultHandler handler = new Analyse5();
    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 void error(SAXParseException e)
  throws SAXParseException
  {
    throw e;
  }

  public void startDocument ()
   throws SAXException {
     ecrit("<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">"); nl();
     ecrit("<html>"); nl();
     ecrit("<head></head>"); nl();
     ecrit("<body>"); nl();
   }
  public void endDocument ()
   throws SAXException {
     ecrit("</dl>"); nl();
     ecrit("</body>"); nl();
     ecrit("</html>"); nl();
     try {
       dest.flush ();
     } catch (IOException e) {
       throw new SAXException ("I/O error", e);
     }
   }
  public void startElement (String namespaceURI,
                             String simpleName,
                             String qualifiedName,
                             Attributes attrs)
    throws SAXException {
    String  name = simpleName;
    if (name.equals(""))
      name = qualifiedName;
    if ( name.equals("situation") ) {
      ecrit("<h5>"+name+"</h5>"); nl();
      ecrit("<p><i>");
    } else if ( name.equals("replique") ) {
      premiereReplique ++;
      if ( premiereReplique == 1) {
        ecrit("<dl>"); nl();
      }
    } else if ( name.equals("personnage") ) {
      ecrit("<dt>");
    } else if ( name.equals("texte") ) {
      ecrit("<dd>");
    }
  }
  public void endElement (String namespaceURI,
                           String simpleName,
                           String qualifiedName)
  throws SAXException {
    String name = simpleName;
    if (name.equals(""))
      name = qualifiedName;
    if (name.equals("situation") ) {
      ecrit("</i></p>"); nl();
    }
  }
  public void characters (char buf [], int offset, int len)
  throws SAXException {
    String s = new String(buf, offset, len);
    ecrit (s);
    nl();
  }
  private void nl()
  throws SAXException {
    String lineEnd =  System.getProperty("line.separator");
    try {
      dest.write (lineEnd);
    } catch (IOException e) {
      throw new SAXException ("I/O error", e);
    }
  }
  private void ecrit(String s)
  throws SAXException {
    try {
      dest.write (s);
      dest.flush ();
    } catch (IOException e) {
      throw new SAXException ("I/O error", e);
    }
  }
}
 
 

commandes et résultats

$ javac Analyse5.java
$ java Analyse5 dialogue6.xml dialogue6.html
$

exercice :

Changer les traitements pour obtenir :
madame pernelle pressée, marchant vite, sur un ton fort
Allons, Flipote, allons, que d'eux je me délivre.
elmire ,sur un ton normal
Vous marchez d'un tel pas qu'on a peine à vous suivre.

 index  suivant