public void marshalDeploymentPlan(JAXBElement jaxbElement,
IFile file) throws Exception {
try {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setListener(marshallerListener);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
marshaller.marshal(jaxbElement, doc);
TransformerFactory xf = TransformerFactory.newInstance();
try {
xf.setAttribute("indent-number", new Integer(4));
} catch (IllegalArgumentException iae) {
//ignore this. http://forums.sun.com/thread.jspa?threadID=562510&messageID=2841867
}
Transformer xformer = xf.newTransformer();
xformer.setOutputProperty(OutputKeys.METHOD, "xml");
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
xformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
Result out = new StreamResult(new OutputStreamWriter(outBuffer,"UTF-8"));
NamespacePrefix.processPrefix(doc);
xformer.transform(new DOMSource(doc), out);
ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
if(file.exists()) {
file.setContents(inBuffer, true, false, null);
} else {
prepareFolder(file.getParent());
file.create(inBuffer, true, null);
}
} catch (JAXBException jaxbException) {
Trace.tracePoint("JAXBException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
throw jaxbException;
} catch (CoreException coreException) {
Trace.tracePoint("CoreException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
throw coreException;
} catch (ParserConfigurationException e) {
Trace.tracePoint("ParserConfigurationException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
throw e;
} catch (TransformerConfigurationException e) {
Trace.tracePoint("TransformerConfigurationException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
throw e;
} catch (UnsupportedEncodingException e) {
Trace.tracePoint("UnsupportedEncodingException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
throw e;
} catch (TransformerException e) {
Trace.tracePoint("TransformerException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
throw e;
}
}
|