1 package org.opendaylight.controller.netconf.nettyutil.handler;
3 import com.google.common.base.Preconditions;
4 import org.openexi.proc.HeaderOptionsOutputType;
5 import org.openexi.proc.common.EXIOptions;
6 import org.openexi.proc.common.EXIOptionsException;
7 import org.openexi.proc.common.GrammarOptions;
8 import org.openexi.proc.grammars.GrammarCache;
9 import org.openexi.sax.EXIReader;
10 import org.openexi.sax.Transmogrifier;
12 public final class NetconfEXICodec {
14 * NETCONF is XML environment, so the use of EXI cookie is not really needed. Adding it
15 * decreases efficiency of encoding by adding human-readable 4 bytes "EXI$" to the head
16 * of the stream. This is really useful, so let's output it now.
18 private static final boolean OUTPUT_EXI_COOKIE = true;
19 private final EXIOptions exiOptions;
21 public NetconfEXICodec(final EXIOptions exiOptions) {
22 this.exiOptions = Preconditions.checkNotNull(exiOptions);
25 private GrammarCache getGrammarCache() {
26 short go = GrammarOptions.DEFAULT_OPTIONS;
27 if (exiOptions.getPreserveComments()) {
28 go = GrammarOptions.addCM(go);
30 if (exiOptions.getPreserveDTD()) {
31 go = GrammarOptions.addDTD(go);
33 if (exiOptions.getPreserveNS()) {
34 go = GrammarOptions.addNS(go);
36 if (exiOptions.getPreservePIs()) {
37 go = GrammarOptions.addPI(go);
40 return new GrammarCache(null, go);
43 EXIReader getReader() throws EXIOptionsException {
44 final EXIReader r = new EXIReader();
45 r.setPreserveLexicalValues(exiOptions.getPreserveLexicalValues());
46 r.setGrammarCache(getGrammarCache());
50 Transmogrifier getTransmogrifier() throws EXIOptionsException {
51 final Transmogrifier transmogrifier = new Transmogrifier();
52 transmogrifier.setAlignmentType(exiOptions.getAlignmentType());
53 transmogrifier.setBlockSize(exiOptions.getBlockSize());
54 transmogrifier.setGrammarCache(getGrammarCache());
55 transmogrifier.setOutputCookie(OUTPUT_EXI_COOKIE);
56 transmogrifier.setOutputOptions(HeaderOptionsOutputType.all);
57 return transmogrifier;