Remove netconf from commons/opendaylight pom
[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 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.w3c.dom.Element;
16 import org.w3c.dom.NodeList;
17
18 public final class EXIParameters {
19     private static final String EXI_PARAMETER_ALIGNMENT = "alignment";
20     static final String EXI_PARAMETER_BYTE_ALIGNED = "byte-aligned";
21     static final String EXI_PARAMETER_BIT_PACKED = "bit-packed";
22     static final String EXI_PARAMETER_COMPRESSED = "compressed";
23     static final String EXI_PARAMETER_PRE_COMPRESSION = "pre-compression";
24
25     private static final String EXI_PARAMETER_FIDELITY = "fidelity";
26     private static final String EXI_FIDELITY_DTD = "dtd";
27     private static final String EXI_FIDELITY_LEXICAL_VALUES = "lexical-values";
28     private static final String EXI_FIDELITY_COMMENTS = "comments";
29     private static final String EXI_FIDELITY_PIS = "pis";
30     private static final String EXI_FIDELITY_PREFIXES = "prefixes";
31
32     private final EXIOptions options;
33
34     private EXIParameters(final EXIOptions options) {
35         this.options = Preconditions.checkNotNull(options);
36     }
37
38
39     public static EXIParameters fromXmlElement(final XmlElement root) throws EXIOptionsException {
40         final EXIOptions options =  new EXIOptions();
41
42         options.setAlignmentType(AlignmentType.bitPacked);
43
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_BIT_PACKED:
51                 options.setAlignmentType(AlignmentType.bitPacked);
52                 break;
53             case EXI_PARAMETER_BYTE_ALIGNED:
54                 options.setAlignmentType(AlignmentType.byteAligned);
55                 break;
56             case EXI_PARAMETER_COMPRESSED:
57                 options.setAlignmentType(AlignmentType.compress);
58                 break;
59             case EXI_PARAMETER_PRE_COMPRESSION:
60                 options.setAlignmentType(AlignmentType.preCompress);
61                 break;
62             }
63         }
64
65         if (root.getElementsByTagName(EXI_PARAMETER_FIDELITY).getLength() > 0) {
66             if (root.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0) {
67                 options.setPreserveDTD(true);
68             }
69             if (root.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0) {
70                 options.setPreserveLexicalValues(true);
71             }
72             if (root.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0) {
73                 options.setPreserveComments(true);
74             }
75             if (root.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0) {
76                 options.setPreservePIs(true);
77             }
78             if (root.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0) {
79                 options.setPreserveNS(true);
80             }
81         }
82         return new EXIParameters(options);
83     }
84
85     public final EXIOptions getOptions() {
86         return options;
87     }
88 }