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