blob: 17392369f7f8acbdcb5ba9dd5c42c2247d87a551 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.aries.jax.rs.rest.management.handler;
import java.io.IOException;
import java.io.OutputStream;
import java.util.function.BiConsumer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.Document;
import org.w3c.dom.Node;
class XMLSerializer {
private final DocumentBuilder db;
public XMLSerializer() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public void serialize(
String rootElement, BiConsumer<Document, Node> consumer,
OutputStream entityStream)
throws Exception {
try {
Document dom = db.newDocument();
Node parentNode = dom;
if (rootElement != null) {
parentNode = dom.createElement(rootElement);
dom.appendChild(parentNode);
}
consumer.accept(dom, parentNode);
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.STANDALONE, "yes");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
tr.transform(
new DOMSource(dom),
new StreamResult(entityStream)
);
}
catch (Exception e) {
throw new IOException(e);
}
}
}