BUG-472: Add EXI codecs without wiring them
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / handler / NetconfEXICodec.java
diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfEXICodec.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfEXICodec.java
new file mode 100644 (file)
index 0000000..c28db18
--- /dev/null
@@ -0,0 +1,63 @@
+package org.opendaylight.controller.netconf.util.handler;
+
+import org.openexi.proc.HeaderOptionsOutputType;
+import org.openexi.proc.common.AlignmentType;
+import org.openexi.proc.common.EXIOptions;
+import org.openexi.proc.common.EXIOptionsException;
+import org.openexi.proc.common.GrammarOptions;
+import org.openexi.proc.grammars.GrammarCache;
+import org.openexi.sax.EXIReader;
+import org.openexi.sax.Transmogrifier;
+
+import com.google.common.base.Preconditions;
+
+final class NetconfEXICodec {
+    /**
+     * NETCONF is XML environment, so the use of EXI cookie is not really needed. Adding it
+     * decreases efficiency of encoding by adding human-readable 4 bytes "EXI$" to the head
+     * of the stream. This is really useful, so let's output it now.
+     */
+    private static final boolean OUTPUT_EXI_COOKIE = true;
+    private final AlignmentType alignmentType;
+    private final EXIOptions exiOptions;
+
+    public NetconfEXICodec(final AlignmentType alignmentType, final EXIOptions exiOptions) {
+        this.alignmentType = Preconditions.checkNotNull(alignmentType);
+        this.exiOptions = Preconditions.checkNotNull(exiOptions);
+    }
+
+    private GrammarCache getGrammarCache() {
+        short go = GrammarOptions.DEFAULT_OPTIONS;
+        if (exiOptions.getPreserveComments()) {
+            go = GrammarOptions.addCM(go);
+        }
+        if (exiOptions.getPreserveDTD()) {
+            go = GrammarOptions.addDTD(go);
+        }
+        if (exiOptions.getPreserveNS()) {
+            go = GrammarOptions.addNS(go);
+        }
+        if (exiOptions.getPreservePIs()) {
+            go = GrammarOptions.addPI(go);
+        }
+
+        return new GrammarCache(null, go);
+    }
+
+    EXIReader getReader() throws EXIOptionsException {
+        final EXIReader r = new EXIReader();
+        r.setPreserveLexicalValues(exiOptions.getPreserveLexicalValues());
+        r.setGrammarCache(getGrammarCache());
+        return r;
+    }
+
+    Transmogrifier getTransmogrifier() throws EXIOptionsException {
+        final Transmogrifier transmogrifier = new Transmogrifier();
+        transmogrifier.setAlignmentType(alignmentType);
+        transmogrifier.setBlockSize(exiOptions.getBlockSize());
+        transmogrifier.setGrammarCache(getGrammarCache());
+        transmogrifier.setOutputCookie(OUTPUT_EXI_COOKIE);
+        transmogrifier.setOutputOptions(HeaderOptionsOutputType.all);
+        return transmogrifier;
+    }
+}