Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

XmlWizard.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.console.wizard;
00026 
00027 import java.awt.BorderLayout;
00028 import java.awt.Font;
00029 import java.awt.event.ActionEvent;
00030 import java.awt.event.ActionListener;
00031 import java.io.File;
00032 import java.io.FileReader;
00033 import java.io.FileWriter;
00034 import java.io.IOException;
00035 import java.util.ArrayList;
00036 
00037 import javax.swing.JFileChooser;
00038 import javax.swing.JFrame;
00039 import javax.swing.JMenu;
00040 import javax.swing.JMenuBar;
00041 import javax.swing.JMenuItem;
00042 import javax.swing.JPanel;
00043 import javax.swing.UIManager;
00044 
00045 import org.dom4j.Document;
00046 import org.dom4j.DocumentException;
00047 import org.dom4j.io.OutputFormat;
00048 import org.dom4j.io.SAXReader;
00049 import org.dom4j.io.XMLWriter;
00050 import org.objectweb.cjdbc.common.i18n.Translate;
00051 import org.objectweb.cjdbc.common.i18n.WizardTranslate;
00052 import org.objectweb.cjdbc.common.util.Constants;
00053 import org.objectweb.cjdbc.common.xml.XmlValidator;
00054 import org.objectweb.cjdbc.console.gui.constants.GuiConstants;
00055 
00056 /**
00057  * This is the main class for the XmlWizard.
00058  * 
00059  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00060  * @version 1.0
00061  */
00062 public class XmlWizard extends JFrame implements ActionListener
00063 {
00064 
00065   private Document     document;
00066   private JFileChooser chooser;
00067   private WizardTabs   wizardTabs;
00068 
00069   /**
00070    * Creates a new <code>XmlWizard</code> object. The wizard will edit an
00071    * already existing file
00072    * 
00073    * @param xmlFile XML file name
00074    * @throws IOException if the file cannot be read
00075    * @throws DocumentException if the xml document cannot be parsed
00076    */
00077   public XmlWizard(String xmlFile) throws DocumentException, IOException
00078   {
00079     this();
00080     loadDocument(xmlFile);
00081   }
00082 
00083   /**
00084    * Creates a new <code>XmlWizard</code> object The wizard will create a new
00085    * document from scratch
00086    */
00087   public XmlWizard()
00088   {
00089     // basic define
00090     super(WizardTranslate.get("init.title.main.frame"));
00091     chooser = new JFileChooser()
00092     {
00093       /**
00094        * @see javax.swing.JFileChooser#accept(java.io.File)
00095        */
00096       public boolean accept(File f)
00097       {
00098         if (f.isDirectory())
00099           return true;
00100         if (f.getName().endsWith(".xml"))
00101           return true;
00102         else
00103           return false;
00104       }
00105     };
00106 
00107     // set some fonts
00108 
00109     UIManager.getDefaults().put("Label.font",
00110         new Font("Helvetica", Font.BOLD, 10));
00111     UIManager.getDefaults().put("ComboBox.font",
00112         new Font("Helvetica", Font.BOLD, 10));
00113     UIManager.getDefaults().put("Border.font",
00114         new Font("Helvetica", Font.BOLD, 10));
00115     UIManager.getDefaults().put("Menu.font",
00116         new Font("Helvetica", Font.BOLD, 10));
00117     UIManager.getDefaults().put("Button.font",
00118         new Font("Helvetica", Font.BOLD, 10));
00119     UIManager.getDefaults().put("MenuItem.font",
00120         new Font("Helvetica", Font.BOLD, 10));
00121 
00122     this.setSize(WizardConstants.FRAME_WIDTH, WizardConstants.FRAME_HEIGHT);
00123     this.getContentPane().setLayout(new BorderLayout());
00124     GuiConstants.centerComponent(this, WizardConstants.FRAME_WIDTH,
00125         WizardConstants.FRAME_HEIGHT);
00126 
00127     // define the menu
00128     JMenuBar menuBar = new JMenuBar();
00129     JMenu menu = new JMenu(WizardTranslate.get("init.menu.1"));
00130     // quit
00131     JMenuItem item1 = new JMenuItem(WizardTranslate
00132         .get(WizardConstants.COMMAND_QUIT));
00133     item1.setActionCommand(WizardConstants.COMMAND_QUIT);
00134     // import
00135     JMenuItem item2 = new JMenuItem(WizardTranslate
00136         .get(WizardConstants.COMMAND_EXPORT_XML));
00137     item2.setActionCommand(WizardConstants.COMMAND_EXPORT_XML);
00138     // export
00139     JMenuItem item3 = new JMenuItem(WizardTranslate
00140         .get(WizardConstants.COMMAND_IMPORT_XML));
00141     item3.setActionCommand(WizardConstants.COMMAND_IMPORT_XML);
00142     // check
00143     JMenuItem item4 = new JMenuItem(WizardTranslate
00144         .get(WizardConstants.COMMAND_CHECK_WIZARD));
00145     item4.setActionCommand(WizardConstants.COMMAND_CHECK_WIZARD);
00146     //  validate
00147     JMenuItem item5 = new JMenuItem(WizardTranslate
00148         .get(WizardConstants.COMMAND_VALIDATE_XML));
00149     item5.setActionCommand(WizardConstants.COMMAND_VALIDATE_XML);
00150     menu.add(item1).addActionListener(this);
00151     menu.add(item2).addActionListener(this);
00152     menu.add(item3).addActionListener(this);
00153     menu.add(item4).addActionListener(this);
00154     menu.add(item5).addActionListener(this);
00155     menu.setVisible(true);
00156     menuBar.add(menu);
00157 
00158     // define the panel that contains the menu
00159     JPanel menuPane = new JPanel();
00160     menuPane.add(menuBar);
00161     this.getContentPane().add(menuPane, BorderLayout.NORTH);
00162 
00163     startWizardTabs();
00164 
00165     // Finish creating the frame
00166     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00167     this.validate();
00168     //this.pack();
00169     this.setVisible(true);
00170   }
00171 
00172   private void startWizardTabs()
00173   {
00174     if (wizardTabs != null)
00175       this.getContentPane().remove(wizardTabs);
00176     wizardTabs = new WizardTabs();
00177     this.getContentPane().add(wizardTabs, BorderLayout.CENTER);
00178   }
00179 
00180   /**
00181    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
00182    */
00183   public void actionPerformed(ActionEvent e)
00184   {
00185     String command = e.getActionCommand();
00186     if (command.equals(WizardConstants.COMMAND_QUIT))
00187       System.exit(1);
00188     else if (command.equals(WizardConstants.COMMAND_EXPORT_XML))
00189       try
00190       {
00191         chooser.showSaveDialog(this);
00192         File document = chooser.getSelectedFile();
00193         if (document != null)
00194           exportDocument(document.getAbsolutePath());
00195       }
00196       catch (IOException e1)
00197       {
00198         e1.printStackTrace();
00199       }
00200     else if (command.equals(WizardConstants.COMMAND_IMPORT_XML))
00201     {
00202       try
00203       {
00204         chooser.showOpenDialog(this);
00205         File document = chooser.getSelectedFile();
00206         if (document != null)
00207           loadDocument(document.getAbsolutePath());
00208       }
00209       catch (Exception e2)
00210       {
00211         e2.printStackTrace();
00212       }
00213     }
00214     else if (command.equals(WizardConstants.COMMAND_CHECK_WIZARD))
00215     {
00216       chooser.showOpenDialog(this);
00217       File document = chooser.getSelectedFile();
00218       if (document != null)
00219       {
00220         try
00221         {
00222           loadDocument(document.getAbsolutePath());
00223           String newDoc = document.getParentFile().getAbsolutePath()
00224               + File.separator + document.getName() + ".check";
00225           exportDocument(newDoc);
00226           validateDocument(newDoc);
00227         }
00228         catch (Exception e2)
00229         {
00230           e2.printStackTrace();
00231         }
00232       }
00233     }
00234     else if (command.equals(WizardConstants.COMMAND_VALIDATE_XML))
00235     {
00236       chooser.showOpenDialog(this);
00237       File document = chooser.getSelectedFile();
00238       if (document != null)
00239         try
00240         {
00241           validateDocument(document.getAbsolutePath());
00242         }
00243         catch (IOException e2)
00244         {
00245           e2.printStackTrace();
00246         }
00247     }
00248   }
00249 
00250   /**
00251    * Load document from a xml file
00252    * 
00253    * @param xmlFile the path to the xml document
00254    * @throws DocumentException if fails to parse
00255    * @throws IOException if fails to read
00256    */
00257   public void loadDocument(String xmlFile) throws DocumentException,
00258       IOException
00259   {
00260     SAXReader reader = new SAXReader();
00261     this.document = reader.read(new FileReader(xmlFile));
00262     if (document != null)
00263     {
00264       startWizardTabs();
00265       wizardTabs.importDocumentFromXml(document);
00266     }
00267   }
00268 
00269   /**
00270    * Save the document to an xml file
00271    * 
00272    * @param xmlFile the path to the xml file to save to
00273    * @throws IOException if fails to write
00274    */
00275   public void saveDocument(String xmlFile) throws IOException
00276   {
00277     OutputFormat format = OutputFormat.createPrettyPrint();
00278     XMLWriter writer = new XMLWriter(new FileWriter(xmlFile), format);
00279     writer.write(document);
00280     writer.close();
00281   }
00282 
00283   /**
00284    * Validate the given XML document contained in the given file.
00285    * 
00286    * @param xmlFile XML file name
00287    * @throws IOException if an error occurs
00288    */
00289   public void validateDocument(String xmlFile) throws IOException
00290   {
00291     XmlValidator validator = new XmlValidator(Constants.C_JDBC_DTD_FILE,
00292         new FileReader(new File(xmlFile)));
00293     XmlValidatorFrame frame = new XmlValidatorFrame(xmlFile);
00294 
00295     if (validator.isDtdValid())
00296       frame.writeLine(Translate.get("virtualdatabase.xml.dtd.validated"));
00297     if (validator.isXmlValid())
00298       frame.writeLine(Translate.get("virtualdatabase.xml.document.validated"));
00299 
00300     if (validator.getWarnings().size() > 0)
00301     {
00302       frame.setWarning();
00303       ArrayList warnings = validator.getWarnings();
00304       for (int i = 0; i < warnings.size(); i++)
00305         frame.writeLine(Translate.get("virtualdatabase.xml.parsing.warning",
00306             warnings.get(i)));
00307     }
00308 
00309     if (!validator.isDtdValid())
00310     {
00311       frame.setWarning();
00312       frame.writeLine(Translate.get("virtualdatabase.xml.dtd.not.validated"));
00313     }
00314     if (!validator.isXmlValid())
00315     {
00316       frame.setWarning();
00317       frame.writeLine(Translate
00318           .get("virtualdatabase.xml.document.not.validated"));
00319     }
00320 
00321     ArrayList errors = validator.getExceptions();
00322     if (errors.size() > 0)
00323       frame.setWarning();
00324     for (int i = 0; i < errors.size(); i++)
00325       frame.writeLine(((Exception) errors.get(i)).getMessage());
00326 
00327   }
00328 
00329   /**
00330    * Save the document to an xml file
00331    * 
00332    * @param xmlFile the path to the xml file to save to
00333    * @throws IOException if fails to write
00334    */
00335   public void exportDocument(String xmlFile) throws IOException
00336   {
00337     this.document = wizardTabs.exportDocumentToXml();
00338     if (document != null)
00339       saveDocument(xmlFile);
00340   }
00341 
00342   /**
00343    * Start the XmlWizard
00344    * 
00345    * @param args Command line arguments
00346    */
00347   public static void main(String[] args)
00348   {
00349 
00350     if (args == null || args.length == 0)
00351       new XmlWizard();
00352     else
00353     {
00354       try
00355       {
00356         new XmlWizard(args[0]);
00357       }
00358       catch (DocumentException e)
00359       {
00360         System.out.println(WizardTranslate.get("init.error.parse.failed"));
00361         new XmlWizard();
00362       }
00363       catch (IOException e)
00364       {
00365         System.out.println(WizardTranslate.get("init.error.read.failed"));
00366         new XmlWizard();
00367       }
00368     }
00369   }
00370 }

Generated on Mon Apr 11 22:01:36 2005 for C-JDBC by  doxygen 1.3.9.1