Example usage for org.w3c.dom Attr getPrefix

List of usage examples for org.w3c.dom Attr getPrefix

Introduction

In this page you can find the example usage for org.w3c.dom Attr getPrefix.

Prototype

public String getPrefix();

Source Link

Document

The namespace prefix of this node, or null if it is unspecified.

Usage

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test// ww w  .  ja v a 2 s. co  m
public void testAddAttributeFromNameWithoutNamespace() throws Exception {
    SOAPEnvelope envelope = saajUtil.createSOAP11Envelope();
    SOAPBody body = envelope.addBody();
    SOAPElement element = body.addChildElement(new QName("urn:test", "test"));
    SOAPElement retValue = element.addAttribute(envelope.createName("attr"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS(null, "attr");
    assertNull(attr.getNamespaceURI());
    String localName = attr.getLocalName();
    if (localName != null) {
        assertEquals("attr", attr.getLocalName());
    }
    assertNull(attr.getPrefix());
    assertEquals("value", attr.getValue());
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*from www.j a va  2s  . c om*/
public void testAddAttributeFromQNameWithoutNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    SOAPElement retValue = element.addAttribute(new QName("attr"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS(null, "attr");
    assertNull(attr.getNamespaceURI());
    String localName = attr.getLocalName();
    // The RI creates a namespace unaware attribute node in this case; we believe
    // it's better to create a namespace aware node.
    if (localName != null) {
        assertEquals("attr", attr.getLocalName());
    }
    assertNull(attr.getPrefix());
    assertEquals("value", attr.getValue());
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * remove a an attribue/*from w  w w  .j  av a  2s.c  o m*/
 * @param oldAttr
 * @return oldAttr
 * @throws DOMException
 */
public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
    makeAttributesEditable();
    Name name = new PrefixedQName(oldAttr.getNamespaceURI(), oldAttr.getLocalName(), oldAttr.getPrefix());
    removeAttribute(name);
    return oldAttr;
}

From source file:org.apache.axis2.jaxws.message.util.impl.XMLStreamReaderFromDOM.java

public String getAttributePrefix(int index) {
    Attr attr = (Attr) getAttributes().get(index);
    return attr.getPrefix();
}

From source file:org.apache.ode.il.OMUtils.java

public static OMElement toOM(Element src, OMFactory omf, OMContainer parent) {
    OMElement omElement = parent == null ? omf.createOMElement(src.getLocalName(), null)
            : omf.createOMElement(src.getLocalName(), null, parent);
    if (src.getNamespaceURI() != null) {
        if (src.getPrefix() != null)
            omElement.setNamespace(omf.createOMNamespace(src.getNamespaceURI(), src.getPrefix()));
        else/*  w w  w.  j  a va2s  .co m*/
            omElement.declareDefaultNamespace(src.getNamespaceURI());
    }

    if (parent == null) {
        NSContext nscontext = DOMUtils.getMyNSContext(src);
        injectNamespaces(omElement, nscontext.toMap());
    } else {
        Map<String, String> nss = DOMUtils.getMyNamespaces(src);
        injectNamespaces(omElement, nss);
    }

    NamedNodeMap attrs = src.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        if (attr.getLocalName().equals("xmlns")
                || (attr.getNamespaceURI() != null && attr.getNamespaceURI().equals(DOMUtils.NS_URI_XMLNS)))
            continue;
        OMNamespace attrOmNs = null;
        String attrNs = attr.getNamespaceURI();
        String attrPrefix = attr.getPrefix();
        if (attrNs != null)
            attrOmNs = omElement.findNamespace(attrNs, null);
        if (attrOmNs == null && attrPrefix != null)
            attrOmNs = omElement.findNamespace(null, attrPrefix);
        omElement.addAttribute(attr.getLocalName(), attr.getValue(), attrOmNs);
    }

    NodeList children = src.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node n = children.item(i);

        switch (n.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            omElement.addChild(omf.createOMText(((CDATASection) n).getTextContent(), XMLStreamConstants.CDATA));
            break;
        case Node.TEXT_NODE:
            omElement.addChild(omf.createOMText(((Text) n).getTextContent(), XMLStreamConstants.CHARACTERS));
            break;
        case Node.ELEMENT_NODE:
            toOM((Element) n, omf, omElement);
            break;
        }

    }

    return omElement;
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * Fetch the non-null namespace prefix from a {@link Attr} that declares
 * a namespace.  (The DOM APIs will return <code>null</code> for a non-prefixed
 * declaration./*from ww w.j ava2s  . c  o m*/
 * @param a the {@link Attr} with the declaration (must be non-<code>null</code).
 * @return the namespace prefix or <code>&quot;&quot;</code> if none was
 * declared, e.g., <code>xmlns=&quot;foo&quot;</code>.
 */
public static String getNSPrefixFromNSAttr(Attr a) {
    assert a != null;
    assert isNSAttribute(a);
    if (a.getPrefix() == null) {
        return "";
    }
    return a.getName().substring(a.getPrefix().length() + 1);
}

From source file:org.apache.woden.internal.DOMWSDLReader.java

protected void parseExtensionAttributes(XMLElement extEl, Class wsdlClass, WSDLElement wsdlObj,
        DescriptionElement desc) throws WSDLException {
    Element domEl = (Element) extEl.getSource();
    NamedNodeMap nodeMap = domEl.getAttributes();
    int length = nodeMap.getLength();

    for (int i = 0; i < length; i++) {
        Attr domAttr = (Attr) nodeMap.item(i);
        String localName = domAttr.getLocalName();
        String namespaceURI = domAttr.getNamespaceURI();
        String prefix = domAttr.getPrefix();
        QName attrType = new QName(namespaceURI, localName, (prefix != null ? prefix : emptyString));
        String attrValue = domAttr.getValue();

        if (namespaceURI != null && !namespaceURI.equals(Constants.NS_STRING_WSDL20)) {
            if (!namespaceURI.equals(Constants.NS_STRING_XMLNS)
                    && !namespaceURI.equals(Constants.NS_STRING_XSI)) //TODO handle xsi attrs elsewhere, without need to register
            {/*from  w w  w .ja va 2  s. c  o  m*/
                //TODO reg namespaces at appropriate element scope, not just at desc.
                //DOMUtils.registerUniquePrefix(prefix, namespaceURI, desc);

                ExtensionRegistry extReg = fWsdlContext.extensionRegistry;
                XMLAttr xmlAttr = extReg.createExtAttribute(wsdlClass, attrType, extEl, attrValue);
                if (xmlAttr != null) //TODO use an 'UnknownAttr' class in place of null
                {
                    wsdlObj.setExtensionAttribute(attrType, xmlAttr);
                }
            } else {
                //TODO parse xmlns namespace declarations - here or elsewhere?
            }
        } else {
            //TODO confirm non-native attrs in WSDL 2.0 namespace will be detected by schema validation,
            //so no need to handle error here.
        }
    }

}

From source file:org.exist.dom.ElementImpl.java

private Node appendChild(Txn transaction, NodeId newNodeId, NodeImplRef last, NodePath lastPath, Node child,
        StreamListener listener) throws DOMException {
    if (last == null || last.getNode() == null)
    //TODO : same test as above ? -pb
    {/*ww  w  .j av  a2s  . c  o m*/
        throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "invalid node");
    }
    final DocumentImpl owner = (DocumentImpl) getOwnerDocument();
    DBBroker broker = null;
    try {
        broker = ownerDocument.getBrokerPool().get(null);
        switch (child.getNodeType()) {
        case Node.DOCUMENT_FRAGMENT_NODE:
            appendChildren(transaction, newNodeId, null, last, lastPath, child.getChildNodes(), listener);
            return null; // TODO: implement document fragments so
        //we can return all newly appended children
        case Node.ELEMENT_NODE:
            // create new element
            final ElementImpl elem = new ElementImpl(
                    new QName(child.getLocalName() == null ? child.getNodeName() : child.getLocalName(),
                            child.getNamespaceURI(), child.getPrefix()),
                    broker.getBrokerPool().getSymbols());
            elem.setNodeId(newNodeId);
            elem.setOwnerDocument(owner);
            final NodeListImpl ch = new NodeListImpl();
            final NamedNodeMap attribs = child.getAttributes();
            int numActualAttribs = 0;
            for (int i = 0; i < attribs.getLength(); i++) {
                final Attr attr = (Attr) attribs.item(i);
                if (!attr.getNodeName().startsWith("xmlns")) {
                    ch.add(attr);
                    numActualAttribs++;
                } else {
                    final String xmlnsDecl = attr.getNodeName();
                    final String prefix = xmlnsDecl.length() == 5 ? "" : xmlnsDecl.substring(6);
                    elem.addNamespaceMapping(prefix, attr.getNodeValue());
                }
            }
            final NodeList cl = child.getChildNodes();
            for (int i = 0; i < cl.getLength(); i++) {
                final Node n = cl.item(i);
                if (n.getNodeType() != Node.ATTRIBUTE_NODE) {
                    ch.add(n);
                }
            }
            elem.setChildCount(ch.getLength());
            if (numActualAttribs != (short) numActualAttribs) {
                throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "Too many attributes");
            }
            elem.setAttributes((short) numActualAttribs);
            lastPath.addComponent(elem.getQName());
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), elem);
            broker.indexNode(transaction, elem, lastPath);
            broker.getIndexController().indexNode(transaction, elem, lastPath, listener);
            elem.setChildCount(0);
            last.setNode(elem);
            //process child nodes
            elem.appendChildren(transaction, newNodeId.newChild(), null, last, lastPath, ch, listener);
            broker.endElement(elem, lastPath, null);
            broker.getIndexController().endElement(transaction, elem, lastPath, listener);
            lastPath.removeLastComponent();
            return elem;
        case Node.TEXT_NODE:
            final TextImpl text = new TextImpl(newNodeId, ((Text) child).getData());
            text.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), text);
            broker.indexNode(transaction, text, lastPath);
            broker.getIndexController().indexNode(transaction, text, lastPath, listener);
            last.setNode(text);
            return text;
        case Node.CDATA_SECTION_NODE:
            final CDATASectionImpl cdata = new CDATASectionImpl(newNodeId, ((CDATASection) child).getData());
            cdata.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), cdata);
            broker.indexNode(transaction, cdata, lastPath);
            last.setNode(cdata);
            return cdata;
        case Node.ATTRIBUTE_NODE:
            final Attr attr = (Attr) child;
            final String ns = attr.getNamespaceURI();
            final String prefix = (Namespaces.XML_NS.equals(ns) ? "xml" : attr.getPrefix());
            String name = attr.getLocalName();
            if (name == null) {
                name = attr.getName();
            }
            final QName attrName = new QName(name, ns, prefix);
            final AttrImpl attrib = new AttrImpl(attrName, attr.getValue(),
                    broker.getBrokerPool().getSymbols());
            attrib.setNodeId(newNodeId);
            attrib.setOwnerDocument(owner);
            if (ns != null && attrName.compareTo(Namespaces.XML_ID_QNAME) == Constants.EQUAL) {
                // an xml:id attribute. Normalize the attribute and set its type to ID
                attrib.setValue(StringValue.trimWhitespace(StringValue.collapseWhitespace(attrib.getValue())));
                attrib.setType(AttrImpl.ID);
            } else {
                attrName.setNameType(ElementValue.ATTRIBUTE);
            }
            broker.insertNodeAfter(transaction, last.getNode(), attrib);
            broker.indexNode(transaction, attrib, lastPath);
            broker.getIndexController().indexNode(transaction, attrib, lastPath, listener);
            last.setNode(attrib);
            return attrib;
        case Node.COMMENT_NODE:
            final CommentImpl comment = new CommentImpl(((Comment) child).getData());
            comment.setNodeId(newNodeId);
            comment.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), comment);
            broker.indexNode(transaction, comment, lastPath);
            last.setNode(comment);
            return comment;
        case Node.PROCESSING_INSTRUCTION_NODE:
            final ProcessingInstructionImpl pi = new ProcessingInstructionImpl(newNodeId,
                    ((ProcessingInstruction) child).getTarget(), ((ProcessingInstruction) child).getData());
            pi.setOwnerDocument(owner);
            //insert the node
            broker.insertNodeAfter(transaction, last.getNode(), pi);
            broker.indexNode(transaction, pi, lastPath);
            last.setNode(pi);
            return pi;
        default:
            throw new DOMException(DOMException.INVALID_MODIFICATION_ERR,
                    "Unknown node type: " + child.getNodeType() + " " + child.getNodeName());
        }
    } catch (final EXistException e) {
        LOG.warn("Exception while appending node: " + e.getMessage(), e);
    } finally {
        if (broker != null)
            broker.release();
    }
    return null;
}

From source file:org.mule.config.spring.parsers.assembly.DefaultBeanAssembler.java

/**
 * Add a property defined by an attribute to the bean we are constructing.
 *
 * <p>Since an attribute value is always a string, we don't have to deal with complex types
 * here - the only issue is whether or not we have a reference.  References are detected
 * by explicit annotation or by the "-ref" at the end of an attribute name.  We do not
 * check the Spring repo to see if a name already exists since that could lead to
 * unpredictable behaviour./*from ww w . ja v a 2 s  . c  om*/
 * (see {@link org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration})
 * @param attribute The attribute to add
 */
public void extendBean(Attr attribute) {
    AbstractBeanDefinition beanDefinition = bean.getBeanDefinition();
    String oldName = SpringXMLUtils.attributeName(attribute);
    String oldValue = attribute.getNodeValue();
    if (attribute.getNamespaceURI() == null) {
        if (!beanConfig.isIgnored(oldName)) {
            logger.debug(attribute + " for " + beanDefinition.getBeanClassName());
            String newName = bestGuessName(beanConfig, oldName, beanDefinition.getBeanClassName());
            Object newValue = beanConfig.translateValue(oldName, oldValue);
            addPropertyWithReference(beanDefinition.getPropertyValues(), beanConfig.getSingleProperty(oldName),
                    newName, newValue);
        }
    } else if (isAnnotationsPropertyAvailable(beanDefinition.getBeanClass())) {
        //Add attribute defining namespace as annotated elements. No reconciliation is done here ie new values override old ones.
        QName name;
        if (attribute.getPrefix() != null) {
            name = new QName(attribute.getNamespaceURI(), attribute.getLocalName(), attribute.getPrefix());
        } else {
            name = new QName(attribute.getNamespaceURI(), attribute.getLocalName());
        }
        Object value = beanConfig.translateValue(oldName, oldValue);
        addAnnotationValue(beanDefinition.getPropertyValues(), name, value);
        MuleContext muleContext = MuleApplicationContext.getCurrentMuleContext().get();
        if (muleContext != null) {
            Map<QName, Set<Object>> annotations = muleContext.getConfigurationAnnotations();
            Set<Object> values = annotations.get(name);
            if (values == null) {
                values = new HashSet<Object>();
                annotations.put(name, values);
            }
            values.add(value);
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot assign " + beanDefinition.getBeanClass() + " to " + AnnotatedObject.class);
        }
    }
}

From source file:org.openestate.io.core.XmlUtils.java

private static void printNode(Element node, int indent) {
    String prefix = (indent > 0) ? StringUtils.repeat(">", indent) + " " : "";
    LOGGER.debug(prefix + "<" + node.getTagName() + "> / " + node.getNamespaceURI() + " / " + node.getPrefix());

    prefix = StringUtils.repeat(">", indent + 1) + " ";
    NamedNodeMap attribs = node.getAttributes();
    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attrib = (Attr) attribs.item(i);
        LOGGER.debug(prefix + "@" + attrib.getName() + " / " + attrib.getNamespaceURI() + " / "
                + attrib.getPrefix());
    }/*from ww w . j av a 2 s  .  c  o  m*/
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            XmlUtils.printNode((Element) child, indent + 1);
        }
    }
}