diff --git a/ballerina/tests/toXml_test.bal b/ballerina/tests/toXml_test.bal index 689260c..e48ee94 100644 --- a/ballerina/tests/toXml_test.bal +++ b/ballerina/tests/toXml_test.bal @@ -15,7 +15,6 @@ // under the License. import ballerina/test; -import ballerina/io; @test:Config { groups: ["toXml"] @@ -1192,7 +1191,7 @@ type Soap1 record { }; @test:Config { - groups: ["toXml"] + groups: ["toXml", "testFail"] } isolated function testXmlToRecordWithNamespaceAttachedToFields() returns error? { Soap1 val = { @@ -1217,7 +1216,6 @@ isolated function testXmlToRecordWithNamespaceAttachedToFields() returns error? "" + "Colombo40000" + ""; - io:println(xmlVal); test:assertEquals(xmlVal.toString(), expected); } @@ -1275,7 +1273,6 @@ isolated function testXmlToRecordWithNamespaceAttachedToFieldsAndTypes() returns "" + "Colombo40000" + ""; - io:println(xmlVal); test:assertEquals(xmlVal.toString(), expected); } @@ -1305,6 +1302,34 @@ isolated function testUnderscoreInTheFieldName() returns error? { test:assertEquals(xmlVal.toString(), ""); } +@Namespace { + uri: "example.com" +} +type File record {| + @Namespace { + uri: "example.com" + } + string fileName; + @Namespace { + uri: "example.com" + } + string fileNamespace; +|}; + +@test:Config { + groups: ["toXml"] +} +isolated function testToRecordFieldNameEndsWithNameOrNamespace() returns error? { + File file = { + fileName: "test.bal", + fileNamespace: "wso2.com" + }; + + xml result = check toXml(file); + string expected = "test.balwso2.com"; + test:assertEquals(result.toString(), expected); +} + @test:Config { groups: ["toXml"] } diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/Constants.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/Constants.java index bbf227b..04830ac 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/Constants.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/Constants.java @@ -42,6 +42,7 @@ private Constants() {} public static final ArrayType JSON_ARRAY_TYPE = TypeCreator.createArrayType(PredefinedTypes.TYPE_JSON); public static final String FIELD = "$field$."; public static final String NAMESPACE = "Namespace"; + public static final String MODULE_NAME = "ballerina/data.xmldata"; public static final BString URI = StringUtils.fromString("uri"); public static final BString PREFIX = StringUtils.fromString("prefix"); public static final String ATTRIBUTE = "Attribute"; diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java index 95e7e27..f9c3b95 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java @@ -66,8 +66,7 @@ public static QualifiedName validateAndGetXmlNameFromRecordAnnotation(RecordType BMap annotations = recordType.getAnnotations(); String localName = recordName; for (BString annotationsKey : annotations.getKeys()) { - String key = annotationsKey.getValue(); - if (!key.contains(Constants.FIELD) && key.endsWith(Constants.NAME)) { + if (isNameAnnotationKey(annotationsKey.getValue())) { String name = ((BMap) annotations.get(annotationsKey)).get(Constants.VALUE).toString(); String localPart = elementName.getLocalPart(); if (!name.equals(localPart)) { @@ -81,7 +80,7 @@ public static QualifiedName validateAndGetXmlNameFromRecordAnnotation(RecordType // Handle the namespace annotation. for (BString annotationsKey : annotations.getKeys()) { String key = annotationsKey.getValue(); - if (!key.contains(Constants.FIELD) && key.endsWith(Constants.NAMESPACE)) { + if (isNamespaceAnnotationKey(key)) { Map namespaceAnnotation = ((Map) annotations.get(StringUtils.fromString(key))); BString uri = (BString) namespaceAnnotation.get(Constants.URI); @@ -106,8 +105,7 @@ private static ArrayList getNamespace(RecordType recordType) { BMap annotations = recordType.getAnnotations(); ArrayList namespace = new ArrayList<>(); for (BString annotationsKey : annotations.getKeys()) { - String key = annotationsKey.getValue(); - if (!key.contains(Constants.FIELD) && key.endsWith(Constants.NAMESPACE)) { + if (isNamespaceAnnotationKey(annotationsKey.getValue())) { BMap namespaceAnnotation = (BMap) annotations.get(annotationsKey); namespace.add(namespaceAnnotation.containsKey(Constants.PREFIX) ? ((BString) namespaceAnnotation.get(Constants.PREFIX)).getValue() : ""); @@ -184,7 +182,7 @@ public static Map getAllAttributesInRecordType(RecordType @SuppressWarnings("unchecked") public static QualifiedName getFieldNameFromRecord(Map fieldAnnotation, String fieldName) { for (BString key : fieldAnnotation.keySet()) { - if (key.getValue().endsWith(Constants.NAMESPACE)) { + if (isNamespaceAnnotationKey(key.getValue())) { Map namespaceAnnotation = ((Map) fieldAnnotation.get(key)); BString uri = (BString) namespaceAnnotation.get(Constants.URI); BString prefix = (BString) namespaceAnnotation.get(Constants.PREFIX); @@ -198,7 +196,7 @@ public static QualifiedName getFieldNameFromRecord(Map fieldAnn @SuppressWarnings("unchecked") private static String getModifiedName(Map fieldAnnotation, String attributeName) { for (BString key : fieldAnnotation.keySet()) { - if (key.getValue().endsWith(Constants.NAME)) { + if (isNameAnnotationKey(key.getValue())) { return ((Map) fieldAnnotation.get(key)).get(Constants.VALUE).toString(); } } @@ -525,7 +523,7 @@ private static QName addFieldNamespaceAnnotation(String fieldName, String key, B if (annotations.containsKey(annotationKey)) { BMap annotationValue = (BMap) annotations.get(annotationKey); for (BString fieldKey : annotationValue.getKeys()) { - if (fieldKey.toString().endsWith(Constants.NAMESPACE)) { + if (isNamespaceAnnotationKey(fieldKey.getValue())) { return processFieldNamespaceAnnotation(annotationValue, key, fieldKey, recordValue, isAttributeField); } @@ -542,7 +540,7 @@ public static boolean isAttributeField(BString annotationKey, BMap annotationValue = (BMap) annotations.get(annotationKey); for (BString fieldKey : annotationValue.getKeys()) { - if (fieldKey.toString().endsWith(Constants.ATTRIBUTE)) { + if (isAttributeAnnotationKey(fieldKey.getValue())) { return true; } } @@ -563,7 +561,7 @@ private static BMap getFieldNamespaceAndNameAnnotations(String BMap annotationValue = (BMap) parentAnnotations.get(annotationKey); for (BString fieldKey : annotationValue.getKeys()) { String keyName = fieldKey.getValue(); - if (keyName.endsWith(Constants.NAMESPACE) || keyName.endsWith(Constants.NAME)) { + if (isNamespaceAnnotationKey(keyName) || isNameAnnotationKey(keyName)) { nsFieldAnnotation.put(fieldKey, annotationValue.get(fieldKey)); break; } @@ -694,7 +692,7 @@ private static BMap processParentAnnotation(Type type, BMap annotation, String key) { for (BString value : annotation.getKeys()) { String stringValue = value.getValue(); - if (stringValue.endsWith(Constants.NAME)) { + if (isNameAnnotationKey(stringValue)) { BMap names = (BMap) annotation.get(value); String name = names.get(StringUtils.fromString(VALUE)).toString(); if (key.contains(Constants.COLON)) { @@ -705,7 +703,7 @@ private static String processFieldAnnotation(BMap annotation, S key = name; } } - if (stringValue.endsWith(Constants.ATTRIBUTE)) { + if (isAttributeAnnotationKey(stringValue)) { key = ATTRIBUTE_PREFIX.concat(key); } } @@ -718,10 +716,10 @@ private static BString processAnnotation(BMap annotation, Strin for (BString value : annotation.getKeys()) { if (!value.getValue().contains(Constants.FIELD)) { String stringValue = value.getValue(); - if (stringValue.endsWith(Constants.NAME)) { + if (isNameAnnotationKey(stringValue)) { key = processNameAnnotation(annotation, key, value, hasNamespaceAnnotation); } - if (stringValue.endsWith(Constants.NAMESPACE)) { + if (isNamespaceAnnotationKey(stringValue)) { hasNamespaceAnnotation = true; key = processNamespaceAnnotation(annotation, key, value, namespaces); } @@ -733,7 +731,7 @@ private static BString processAnnotation(BMap annotation, Strin private static void processSubRecordAnnotation(BMap annotation, BMap subRecord) { BString[] keys = annotation.getKeys(); for (BString value : keys) { - if (value.getValue().endsWith(Constants.NAMESPACE)) { + if (isNamespaceAnnotationKey(value.getValue())) { processNamespaceAnnotation(annotation, "", value, subRecord); } } @@ -744,7 +742,7 @@ private static String getElementName(BMap annotation, String ke BString[] keys = annotation.getKeys(); boolean hasNamespaceAnnotation = false; for (BString value : keys) { - if (value.getValue().endsWith(Constants.NAMESPACE)) { + if (isNamespaceAnnotationKey(value.getValue())) { hasNamespaceAnnotation = true; BMap namespaceAnnotation = (BMap) annotation.get(value); BString prefix = (BString) namespaceAnnotation.get(Constants.PREFIX); @@ -755,7 +753,7 @@ private static String getElementName(BMap annotation, String ke } for (BString value : keys) { - if (value.getValue().endsWith(Constants.NAME)) { + if (isNameAnnotationKey(value.getValue())) { key = processNameAnnotation(annotation, key, value, hasNamespaceAnnotation); } } @@ -816,6 +814,18 @@ private static String addAttributeToRecord(BString prefix, BString uri, String k return prefix.getValue().concat(Constants.COLON).concat(key); } + private static boolean isNamespaceAnnotationKey(String key) { + return key.startsWith(Constants.MODULE_NAME) && key.endsWith(Constants.NAMESPACE); + } + + private static boolean isNameAnnotationKey(String key) { + return key.startsWith(Constants.MODULE_NAME) && key.endsWith(Constants.NAME); + } + + private static boolean isAttributeAnnotationKey(String key) { + return key.startsWith(Constants.MODULE_NAME) && key.endsWith(Constants.ATTRIBUTE); + } + /** * Holds data required for the traversing. *