import org.xml.sax.Attributes; import org.xml.sax.XMLReader; import org.xml.sax.SAXException; import org.xml.sax.InputSource; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.FileReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import javax.swing.JFrame; import javax.swing.JFileChooser; import javax.swing.JOptionPane; public class pmReadXML extends DefaultHandler { private final String[] strReadTags = {"name", "email", "age", "des", "pic"}; private final String strFileType = "Demonisch user profile"; private boolean getFileType = false; private boolean readData = false; private boolean notProfileFile = false; private String strCurrentTag = ""; private File file = null; private Properties props = null; private JFrame mainApp = null; public File getFile () { return file; } public Properties getProperties () { return props; } public pmReadXML (JFrame mainApp) { this.mainApp = mainApp; } public boolean open () throws dialogueException { String strFile = ""; JFileChooser fileOpen = new JFileChooser(); boolean validFile = false; do { fileOpen.addChoosableFileFilter(new fileTypeXML()); fileOpen.setAcceptAllFileFilterUsed(false); fileOpen.setCurrentDirectory(new File(System.getProperty("user.dir"))); fileOpen.setApproveButtonToolTipText("Open this profile"); fileOpen.setDialogType(JFileChooser.OPEN_DIALOG); fileOpen.setDialogTitle("Open Profile"); int returnValue = fileOpen.showDialog(mainApp, "Open"); if (returnValue == JFileChooser.APPROVE_OPTION) { strFile = fileOpen.getSelectedFile().getAbsolutePath(); String strExt = pmValidData.getFileExt(strFile); if (strExt.equals("")) { strExt = "xml"; strFile += ".xml"; } if (!strExt.equalsIgnoreCase("xml")) { throw new dialogueException("The selected file is not an xml document\n" + strFile, "Invalid file format", JOptionPane.WARNING_MESSAGE); } file = new File(strFile); if (file.exists()) { validFile = true; } else { throw new dialogueException("The specified file does not exist\n" + file.getAbsolutePath(), "Could not find file", JOptionPane.WARNING_MESSAGE); } } else { return false; } } while (!validFile); props = new Properties(); XMLReader xr = null; try { xr = XMLReaderFactory.createXMLReader(); } catch (SAXException ex) { throw new dialogueException("An internal error occured when an xml parser could not be created", "Critical Error", JOptionPane.ERROR_MESSAGE); } xr.setContentHandler(this); try { xr.parse(new InputSource(new FileReader(file))); } catch (FileNotFoundException ex) //Should never hapen { throw new dialogueException("The specified file does not exist\n" + file.getAbsolutePath(), "Could not find file", JOptionPane.WARNING_MESSAGE); } catch (IOException ex) { throw new dialogueException("ACould not read from the file\n" + file.getAbsolutePath(), "Error reading from file", JOptionPane.ERROR_MESSAGE); } catch (SAXException ex) { //Because extending the methods, can not throw the dialogueException from the defaultHandler methods //Instead set the boolean flag, throw a SAXException and check it here if (notProfileFile) { throw new dialogueException("This is not a valid user profile for the Demonisch profileMaker", "Invalid File Format", JOptionPane.ERROR_MESSAGE); } throw new dialogueException("The profile document contains syntax errors. If you understand xml then try to fix the document\nOtherwise overwrite it with a new profile\n" + ex.getMessage(), "Corrupt File", JOptionPane.ERROR_MESSAGE); } return true; } public void startElement (String nameSpaceURI, String strTag, String qName, Attributes attr) throws SAXException { if (strTag.equalsIgnoreCase("fileType")) { getFileType = true; } else { for (int i = 0; i < strReadTags.length; i++) { if (strReadTags[i].equalsIgnoreCase(strTag)) { readData = true; strCurrentTag = strTag; break; } } } } public void characters (char[] value, int start, int length) throws SAXException { if (getFileType) { if (!strFileType.equalsIgnoreCase(valueToString(value, start, length))) { notProfileFile = true; throw new SAXException(""); } getFileType = false; } else if (readData) { String strData = valueToString(value, start, length); props.setProperty(strCurrentTag, strData); readData = false; } } public void endElement (String nameSpaceURI, String strTag, String qName) throws SAXException { if (readData) //If readData has been left on (no chars between two profile tags) { props.setProperty(strCurrentTag, ""); readData = false; } } //Converts the char array for the data into a string private String valueToString (char[] value, int start, int length) { if (length <= 0) { return ""; } char[] c = new char[length]; for (int i = 0; i < c.length; i++) { c[i] = value[start + i]; } return new String(c); } }