use of com.sun.org.apache.xml.internal.serialize.XMLSerializer in project tdi-studio-se by Talend.
the class AutoConvertTypesUtils method save.
public static boolean save(List<AutoConversionType> beans, File file) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
OutputStreamWriter output = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() {
@Override
public void error(final SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void fatalError(final SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void warning(final SAXParseException exception) throws SAXException {
throw exception;
}
});
Document document = builder.newDocument();
//document.setXmlVersion("1.0");//$NON-NLS-1$
//$NON-NLS-1$
Element root = document.createElement("mapping");
document.appendChild(root);
for (int i = 0; i < beans.size(); i++) {
AutoConversionType bean = beans.get(i);
//$NON-NLS-1$
Element typeNode = document.createElement("conversionType");
//$NON-NLS-1$
typeNode.setAttribute("source", bean.getSourceDataType());
//$NON-NLS-1$
typeNode.setAttribute("target", bean.getTargetDataType());
//$NON-NLS-1$
typeNode.setAttribute("function", bean.getConversionFunction());
root.appendChild(typeNode);
}
// save into file
if (document != null) {
XMLSerializer serializer = new XMLSerializer();
OutputFormat outputFormat = new OutputFormat();
outputFormat.setIndenting(true);
serializer.setOutputFormat(outputFormat);
output = new OutputStreamWriter(new FileOutputStream(file));
serializer.setOutputCharStream(output);
serializer.serialize(document);
// update
beanList = new ArrayList<>();
beanList.addAll(beans);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} finally {
if (output != null) {
output.close();
}
}
return true;
}
Aggregations