|
| 1 | +/* |
| 2 | + * Copyright 2021 StreamThoughts. |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. |
| 7 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | + * (the "License"); you may not use this file except in compliance with |
| 9 | + * the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package io.streamthoughts.kafka.connect.filepulse.xml; |
| 20 | + |
| 21 | +import org.apache.kafka.common.config.AbstractConfig; |
| 22 | +import org.apache.kafka.common.config.ConfigDef; |
| 23 | + |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * |
| 31 | + */ |
| 32 | +public class XMLCommonConfig extends AbstractConfig { |
| 33 | + |
| 34 | + public static final String XML_FORCE_ARRAY_ON_FIELDS_CONFIG = "xml.force.array.on.fields"; |
| 35 | + private static final String XML_FORCE_ARRAY_ON_FIELDS_FORCE_DOC = "The comma-separated list of fields for which an array-type must be forced"; |
| 36 | + |
| 37 | + public static final String XML_PARSER_VALIDATING_ENABLED_CONFIG = "xml.parser.validating.enabled"; |
| 38 | + private static final String XML_PARSER_VALIDATING_ENABLED_DOC = " Specifies that the parser will validate documents as they are parsed (default: false)."; |
| 39 | + |
| 40 | + public static final String XML_PARSER_NAMESPACE_AWARE_ENABLED_CONFIG = "xml.parser.namespace.aware.enabled"; |
| 41 | + private static final String XML_PARSER_NAMESPACE_AWARE_ENABLED_DOC = "Specifies that the XML parser will provide support for XML namespaces (default: false)."; |
| 42 | + |
| 43 | + public static final String XML_EXCLUDE_EMPTY_ELEMENTS_CONFIG = "xml.exclude.empty.elements"; |
| 44 | + private static final String XML_EXCLUDE_EMPTY_ELEMENTS_DOC = "Specifies that the reader should exclude element having no field (default: false)."; |
| 45 | + |
| 46 | + public static final String XML_DATA_TYPE_INFERENCE_ENABLED_CONFIG = "xml.data.type.inference.enabled"; |
| 47 | + private static final String XML_DATA_TYPE_INFERENCE_ENABLED_DOC = "Specifies that the reader should try to infer the type of data nodes. (default: false)."; |
| 48 | + |
| 49 | + private final String keyPrefix; |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a new {@link XMLCommonConfig} instance. |
| 53 | + * |
| 54 | + * @param originals the reader configuration. |
| 55 | + */ |
| 56 | + protected XMLCommonConfig(final String keyPrefix, |
| 57 | + final ConfigDef configDef, |
| 58 | + final Map<String, ?> originals) { |
| 59 | + super(configDef, originals); |
| 60 | + this.keyPrefix = keyPrefix; |
| 61 | + } |
| 62 | + |
| 63 | + private String withKeyPrefix(final String configKey) { |
| 64 | + return keyPrefix + configKey; |
| 65 | + } |
| 66 | + |
| 67 | + public boolean isValidatingEnabled() { |
| 68 | + return getBoolean(withKeyPrefix(XML_PARSER_VALIDATING_ENABLED_CONFIG)); |
| 69 | + } |
| 70 | + |
| 71 | + public boolean isNamespaceAwareEnabled() { |
| 72 | + return getBoolean(withKeyPrefix(XML_PARSER_NAMESPACE_AWARE_ENABLED_CONFIG)); |
| 73 | + } |
| 74 | + |
| 75 | + public boolean isEmptyElementExcluded() { |
| 76 | + return getBoolean(withKeyPrefix(XML_EXCLUDE_EMPTY_ELEMENTS_CONFIG)); |
| 77 | + } |
| 78 | + |
| 79 | + public boolean isDataTypeInferenceEnabled() { |
| 80 | + return getBoolean(withKeyPrefix(XML_DATA_TYPE_INFERENCE_ENABLED_CONFIG)); |
| 81 | + } |
| 82 | + |
| 83 | + public List<String> forceArrayFields() { |
| 84 | + return getList(withKeyPrefix(XML_FORCE_ARRAY_ON_FIELDS_CONFIG)); |
| 85 | + } |
| 86 | + |
| 87 | + public static ConfigDef buildConfigDefWith(final String group, |
| 88 | + final String keyPrefix, |
| 89 | + final ConfigDef.ConfigKey... additional) { |
| 90 | + return buildConfigDefWith(group, keyPrefix, Arrays.asList(additional)); |
| 91 | + } |
| 92 | + public static ConfigDef buildConfigDefWith(final String group, |
| 93 | + final String keyPrefix, |
| 94 | + final Iterable<ConfigDef.ConfigKey> additional) { |
| 95 | + int filterGroupCounter = 0; |
| 96 | + final ConfigDef def = new ConfigDef() |
| 97 | + .define( |
| 98 | + keyPrefix + XML_FORCE_ARRAY_ON_FIELDS_CONFIG, |
| 99 | + ConfigDef.Type.LIST, |
| 100 | + Collections.emptyList(), |
| 101 | + ConfigDef.Importance.MEDIUM, |
| 102 | + XML_FORCE_ARRAY_ON_FIELDS_FORCE_DOC, |
| 103 | + group, |
| 104 | + filterGroupCounter++, |
| 105 | + ConfigDef.Width.NONE, |
| 106 | + XML_FORCE_ARRAY_ON_FIELDS_FORCE_DOC |
| 107 | + ) |
| 108 | + .define( |
| 109 | + keyPrefix + XML_PARSER_VALIDATING_ENABLED_CONFIG, |
| 110 | + ConfigDef.Type.BOOLEAN, |
| 111 | + false, |
| 112 | + ConfigDef.Importance.LOW, |
| 113 | + XML_PARSER_VALIDATING_ENABLED_DOC, |
| 114 | + group, |
| 115 | + filterGroupCounter++, |
| 116 | + ConfigDef.Width.NONE, |
| 117 | + keyPrefix + XML_PARSER_VALIDATING_ENABLED_CONFIG |
| 118 | + ) |
| 119 | + .define( |
| 120 | + keyPrefix + XML_PARSER_NAMESPACE_AWARE_ENABLED_CONFIG, |
| 121 | + ConfigDef.Type.BOOLEAN, |
| 122 | + false, |
| 123 | + ConfigDef.Importance.LOW, |
| 124 | + XML_PARSER_NAMESPACE_AWARE_ENABLED_DOC, |
| 125 | + group, |
| 126 | + filterGroupCounter++, |
| 127 | + ConfigDef.Width.NONE, |
| 128 | + keyPrefix + XML_PARSER_NAMESPACE_AWARE_ENABLED_CONFIG |
| 129 | + ) |
| 130 | + .define( |
| 131 | + keyPrefix + XML_EXCLUDE_EMPTY_ELEMENTS_CONFIG, |
| 132 | + ConfigDef.Type.BOOLEAN, |
| 133 | + false, |
| 134 | + ConfigDef.Importance.LOW, |
| 135 | + XML_EXCLUDE_EMPTY_ELEMENTS_DOC, |
| 136 | + group, |
| 137 | + filterGroupCounter++, |
| 138 | + ConfigDef.Width.NONE, |
| 139 | + keyPrefix + XML_EXCLUDE_EMPTY_ELEMENTS_CONFIG |
| 140 | + ) |
| 141 | + .define( |
| 142 | + keyPrefix + XML_DATA_TYPE_INFERENCE_ENABLED_CONFIG, |
| 143 | + ConfigDef.Type.BOOLEAN, |
| 144 | + false, |
| 145 | + ConfigDef.Importance.LOW, |
| 146 | + XML_DATA_TYPE_INFERENCE_ENABLED_DOC, |
| 147 | + group, |
| 148 | + filterGroupCounter++, |
| 149 | + ConfigDef.Width.NONE, |
| 150 | + keyPrefix + XML_EXCLUDE_EMPTY_ELEMENTS_CONFIG |
| 151 | + ); |
| 152 | + |
| 153 | + for (ConfigDef.ConfigKey configKey : additional) { |
| 154 | + def.define( |
| 155 | + configKey.name, |
| 156 | + configKey.type, |
| 157 | + configKey.defaultValue, |
| 158 | + configKey.validator, |
| 159 | + configKey.importance, |
| 160 | + configKey.documentation, |
| 161 | + group, |
| 162 | + filterGroupCounter++, |
| 163 | + configKey.width, |
| 164 | + configKey.displayName |
| 165 | + ); |
| 166 | + } |
| 167 | + return def; |
| 168 | + } |
| 169 | +} |
0 commit comments