Skip to content

Commit

Permalink
Merge pull request ballerina-platform#10 from prakanth97/fix_same_name
Browse files Browse the repository at this point in the history
Support same element and attribute names with different namespaces
  • Loading branch information
prakanth97 authored Nov 28, 2023
2 parents 17ed1e8 + 8d3ff90 commit ba52683
Show file tree
Hide file tree
Showing 12 changed files with 1,187 additions and 374 deletions.
416 changes: 391 additions & 25 deletions ballerina/tests/fromXml_test.bal

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@

import io.ballerina.runtime.api.PredefinedTypes;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.flags.SymbolFlags;
import io.ballerina.runtime.api.types.ArrayType;
import io.ballerina.runtime.api.types.Field;
import io.ballerina.runtime.api.types.MapType;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.api.values.BXmlItem;
import io.ballerina.stdlib.data.xml.QualifiedName;

/**
* Constants used in Ballerina XmlData library.
*
* @since 0.1.0
* @since 0.0.1
*/
public class Constants {

Expand All @@ -44,16 +43,19 @@ private Constants() {}
public static final ArrayType ANYDATA_ARRAY_TYPE = TypeCreator.createArrayType(PredefinedTypes.TYPE_ANYDATA);
public static final MapType ANYDATA_MAP_TYPE = TypeCreator.createMapType(PredefinedTypes.TYPE_ANYDATA);
public static final String FIELD = "$field$.";
public static final String NAME_SPACE = "Namespace";
public static final String NAMESPACE = "Namespace";
public static final BString URI = StringUtils.fromString("uri");
public static final BString PREFIX = StringUtils.fromString("prefix");
public static final String ATTRIBUTE = "Attribute";
public static final int DEFAULT_TYPE_FLAG = 2049;
public static final String NAME = "Name";
public static final BString VALUE = StringUtils.fromString("value");
public static final String CONTENT = "#content";
public static final QualifiedName CONTENT_QNAME = new QualifiedName("", CONTENT, "");
public static final String XMLNS = "xmlns";
public static final Field CONTENT_FIELD =
TypeCreator.createField(PredefinedTypes.TYPE_ANYDATA, Constants.CONTENT, SymbolFlags.REQUIRED);
public static final String FIELD_REGEX = "\\$field\\$\\.";
public static final int NS_PREFIX_BEGIN_INDEX = BXmlItem.XMLNS_NS_URI_PREFIX.length();
public static final String RECORD = "record";
public static final String RECORD_OR_MAP = "record or map";
public static final String ANON_TYPE = "$anonType$";
}
240 changes: 151 additions & 89 deletions native/src/main/java/io/ballerina/stdlib/data/utils/DataUtils.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.stdlib.data.utils;

/**
* Represents a diagnostic error code.
*
* @since 0.0.1
*/
public enum DiagnosticErrorCode {

UNSUPPORTED_TYPE("BDE_0001", "unsupported.type"),
XML_ROOT_MISSING("BDE_0002", "xml.root.missing"),
INVALID_REST_TYPE("BDE_0003", "invalid.rest.type"),
ARRAY_SIZE_MISMATCH("BDE_0004", "array.size.mismatch"),
REQUIRED_FIELD_NOT_PRESENT("BDE_0005", "required.field.not.present"),
REQUIRED_ATTRIBUTE_NOT_PRESENT("BDE_0006", "required.attribute.not.present"),
DUPLICATE_FIELD("BDE_0007", "duplicate.field"),
FOUND_ARRAY_FOR_NON_ARRAY_TYPE("BDE_0008", "found.array.for.non.array.type"),
EXPECTED_ANYDATA_OR_JSON("BDE_0009", "expected.anydata.or.json"),
NAMESPACE_MISMATCH("BDE_0010", "namespace.mismatch"),
TYPE_NAME_MISMATCH_WITH_XML_ELEMENT("BDE_0011", "type.name.mismatch.with.xml.element");

String diagnosticId;
String messageKey;

DiagnosticErrorCode(String diagnosticId, String messageKey) {
this.diagnosticId = diagnosticId;
this.messageKey = messageKey;
}

public String messageKey() {
return messageKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.stdlib.data.utils;

import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BError;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
* Diagnostic log for data module.
*
* @since 0.0.1
*/
public class DiagnosticLog {
private static final String ERROR_PREFIX = "error";
private static final String ERROR = "ConversionError";
private static final ResourceBundle MESSAGES = ResourceBundle.getBundle("error", Locale.getDefault());

public static BError error(DiagnosticErrorCode code, Object... args) {
String msg = formatMessage(code, args);
return getXmlError(msg);
}

private static String formatMessage(DiagnosticErrorCode code, Object[] args) {
String msgKey = MESSAGES.getString(ERROR_PREFIX + "." + code.messageKey());
return MessageFormat.format(msgKey, args);
}

public static BError getXmlError(String message) {
return ErrorCreator.createError(ModuleUtils.getModule(), ERROR, StringUtils.fromString(message),
null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* This class will hold module related utility functions.
*
* @since 0.1.0
* @since 0.0.1
*/
public class ModuleUtils {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* Java Input Stream based on Ballerina byte block stream. <code>stream<byte[], error?></code>
*
* @since 0.1.0
* @since 0.0.1
*/
public class BallerinaByteBlockInputStream extends InputStream {

Expand Down
5 changes: 3 additions & 2 deletions native/src/main/java/io/ballerina/stdlib/data/xml/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.runtime.api.values.BXml;
import io.ballerina.stdlib.data.utils.DataUtils;
import io.ballerina.stdlib.data.utils.DiagnosticLog;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
Expand All @@ -39,7 +40,7 @@
/**
* Xml conversion.
*
* @since 0.1.0
* @since 0.0.1
*/
public class Native {

Expand All @@ -50,7 +51,7 @@ public static Object fromXmlWithType(BXml xml, BMap<BString, Object> options, BT
try {
return XmlTraversal.traverse(xml, typed.getDescribingType());
} catch (Exception e) {
return DataUtils.getXmlError(e.getMessage());
return DiagnosticLog.getXmlError(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.stdlib.data.xml;

/**
* Represents a qualified name.
*
* @since 0.0.1
*/
public class QualifiedName {
public static final String NS_ANNOT_NOT_DEFINED = "$$ns_annot_not_defined$$";
private String localPart;
private String namespaceURI;
private String prefix;

public QualifiedName(String namespaceURI, String localPart, String prefix) {
this.localPart = localPart;
this.namespaceURI = namespaceURI;
this.prefix = prefix;
}

public QualifiedName(String localPart) {
this.localPart = localPart;
this.namespaceURI = "";
this.prefix = "";
}

public String getLocalPart() {
return localPart;
}

public void setLocalPart(String localPart) {
this.localPart = localPart;
}

public String getNamespaceURI() {
return namespaceURI;
}

public String getPrefix() {
return prefix;
}

@Override
public int hashCode() {
return localPart.hashCode();
}

@Override
public boolean equals(Object objectToTest) {
if (objectToTest == this) {
return true;
}

if (objectToTest == null || !(objectToTest instanceof QualifiedName)) {
return false;
}

QualifiedName qName = (QualifiedName) objectToTest;

if (qName.namespaceURI.equals(NS_ANNOT_NOT_DEFINED) || namespaceURI.equals(NS_ANNOT_NOT_DEFINED)) {
return localPart.equals(qName.localPart);
}
return localPart.equals(qName.localPart) && namespaceURI.equals(qName.namespaceURI) &&
prefix.equals(qName.prefix);
}
}
Loading

0 comments on commit ba52683

Please sign in to comment.