use of org.apache.axiom.om.impl.llom.OMAttributeImpl in project ballerina by ballerina-lang.
the class BXMLItem method setAttribute.
/**
* {@inheritDoc}
*/
@Override
public void setAttribute(String localName, String namespaceUri, String prefix, String value) {
if (nodeType != XMLNodeType.ELEMENT) {
return;
}
if (localName == null || localName.isEmpty()) {
throw new BLangRuntimeException("localname of the attribute cannot be empty");
}
// Validate whether the attribute name is an XML supported qualified name, according to the XML recommendation.
XMLValidationUtils.validateXMLName(localName);
XMLValidationUtils.validateXMLName(prefix);
// If the attribute already exists, update the value.
OMElement node = (OMElement) omNode;
QName qname = getQName(localName, namespaceUri, prefix);
OMAttribute attr = node.getAttribute(qname);
if (attr != null) {
attr.setAttributeValue(value);
return;
}
// If the prefix is 'xmlns' then this is a namespace addition
if (prefix != null && prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
node.declareNamespace(value, localName);
return;
}
// If the namespace is null/empty, only the local part exists. Therefore add a simple attribute.
if (namespaceUri == null || namespaceUri.isEmpty()) {
attr = new OMAttributeImpl();
attr.setAttributeValue(value);
attr.setLocalName(localName);
node.addAttribute(attr);
return;
}
// treat this attribute-add operation as a namespace addition.
if ((node.getDefaultNamespace() != null && namespaceUri.equals(node.getDefaultNamespace().getNamespaceURI())) || namespaceUri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
node.declareNamespace(value, localName);
return;
}
OMNamespace ns = null;
if (prefix != null && !prefix.isEmpty()) {
OMNamespace existingNs = node.findNamespaceURI(prefix);
// If a namespace exists with the same prefix but a different uri, then do not add the new attribute.
if (existingNs != null && !namespaceUri.equals(existingNs.getNamespaceURI())) {
throw new BLangRuntimeException("failed to add attribute '" + prefix + ":" + localName + "'. prefix '" + prefix + "' is already bound to namespace '" + existingNs.getNamespaceURI() + "'");
}
ns = new OMNamespaceImpl(namespaceUri, prefix);
node.addAttribute(localName, value, ns);
return;
}
// We reach here if the namespace prefix is null/empty, and a namespace uri exists
if (namespaceUri != null && !namespaceUri.isEmpty()) {
prefix = null;
// Find a prefix that has the same namespaceUri, out of the defined namespaces
Iterator<String> prefixes = node.getNamespaceContext(false).getPrefixes(namespaceUri);
while (prefixes.hasNext()) {
String definedPrefix = prefixes.next();
if (definedPrefix.isEmpty()) {
continue;
}
prefix = definedPrefix;
break;
}
if (prefix != null && prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
// If found, and if its the default namespace, add a namespace decl
node.declareNamespace(value, localName);
return;
}
// else use the prefix. If the prefix is null, it will generate a random prefix.
ns = new OMNamespaceImpl(namespaceUri, prefix);
}
node.addAttribute(localName, value, ns);
}
Aggregations