/*
* Copyright (c) 2014 Data Harmonisation Panel
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* Data Harmonisation Panel <http://www.dhpanel.eu>
*/
package eu.esdihumboldt.hale.common.schema.persist.hsd;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Element;
import eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException;
import eu.esdihumboldt.hale.common.core.io.ProgressIndicator;
import eu.esdihumboldt.hale.common.core.io.report.IOReport;
import eu.esdihumboldt.hale.common.core.io.report.IOReporter;
import eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl;
import eu.esdihumboldt.hale.common.schema.io.impl.AbstractSchemaWriter;
import eu.esdihumboldt.util.groovy.xml.NSDOMBuilder;
/**
* Writes the HALE schema model to XML.
*
* @author Simon Templer
*/
public class HaleSchemaWriter extends AbstractSchemaWriter {
@Override
public boolean isCancelable() {
return false;
}
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
throws IOProviderConfigurationException, IOException {
progress.begin("Save schema", ProgressIndicator.UNKNOWN);
try (OutputStream out = getTarget().getOutput()) {
// create DOM
NSDOMBuilder builder = SchemaToXml.createBuilder();
Element root = new SchemaToXml().schemasToXml(builder, getSchemas().getSchemas());
// configure transformer for serialization
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
// TODO configurable?!
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
// serialize DOM
DOMSource source = new DOMSource(root);
StreamResult result = new StreamResult(out);
transformer.transform(source, result);
reporter.setSuccess(true);
} catch (Exception e) {
reporter.error(new IOMessageImpl(e.getMessage(), e));
reporter.setSuccess(false);
} finally {
progress.end();
}
return reporter;
}
@Override
protected String getDefaultTypeName() {
return "HALE Schema Definition";
}
}