BUG 2509 : Removing all journal entries from a Followers in-memory journal causes...
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / NetconfEXICodec.java
1 package org.opendaylight.controller.netconf.nettyutil.handler;
2
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;
11
12 public final class NetconfEXICodec {
13     /**
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.
17      */
18     private static final boolean OUTPUT_EXI_COOKIE = true;
19     private final EXIOptions exiOptions;
20
21     public NetconfEXICodec(final EXIOptions exiOptions) {
22         this.exiOptions = Preconditions.checkNotNull(exiOptions);
23     }
24
25     private GrammarCache getGrammarCache() {
26         short go = GrammarOptions.DEFAULT_OPTIONS;
27         if (exiOptions.getPreserveComments()) {
28             go = GrammarOptions.addCM(go);
29         }
30         if (exiOptions.getPreserveDTD()) {
31             go = GrammarOptions.addDTD(go);
32         }
33         if (exiOptions.getPreserveNS()) {
34             go = GrammarOptions.addNS(go);
35         }
36         if (exiOptions.getPreservePIs()) {
37             go = GrammarOptions.addPI(go);
38         }
39
40         return new GrammarCache(null, go);
41     }
42
43     EXIReader getReader() throws EXIOptionsException {
44         final EXIReader r = new EXIReader();
45         r.setPreserveLexicalValues(exiOptions.getPreserveLexicalValues());
46         r.setGrammarCache(getGrammarCache());
47         return r;
48     }
49
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;
58     }
59 }