/* Jaxe - Editeur XML en Java Copyright (C) 2002 Observatoire de Paris-Meudon Ce programme est un logiciel libre ; vous pouvez le redistribuer et/ou le modifier conformément aux dispositions de la Licence Publique Générale GNU, telle que publiée par la Free Software Foundation ; version 2 de la licence, ou encore (à votre choix) toute version ultérieure. Ce programme est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE ; sans même la garantie implicite de COMMERCIALISATION ou D'ADAPTATION A UN OBJET PARTICULIER. Pour plus de détail, voir la Licence Publique Générale GNU . Vous devez avoir reçu un exemplaire de la Licence Publique Générale GNU en même temps que ce programme ; si ce n'est pas le cas, écrivez à la Free Software Foundation Inc., 675 Mass Ave, Cambridge, MA 02139, Etats-Unis. */ package jaxe; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FileDialog; import java.awt.FlowLayout; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Properties; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.html.HTMLDocument; import javax.xml.transform.SourceLocator; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.xerces.parsers.DOMParser; import org.apache.xml.utils.SAXSourceLocator; import org.apache.xml.utils.WrappedRuntimeException; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import textbender.recombinant.common.lang.ClassLoaderX; /** * Fenêtre d'affichage du rendu HTML après transformation XSL */ public class HTMLFrame extends JFrame { JaxeDocument doc; HTMLDocument htmldoc; JTextPane textPane; JButton boutonNav; public HTMLFrame(JaxeDocument doc) { newdoc(doc); } public void newdoc(JaxeDocument doc) { this.doc = doc; if (doc.cfg == null) return; // ajouter un message d'erreur ? htmldoc = new HTMLDocument(); Rectangle fr = doc.jframe.getBounds(); setLocation(fr.x + fr.width/2, fr.y + fr.height/2); setSize(new Dimension(620, 460)); affichage(); // on ne recalcule pas le HTML à l'ouverture si le fichier HTML existe déjà if (doc.fsave != null) { File htmlFile = fichierHTML(doc.fsave); if (htmlFile.exists()) { try { URL url = htmlFile.toURL(); textPane.setPage(url); setTitle(htmlFile.getName()); } catch (Exception ex) { afficherErreur(ex); } setVisible(true); } else miseAJour(); } else { JOptionPane.showMessageDialog(doc.jframe, JaxeResourceBundle.getRB().getString("html.SauverAvant"), JaxeResourceBundle.getRB().getString("erreur.Erreur"), JOptionPane.ERROR_MESSAGE); } } protected void affichage() { textPane = new JTextPane(htmldoc); textPane.setEditable(false); JScrollPane paneScrollPane = new JScrollPane(textPane); paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); paneScrollPane.setPreferredSize(new Dimension(600, 400)); JPanel boutonsP = new JPanel(); boutonsP.setLayout(new FlowLayout()); JButton boutonMAJ = new JButton(JaxeResourceBundle.getRB().getString("html.MiseAJour")); boutonMAJ.addActionListener(new MAJListener()); boutonsP.add(boutonMAJ); boutonNav = new JButton(); boutonNav.setAction(new AbstractAction(JaxeResourceBundle.getRB().getString("html.Navigateur")) { public void actionPerformed(ActionEvent e) { lancerNavigateur(); } }); boutonsP.add(boutonNav); JPanel contentPane = new JPanel(new BorderLayout()); contentPane.add(paneScrollPane, BorderLayout.CENTER); contentPane.add(boutonsP, BorderLayout.NORTH); setContentPane(contentPane); } /** * nom du fichier HTML pour un fichier XML donné */ public static File fichierHTML(File fichierXML) { String nomFsave = fichierXML.getName(); int ie = nomFsave.lastIndexOf('.'); if (ie != -1) { if ("html".equals(nomFsave.substring(ie+1))) ; // pour terminer en .html.html else nomFsave = nomFsave.substring(0, ie); } return(new File(fichierXML.getParent() + File.separatorChar + nomFsave + ".html")); } public void miseAJour() { if (doc.fsave == null) { JOptionPane.showMessageDialog(doc.jframe, JaxeResourceBundle.getRB().getString("html.SauverAvant"), JaxeResourceBundle.getRB().getString("erreur.Erreur"), JOptionPane.ERROR_MESSAGE); return; } // File[] xslFileTab = doc.cfg.getXSLFiles() ; String[] xslFileTab = doc.cfg.getXSLFiles() ; if (xslFileTab == null || xslFileTab.length == 0) return; org.w3c.dom.Document XMLdoc = doc.DOMdoc; // File xslFile ; String xslFile ; File outFile = null; // les feuilles de styles sont utilisées les unes à la suite des autres // la dernière doit transformer du XML en HTML for (int i = 0 ; i < xslFileTab.length ; i++) { xslFile = xslFileTab[i] ; if (xslFile == null) { JOptionPane.showMessageDialog(doc.jframe, JaxeResourceBundle.getRB().getString("erreur.XSLNonTrouve"), JaxeResourceBundle.getRB().getString("erreur.Erreur"), JOptionPane.ERROR_MESSAGE); return; } try { TransformerFactory tFactory = TransformerFactory.newInstance() ; InputStream stream = getClass().getClassLoader().getResourceAsStream(xslFile); ClassLoaderX.checked(xslFile,stream); // Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile)) ; Transformer transformer = tFactory.newTransformer(new StreamSource(stream)) ; ArrayList parametres = doc.cfg.getXSLParam(xslFile) ; for (int j = 0 ; j < parametres.size() ; j++) { String[] parametre = (String[])parametres.get(j) ; if ("jaxe-fichier-xml".equals(parametre[0])) transformer.setParameter("jaxe-fichier-xml", doc.fsave.getAbsolutePath()); else if ("jaxe-fichier-xsl".equals(parametre[0])) // transformer.setParameter("jaxe-fichier-xsl", xslFile.getAbsolutePath()); transformer.setParameter("jaxe-fichier-xsl", xslFile); else transformer.setParameter(parametre[0],parametre[1]) ; } if (i == xslFileTab.length - 1) outFile = fichierHTML(doc.fsave); else { outFile = java.io.File.createTempFile("tmp",".xml") ; outFile.deleteOnExit() ; } DOMSource ds = new DOMSource(XMLdoc); if (doc.fsave != null) ds.setSystemId(doc.fsave.toURL().toExternalForm()); transformer.transform(ds, new StreamResult(new FileOutputStream(outFile))); } catch (Exception ex) { afficherErreur(ex); } if (i < xslFileTab.length - 1) { try { DOMParser parser = new DOMParser(); parser.parse(outFile.getPath()); XMLdoc = parser.getDocument(); } catch (Exception e) { e.printStackTrace(System.err); return; } } } try { affichage(); // pour refaire textPane, sinon update marche pas URL url = outFile.toURL(); textPane.setPage(url); setTitle(outFile.getName()); } catch (Exception ex) { afficherErreur(ex); } setVisible(true); } protected void afficherErreur(Exception ex) { String msg = ex.getMessage(); if (ex instanceof TransformerException) { SourceLocator loc = null; Throwable cause = ex; do { if (cause instanceof SAXParseException) loc = new SAXSourceLocator((SAXParseException)cause); else if (cause instanceof TransformerException) { SourceLocator loc2 = ((TransformerException)cause).getLocator(); if (loc2 != null) loc = loc2; } if (cause instanceof TransformerException) cause = ((TransformerException)cause).getCause(); else if (cause instanceof WrappedRuntimeException) cause = ((WrappedRuntimeException)cause).getException(); else if (cause instanceof SAXException) cause = ((SAXException)cause).getException(); else cause = null; } while (cause != null); if (loc != null) msg += " at line " + loc.getLineNumber(); } if (msg != null) { int ic = 0; for (int i=0; i 40 && msg.charAt(i)==' ') { ic = i; msg = msg.substring(0,i) + "\n" + msg.substring(i+1); } } JOptionPane.showMessageDialog(doc.jframe, ex.getClass().getName() + ": " + msg, JaxeResourceBundle.getRB().getString("erreur.Erreur"), JOptionPane.ERROR_MESSAGE); } class MAJListener implements ActionListener { public void actionPerformed(ActionEvent e) { miseAJour(); } } public void lancerNavigateur() { Properties prefs = Preferences.getPref(); String cheminNav = prefs.getProperty("navigateur"); if (cheminNav == null) { defNavigateur(); prefs = Preferences.getPref(); cheminNav = prefs.getProperty("navigateur"); if (cheminNav == null) return; } String[] acmd; File htmlFile = fichierHTML(doc.fsave); if (System.getProperty("os.name").startsWith("Mac")) { acmd = new String[4]; acmd[0] = "/usr/bin/open"; acmd[1] = "-a"; acmd[2] = cheminNav; acmd[3] = htmlFile.getAbsolutePath(); } else { acmd = new String[2]; acmd[0] = cheminNav; acmd[1] = htmlFile.getAbsolutePath(); } try { Runtime.getRuntime().exec(acmd); } catch (IOException ex) { System.err.println(ex.getClass().getName() + ": " + ex.getMessage()); } } public void defNavigateur() { FileDialog fdlg = new FileDialog(this, JaxeResourceBundle.getRB().getString("html.DefNavigateur"), FileDialog.LOAD); fdlg.show(); String chemin = null; String dir = fdlg.getDirectory(); if (dir != null && dir.endsWith(File.separator)) dir = dir.substring(0, dir.length()-1); String nom = fdlg.getFile(); if (dir == null) chemin = nom; else if (nom != null) chemin = dir + File.separator + nom; if (chemin != null) { Properties prefs = Preferences.getPref(); prefs.setProperty("navigateur", chemin); Preferences.enregistrerPref(prefs); } } }