00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00058
00059
00060
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
00071
00072
00073
00074
00075
00076
00077 public XmlWizard(String xmlFile) throws DocumentException, IOException
00078 {
00079 this();
00080 loadDocument(xmlFile);
00081 }
00082
00083
00084
00085
00086
00087 public XmlWizard()
00088 {
00089
00090 super(WizardTranslate.get("init.title.main.frame"));
00091 chooser = new JFileChooser()
00092 {
00093
00094
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
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
00128 JMenuBar menuBar = new JMenuBar();
00129 JMenu menu = new JMenu(WizardTranslate.get("init.menu.1"));
00130
00131 JMenuItem item1 = new JMenuItem(WizardTranslate
00132 .get(WizardConstants.COMMAND_QUIT));
00133 item1.setActionCommand(WizardConstants.COMMAND_QUIT);
00134
00135 JMenuItem item2 = new JMenuItem(WizardTranslate
00136 .get(WizardConstants.COMMAND_EXPORT_XML));
00137 item2.setActionCommand(WizardConstants.COMMAND_EXPORT_XML);
00138
00139 JMenuItem item3 = new JMenuItem(WizardTranslate
00140 .get(WizardConstants.COMMAND_IMPORT_XML));
00141 item3.setActionCommand(WizardConstants.COMMAND_IMPORT_XML);
00142
00143 JMenuItem item4 = new JMenuItem(WizardTranslate
00144 .get(WizardConstants.COMMAND_CHECK_WIZARD));
00145 item4.setActionCommand(WizardConstants.COMMAND_CHECK_WIZARD);
00146
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
00159 JPanel menuPane = new JPanel();
00160 menuPane.add(menuBar);
00161 this.getContentPane().add(menuPane, BorderLayout.NORTH);
00162
00163 startWizardTabs();
00164
00165
00166 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00167 this.validate();
00168
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
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
00252
00253
00254
00255
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
00271
00272
00273
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
00285
00286
00287
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
00331
00332
00333
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
00344
00345
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 }