// JS2-903: fragments are getting stripped out on write if the current user does not have edit access to write to the file
document.checkAccess(JetspeedActions.EDIT);
}
catch (SecurityException se)
{
throw new FailedToUpdateDocumentException("Insufficient Access: no edit access, cannot write.");
}
documentImpl.setPermissionsEnabled(false);
documentImpl.setConstraintsEnabled(false);
}
documentImpl.marshalling();
// marshal page to disk
String fileName = path;
if (!fileName.endsWith(this.documentType))
{
fileName = path + this.documentType;
}
File f = new File(this.documentRootDir, fileName);
absolutePath = f.getAbsolutePath();
// marshal: use SAX II handler to filter document XML for
// page and folder menu definition menu elements ordered
// polymorphic collection to strip artifical <menu-element>
// tags enabling Castor XML binding; see JETSPEED-INF/castor/page-mapping.xml
writer = new OutputStreamWriter(new FileOutputStream(f), PSML_DOCUMENT_ENCODING);
XMLWriter xmlWriter = new XMLWriter(writer, this.format);
final ContentHandler handler = xmlWriter;
Marshaller marshaller = new Marshaller(new ContentHandler()
{
private int menuDepth = 0;
public void characters(char[] ch, int start, int length) throws SAXException
{
handler.characters(ch, start, length);
}
public void endDocument() throws SAXException
{
handler.endDocument();
}
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
{
handler.ignorableWhitespace(ch, start, length);
}
public void processingInstruction(String target, String data) throws SAXException
{
handler.processingInstruction(target, data);
}
public void setDocumentLocator(Locator locator)
{
handler.setDocumentLocator(locator);
}
public void startDocument() throws SAXException
{
handler.startDocument();
}
public void endElement(String uri, String localName, String qName) throws SAXException {
// track menu depth
if (qName.equals("menu"))
{
menuDepth--;
}
// filter menu-element noded within menu definition
if ((menuDepth == 0) || !qName.equals("menu-element"))
{
handler.endElement(uri, localName, qName);
}
}
public void endPrefixMapping(String prefix) throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
handler.skippedEntity(name);
}
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
// filter menu-element noded within menu definition
if ((menuDepth == 0) || !qName.equals("menu-element"))
{
handler.startElement(uri,localName, qName, atts);
}
// track menu depth
if (qName.equals("menu"))
{
menuDepth++;
}
}
public void startPrefixMapping(String prefix, String uri) throws SAXException {
}
});
marshaller.setResolver((XMLClassDescriptorResolver) classDescriptorResolver);
marshaller.setValidation(false); // results in better performance
marshaller.marshal(document);
}
catch (MarshalException e)
{
log.error("Could not marshal the file " + absolutePath, e);
throw new FailedToUpdateDocumentException(e);
}
catch (ValidationException e)
{
log.error("Document " + absolutePath + " is not valid", e);
throw new FailedToUpdateDocumentException(e);
}
catch (IOException e)
{
log.error("Could not save the file " + absolutePath, e);
throw new FailedToUpdateDocumentException(e);
}
catch (Exception e)
{
log.error("Error while saving " + absolutePath, e);
throw new FailedToUpdateDocumentException(e);
}
finally
{
// restore permissions / constraints
documentImpl.setPermissionsEnabled(handlerFactory.getPermissionsEnabled());