use of org.apache.axiom.om.impl.common.OMNamespaceImpl in project webservices-axiom by apache.
the class CustomBuilderManager method getAction.
private Runnable getAction(CoreNode node, int depth, int firstCustomBuilder) {
lastCandidateElement = null;
lastCandidateDepth = -1;
if (node instanceof AxiomElement && (node instanceof AxiomSOAPHeaderBlock || !(node instanceof AxiomSOAPElement))) {
final AxiomElement element = (AxiomElement) node;
if (registrations != null) {
for (int i = firstCustomBuilder; i < registrations.size(); i++) {
CustomBuilderRegistration registration = registrations.get(i);
final String namespaceURI = element.coreGetNamespaceURI();
final String localName = element.coreGetLocalName();
if (registration.getSelector().accepts(element.getParent(), depth, namespaceURI, localName)) {
final CustomBuilder customBuilder = registration.getCustomBuilder();
if (log.isDebugEnabled()) {
log.debug("Custom builder " + customBuilder + " accepted element {" + namespaceURI + "}" + localName + " at depth " + depth);
}
return new Runnable() {
@Override
public void run() {
if (log.isDebugEnabled()) {
log.debug("Invoking custom builder " + customBuilder);
}
OMDataSource dataSource = customBuilder.create(element);
Class<? extends AxiomSourcedElement> type;
if (element instanceof AxiomSOAP11HeaderBlock) {
type = AxiomSOAP11HeaderBlock.class;
} else if (element instanceof AxiomSOAP12HeaderBlock) {
type = AxiomSOAP12HeaderBlock.class;
} else {
type = AxiomSourcedElement.class;
}
if (log.isDebugEnabled()) {
log.debug("Replacing element with new sourced element of type " + type);
}
AxiomSourcedElement newElement = element.coreCreateNode(type);
newElement.init(localName, new OMNamespaceImpl(namespaceURI, null), dataSource);
try {
element.coreReplaceWith(newElement, AxiomSemantics.INSTANCE);
} catch (CoreModelException ex) {
throw AxiomExceptionTranslator.translate(ex);
}
}
};
}
}
}
// Save a reference to the element so that we can process it when another custom builder is registered
lastCandidateElement = element;
lastCandidateDepth = depth;
}
return null;
}
use of org.apache.axiom.om.impl.common.OMNamespaceImpl in project webservices-axiom by apache.
the class OMNamespaceCache method getOMNamespace.
public OMNamespace getOMNamespace(String uri, String prefix) {
if (uri.isEmpty() && prefix.isEmpty()) {
return null;
}
int index = index(uri, prefix);
while (true) {
OMNamespace ns = items[index];
if (ns == null) {
break;
} else if (ns.getNamespaceURI().equals(uri) && ns.getPrefix().equals(prefix)) {
return ns;
}
if (++index == items.length) {
index = 0;
}
}
if (items.length < size * 4 / 3) {
OMNamespace[] oldItems = items;
items = new OMNamespace[items.length * 2];
for (OMNamespace ns : oldItems) {
if (ns != null) {
items[freeIndex(ns.getNamespaceURI(), ns.getPrefix())] = ns;
}
}
index = freeIndex(uri, prefix);
}
OMNamespace ns = new OMNamespaceImpl(uri, prefix);
items[index] = ns;
size++;
return ns;
}
use of org.apache.axiom.om.impl.common.OMNamespaceImpl 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);
}
use of org.apache.axiom.om.impl.common.OMNamespaceImpl in project webservices-axiom by apache.
the class OMFactoryImpl method createOMAttribute.
@Override
public final OMAttribute createOMAttribute(String localName, OMNamespace ns, String value) {
if (ns != null && ns.getPrefix() == null) {
String namespaceURI = ns.getNamespaceURI();
if (namespaceURI.length() == 0) {
ns = null;
} else {
ns = new OMNamespaceImpl(namespaceURI, generatePrefix(namespaceURI));
}
}
if (ns != null) {
if (ns.getNamespaceURI().length() == 0) {
if (ns.getPrefix().length() > 0) {
throw new IllegalArgumentException("Cannot create a prefixed attribute with an empty namespace name");
} else {
ns = null;
}
} else if (ns.getPrefix().length() == 0) {
throw new IllegalArgumentException("Cannot create an unprefixed attribute with a namespace");
}
}
AxiomAttribute attr = createNode(AxiomAttribute.class);
attr.internalSetLocalName(localName);
try {
attr.coreSetCharacterData(value, AxiomSemantics.INSTANCE);
} catch (CoreModelException ex) {
throw AxiomExceptionTranslator.translate(ex);
}
attr.internalSetNamespace(ns);
attr.coreSetType("CDATA");
return attr;
}
Aggregations