Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML to JSON legacy component - changes / developments #28

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CustomXmlJsonDataFormat implements DataFormat {
static final Logger logger = Logger.getLogger(CustomXmlJsonDataFormat.class);

// XML to JSON options
private static boolean forceTopLevelObject, skipWhitespace, trimSpaces, skipNamespaces, removeNamespacePrefixes,
private boolean forceTopLevelObject, skipWhitespace, trimSpaces, skipNamespaces, removeNamespacePrefixes,
typeHints;

// XML to JSON
Expand Down Expand Up @@ -76,47 +76,47 @@ private void setContentTypeHeader(Exchange exchange, String contentType) {
}
}

public static boolean isForceTopLevelObject() {
public boolean isForceTopLevelObject() {
return forceTopLevelObject;
}

public void setForceTopLevelObject(boolean forceTopLevelObject) {
this.forceTopLevelObject = forceTopLevelObject;
}

public static boolean isSkipWhitespace() {
public boolean isSkipWhitespace() {
return skipWhitespace;
}

public void setSkipWhitespace(boolean skipWhitespace) {
this.skipWhitespace = skipWhitespace;
}

public static boolean isTrimSpaces() {
public boolean isTrimSpaces() {
return trimSpaces;
}

public void setTrimSpaces(boolean trimSpaces) {
this.trimSpaces = trimSpaces;
}

public static boolean isSkipNamespaces() {
public boolean isSkipNamespaces() {
return skipNamespaces;
}

public void setSkipNamespaces(boolean skipNamespaces) {
this.skipNamespaces = skipNamespaces;
}

public static boolean isRemoveNamespacePrefixes() {
public boolean isRemoveNamespacePrefixes() {
return removeNamespacePrefixes;
}

public void setRemoveNamespacePrefixes(boolean removeNamespacePrefixes) {
this.removeNamespacePrefixes = removeNamespacePrefixes;
}

public static boolean isTypeHints() {
public boolean isTypeHints() {
return typeHints;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public JsonNode convertXmlToJson(
String classAttr = element.getAttribute(JSON_XML_ATTR_CLASS);
String typeAttr = element.getAttribute(JSON_XML_ATTR_TYPE);

boolean hasAttributes = element.hasAttributes();
boolean isRootNode = (level == 0);
boolean isRootArray = isRootArray(level, numberOfChildren, numberOfSiblings, parentSiblings, classAttr, parentClass);
boolean isObject = isObject(level, classAttr);
boolean isRootArray = isRootArray(level, numberOfChildren, numberOfSiblings, parentSiblings, classAttr, parentClass, hasAttributes);
boolean isObject = isObject(level, numberOfChildren, classAttr);
boolean isOneValue = isOneValue(level, numberOfSiblings, parentClass);
boolean isSingleChildren = isSingleChildren(level, numberOfChildren, classAttr);
boolean isAttributeObject = isAttributeObject(level, parentClass);
Expand Down Expand Up @@ -130,11 +131,18 @@ private JsonNode processElementNode(
isFirstSibling
);
} else {
extractChildAsOtherInArrayNode(
level, rootArrayNode, numberOfSiblings, classAttr, (Element) childNode, childElement,
isFirstSibling
);
rootObjectNode.set(((Element) childNode).getTagName(), rootArrayNode);
if(numberOfChildren == 1) {
extractChildAsOtherInObjectNode(
level, rootObjectNode, numberOfSiblings, classAttr, (Element) childNode, childElement,
isFirstSibling
);
} else {
extractChildAsOtherInArrayNode(
level, rootArrayNode, numberOfSiblings, classAttr, (Element) childNode, childElement,
isFirstSibling
);
rootObjectNode.set(((Element) childNode).getTagName(), rootArrayNode);
}
}
} else {
// extract child as other type and add into the object node
Expand Down Expand Up @@ -172,7 +180,11 @@ private JsonNode processTextNode(
if(xmlJsonDataFormat.isTypeHints()) {
rootArrayNode.add(textNode.getTextContent());
} else {
rootObjectNode.put(JSON_XML_TEXT_FIELD, childNode.getTextContent());
if(element.hasAttributes()) {
rootObjectNode.put(JSON_XML_TEXT_FIELD, childNode.getTextContent());
} else {
rootArrayNode.add(textNode.getTextContent());
}
}
} else {
//process text node identified as other
Expand All @@ -198,7 +210,8 @@ private void addAttributesInObjectNode(Element element, ObjectNode rootObjectNod

// check if it's a root array
private boolean isRootArray(
int level, int numberOfChildren, int numberOfSiblings, int parentSiblings, String classAttr, String parentClass
int level, int numberOfChildren, int numberOfSiblings, int parentSiblings, String classAttr,
String parentClass, boolean hasAttributes
) {
boolean isRootArray = false;
if(xmlJsonDataFormat.isTypeHints()) {
Expand All @@ -208,10 +221,14 @@ private boolean isRootArray(
if (level == 1) {
isRootArray = true;
}
if (level == 2 && parentClass != null && parentClass.equalsIgnoreCase(JSON_XML_ATTR_CLASS_ARRAY)) {
if (level == 2 && parentClass != null &&
(parentClass.equals("") || parentClass.equalsIgnoreCase(JSON_XML_ATTR_CLASS_ARRAY))
) {
isRootArray = true;
}
if (level == 2 && classAttr != null && classAttr.equalsIgnoreCase(JSON_XML_ATTR_CLASS_OBJECT)) {
if (level == 2 && classAttr != null &&
((classAttr.equals("") && numberOfChildren > 1) || classAttr.equalsIgnoreCase(JSON_XML_ATTR_CLASS_OBJECT))
) {
if (parentSiblings > 1) {
isRootArray = true;
} else {
Expand All @@ -227,7 +244,12 @@ private boolean isRootArray(
if(level == 0 && numberOfChildren == 1) {
isRootArray = true;
}
if(level == 2 && parentClass!=null && parentClass.equalsIgnoreCase(JSON_XML_ATTR_CLASS_ARRAY)) {
if (level == 1 && classAttr!=null && classAttr.equals("")) {
isRootArray = true;
}
if(level == 2 && parentClass!=null &&
(parentClass.equals("") || parentClass.equalsIgnoreCase(JSON_XML_ATTR_CLASS_ARRAY))
) {
isRootArray = true;
}
if(level == 2 && classAttr!=null && classAttr.equalsIgnoreCase(JSON_XML_ATTR_CLASS_OBJECT)) {
Expand All @@ -237,14 +259,19 @@ private boolean isRootArray(
isRootArray = false;
}
}
if (level == 3 && numberOfSiblings == 1 && !hasAttributes) {
isRootArray = true;
}
}
return isRootArray;
}

// check if it's an object
private boolean isObject(int level, String classAttr) {
private boolean isObject(int level, int numberOfChildren, String classAttr) {
boolean isObject = false;
if(level == 2 && classAttr!=null && classAttr.equalsIgnoreCase(JSON_XML_ATTR_CLASS_OBJECT)) {
if(level == 2 && classAttr!=null &&
((classAttr.equals("") && numberOfChildren > 1) || classAttr.equalsIgnoreCase(JSON_XML_ATTR_CLASS_OBJECT))
) {
isObject = true;
}
return isObject;
Expand Down Expand Up @@ -341,9 +368,13 @@ private void extractChildAsObject(
});
}
} else {
rootObjectNode.set(
childElement.getTagName(), addNodeWithAttributeInfo(childElement, childElement.getTextContent())
);
if(classAttr!=null && !classAttr.equals("")) {
rootObjectNode.set(
childElement.getTagName(), addNodeWithAttributeInfo(childElement, childElement.getTextContent())
);
} else {
rootObjectNode.put(childElement.getTagName(), childElement.getTextContent());
}
}
}

Expand Down
Loading