diff --git a/ballerina/xml_api.bal b/ballerina/xml_api.bal index e2e1d17..4491e9e 100644 --- a/ballerina/xml_api.bal +++ b/ballerina/xml_api.bal @@ -36,7 +36,7 @@ public type ElementConfig record {| |}; # Annotation to define schema rules for an XML element in Ballerina. -public const annotation ElementConfig Element on type, record field; +public const annotation ElementConfig Element on record field; # Defines the configuration for an XML sequence in the XML schema (XSD). public type SequenceConfig record {| @@ -44,7 +44,7 @@ public type SequenceConfig record {| |}; # Annotation to define schema rules for an XML sequence in Ballerina. -public const annotation SequenceConfig Sequence on type, record field; +public const annotation SequenceConfig Sequence on record field; # Defines the configuration for an XML choice in the XML schema (XSD). public type ChoiceConfig record {| @@ -52,7 +52,7 @@ public type ChoiceConfig record {| |}; # Annotation to define schema rules for an XML choice in Ballerina. -public const annotation ChoiceConfig Choice on type, record field; +public const annotation ChoiceConfig Choice on record field; # Defines the configuration for the sequence order in the XML schema (XSD). public type SequenceOrderConfig record {| @@ -61,7 +61,7 @@ public type SequenceOrderConfig record {| |}; # Annotation to define schema rules for the sequence order in Ballerina. -public const annotation SequenceOrderConfig SequenceOrder on type, record field; +public const annotation SequenceOrderConfig SequenceOrder on record field; # Defines the name of the XML element. public type NameConfig record {| @@ -470,14 +470,15 @@ isolated function addNamespaces(map allNamespaces, map namespace } } -# Validates an XML document against a provided XML schema. +# Validates an XML document against an XML schema. # # The schema can either be a file path to the `.xsd` file or a Ballerina record type that represents -# the XSD structure. The function checks if the `xmlValue` conforms to the provided schema. +# the schema defined by the XSD. The function checks if the XML value conforms to the specified schema. # -# + schema - A `string` representing the file path to the `.xsd` file or a Ballerina record type representing the XSD. +# + schema - A string representing the file path to the `.xsd` file or a +# Ballerina record type representing the schema defined by the XSD. # + xmlValue - The XML document that needs to be validated against the schema. -# + return - Returns `()` if the XML is valid according to the schema, otherwise returns `Error`. +# + return - Returns `()` if the XML value is valid according to the schema, otherwise returns `Error`. public function validate(xml xmlValue, string|typedesc schema) returns Error? = @java:Method {'class: "io.ballerina.lib.data.xmldata.xml.Native"} external; diff --git a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java index a174bfc..7e12240 100644 --- a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java +++ b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java @@ -27,6 +27,8 @@ import java.util.List; import java.util.stream.Collectors; +import static io.ballerina.lib.data.xmldata.compiler.CompilerPluginTestUtils.getErrorMessage; + /** * This class includes tests for Ballerina Xmldata compiler plugin. */ @@ -39,7 +41,7 @@ public void testDuplicateFieldNegative1() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 1); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "invalid field: duplicate field found"); } @@ -51,7 +53,7 @@ public void testDuplicateFieldNegative2() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 1); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "invalid field: duplicate field found"); } @@ -63,7 +65,7 @@ public void testChildRecordWithNameAnnotNegative() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.WARNING)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 1); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "invalid annotation attachment: child record does not allow name annotation"); } @@ -75,13 +77,13 @@ public void testDuplicateFieldInInlineRecordsNegative() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 4); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "invalid field: duplicate field found"); - Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1), "invalid field: duplicate field found"); - Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2), "invalid field: duplicate field found"); - Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3), "invalid field: duplicate field found"); } @@ -93,17 +95,17 @@ public void testUnionTypeNegative() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 6); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "invalid type: expected a record type"); - Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1), "invalid field: duplicate field found"); - Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2), "invalid field: duplicate field found"); - Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3), "invalid type: expected a record type"); - Assert.assertEquals(errorDiagnosticsList.get(4).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 4), "invalid field: duplicate field found"); - Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 5), "invalid field: duplicate field found"); } @@ -115,7 +117,7 @@ public void testCompilerPluginWithAProjectWithSubModule() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 1); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "invalid field: duplicate field found"); List warningDiagnosticsList = diagnosticResult.diagnostics().stream() @@ -134,53 +136,53 @@ public void testCompilerPluginWithXsdAnnotation() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 24); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(4).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 4), "Invalid sequence member: Sequence members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 5), "Invalid sequence member: Sequence members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(6).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 6), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(7).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 7), "Invalid sequence member: Sequence members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(8).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 8), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(9).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 9), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(10).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 10), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(11).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 11), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(12).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 12), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(13).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 13), "Invalid choice member: Choice members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(14).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 14), "Invalid choice member: Choice members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(15).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 15), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(16).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 16), "Invalid choice member: Choice members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(17).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 17), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(18).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 18), "A record field cannot contains sequence/choice/element/attribute annotations simultaneously"); - Assert.assertEquals(errorDiagnosticsList.get(19).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 19), "A record field cannot contains sequence/choice/element/attribute annotations simultaneously"); - Assert.assertEquals(errorDiagnosticsList.get(20).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 20), "A record field cannot contains sequence/choice/element/attribute annotations simultaneously"); - Assert.assertEquals(errorDiagnosticsList.get(20).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 21), "A record field cannot contains sequence/choice/element/attribute annotations simultaneously"); - Assert.assertEquals(errorDiagnosticsList.get(13).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 22), "Invalid choice member: Choice members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(20).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 23), "A record field cannot contains sequence/choice/element/attribute annotations simultaneously"); } @@ -192,17 +194,17 @@ public void testCompilerPluginWithXsdAnnotation2() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 6); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(4).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 4), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 5), "invalid xsd annotation: record type or record array type expected"); } @@ -214,23 +216,23 @@ public void testCompilerPluginWithXsdAnnotation3() { .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) .collect(Collectors.toList()); Assert.assertEquals(errorDiagnosticsList.size(), 9); - Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0), "Invalid sequence member: Order should be defined in in all fields"); - Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1), "Invalid sequence member: Sequence members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2), "Invalid sequence member: Order should be defined in in all fields"); - Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3), "Invalid sequence member: Sequence members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(4).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 4), "Invalid sequence member: Order should be defined in in all fields"); - Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 5), "Invalid sequence member: Order should be defined in in all fields"); - Assert.assertEquals(errorDiagnosticsList.get(6).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 6), "Invalid choice member: Choice members should be defined in a closed record"); - Assert.assertEquals(errorDiagnosticsList.get(7).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 7), "invalid xsd annotation: record type or record array type expected"); - Assert.assertEquals(errorDiagnosticsList.get(8).diagnosticInfo().messageFormat(), + Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 8), "Invalid choice member: Choice members should be defined in a closed record"); } } diff --git a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTestUtils.java b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTestUtils.java index b54f3a3..280959b 100644 --- a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTestUtils.java +++ b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTestUtils.java @@ -23,9 +23,11 @@ import io.ballerina.projects.directory.BuildProject; import io.ballerina.projects.environment.Environment; import io.ballerina.projects.environment.EnvironmentBuilder; +import io.ballerina.tools.diagnostics.Diagnostic; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; /** * Utility functions related to compiler plugins tests. @@ -43,4 +45,8 @@ static Package loadPackage(String path) { BuildProject project = BuildProject.load(projectEnvironmentBuilder, projectDirPath); return project.currentPackage(); } + + static String getErrorMessage(List errorDiagnosticsList, int index) { + return errorDiagnosticsList.get(index).diagnosticInfo().messageFormat(); + } } 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 e85b5ae..5615006 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 @@ -525,14 +525,14 @@ private static BMap addFields(BMap input, Type static BString[] getOrderedRecordKeysIfXsdSequencePresent(BMap input, HashMap xsdSequencePriorityOrder) { HashMap localPartKeys = getLocalPartKeys(input); - if (!xsdSequencePriorityOrder.isEmpty()) { + if (xsdSequencePriorityOrder.isEmpty()) { + return input.getKeys(); + } else { return xsdSequencePriorityOrder.entrySet().stream() .filter(entry -> localPartKeys.containsKey(entry.getKey())) .sorted(Comparator.comparingInt(Map.Entry::getValue)) .map(entry -> StringUtils.fromString(localPartKeys.get(entry.getKey()))) .toArray(BString[]::new); - } else { - return input.getKeys(); } } @@ -1144,19 +1144,22 @@ public static HashMap getXsdSequencePriorityOrder(Type type, bo BMap annotations = fieldType.getAnnotations(); for (BString annotationKey : annotations.getKeys()) { String key = annotationKey.getValue(); - if (key.contains(Constants.FIELD)) { - String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); - Map fieldAnnotation = (Map) annotations.get(annotationKey); - for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { - String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); - if (fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { - if (fieldAnnotationKeyStr.endsWith(Constants.ORDER)) { - BMap fieldAnnotationValue = - (BMap) fieldAnnotation.get(fieldAnnotationKey); - elementPriorityOrder.put(fieldName, - fieldAnnotationValue.getIntValue(Constants.VALUE).intValue()); - } - } + if (!key.contains(Constants.FIELD)) { + continue; + } + + String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); + Map fieldAnnotation = (Map) annotations.get(annotationKey); + for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { + String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); + if (!fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { + continue; + } + if (fieldAnnotationKeyStr.endsWith(Constants.ORDER)) { + BMap fieldAnnotationValue = + (BMap) fieldAnnotation.get(fieldAnnotationKey); + elementPriorityOrder.put(fieldName, + fieldAnnotationValue.getIntValue(Constants.VALUE).intValue()); } } } @@ -1169,30 +1172,33 @@ public static HashMap getFieldNamesWithModelGroupAnnotat BMap annotations = fieldType.getAnnotations(); for (BString annotationKey : annotations.getKeys()) { String key = annotationKey.getValue(); - if (key.contains(Constants.FIELD)) { - String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); - if (elementNamesMap.containsKey(fieldName)) { - fieldName = elementNamesMap.get(fieldName); + if (!key.contains(Constants.FIELD)) { + continue; + } + + String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); + if (elementNamesMap.containsKey(fieldName)) { + fieldName = elementNamesMap.get(fieldName); + } + + Map fieldAnnotation = (Map) annotations.get(annotationKey); + for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { + String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); + if (!fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { + continue; + } + if (fieldAnnotationKeyStr.endsWith(Constants.SEQUENCE)) { + SequenceInfo sequenceInfo = new SequenceInfo(fieldName, + (BMap) fieldAnnotation.get(fieldAnnotationKey), + fieldType, null); + fieldNamesWithModelGroupAnnotations.put(fieldName, sequenceInfo); } - Map fieldAnnotation = (Map) annotations.get(annotationKey); - for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { - String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); - if (fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { - if (fieldAnnotationKeyStr.endsWith(Constants.SEQUENCE)) { - SequenceInfo sequenceInfo = new SequenceInfo(fieldName, - (BMap) fieldAnnotation.get(fieldAnnotationKey), - fieldType, null); - fieldNamesWithModelGroupAnnotations.put(fieldName, sequenceInfo); - } - - if (fieldAnnotationKeyStr.endsWith(Constants.CHOICE)) { - ChoiceInfo choiceInfo = new ChoiceInfo(fieldName, - (BMap) fieldAnnotation.get(fieldAnnotationKey), - fieldType, null); - fieldNamesWithModelGroupAnnotations.put(fieldName, choiceInfo); - } - } + if (fieldAnnotationKeyStr.endsWith(Constants.CHOICE)) { + ChoiceInfo choiceInfo = new ChoiceInfo(fieldName, + (BMap) fieldAnnotation.get(fieldAnnotationKey), + fieldType, null); + fieldNamesWithModelGroupAnnotations.put(fieldName, choiceInfo); } } } @@ -1205,22 +1211,26 @@ public static HashMap getFieldNamesWithElementGroupAnnotati BMap annotations = fieldType.getAnnotations(); for (BString annotationKey : annotations.getKeys()) { String key = annotationKey.getValue(); - if (key.contains(Constants.FIELD)) { - String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); - if (elementNamesMap.containsKey(fieldName)) { - fieldName = elementNamesMap.get(fieldName); + if (!key.contains(Constants.FIELD)) { + continue; + } + + String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); + if (elementNamesMap.containsKey(fieldName)) { + fieldName = elementNamesMap.get(fieldName); + } + + Map fieldAnnotation = (Map) annotations.get(annotationKey); + for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { + String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); + if (!fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { + continue; } - Map fieldAnnotation = (Map) annotations.get(annotationKey); - for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { - String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); - if (fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { - if (fieldAnnotationKeyStr.endsWith(Constants.ELEMENT)) { - ElementInfo elementInfo = new ElementInfo(xmlElementName, fieldName, - (BMap) fieldAnnotation.get(fieldAnnotationKey)); - fieldNamesWithElementInfoAnnotations.put(fieldName, elementInfo); - } - } + if (fieldAnnotationKeyStr.endsWith(Constants.ELEMENT)) { + ElementInfo elementInfo = new ElementInfo(xmlElementName, fieldName, + (BMap) fieldAnnotation.get(fieldAnnotationKey)); + fieldNamesWithElementInfoAnnotations.put(fieldName, elementInfo); } } } @@ -1234,19 +1244,23 @@ public static ArrayList getFieldNamesWithSequenceAnnotations(RecordType BMap annotations = fieldType.getAnnotations(); for (BString annotationKey : annotations.getKeys()) { String key = annotationKey.getValue(); - if (key.contains(Constants.FIELD)) { - String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); - Map fieldAnnotation = (Map) annotations.get(annotationKey); - for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { - String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); - if (fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { - if (fieldAnnotationKeyStr.endsWith(Constants.SEQUENCE)) { - if (elementNamesMap.containsKey(fieldName)) { - fieldNamesWithModelGroupAnnotations.add(elementNamesMap.get(fieldName)); - } else { - fieldNamesWithModelGroupAnnotations.add(fieldName); - } - } + if (!key.contains(Constants.FIELD)) { + continue; + } + + String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); + Map fieldAnnotation = (Map) annotations.get(annotationKey); + for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { + String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); + if (!fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { + continue; + } + + if (fieldAnnotationKeyStr.endsWith(Constants.SEQUENCE)) { + if (elementNamesMap.containsKey(fieldName)) { + fieldNamesWithModelGroupAnnotations.add(elementNamesMap.get(fieldName)); + } else { + fieldNamesWithModelGroupAnnotations.add(fieldName); } } } @@ -1260,20 +1274,23 @@ public static HashMap getElementNameMap(Type type) { BMap annotations = recordType.getAnnotations(); for (BString annotationKey : annotations.getKeys()) { String key = annotationKey.getValue(); - if (key.contains(Constants.FIELD)) { - String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); - Map fieldAnnotation = (Map) annotations.get(annotationKey); - for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { - String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); - if (fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { - if (fieldAnnotationKeyStr.endsWith(Constants.NAME)) { - BMap fieldAnnotationValue = - (BMap) fieldAnnotation.get(fieldAnnotationKey); - String xmlElementName = StringUtils.getStringValue(fieldAnnotationValue - .getStringValue(Constants.VALUE)); - names.put(xmlElementName, fieldName); - } - } + if (!key.contains(Constants.FIELD)) { + continue; + } + String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); + Map fieldAnnotation = (Map) annotations.get(annotationKey); + for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { + String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); + if (!fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { + continue; + } + + if (fieldAnnotationKeyStr.endsWith(Constants.NAME)) { + BMap fieldAnnotationValue = + (BMap) fieldAnnotation.get(fieldAnnotationKey); + String xmlElementName = StringUtils.getStringValue(fieldAnnotationValue + .getStringValue(Constants.VALUE)); + names.put(xmlElementName, fieldName); } } } @@ -1282,36 +1299,6 @@ public static HashMap getElementNameMap(Type type) { return names; } -// public static BMap getXmlElementModelGroupMap(BTypedesc typed) { -// Type type = TypeUtils.getReferredType(typed.getDescribingType()); -// if (type.getTag() != TypeTags.RECORD_TYPE_TAG) { -// return null; -// } -// -// BMap xmlModelGroupMap = ValueCreator -// .createMapValue(TypeCreator.createMapType(PredefinedTypes.TYPE_BOOLEAN)); -// RecordType recordType = (RecordType) type; -// BMap annotations = recordType.getAnnotations(); -// for (BString annotationKey : annotations.getKeys()) { -// String key = annotationKey.getValue(); -// if (key.contains(Constants.FIELD)) { -// String fieldName = key.split(Constants.FIELD_REGEX)[1].replaceAll("\\\\", ""); -// Map fieldAnnotation = (Map) annotations.get(annotationKey); -// for (BString fieldAnnotationKey : fieldAnnotation.keySet()) { -// String fieldAnnotationKeyStr = fieldAnnotationKey.getValue(); -// if (fieldAnnotationKeyStr.startsWith(Constants.MODULE_NAME)) { -// if (fieldAnnotationKeyStr.endsWith(Constants.SEQUENCE) -// || fieldAnnotationKeyStr.endsWith(Constants.CHOICE)) { -// xmlModelGroupMap.put(StringUtils.fromString(fieldName), true); -// } else { -// xmlModelGroupMap.put(StringUtils.fromString(fieldName), false); -// } -// } -// } -// } -// } -// return xmlModelGroupMap; -// } /** * Holds data required for the processing. * diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/xml/Native.java b/native/src/main/java/io/ballerina/lib/data/xmldata/xml/Native.java index bbcf00a..b363a8c 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/xml/Native.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/xml/Native.java @@ -84,7 +84,7 @@ public static Object parseStream(Environment env, BStream xml, BMap