2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.netconf.util.handler;
10 import java.io.ByteArrayInputStream;
11 import java.io.OutputStream;
13 import org.opendaylight.controller.netconf.api.NetconfMessage;
14 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
15 import org.openexi.sax.Transmogrifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.xml.sax.InputSource;
20 import com.google.common.base.Preconditions;
22 import io.netty.buffer.ByteBuf;
23 import io.netty.buffer.ByteBufOutputStream;
24 import io.netty.channel.ChannelHandlerContext;
25 import io.netty.handler.codec.MessageToByteEncoder;
27 public final class NetconfMessageToEXIEncoder extends MessageToByteEncoder<NetconfMessage> {
29 private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToEXIEncoder.class);
31 //private static final SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
32 private final NetconfEXICodec codec;
34 public NetconfMessageToEXIEncoder(final NetconfEXICodec codec) {
35 this.codec = Preconditions.checkNotNull(codec);
39 protected void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) throws Exception {
40 LOG.trace("Sent to encode : {}", XmlUtil.toString(msg.getDocument()));
42 try (final OutputStream os = new ByteBufOutputStream(out)) {
43 final Transmogrifier transmogrifier = codec.getTransmogrifier();
44 transmogrifier.setOutputStream(os);
46 // FIXME transformer not working, see EXILibTest
47 transmogrifier.encode(new InputSource(new ByteArrayInputStream(XmlUtil.toString(msg.getDocument()).getBytes())));
48 //final Transformer transformer = saxTransformerFactory.newTransformer();
49 //transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(transmogrifier.getSAXTransmogrifier()));