Skip to content

Commit 8f648c8

Browse files
committed
feat(dataformat): add new config props to exclude all XML attributes (#174)
Resolves: #174
1 parent 355b6e4 commit 8f648c8

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

connect-file-pulse-dataformat/src/main/java/io/streamthoughts/kafka/connect/filepulse/xml/XMLCommonConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class XMLCommonConfig extends AbstractConfig {
4343
public static final String XML_EXCLUDE_EMPTY_ELEMENTS_CONFIG = "xml.exclude.empty.elements";
4444
private static final String XML_EXCLUDE_EMPTY_ELEMENTS_DOC = "Specifies that the reader should exclude element having no field (default: false).";
4545

46+
public static final String XML_EXCLUDE_NODE_ATTRIBUTES_CONFIG = "xml.exclude.node.attributes";
47+
private static final String XML_EXCLUDE_NODE_ATTRIBUTES_DOC = "Specifies that the reader should exclude node attributes (default: false).";
48+
4649
public static final String XML_DATA_TYPE_INFERENCE_ENABLED_CONFIG = "xml.data.type.inference.enabled";
4750
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).";
4851

@@ -76,6 +79,10 @@ public boolean isEmptyElementExcluded() {
7679
return getBoolean(withKeyPrefix(XML_EXCLUDE_EMPTY_ELEMENTS_CONFIG));
7780
}
7881

82+
public boolean isNodeAttributesExcluded() {
83+
return getBoolean(withKeyPrefix(XML_EXCLUDE_NODE_ATTRIBUTES_CONFIG));
84+
}
85+
7986
public boolean isDataTypeInferenceEnabled() {
8087
return getBoolean(withKeyPrefix(XML_DATA_TYPE_INFERENCE_ENABLED_CONFIG));
8188
}
@@ -138,6 +145,17 @@ public static ConfigDef buildConfigDefWith(final String group,
138145
ConfigDef.Width.NONE,
139146
keyPrefix + XML_EXCLUDE_EMPTY_ELEMENTS_CONFIG
140147
)
148+
.define(
149+
keyPrefix + XML_EXCLUDE_NODE_ATTRIBUTES_CONFIG,
150+
ConfigDef.Type.BOOLEAN,
151+
false,
152+
ConfigDef.Importance.LOW,
153+
XML_EXCLUDE_NODE_ATTRIBUTES_DOC,
154+
group,
155+
filterGroupCounter++,
156+
ConfigDef.Width.NONE,
157+
keyPrefix + XML_EXCLUDE_NODE_ATTRIBUTES_CONFIG
158+
)
141159
.define(
142160
keyPrefix + XML_DATA_TYPE_INFERENCE_ENABLED_CONFIG,
143161
ConfigDef.Type.BOOLEAN,

connect-file-pulse-dataformat/src/main/java/io/streamthoughts/kafka/connect/filepulse/xml/XMLNodeToStructConverter.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public final class XMLNodeToStructConverter implements Function<Node, TypedStruc
5858

5959
private boolean excludeEmptyElement = false;
6060

61+
private boolean excludeNodeAttributes = false;
62+
6163
private boolean isTypeInferenceEnabled = false;
6264

6365
private FieldPaths forceArrayFields = FieldPaths.empty();
@@ -67,6 +69,12 @@ public XMLNodeToStructConverter setExcludeEmptyElement(boolean excludeEmptyEleme
6769
return this;
6870
}
6971

72+
public XMLNodeToStructConverter setExcludeNodeAttributes(boolean excludeNodeAttributes) {
73+
this.excludeNodeAttributes = excludeNodeAttributes;
74+
return this;
75+
}
76+
77+
7078
public XMLNodeToStructConverter setForceArrayFields(final FieldPaths forceArrayFields) {
7179
this.forceArrayFields = forceArrayFields;
7280
return this;
@@ -149,7 +157,7 @@ private TypedValue toTypedValue(final String text) {
149157
return isTypeInferenceEnabled ? TypedValue.parse(text) : TypedValue.string(text);
150158
}
151159

152-
private static Optional<TypedValue> readTextNodeValue(final Node node, final TypedValue data) {
160+
private Optional<TypedValue> readTextNodeValue(final Node node, final TypedValue data) {
153161
// Check if TextNode as no attribute
154162
final NamedNodeMap attributes = node.getAttributes();
155163
if (attributes == null || attributes.getLength() == 0) {
@@ -240,7 +248,7 @@ private static List<Node> collectAllNotNewLineNodes(final NodeList nodes) {
240248

241249
private static boolean isTextNode(final Node n) {
242250
return isNodeOfType(n, Node.TEXT_NODE) ||
243-
isNodeOfType(n, Node.CDATA_SECTION_NODE);
251+
isNodeOfType(n, Node.CDATA_SECTION_NODE);
244252
}
245253

246254
private static boolean isElementNode(final Node n) {
@@ -251,9 +259,9 @@ private static boolean isNodeOfType(final Node node, int textNode) {
251259
return node.getNodeType() == textNode;
252260
}
253261

254-
private static void addAllNodeAttributes(final TypedStruct struct,
255-
final NamedNodeMap attributes) {
256-
if (attributes == null) return;
262+
private void addAllNodeAttributes(final TypedStruct struct,
263+
final NamedNodeMap attributes) {
264+
if (excludeNodeAttributes || attributes == null) return;
257265

258266
for (int i = 0; i < attributes.getLength(); i++) {
259267
Attr attr = (Attr) attributes.item(i);

connect-file-pulse-filesystems/filepulse-commons-fs/src/main/java/io/streamthoughts/kafka/connect/filepulse/xml/XMLFileInputIterator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public XMLFileInputIterator(final XMLFileInputReaderConfig config,
8282

8383
this.converter = new XMLNodeToStructConverter()
8484
.setExcludeEmptyElement(config.isEmptyElementExcluded())
85+
.setExcludeNodeAttributes(config.isNodeAttributesExcluded())
8586
.setForceArrayFields(FieldPaths.from(config.forceArrayFields()))
8687
.setTypeInferenceEnabled(config.isDataTypeInferenceEnabled());
8788

connect-file-pulse-filters/src/main/java/io/streamthoughts/kafka/connect/filepulse/filter/XmlToStructFilter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public void configure(final Map<String, ?> configs) {
7272
var filterConfig = new XmlToStructFilterConfig(configs);
7373
this.converter = new XMLNodeToStructConverter()
7474
.setExcludeEmptyElement(filterConfig.isEmptyElementExcluded())
75+
.setExcludeNodeAttributes(filterConfig.isNodeAttributesExcluded())
7576
.setForceArrayFields(FieldPaths.from(filterConfig.forceArrayFields()))
7677
.setTypeInferenceEnabled(filterConfig.isDataTypeInferenceEnabled());
7778

0 commit comments

Comments
 (0)