2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.util.xml;
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;
15 import com.google.common.base.Preconditions;
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";
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";
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";
36 private final EXIOptions options;
38 private EXIParameters(final EXIOptions options) {
39 this.options = Preconditions.checkNotNull(options);
42 public static EXIParameters fromNetconfMessage(final NetconfMessage root) throws EXIOptionsException {
43 return fromXmlElement(XmlElement.fromDomDocument(root.getDocument()));
46 public static EXIParameters fromXmlElement(final XmlElement root) throws EXIOptionsException {
47 final EXIOptions options = new EXIOptions();
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);
62 if (root.getElementsByTagName(EXI_PARAMETER_FIDELITY).getLength() > 0) {
63 if (root.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0) {
64 options.setPreserveDTD(true);
66 if (root.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0) {
67 options.setPreserveLexicalValues(true);
69 if (root.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0) {
70 options.setPreserveComments(true);
72 if (root.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0) {
73 options.setPreservePIs(true);
75 if (root.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0) {
76 options.setPreserveNS(true);
80 if (root.getElementsByTagName(EXI_PARAMETER_SCHEMA).getLength() > 0) {
82 GrammarFactory grammarFactory = GrammarFactory.newInstance();
84 .getElementsByTagName(EXI_PARAMETER_SCHEMA_NONE)
86 this.grammars = grammarFactory.createSchemaLessGrammars();
89 if (operationElement.getElementsByTagName(
90 EXI_PARAMETER_SCHEMA_BUILT_IN).getLength() > 0) {
91 this.grammars = grammarFactory.createXSDTypesOnlyGrammars();
94 if (operationElement.getElementsByTagName(
95 EXI_PARAMETER_SCHEMA_BASE_1_1).getLength() > 0) {
96 this.grammars = grammarFactory
97 .createGrammars(NETCONF_XSD_LOCATION);
103 return new EXIParameters(options);
106 public final EXIOptions getOptions() {