//--------------------------------------------------------------------- //--------------------------------------------------------------------- /* --------------------- Utilisation de DOM: --------------------- */ //--------------------------------------------------------------------- //--------------------------------------------------------------------- // Java de base // import java.io.*; import java.util.*; // Traitement XML // import javax.xml.parsers.*; import org.w3c.dom.*; /* 1.5 import javax.xml.xpath.*; */ //--------------------------------------------------------------------- //--------------------------------------------------------------------- /** * Definition des 'points de sortie' ('exits') de l'application. */ interface DOMExits { // debut de classe //--------------------------------------------------------------------- public void nodeStart (Node n); public void nodeEnd (Node n); public void attrListStart (); public void attrListEnd (); public void attrTrt (Node n); public void innerListStart (); public void innerListEnd (); //--------------------------------------------------------------------- // fin de classe } //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- /** * 'Adaptateur' des exits de l'application.
* (Traitements par défaut) */ abstract class DOMExitsAdapter implements DOMExits { // debut de classe //--------------------------------------------------------------------- public void nodeStart (Node n) {} public void nodeEnd (Node n) {} public void attrListStart () {} public void attrListEnd () {} public void attrTrt (Node n) {} public void innerListStart () {} public void innerListEnd () {} /** * Fonction utilitaire d'impression: */ void a (String p) { System.out.println(p); } /** * Fonction utilitaire d'impression: */ void b (String p) { if (! DOM.debug) return; System.err.println(p); } /** * Fonction utilitaire d'impression: */ void c (String p) { if (! DOM.debug) return; System.err.print(p); } /** * Fonction utilitaire d'impression: */ void ta (int wi) { for (int i = 0; i < wi; i++) { System.out.print(" "); } } /** * Fonction utilitaire d'impression: */ void tb (int wi) { if (! DOM.debug) return; for (int i = 0; i < wi; i++) { System.out.print(" "); } } //--------------------------------------------------------------------- // fin de classe } //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- /** * Traitement effectif des exits de l'application. */ class DefaultTrueExits extends DOMExitsAdapter { // debut de classe //--------------------------------------------------------------------- // ---/--- //--------------------------------------------------------------------- // fin de classe } //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- /** * Lecture d'un document X.M.L. en DOM. */ public class DOM { // debut de classe //--------------------------------------------------------------------- public static DOM theAppli; public static boolean debug = false; public static void main (String[] args) throws Exception { a(" "); a("-----------------------------------"); a(" Lecture d'un document XML en DOM: "); a("-----------------------------------"); a("\nB.M.G. version Automne 2008"); a("\nLigne de commande:"); a("\tjava\tDOM\t[-debug]\t[-exits=...]"); for (int w = 0; w < args.length; w++) { a("\t"+w+"\t"+args[w]); if (args[w].startsWith("-debug")) { debug = true; } else if (args[w].startsWith("-exits=")) { Class c = Class.forName(args[w].substring(7)); theTrueExits = (DOMExits)c.newInstance(); } } a("\n"+(new Date()).toString()+" demarre..."); theAppli = new DOM(); theAppli.myGo(args); a("\n"+(new Date()).toString()+" ...termine !"); } /** * Constructeur sans paramètre. */ public DOM () throws Exception { super(); } /** * Fonction utilitaire d'impression: */ static void a (String p) { System.out.println(p); } /** * Fonction utilitaire d'impression: */ static void b (String p) { if (! debug) return; System.err.println(p); } /** * Fonction utilitaire d'impression: */ static void ta (int wi) { for (int i = 0; i < wi; i++) { System.out.print(" "); } } /** * Fonction utilitaire d'impression: */ static void tb (int wi) { if (! debug) return; for (int i = 0; i < wi; i++) { System.err.print(" "); } } /** * Usine à lecteur de documents. */ DocumentBuilderFactory theFactory; /** * Lecteur de documents. */ DocumentBuilder theParser; /** * Le document sous format d'arbre DOM. */ Document theDocument; /** * Les traitements appelés aux points d'exit. */ static DOMExits theTrueExits = new DefaultTrueExits(); /** * Fonction principale de l'application. */ public void myGo (String[] args) throws Exception { b("start of generation\n"); theFactory = DocumentBuilderFactory.newInstance(); theFactory.setIgnoringComments(true); theFactory.setIgnoringElementContentWhitespace(true); b("Caracteristiques du parser fabrique:"); b("isNamespaceAware: "+theFactory.isNamespaceAware()); b("isValidating: "+theFactory.isValidating()); b("isCoalescing: "+theFactory.isCoalescing()); b("isExpandEntityReferences: "+theFactory.isExpandEntityReferences()); b("isIgnoringComments: "+theFactory.isIgnoringComments()); b("isIgnoringElementContentWhitespace: "+theFactory.isIgnoringElementContentWhitespace()); theParser = theFactory.newDocumentBuilder(); theDocument = theParser.parse(System.in); b("\nAffichage sequentiel:"); b("---------------------\n"); affiche(0, theDocument); /* b("\nAffichage en acces direct:"); b("--------------------------\n"); NodeList inside = theDocument.getElementsByTagName("corps"); for (int wi = 0; wi < inside.getLength(); wi++) { affiche(0, inside.item(wi)); } */ /* 1.5 XPath: XPath theXPath = XPathFactory.newInstance().newXPath(); String theXPathExpr = "nodename"; Node theNodeThruXPath = (Node)theXPath.evaluate (theXPathExpr, theDocument, XPathConstants.NODE); 1.5 XPath */ b("end of generation\n"); } /** * Texte sans contenu significatif ? */ boolean jetable (String s) { if (s == null) return true; if (s.equals("")) return true; for (int i = 0; i < s.length(); i++) { String l = s.substring(i, i+1); if (" \t\n".indexOf(l) == -1) return false; } return true; } /** * Affichage de l'arborescence du document. */ public void affiche (int level, Node noeud) { // on elimine les textes intercalaires // if (noeud.getNodeName().equals("#text")) return; boolean bbb; // // appel d'exit // theTrueExits.nodeStart(noeud); tb(level); b("name: "+noeud.getNodeName()); tb(level); b("type: "+noeud.getNodeType()); bbb = jetable(noeud.getNodeValue()); if (! bbb) { tb(level); b("value: <"+noeud.getNodeValue()+">"); } // attributs (de la balise d'entree) NamedNodeMap nnm = noeud.getAttributes(); if (nnm != null) { // // appel d'exit // theTrueExits.attrListStart(); tb(level); b("nbAttr:"+nnm.getLength()); Node theAttr; for(int wj=0; wj < nnm.getLength(); wj++) { boolean bb2; theAttr = nnm.item(wj); // on elimine les textes intercalaires if (theAttr.getNodeName().equals("#text")) continue; bb2 = jetable(theAttr.getNodeValue()); tb(level); b("type: "+theAttr.getNodeType()+"\t" +"name: "+theAttr.getNodeName()+"\t"); tb(level+1); if (bb2) b("jetable"); else { b("value: "+theAttr.getNodeValue()); // // appel d'exit // theTrueExits.attrTrt(theAttr); } } // // appel d'exit // theTrueExits.attrListEnd(); } // noeuds subordonnés: appel récursif (en profondeur) // // appel d'exit // theTrueExits.innerListStart(); NodeList inside = noeud.getChildNodes(); for (int wi = 0; wi < inside.getLength(); wi++) { affiche(level+1, inside.item(wi)); } // // appel d'exit // theTrueExits.innerListEnd(); // // appel d'exit // theTrueExits.nodeEnd(noeud); } //--------------------------------------------------------------------- // fin de classe } //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- //---------------------------------------------------------------------