/* 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.FlowLayout; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.net.URI; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.Document; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import textbender.recombinant.common.xml.sax.ext.EntityResolverCP; /** * Fenêtre de validation du fichier avec le schéma */ public class ValidationFrame extends JFrame implements MouseListener, ActionListener { JaxeDocument doc; JaxeFrame jframe; JEditorPane editorPane; String contenu; ArrayList erreurs; public ValidationFrame(JaxeDocument doc, JaxeFrame jframe) { super(JaxeResourceBundle.getRB().getString("validation.Validation")); this.jframe = jframe; newdoc(doc); } public void newdoc(JaxeDocument doc) { this.doc = doc; if (doc.cfg == null) { JOptionPane.showMessageDialog(jframe, JaxeResourceBundle.getRB().getString("validation.Schema"), JaxeResourceBundle.getRB().getString("erreur.Erreur"), JOptionPane.ERROR_MESSAGE); return; } Rectangle fr = jframe.getBounds(); setLocation(fr.x + fr.width/2, fr.y + fr.height/2); setSize(new Dimension(620, 460)); affichage(); miseAJour(); } protected void affichage() { editorPane = new JEditorPane(); editorPane.setEditable(false); JScrollPane paneScrollPane = new JScrollPane(editorPane); 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("validation.MiseAJour")); boutonMAJ.addActionListener(this); boutonsP.add(boutonMAJ); JPanel contentPane = new JPanel(new BorderLayout()); contentPane.add(paneScrollPane, BorderLayout.CENTER); contentPane.add(boutonsP, BorderLayout.NORTH); setContentPane(contentPane); editorPane.addMouseListener(this); } protected void print(String s) { contenu += s; } protected void println(String s) { contenu += s + "\n"; } public void miseAJour() { if (doc.fsave == null) { JOptionPane.showMessageDialog(jframe, JaxeResourceBundle.getRB().getString("validation.SauverAvant"), JaxeResourceBundle.getRB().getString("erreur.Erreur"), JOptionPane.ERROR_MESSAGE); return; } String nomFichier = doc.fsave.getPath(); // if (doc.cfg.schemaFile == null) // // la validation XML n'est pas possible sans schéma XML // // ce n'est pas utile si on utilise la syntaxe simplifiée // return; // String nomSchema = doc.cfg.schemaFile.getAbsolutePath(); String nomSchema = doc.cfg.nomSchema(); contenu = ""; erreurs = new ArrayList(); boolean[] terreur = new boolean[1]; try { DOMParser parser = new DOMParser(); terreur[0] = false; ErrorStorer ef = new ErrorStorer(terreur); parser.setErrorHandler(ef); parser.setFeature("http://apache.org/xml/features/allow-java-encodings", true); parser.setFeature("http://xml.org/sax/features/validation", true); //parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true); // nomSchema = (new File(nomSchema)).toURL().toString(); // to fix problem with xerces and é // nomSchema = (new URI(nomSchema)).toASCIIString(); // to fix problem with xerces and é // while (nomSchema.indexOf(' ') != -1) { // int is = nomSchema.indexOf(' '); // nomSchema = nomSchema.substring(0, is) + "%20" + nomSchema.substring(is + 1); // } //// URI (above) should take care of that // if (doc.cfg.targetNamespace == null || "".equals(doc.cfg.targetNamespace)) { // parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", // "anyURI " + nomSchema); // parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", // nomSchema); // } else { // parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", // doc.cfg.targetNamespace + " " + nomSchema); // } ///// but that no longer works well. At least for the XHTML docs that I'm testing, with DTDs, this alone is enough: parser.setEntityResolver(new EntityResolverCP(new String[] {"."})); // DTDs etc. are on classpath,. But it does not pick up schemas there, perhaps because they are not declared in the document? nomFichier = (new File(nomFichier)).toURL().toString(); // to fix problem with xerces and é parser.parse(nomFichier); Document domdoc = parser.getDocument(); } catch (Exception e) { e.printStackTrace(System.err); return; } if (!terreur[0]) print(JaxeResourceBundle.getRB().getString("validation.parfait")); editorPane.setText(contenu); setVisible(true); } class ErrorStorer implements ErrorHandler { boolean terreur[]; public ErrorStorer(boolean terreur[]) { this.terreur = terreur; } public void warning(SAXParseException ex) { handleError(ex, "Warning"); } public void error(SAXParseException ex) { handleError(ex, "Error"); } public void fatalError(SAXParseException ex) throws SAXException { handleError(ex, "Fatal error"); } private void handleError(SAXParseException ex, String terr) { erreurs.add(new Erreur(ex.getLineNumber(), contenu.length())); terreur[0] = true; println(terr + " at line number " + ex.getLineNumber() + ": "); println(ex.getMessage() + "\n"); } } public void actionPerformed(ActionEvent e) { miseAJour(); } public void mouseClicked(MouseEvent e) { int pos = editorPane.viewToModel(e.getPoint()); int p1 = 0; int p2 = 0; int ligne = 0; for (int i=0; i pos) { p2 = err.p1; break; } else { p1 = err.p1; ligne = err.ligne; } } if (p2 == 0) p2 = contenu.length(); editorPane.setCaretPosition(p1); editorPane.moveCaretPosition(p2); if (jframe.getSourceFrame() == null) jframe.setSourceFrame(new SourceFrame(doc, jframe)); else jframe.getSourceFrame().miseAJour(); jframe.getSourceFrame().selectLigne(ligne); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } class Erreur { public int ligne; public int p1; public Erreur(int ligne, int p1) { this.ligne = ligne; this.p1 = p1; } } }