import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Properties;
public class pmWriteXML
{
private static final String[] strHeader = {"",
"",
"",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"",
"]>"};
private static final String strType = " Demonisch user profile";
private static final String strVer = " 1.0";
private static final String[] strExpectedFields = {"name", "email", "age", "des", "pic"};
private static void writeProps (Properties props, BufferedWriter outStream) throws dialogueException, IOException
{
String strLine = "";
String strField = "";
for (int i = 0; i < strExpectedFields.length; i++)
{
strField = strExpectedFields[i];
strLine = props.getProperty(strField);
if (strLine == null)
{
throw new dialogueException("An internal error occured when a field from the profile was not found: " + strField + " is missing", "Critical Error", JOptionPane.ERROR_MESSAGE);
}
strLine = pmValidData.removeAllHTMLTags(strLine); //HTML tags would cause problems with XML tags
writeLine(" <" + strField + ">" + strLine + "" + strField + ">", outStream);
}
}
public static File save (JFrame mainApp, File file, Properties props) throws dialogueException
{
if (file == null)
{
String strFile = "";
JFileChooser fileSave = new JFileChooser();
boolean validFile = false;
do
{
fileSave.addChoosableFileFilter(new fileTypeXML());
fileSave.setAcceptAllFileFilterUsed(false);
fileSave.setCurrentDirectory(new File(System.getProperty("user.dir")));
fileSave.setApproveButtonToolTipText("Save the profile here");
fileSave.setDialogType(JFileChooser.SAVE_DIALOG);
fileSave.setDialogTitle("Save Profile");
int returnValue = fileSave.showDialog(mainApp, "Save");
if (returnValue == JFileChooser.APPROVE_OPTION)
{
strFile = fileSave.getSelectedFile().getAbsolutePath();
String strExt = pmValidData.getFileExt(strFile);
if (strExt.equals(""))
{
strExt = "xml";
strFile += ".xml";
}
if (!strExt.equalsIgnoreCase("xml"))
{
throw new dialogueException("Can only save profiles as XML documents\nIlegal: " + strFile, "Invalid Format", JOptionPane.WARNING_MESSAGE);
}
file = new File(strFile);
if (file.exists())
{
int choice = JOptionPane.showConfirmDialog(mainApp, "The specified file already exists, do you wish to over write it?", "Overwrite confirm", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION)
{
validFile = true;
}
}
else
{
validFile = true;
}
}
else
{
return null;
}
}
while (!validFile);
}
BufferedWriter outStream = null;
if (file.exists()) //If the file already exists
{
if (!file.canWrite()) //If the file can not be written to
{
throw new dialogueException("Can not write to file\n" + file.getAbsolutePath() + "\nMost likely because the file is read only", "Can not save", JOptionPane.ERROR_MESSAGE);
}
}
try
{
outStream = new BufferedWriter(new FileWriter(file));
}
catch (IOException ex)
{
throw new dialogueException("Could not write to the file\n" + file.getAbsolutePath() + "\n" + ex.getMessage(), "Can not save", JOptionPane.ERROR_MESSAGE);
}
try
{
for (int i = 0; i < strHeader.length; i++)
{
writeLine(strHeader[i], outStream);
}
outStream.newLine();
writeLine("", outStream);
outStream.newLine();
writeLine(" ", outStream);
outStream.newLine();
writeLine(strType, outStream);
writeLine(strVer, outStream);
writeLine(" " + getTimeStamp() + "", outStream);
outStream.newLine();
writeLine(" ", outStream);
outStream.newLine();
writeLine(" ", outStream);
outStream.newLine();
writeProps(props, outStream);
outStream.newLine();
writeLine(" ", outStream);
outStream.newLine();
writeLine("", outStream);
outStream.close();
}
catch (IOException ex)
{
throw new dialogueException("An error occured when writing to the file: " + ex.getMessage(), "Can not save", JOptionPane.ERROR_MESSAGE);
}
return file;
}
private static void writeLine (String strLine, BufferedWriter outStream) throws IOException
{
outStream.write(strLine, 0, strLine.length());
outStream.newLine();
}
private static String getTimeStamp ()
{
Calendar calNow = Calendar.getInstance();
DecimalFormat decFormat = new DecimalFormat("00");
return decFormat.format(calNow.get(Calendar.DAY_OF_MONTH)) + "/" + decFormat.format((calNow.get(Calendar.MONTH) + 1)) + "/" + calNow.get(Calendar.YEAR) + ", at " + decFormat.format(calNow.get(Calendar.HOUR_OF_DAY)) + ":" + decFormat.format(calNow.get(Calendar.MINUTE));
}
}