1 24 25 package org.objectweb.cjdbc.console.wizard; 26 27 import java.awt.BorderLayout ; 28 import java.awt.Font ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.io.File ; 32 import java.io.FileReader ; 33 import java.io.FileWriter ; 34 import java.io.IOException ; 35 import java.util.ArrayList ; 36 37 import javax.swing.JFileChooser ; 38 import javax.swing.JFrame ; 39 import javax.swing.JMenu ; 40 import javax.swing.JMenuBar ; 41 import javax.swing.JMenuItem ; 42 import javax.swing.JPanel ; 43 import javax.swing.UIManager ; 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 62 public class XmlWizard extends JFrame implements ActionListener 63 { 64 65 private Document document; 66 private JFileChooser chooser; 67 private WizardTabs wizardTabs; 68 69 77 public XmlWizard(String xmlFile) throws DocumentException, IOException 78 { 79 this(); 80 loadDocument(xmlFile); 81 } 82 83 87 public XmlWizard() 88 { 89 super(WizardTranslate.get("init.title.main.frame")); 91 chooser = new JFileChooser () 92 { 93 96 public boolean accept(File 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 109 UIManager.getDefaults().put("Label.font", 110 new Font ("Helvetica", Font.BOLD, 10)); 111 UIManager.getDefaults().put("ComboBox.font", 112 new Font ("Helvetica", Font.BOLD, 10)); 113 UIManager.getDefaults().put("Border.font", 114 new Font ("Helvetica", Font.BOLD, 10)); 115 UIManager.getDefaults().put("Menu.font", 116 new Font ("Helvetica", Font.BOLD, 10)); 117 UIManager.getDefaults().put("Button.font", 118 new Font ("Helvetica", Font.BOLD, 10)); 119 UIManager.getDefaults().put("MenuItem.font", 120 new Font ("Helvetica", Font.BOLD, 10)); 121 122 this.setSize(WizardConstants.FRAME_WIDTH, WizardConstants.FRAME_HEIGHT); 123 this.getContentPane().setLayout(new BorderLayout ()); 124 GuiConstants.centerComponent(this, WizardConstants.FRAME_WIDTH, 125 WizardConstants.FRAME_HEIGHT); 126 127 JMenuBar menuBar = new JMenuBar (); 129 JMenu menu = new JMenu (WizardTranslate.get("init.menu.1")); 130 JMenuItem item1 = new JMenuItem (WizardTranslate 132 .get(WizardConstants.COMMAND_QUIT)); 133 item1.setActionCommand(WizardConstants.COMMAND_QUIT); 134 JMenuItem item2 = new JMenuItem (WizardTranslate 136 .get(WizardConstants.COMMAND_EXPORT_XML)); 137 item2.setActionCommand(WizardConstants.COMMAND_EXPORT_XML); 138 JMenuItem item3 = new JMenuItem (WizardTranslate 140 .get(WizardConstants.COMMAND_IMPORT_XML)); 141 item3.setActionCommand(WizardConstants.COMMAND_IMPORT_XML); 142 JMenuItem item4 = new JMenuItem (WizardTranslate 144 .get(WizardConstants.COMMAND_CHECK_WIZARD)); 145 item4.setActionCommand(WizardConstants.COMMAND_CHECK_WIZARD); 146 JMenuItem item5 = new JMenuItem (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 JPanel menuPane = new JPanel (); 160 menuPane.add(menuBar); 161 this.getContentPane().add(menuPane, BorderLayout.NORTH); 162 163 startWizardTabs(); 164 165 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 167 this.validate(); 168 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 183 public void actionPerformed(ActionEvent e) 184 { 185 String 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 file = chooser.getSelectedFile(); 193 if (file != null) 194 exportDocument(file.getAbsolutePath()); 195 } 196 catch (IOException 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 file = chooser.getSelectedFile(); 206 if (file != null) 207 loadDocument(file.getAbsolutePath()); 208 } 209 catch (Exception e2) 210 { 211 e2.printStackTrace(); 212 } 213 } 214 else if (command.equals(WizardConstants.COMMAND_CHECK_WIZARD)) 215 { 216 chooser.showOpenDialog(this); 217 File file = chooser.getSelectedFile(); 218 if (file != null) 219 { 220 try 221 { 222 loadDocument(file.getAbsolutePath()); 223 String newDoc = file.getParentFile().getAbsolutePath() 224 + File.separator + file.getName() + ".check"; 225 exportDocument(newDoc); 226 validateDocument(newDoc); 227 } 228 catch (Exception e2) 229 { 230 e2.printStackTrace(); 231 } 232 } 233 } 234 else if (command.equals(WizardConstants.COMMAND_VALIDATE_XML)) 235 { 236 chooser.showOpenDialog(this); 237 File file = chooser.getSelectedFile(); 238 if (file != null) 239 try 240 { 241 validateDocument(file.getAbsolutePath()); 242 } 243 catch (IOException e2) 244 { 245 e2.printStackTrace(); 246 } 247 } 248 } 249 250 257 public void loadDocument(String xmlFile) throws DocumentException, 258 IOException 259 { 260 SAXReader reader = new SAXReader(); 261 this.document = reader.read(new FileReader (xmlFile)); 262 if (document != null) 263 { 264 startWizardTabs(); 265 wizardTabs.importDocumentFromXml(document); 266 } 267 } 268 269 275 public void saveDocument(String xmlFile) throws IOException 276 { 277 OutputFormat format = OutputFormat.createPrettyPrint(); 278 XMLWriter writer = new XMLWriter(new FileWriter (xmlFile), format); 279 writer.write(document); 280 writer.close(); 281 } 282 283 289 public void validateDocument(String xmlFile) throws IOException 290 { 291 XmlValidator validator = new XmlValidator(Constants.C_JDBC_DTD_FILE, 292 new FileReader (new File (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 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 errors = validator.getExceptions(); 322 if (errors.size() > 0) 323 frame.setWarning(); 324 for (int i = 0; i < errors.size(); i++) 325 frame.writeLine(((Exception ) errors.get(i)).getMessage()); 326 327 } 328 329 335 public void exportDocument(String xmlFile) throws IOException 336 { 337 this.document = wizardTabs.exportDocumentToXml(); 338 if (document != null) 339 saveDocument(xmlFile); 340 } 341 342 347 public static void main(String [] 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 e) 364 { 365 System.out.println(WizardTranslate.get("init.error.read.failed")); 366 new XmlWizard(); 367 } 368 } 369 } 370 } | Popular Tags |