// 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);
Serializer serializer = new XMLSerializer(writer, this.format);
final DocumentHandler handler = serializer.asDocumentHandler();
Marshaller marshaller = new Marshaller(new DocumentHandler()
{
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 endElement(String name) throws SAXException
{
// track menu depth
if (name.equals("menu"))
{
menuDepth--;
}
// filter menu-element noded within menu definition
if ((menuDepth == 0) || !name.equals("menu-element"))
{
handler.endElement(name);
}
}
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 startElement(String name, AttributeList atts) throws SAXException
{
// filter menu-element noded within menu definition
if ((menuDepth == 0) || !name.equals("menu-element"))
{
handler.startElement(name, atts);
}
// track menu depth
if (name.equals("menu"))
{
menuDepth++;
}
}
});
marshaller.setMapping(this.mapping);
marshaller.marshal(document);
}
catch (MarshalException e)
{
log.error("Could not marshal the file " + f.getAbsolutePath(), e);
throw new FailedToUpdateDocumentException(e);