KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > wizard > XmlWizard


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.wizard;
26
27 import java.awt.BorderLayout JavaDoc;
28 import java.awt.Font JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileReader JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 import javax.swing.JFileChooser JavaDoc;
38 import javax.swing.JFrame JavaDoc;
39 import javax.swing.JMenu JavaDoc;
40 import javax.swing.JMenuBar JavaDoc;
41 import javax.swing.JMenuItem JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44
45 import org.dom4j.Document;
46 import org.dom4j.DocumentException;
47 import org.dom4j.io.OutputFormat;
48 import org.dom4j.io.SAXReader;
49 import org.dom4j.io.XMLWriter;
50 import org.objectweb.cjdbc.common.i18n.Translate;
51 import org.objectweb.cjdbc.common.i18n.WizardTranslate;
52 import org.objectweb.cjdbc.common.util.Constants;
53 import org.objectweb.cjdbc.common.xml.XmlValidator;
54 import org.objectweb.cjdbc.console.gui.constants.GuiConstants;
55
56 /**
57  * This is the main class for the XmlWizard.
58  *
59  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
60  * @version 1.0
61  */

62 public class XmlWizard extends JFrame JavaDoc implements ActionListener JavaDoc
63 {
64
65   private Document document;
66   private JFileChooser JavaDoc chooser;
67   private WizardTabs wizardTabs;
68
69   /**
70    * Creates a new <code>XmlWizard</code> object. The wizard will edit an
71    * already existing file
72    *
73    * @param xmlFile XML file name
74    * @throws IOException if the file cannot be read
75    * @throws DocumentException if the xml document cannot be parsed
76    */

77   public XmlWizard(String JavaDoc xmlFile) throws DocumentException, IOException JavaDoc
78   {
79     this();
80     loadDocument(xmlFile);
81   }
82
83   /**
84    * Creates a new <code>XmlWizard</code> object The wizard will create a new
85    * document from scratch
86    */

87   public XmlWizard()
88   {
89     // basic define
90
super(WizardTranslate.get("init.title.main.frame"));
91     chooser = new JFileChooser JavaDoc()
92     {
93       /**
94        * @see javax.swing.JFileChooser#accept(java.io.File)
95        */

96       public boolean accept(File JavaDoc f)
97       {
98         if (f.isDirectory())
99           return true;
100         if (f.getName().endsWith(".xml"))
101           return true;
102         else
103           return false;
104       }
105     };
106
107     // set some fonts
108

109     UIManager.getDefaults().put("Label.font",
110         new Font JavaDoc("Helvetica", Font.BOLD, 10));
111     UIManager.getDefaults().put("ComboBox.font",
112         new Font JavaDoc("Helvetica", Font.BOLD, 10));
113     UIManager.getDefaults().put("Border.font",
114         new Font JavaDoc("Helvetica", Font.BOLD, 10));
115     UIManager.getDefaults().put("Menu.font",
116         new Font JavaDoc("Helvetica", Font.BOLD, 10));
117     UIManager.getDefaults().put("Button.font",
118         new Font JavaDoc("Helvetica", Font.BOLD, 10));
119     UIManager.getDefaults().put("MenuItem.font",
120         new Font JavaDoc("Helvetica", Font.BOLD, 10));
121
122     this.setSize(WizardConstants.FRAME_WIDTH, WizardConstants.FRAME_HEIGHT);
123     this.getContentPane().setLayout(new BorderLayout JavaDoc());
124     GuiConstants.centerComponent(this, WizardConstants.FRAME_WIDTH,
125         WizardConstants.FRAME_HEIGHT);
126
127     // define the menu
128
JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
129     JMenu JavaDoc menu = new JMenu JavaDoc(WizardTranslate.get("init.menu.1"));
130     // quit
131
JMenuItem JavaDoc item1 = new JMenuItem JavaDoc(WizardTranslate
132         .get(WizardConstants.COMMAND_QUIT));
133     item1.setActionCommand(WizardConstants.COMMAND_QUIT);
134     // import
135
JMenuItem JavaDoc item2 = new JMenuItem JavaDoc(WizardTranslate
136         .get(WizardConstants.COMMAND_EXPORT_XML));
137     item2.setActionCommand(WizardConstants.COMMAND_EXPORT_XML);
138     // export
139
JMenuItem JavaDoc item3 = new JMenuItem JavaDoc(WizardTranslate
140         .get(WizardConstants.COMMAND_IMPORT_XML));
141     item3.setActionCommand(WizardConstants.COMMAND_IMPORT_XML);
142     // check
143
JMenuItem JavaDoc item4 = new JMenuItem JavaDoc(WizardTranslate
144         .get(WizardConstants.COMMAND_CHECK_WIZARD));
145     item4.setActionCommand(WizardConstants.COMMAND_CHECK_WIZARD);
146     // validate
147
JMenuItem JavaDoc item5 = new JMenuItem JavaDoc(WizardTranslate
148         .get(WizardConstants.COMMAND_VALIDATE_XML));
149     item5.setActionCommand(WizardConstants.COMMAND_VALIDATE_XML);
150     menu.add(item1).addActionListener(this);
151     menu.add(item2).addActionListener(this);
152     menu.add(item3).addActionListener(this);
153     menu.add(item4).addActionListener(this);
154     menu.add(item5).addActionListener(this);
155     menu.setVisible(true);
156     menuBar.add(menu);
157
158     // define the panel that contains the menu
159
JPanel JavaDoc menuPane = new JPanel JavaDoc();
160     menuPane.add(menuBar);
161     this.getContentPane().add(menuPane, BorderLayout.NORTH);
162
163     startWizardTabs();
164
165     // Finish creating the frame
166
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
167     this.validate();
168     //this.pack();
169
this.setVisible(true);
170   }
171
172   private void startWizardTabs()
173   {
174     if (wizardTabs != null)
175       this.getContentPane().remove(wizardTabs);
176     wizardTabs = new WizardTabs();
177     this.getContentPane().add(wizardTabs, BorderLayout.CENTER);
178   }
179
180   /**
181    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
182    */

183   public void actionPerformed(ActionEvent JavaDoc e)
184   {
185     String JavaDoc command = e.getActionCommand();
186     if (command.equals(WizardConstants.COMMAND_QUIT))
187       System.exit(1);
188     else if (command.equals(WizardConstants.COMMAND_EXPORT_XML))
189       try
190       {
191         chooser.showSaveDialog(this);
192         File JavaDoc file = chooser.getSelectedFile();
193         if (file != null)
194           exportDocument(file.getAbsolutePath());
195       }
196       catch (IOException JavaDoc e1)
197       {
198         e1.printStackTrace();
199       }
200     else if (command.equals(WizardConstants.COMMAND_IMPORT_XML))
201     {
202       try
203       {
204         chooser.showOpenDialog(this);
205         File JavaDoc file = chooser.getSelectedFile();
206         if (file != null)
207           loadDocument(file.getAbsolutePath());
208       }
209       catch (Exception JavaDoc e2)
210       {
211         e2.printStackTrace();
212       }
213     }
214     else if (command.equals(WizardConstants.COMMAND_CHECK_WIZARD))
215     {
216       chooser.showOpenDialog(this);
217       File JavaDoc file = chooser.getSelectedFile();
218       if (file != null)
219       {
220         try
221         {
222           loadDocument(file.getAbsolutePath());
223           String JavaDoc newDoc = file.getParentFile().getAbsolutePath()
224               + File.separator + file.getName() + ".check";
225           exportDocument(newDoc);
226           validateDocument(newDoc);
227         }
228         catch (Exception JavaDoc e2)
229         {
230           e2.printStackTrace();
231         }
232       }
233     }
234     else if (command.equals(WizardConstants.COMMAND_VALIDATE_XML))
235     {
236       chooser.showOpenDialog(this);
237       File JavaDoc file = chooser.getSelectedFile();
238       if (file != null)
239         try
240         {
241           validateDocument(file.getAbsolutePath());
242         }
243         catch (IOException JavaDoc e2)
244         {
245           e2.printStackTrace();
246         }
247     }
248   }
249
250   /**
251    * Load document from a xml file
252    *
253    * @param xmlFile the path to the xml document
254    * @throws DocumentException if fails to parse
255    * @throws IOException if fails to read
256    */

257   public void loadDocument(String JavaDoc xmlFile) throws DocumentException,
258       IOException JavaDoc
259   {
260     SAXReader reader = new SAXReader();
261     this.document = reader.read(new FileReader JavaDoc(xmlFile));
262     if (document != null)
263     {
264       startWizardTabs();
265       wizardTabs.importDocumentFromXml(document);
266     }
267   }
268
269   /**
270    * Save the document to an xml file
271    *
272    * @param xmlFile the path to the xml file to save to
273    * @throws IOException if fails to write
274    */

275   public void saveDocument(String JavaDoc xmlFile) throws IOException JavaDoc
276   {
277     OutputFormat format = OutputFormat.createPrettyPrint();
278     XMLWriter writer = new XMLWriter(new FileWriter JavaDoc(xmlFile), format);
279     writer.write(document);
280     writer.close();
281   }
282
283   /**
284    * Validate the given XML document contained in the given file.
285    *
286    * @param xmlFile XML file name
287    * @throws IOException if an error occurs
288    */

289   public void validateDocument(String JavaDoc xmlFile) throws IOException JavaDoc
290   {
291     XmlValidator validator = new XmlValidator(Constants.C_JDBC_DTD_FILE,
292         new FileReader JavaDoc(new File JavaDoc(xmlFile)));
293     XmlValidatorFrame frame = new XmlValidatorFrame(xmlFile);
294
295     if (validator.isDtdValid())
296       frame.writeLine(Translate.get("virtualdatabase.xml.dtd.validated"));
297     if (validator.isXmlValid())
298       frame.writeLine(Translate.get("virtualdatabase.xml.document.validated"));
299
300     if (validator.getWarnings().size() > 0)
301     {
302       frame.setWarning();
303       ArrayList JavaDoc warnings = validator.getWarnings();
304       for (int i = 0; i < warnings.size(); i++)
305         frame.writeLine(Translate.get("virtualdatabase.xml.parsing.warning",
306             warnings.get(i)));
307     }
308
309     if (!validator.isDtdValid())
310     {
311       frame.setWarning();
312       frame.writeLine(Translate.get("virtualdatabase.xml.dtd.not.validated"));
313     }
314     if (!validator.isXmlValid())
315     {
316       frame.setWarning();
317       frame.writeLine(Translate
318           .get("virtualdatabase.xml.document.not.validated"));
319     }
320
321     ArrayList JavaDoc errors = validator.getExceptions();
322     if (errors.size() > 0)
323       frame.setWarning();
324     for (int i = 0; i < errors.size(); i++)
325       frame.writeLine(((Exception JavaDoc) errors.get(i)).getMessage());
326
327   }
328
329   /**
330    * Save the document to an xml file
331    *
332    * @param xmlFile the path to the xml file to save to
333    * @throws IOException if fails to write
334    */

335   public void exportDocument(String JavaDoc xmlFile) throws IOException JavaDoc
336   {
337     this.document = wizardTabs.exportDocumentToXml();
338     if (document != null)
339       saveDocument(xmlFile);
340   }
341
342   /**
343    * Start the XmlWizard
344    *
345    * @param args Command line arguments
346    */

347   public static void main(String JavaDoc[] args)
348   {
349
350     if (args == null || args.length == 0)
351       new XmlWizard();
352     else
353     {
354       try
355       {
356         new XmlWizard(args[0]);
357       }
358       catch (DocumentException e)
359       {
360         System.out.println(WizardTranslate.get("init.error.parse.failed"));
361         new XmlWizard();
362       }
363       catch (IOException JavaDoc e)
364       {
365         System.out.println(WizardTranslate.get("init.error.read.failed"));
366         new XmlWizard();
367       }
368     }
369   }
370 }
Popular Tags