Example usage for javax.xml.stream.events StartElement getNamespaceURI

List of usage examples for javax.xml.stream.events StartElement getNamespaceURI

Introduction

In this page you can find the example usage for javax.xml.stream.events StartElement getNamespaceURI.

Prototype

public String getNamespaceURI(String prefix);

Source Link

Document

Gets the value that the prefix is bound to in the context of this element.

Usage

From source file:org.codice.ddf.spatial.ogc.wfs.transformer.handlebars.HandlebarsWfsFeatureTransformer.java

private StartElement addNamespacesToStartElement(StartElement startElement) {
    String prefix = startElement.getName().getPrefix();
    if (StringUtils.isBlank(prefix)) {
        return startElement;
    }//from www  .  j  a  v  a2 s  .c  o m

    if (isPrefixBound(startElement, prefix)) {
        return startElement;
    }

    return XML_EVENT_FACTORY.createStartElement(prefix, startElement.getNamespaceURI(prefix),
            startElement.getName().getLocalPart(), startElement.getAttributes(),
            Collections
                    .singletonList(
                            XML_EVENT_FACTORY.createNamespace(prefix, startElement.getNamespaceURI(prefix)))
                    .iterator());
}

From source file:org.odftoolkit.odfdom.pkg.rdfa.URIExtractorImpl.java

public String expandCURIE(StartElement element, String value, EvalContext context) {

    if (value.startsWith("_:")) {
        if (!settings.contains(Setting.ManualNamespaces))
            return value;
        if (element.getNamespaceURI("_") == null)
            return value;
    }/* w w w.  ja v  a  2 s.  c o  m*/
    if (settings.contains(Setting.FormMode) && // variable
            value.startsWith("?")) {
        return value;
    }
    int offset = value.indexOf(":") + 1;
    if (offset == 0) {
        return null;
    }
    String prefix = value.substring(0, offset - 1);

    // Apparently these are not allowed to expand
    if ("xml".equals(prefix) || "xmlns".equals(prefix))
        return null;

    String namespaceURI = null;
    if (prefix.length() == 0) {
        namespaceURI = "http://www.w3.org/1999/xhtml/vocab#";
    } else {
        namespaceURI = element.getNamespaceURI(prefix);
        if (isForSAX) {
            if (namespaceURI != null) {
                if (xmlnsMap == Collections.EMPTY_MAP)
                    xmlnsMap = new HashMap<String, String>();
                xmlnsMap.put(prefix, namespaceURI);
            }
        } else {
            if (namespaceURI == null) {
                namespaceURI = xmlnsMap.get(prefix);
            }
        }
    }
    if (namespaceURI == null) {
        return null;
        // throw new RuntimeException("Unknown prefix: " + prefix);
    }

    return namespaceURI + value.substring(offset);
}

From source file:org.onosproject.xmpp.core.ctl.handlers.XmlStreamDecoderTest.java

@Test
public void testDecodeXmppStanza() throws Exception {
    XmlStreamDecoder decoder = new XmlStreamDecoder();
    ByteBuf buffer = Unpooled.buffer();//from  w  w  w .j ava 2 s . com
    buffer.writeBytes(subscribeMsg.getBytes(Charsets.UTF_8));
    List<Object> list = Lists.newArrayList();
    decoder.decode(new ChannelHandlerContextAdapter(), buffer, list);
    assertThat(list.size(), is(10));
    list.forEach(object -> {
        assertThat(object, is(instanceOf(XMLEvent.class)));
    });
    assertThat(((XMLEvent) list.get(0)).isStartDocument(), is(true));
    XMLEvent secondEvent = (XMLEvent) list.get(1);
    assertThat(secondEvent.isStartElement(), is(true));
    StartElement secondEventAsStartElement = (StartElement) secondEvent;
    assertThat(secondEventAsStartElement.getName().getLocalPart(), is("iq"));
    assertThat(Lists.newArrayList(secondEventAsStartElement.getAttributes()).size(), is(4));
    assertThat(secondEventAsStartElement.getAttributeByName(QName.valueOf("type")).getValue(), is("set"));
    assertThat(secondEventAsStartElement.getAttributeByName(QName.valueOf("from")).getValue(),
            is("test@xmpp.org"));
    assertThat(secondEventAsStartElement.getAttributeByName(QName.valueOf("to")).getValue(),
            is("xmpp.onosproject.org"));
    assertThat(secondEventAsStartElement.getAttributeByName(QName.valueOf("id")).getValue(), is("sub1"));
    XMLEvent fourthEvent = (XMLEvent) list.get(3);
    assertThat(fourthEvent.isStartElement(), is(true));
    StartElement fourthEventAsStartElement = (StartElement) fourthEvent;
    assertThat(fourthEventAsStartElement.getName().getLocalPart(), is("pubsub"));
    assertThat(fourthEventAsStartElement.getNamespaceURI(""), is("http://jabber.org/protocol/pubsub"));
    XMLEvent fifthEvent = (XMLEvent) list.get(5);
    assertThat(fifthEvent.isStartElement(), is(true));
    StartElement fifthEventAsStartElement = (StartElement) fifthEvent;
    assertThat(fifthEventAsStartElement.getName().getLocalPart(), is("subscribe"));
    assertThat(fifthEventAsStartElement.getAttributeByName(QName.valueOf("node")).getValue(), is("test"));
    XMLEvent sixthEvent = (XMLEvent) list.get(6);
    assertThat(sixthEvent.isEndElement(), is(true));
    EndElement sixthEventAsEndElement = (EndElement) sixthEvent;
    assertThat(sixthEventAsEndElement.getName().getLocalPart(), is("subscribe"));
    XMLEvent seventhEvent = (XMLEvent) list.get(8);
    assertThat(seventhEvent.isEndElement(), is(true));
    EndElement seventhEventAsEndElement = (EndElement) seventhEvent;
    assertThat(seventhEventAsEndElement.getName().getLocalPart(), is("pubsub"));
    XMLEvent eighthEvent = (XMLEvent) list.get(9);
    assertThat(eighthEvent.isEndElement(), is(true));
    EndElement eighthEventAsEndElement = (EndElement) eighthEvent;
    assertThat(eighthEventAsEndElement.getName().getLocalPart(), is("iq"));
}