Search in sources :

Example 1 with OMNamespaceImpl

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;
}
Also used : OMDataSource(org.apache.axiom.om.OMDataSource) AxiomSOAPHeaderBlock(org.apache.axiom.soap.impl.intf.AxiomSOAPHeaderBlock) CustomBuilder(org.apache.axiom.om.ds.custombuilder.CustomBuilder) AxiomSOAP12HeaderBlock(org.apache.axiom.soap.impl.intf.AxiomSOAP12HeaderBlock) AxiomSOAPElement(org.apache.axiom.soap.impl.intf.AxiomSOAPElement) AxiomSourcedElement(org.apache.axiom.om.impl.intf.AxiomSourcedElement) CoreModelException(org.apache.axiom.core.CoreModelException) AxiomElement(org.apache.axiom.om.impl.intf.AxiomElement) AxiomSOAP11HeaderBlock(org.apache.axiom.soap.impl.intf.AxiomSOAP11HeaderBlock) OMNamespaceImpl(org.apache.axiom.om.impl.common.OMNamespaceImpl)

Example 2 with OMNamespaceImpl

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;
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) OMNamespaceImpl(org.apache.axiom.om.impl.common.OMNamespaceImpl)

Example 3 with OMNamespaceImpl

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);
}
Also used : BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMAttributeImpl(org.apache.axiom.om.impl.llom.OMAttributeImpl) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) OMNamespaceImpl(org.apache.axiom.om.impl.common.OMNamespaceImpl)

Example 4 with OMNamespaceImpl

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;
}
Also used : CoreModelException(org.apache.axiom.core.CoreModelException) AxiomAttribute(org.apache.axiom.om.impl.intf.AxiomAttribute) OMNamespaceImpl(org.apache.axiom.om.impl.common.OMNamespaceImpl)

Aggregations

OMNamespaceImpl (org.apache.axiom.om.impl.common.OMNamespaceImpl)4 CoreModelException (org.apache.axiom.core.CoreModelException)2 OMNamespace (org.apache.axiom.om.OMNamespace)2 QName (javax.xml.namespace.QName)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMDataSource (org.apache.axiom.om.OMDataSource)1 OMElement (org.apache.axiom.om.OMElement)1 CustomBuilder (org.apache.axiom.om.ds.custombuilder.CustomBuilder)1 AxiomAttribute (org.apache.axiom.om.impl.intf.AxiomAttribute)1 AxiomElement (org.apache.axiom.om.impl.intf.AxiomElement)1 AxiomSourcedElement (org.apache.axiom.om.impl.intf.AxiomSourcedElement)1 OMAttributeImpl (org.apache.axiom.om.impl.llom.OMAttributeImpl)1 AxiomSOAP11HeaderBlock (org.apache.axiom.soap.impl.intf.AxiomSOAP11HeaderBlock)1 AxiomSOAP12HeaderBlock (org.apache.axiom.soap.impl.intf.AxiomSOAP12HeaderBlock)1 AxiomSOAPElement (org.apache.axiom.soap.impl.intf.AxiomSOAPElement)1 AxiomSOAPHeaderBlock (org.apache.axiom.soap.impl.intf.AxiomSOAPHeaderBlock)1 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)1