Merge "Bug 509: Fixed incorrect merging of Data Store Writes / Events"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / xml / 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.util.xml;
9
10 import org.opendaylight.controller.netconf.api.NetconfMessage;
11 import org.openexi.proc.common.AlignmentType;
12 import org.openexi.proc.common.EXIOptions;
13 import org.openexi.proc.common.EXIOptionsException;
14
15 import com.google.common.base.Preconditions;
16
17 public final class EXIParameters {
18     private static final String EXI_PARAMETER_ALIGNMENT = "alignment";
19     private static final String EXI_PARAMETER_BYTE_ALIGNED = "byte-aligned";
20     private static final String EXI_PARAMETER_BIT_PACKED = "bit-packed";
21     private static final String EXI_PARAMETER_COMPRESSED = "compressed";
22     private static final String EXI_PARAMETER_PRE_COMPRESSION = "pre-compression";
23
24     private static final String EXI_PARAMETER_FIDELITY = "fidelity";
25     private static final String EXI_FIDELITY_DTD = "dtd";
26     private static final String EXI_FIDELITY_LEXICAL_VALUES = "lexical-values";
27     private static final String EXI_FIDELITY_COMMENTS = "comments";
28     private static final String EXI_FIDELITY_PIS = "pis";
29     private static final String EXI_FIDELITY_PREFIXES = "prefixes";
30
31     private static final String EXI_PARAMETER_SCHEMA = "schema";
32     private static final String EXI_PARAMETER_SCHEMA_NONE = "none";
33     private static final String EXI_PARAMETER_SCHEMA_BUILT_IN = "builtin";
34     private static final String EXI_PARAMETER_SCHEMA_BASE_1_1 = "base:1.1";
35
36     private final EXIOptions options;
37
38     private EXIParameters(final EXIOptions options) {
39         this.options = Preconditions.checkNotNull(options);
40     }
41
42     public static EXIParameters fromNetconfMessage(final NetconfMessage root) throws EXIOptionsException {
43         return fromXmlElement(XmlElement.fromDomDocument(root.getDocument()));
44     }
45
46     public static EXIParameters fromXmlElement(final XmlElement root) throws EXIOptionsException {
47         final EXIOptions options =  new EXIOptions();
48
49         options.setAlignmentType(AlignmentType.bitPacked);
50         if (root.getElementsByTagName(EXI_PARAMETER_ALIGNMENT).getLength() > 0) {
51             if (root.getElementsByTagName(EXI_PARAMETER_BIT_PACKED).getLength() > 0) {
52                 options.setAlignmentType(AlignmentType.bitPacked);
53             } else if (root.getElementsByTagName(EXI_PARAMETER_BYTE_ALIGNED).getLength() > 0) {
54                 options.setAlignmentType(AlignmentType.byteAligned);
55             } else if (root.getElementsByTagName(EXI_PARAMETER_COMPRESSED).getLength() > 0) {
56                 options.setAlignmentType(AlignmentType.compress);
57             } else if (root.getElementsByTagName(EXI_PARAMETER_PRE_COMPRESSION).getLength() > 0) {
58                 options.setAlignmentType(AlignmentType.preCompress);
59             }
60         }
61
62         if (root.getElementsByTagName(EXI_PARAMETER_FIDELITY).getLength() > 0) {
63             if (root.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0) {
64                 options.setPreserveDTD(true);
65             }
66             if (root.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0) {
67                 options.setPreserveLexicalValues(true);
68             }
69             if (root.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0) {
70                 options.setPreserveComments(true);
71             }
72             if (root.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0) {
73                 options.setPreservePIs(true);
74             }
75             if (root.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0) {
76                 options.setPreserveNS(true);
77             }
78         }
79
80         if (root.getElementsByTagName(EXI_PARAMETER_SCHEMA).getLength() > 0) {
81 /*
82                         GrammarFactory grammarFactory = GrammarFactory.newInstance();
83                         if (operationElement
84                                 .getElementsByTagName(EXI_PARAMETER_SCHEMA_NONE)
85                                 .getLength() > 0) {
86                             this.grammars = grammarFactory.createSchemaLessGrammars();
87                         }
88
89                         if (operationElement.getElementsByTagName(
90                                 EXI_PARAMETER_SCHEMA_BUILT_IN).getLength() > 0) {
91                             this.grammars = grammarFactory.createXSDTypesOnlyGrammars();
92                         }
93
94                         if (operationElement.getElementsByTagName(
95                                 EXI_PARAMETER_SCHEMA_BASE_1_1).getLength() > 0) {
96                             this.grammars = grammarFactory
97                                     .createGrammars(NETCONF_XSD_LOCATION);
98                         }
99 */
100
101         }
102
103         return new EXIParameters(options);
104     }
105
106     public final EXIOptions getOptions() {
107         return options;
108     }
109 }