Merge "Use transaction chain in SalNodeWriter"
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / exi / EXIParameters.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.nettyutil.handler.exi;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.config.util.xml.XmlElement;
12 import org.openexi.proc.common.AlignmentType;
13 import org.openexi.proc.common.EXIOptions;
14 import org.openexi.proc.common.EXIOptionsException;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.NodeList;
19
20 public final class EXIParameters {
21     private static final String EXI_PARAMETER_ALIGNMENT = "alignment";
22     static final String EXI_PARAMETER_BYTE_ALIGNED = "byte-aligned";
23     static final String EXI_PARAMETER_BIT_PACKED = "bit-packed";
24     static final String EXI_PARAMETER_COMPRESSED = "compressed";
25     static final String EXI_PARAMETER_PRE_COMPRESSION = "pre-compression";
26
27     private static final String EXI_PARAMETER_FIDELITY = "fidelity";
28     private static final String EXI_FIDELITY_DTD = "dtd";
29     private static final String EXI_FIDELITY_LEXICAL_VALUES = "lexical-values";
30     private static final String EXI_FIDELITY_COMMENTS = "comments";
31     private static final String EXI_FIDELITY_PIS = "pis";
32     private static final String EXI_FIDELITY_PREFIXES = "prefixes";
33
34     private final EXIOptions options;
35     private static final Logger LOG = LoggerFactory.getLogger(EXIParameters.class);
36
37     private EXIParameters(final EXIOptions options) {
38         this.options = Preconditions.checkNotNull(options);
39     }
40
41
42     public static EXIParameters fromXmlElement(final XmlElement root) throws EXIOptionsException {
43         final EXIOptions options =  new EXIOptions();
44         final NodeList alignmentElements = root.getElementsByTagName(EXI_PARAMETER_ALIGNMENT);
45         if (alignmentElements.getLength() > 0) {
46             final Element alignmentElement = (Element) alignmentElements.item(0);
47             final String alignmentTextContent = alignmentElement.getTextContent().trim();
48
49             switch (alignmentTextContent) {
50             case EXI_PARAMETER_BYTE_ALIGNED:
51                 options.setAlignmentType(AlignmentType.byteAligned);
52                 break;
53             case EXI_PARAMETER_COMPRESSED:
54                 options.setAlignmentType(AlignmentType.compress);
55                 break;
56             case EXI_PARAMETER_PRE_COMPRESSION:
57                 options.setAlignmentType(AlignmentType.preCompress);
58                 break;
59             default:
60                 LOG.warn("Unexpected value in alignmentTextContent: {} , using default value", alignmentTextContent);
61             case EXI_PARAMETER_BIT_PACKED:
62                 options.setAlignmentType(AlignmentType.bitPacked);
63                 break;
64             }
65         } else {
66             options.setAlignmentType(AlignmentType.bitPacked);
67         }
68
69         final NodeList fidelityElements = root.getElementsByTagName(EXI_PARAMETER_FIDELITY);
70         if (fidelityElements.getLength() > 0) {
71             final Element fidelityElement = (Element) fidelityElements.item(0);
72             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0) {
73                 options.setPreserveDTD(true);
74             }
75             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0) {
76                 options.setPreserveLexicalValues(true);
77             }
78             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0) {
79                 options.setPreserveComments(true);
80             }
81             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0) {
82                 options.setPreservePIs(true);
83             }
84             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0) {
85                 options.setPreserveNS(true);
86             }
87         }
88         return new EXIParameters(options);
89     }
90
91     public EXIOptions getOptions() {
92         return options;
93     }
94 }