return false;
}
public void savePackage(String xpdlId, String filename) {
XPDLHandler xpdlhandler = JaWEManager.getInstance().getXPDLHandler();
Package pkg = xpdlhandler.getPackageById(xpdlId);
String oldFilename = xpdlhandler.getAbsoluteFilePath(pkg);
try {
// if SaveAs was performed and the document was previously saved,
// change ExternalPackage's relative paths
boolean isNewFile = !filename.equals(oldFilename);
if (oldFilename != null && isNewFile) {
boolean crossRefs = JaWEManager.getInstance().getXPDLUtils().doesCrossreferenceExist(pkg);
int r = JOptionPane.YES_OPTION;
if (crossRefs) {
r = JOptionPane.showConfirmDialog(getJaWEFrame(),
settings.getLanguageDependentString("MessageCrossReferenceExistDoYouWantToProceed"),
getJaWEFrame().getAppTitle(),
JOptionPane.YES_NO_OPTION);
}
if (r == JOptionPane.YES_OPTION) {
updateExternalPackagesRelativePaths(pkg, filename);
} else {
return;
}
}
Document document = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbuilder = dbf.newDocumentBuilder();
document = dbuilder.newDocument();
// retrieve the file writter
RandomAccessFile raf = xpdlhandler.getRaf(pkg);
// output stream will either be the FileOutputStream in the
// case of save as, or the ByteArrayOutputStream if we are
// saving an existing file
OutputStream os;
if (isNewFile) {
// try to open random access file as rw, if it fails
// the saving shouldn't occur
try {
File f = new File(filename);
RandomAccessFile r = new RandomAccessFile(f, "rw");
FileLock flck = r.getChannel().tryLock();
flck.release();
r.close(); // Harald Meister
// this exception happens when using jdk1.4 under Linux
// if it happens, just catch it and proceed with saving
// because Linux with jdk1.4.0 doesn't support locking
} catch (IOException ioe) {
// ioe.printStackTrace();
// this happens when the locking fails, and null is returned,
// and after that release method is called on the null;
// This means that the file we want to save the given
// package as, is already locked, so we do not allow saving
} catch (NullPointerException npe) {
throw new Exception();
}
// if we are at this point, this means either the locking
// succeeded, or we use jdk1.4 under Linux that does not
// support locking
os = new FileOutputStream(filename);
} else {
os = new ByteArrayOutputStream();
}
// Here we get all document elements set
JaWEManager.getInstance().getXPDLHandler().getXPDLRepositoryHandler().toXML(document, pkg);
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty("encoding", settings.getEncoding());
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
if (!isNewFile && raf != null && os instanceof ByteArrayOutputStream) {
if (raf != null) {
// must go to the beginning - otherwise, it will not
// truncate the file correctly in some Java-OS combination
raf.seek(0);
raf.getChannel().truncate(0);
raf.write(((ByteArrayOutputStream) os).toByteArray());
}
}
os.close();
XPDLListenerAndObservable xpdl = getXPDLListenerObservable(pkg);
if (xpdl != null) {
xpdl.setModified(false);
}
if (isNewFile) {
xpdlhandler.registerPackageFilename(filename, pkg);
}
try {
System.setProperty("user.dir", xpdlhandler.getParentDirectory(pkg));
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (NonWritableChannelException nwcex) {
nwcex.printStackTrace();