Merge "Fixed for bug 1197"
[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.netconf.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
16 public final class EXIParameters {
17     private static final String EXI_PARAMETER_ALIGNMENT = "alignment";
18     private static final String EXI_PARAMETER_BYTE_ALIGNED = "byte-aligned";
19     private static final String EXI_PARAMETER_BIT_PACKED = "bit-packed";
20     private static final String EXI_PARAMETER_COMPRESSED = "compressed";
21     private static final String EXI_PARAMETER_PRE_COMPRESSION = "pre-compression";
22
23     private static final String EXI_PARAMETER_FIDELITY = "fidelity";
24     private static final String EXI_FIDELITY_DTD = "dtd";
25     private static final String EXI_FIDELITY_LEXICAL_VALUES = "lexical-values";
26     private static final String EXI_FIDELITY_COMMENTS = "comments";
27     private static final String EXI_FIDELITY_PIS = "pis";
28     private static final String EXI_FIDELITY_PREFIXES = "prefixes";
29
30     private final EXIOptions options;
31
32     private EXIParameters(final EXIOptions options) {
33         this.options = Preconditions.checkNotNull(options);
34     }
35
36
37     public static EXIParameters fromXmlElement(final XmlElement root) throws EXIOptionsException {
38         final EXIOptions options =  new EXIOptions();
39
40         options.setAlignmentType(AlignmentType.bitPacked);
41         if (root.getElementsByTagName(EXI_PARAMETER_ALIGNMENT).getLength() > 0) {
42             if (root.getElementsByTagName(EXI_PARAMETER_BIT_PACKED).getLength() > 0) {
43                 options.setAlignmentType(AlignmentType.bitPacked);
44             } else if (root.getElementsByTagName(EXI_PARAMETER_BYTE_ALIGNED).getLength() > 0) {
45                 options.setAlignmentType(AlignmentType.byteAligned);
46             } else if (root.getElementsByTagName(EXI_PARAMETER_COMPRESSED).getLength() > 0) {
47                 options.setAlignmentType(AlignmentType.compress);
48             } else if (root.getElementsByTagName(EXI_PARAMETER_PRE_COMPRESSION).getLength() > 0) {
49                 options.setAlignmentType(AlignmentType.preCompress);
50             }
51         }
52
53         if (root.getElementsByTagName(EXI_PARAMETER_FIDELITY).getLength() > 0) {
54             if (root.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0) {
55                 options.setPreserveDTD(true);
56             }
57             if (root.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0) {
58                 options.setPreserveLexicalValues(true);
59             }
60             if (root.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0) {
61                 options.setPreserveComments(true);
62             }
63             if (root.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0) {
64                 options.setPreservePIs(true);
65             }
66             if (root.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0) {
67                 options.setPreserveNS(true);
68             }
69         }
70         return new EXIParameters(options);
71     }
72
73     public final EXIOptions getOptions() {
74         return options;
75     }
76 }