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

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

Introduction

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

Prototype

public Attribute getAttributeByName(QName name);

Source Link

Document

Returns the attribute referred to by the qname.

Usage

From source file:org.apereo.portal.io.xml.PortalDataKey.java

protected String getAttributeValue(StartElement startElement, QName name) {
    final Attribute versionAttr = startElement.getAttributeByName(name);
    if (versionAttr != null) {
        return versionAttr.getValue();
    }//  w ww . ja v a 2  s  .  c om

    return null;
}

From source file:org.apereo.portal.portlet.registry.PortletWindowRegistryImpl.java

@Override
public Tuple<IPortletWindow, StartElement> getPortletWindow(HttpServletRequest request, StartElement element) {
    //Check if the layout node explicitly specifies the window id
    final Attribute windowIdAttribute = element.getAttributeByName(PORTLET_WINDOW_ID_ATTR_NAME);
    if (windowIdAttribute != null) {
        final String windowIdStr = windowIdAttribute.getValue();
        final IPortletWindowId portletWindowId = this.getPortletWindowId(request, windowIdStr);
        final IPortletWindow portletWindow = this.getPortletWindow(request, portletWindowId);
        return new Tuple<IPortletWindow, StartElement>(portletWindow, element);
    }/*  www. j  a v  a  2s  .  c o  m*/

    //No explicit window id, look it up based on the layout node id
    final Attribute nodeIdAttribute = element.getAttributeByName(IUserLayoutManager.ID_ATTR_NAME);
    final String layoutNodeId = nodeIdAttribute.getValue();

    IPortletWindow portletWindow = this.getOrCreateDefaultPortletWindowByLayoutNodeId(request, layoutNodeId);
    if (portletWindow == null) {
        //No window for the layout node, return null
        return null;
    }

    final IPortalRequestInfo portalRequestInfo = this.urlSyntaxProvider.getPortalRequestInfo(request);
    if (portalRequestInfo.getUrlState() == UrlState.DETACHED) {
        /*
         * We want to handle DETACHED portlet windows differently/explicitly,
         * but we need to be aware there may be other portlets on the page
         * besides the targeted one.  These would be portlets in regions
         * (Respondr theme) -- such as DynamicRespondrSkin.
         *
         * We need to confirm, therefore, that this is actually the portlet
         * in DETACHED.  If it is, we'll send back a 'stateless' PortletWindow.
         */
        if (portalRequestInfo.getTargetedPortletWindowId().toString().contains("_" + layoutNodeId + "_")) {
            final IPortletWindowId portletWindowId = portletWindow.getPortletWindowId();
            portletWindow = this.getOrCreateStatelessPortletWindow(request, portletWindowId);
        }

    }

    element = this.addPortletWindowId(element, portletWindow.getPortletWindowId());

    return new Tuple<IPortletWindow, StartElement>(portletWindow, element);
}

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

private String getIdAttributeValue(StartElement startElement, Map<String, String> namespaces,
        String namespaceAlias) {/*from w ww . j  a v a 2 s. c  om*/
    String id = null;
    javax.xml.stream.events.Attribute idAttribute = startElement
            .getAttributeByName(new QName(namespaces.get(namespaceAlias), "id"));
    if (idAttribute != null) {
        id = idAttribute.getValue();
    }

    if (StringUtils.isBlank(id)) {
        for (Iterator i = startElement.getAttributes(); i.hasNext();) {
            idAttribute = (javax.xml.stream.events.Attribute) i.next();
            if (idAttribute != null && idAttribute.getName().getLocalPart().equals("id")) {
                id = idAttribute.getValue();
            }
        }
    }

    return id;
}

From source file:org.jasig.portal.layout.TransientUserLayoutXMLEventReader.java

@Override
protected Deque<XMLEvent> getAdditionalEvents(XMLEvent event) {
    if (event.isStartElement()) {
        final StartElement startElement = event.asStartElement();

        //All following logic requires an ID attribute, ignore any element without one
        final Attribute idAttribute = startElement.getAttributeByName(IUserLayoutManager.ID_ATTR_NAME);
        if (idAttribute == null) {
            return null;
        }//from   w ww . j  av  a  2  s .co m

        //Handle adding a transient folder to the root element
        if (this.rootFolderId.equals(idAttribute.getValue())) {
            final QName name = startElement.getName();
            final String namespaceURI = name.getNamespaceURI();
            final String prefix = name.getPrefix();

            final Deque<XMLEvent> transientEventBuffer = new LinkedList<XMLEvent>();

            final Collection<Attribute> transientFolderAttributes = new LinkedList<Attribute>();
            transientFolderAttributes.add(
                    EVENT_FACTORY.createAttribute("ID", TransientUserLayoutManagerWrapper.TRANSIENT_FOLDER_ID));
            transientFolderAttributes.add(EVENT_FACTORY.createAttribute("type", "regular"));
            transientFolderAttributes.add(EVENT_FACTORY.createAttribute("hidden", "true"));
            transientFolderAttributes.add(EVENT_FACTORY.createAttribute("unremovable", "true"));
            transientFolderAttributes.add(EVENT_FACTORY.createAttribute("immutable", "true"));
            transientFolderAttributes.add(EVENT_FACTORY.createAttribute("name", "Transient Folder"));

            final StartElement transientFolder = EVENT_FACTORY.createStartElement(prefix, namespaceURI,
                    IUserLayoutManager.FOLDER, transientFolderAttributes.iterator(), null);
            transientEventBuffer.add(transientFolder);

            //append channel element iff subscribeId describes a transient channel, and not a regular layout channel
            final String subscribeId = this.userLayoutManager.getFocusedId();
            if (null != subscribeId && !subscribeId.equals("")
                    && this.userLayoutManager.isTransientChannel(subscribeId)) {
                IPortletDefinition chanDef = null;
                try {
                    chanDef = this.userLayoutManager.getChannelDefinition(subscribeId);
                } catch (Exception e) {
                    this.logger.error("Could not obtain IChannelDefinition for subscribe id: " + subscribeId,
                            e);
                }

                if (chanDef != null) {
                    //TODO Move IChannelDefinition/IPortletDefinition -> StAX events code somewhere reusable
                    final Collection<Attribute> channelAttrs = new LinkedList<Attribute>();
                    channelAttrs.add(EVENT_FACTORY.createAttribute("ID", subscribeId));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("typeID",
                            Integer.toString(chanDef.getType().getId())));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("hidden", "false"));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("unremovable", "true"));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("name", chanDef.getName()));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("description", chanDef.getDescription()));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("title", chanDef.getTitle()));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("chanID",
                            chanDef.getPortletDefinitionId().getStringId()));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("fname", chanDef.getFName()));
                    channelAttrs.add(
                            EVENT_FACTORY.createAttribute("timeout", Integer.toString(chanDef.getTimeout())));
                    channelAttrs.add(EVENT_FACTORY.createAttribute("transient", "true"));

                    final StartElement startChannel = EVENT_FACTORY.createStartElement(prefix, namespaceURI,
                            IUserLayoutManager.CHANNEL, channelAttrs.iterator(), null);
                    transientEventBuffer.offer(startChannel);

                    // add channel parameter elements
                    for (final IPortletDefinitionParameter parm : chanDef.getParameters()) {
                        final Collection<Attribute> parameterAttrs = new LinkedList<Attribute>();
                        parameterAttrs.add(EVENT_FACTORY.createAttribute("name", parm.getName()));
                        parameterAttrs.add(EVENT_FACTORY.createAttribute("value", parm.getValue()));

                        final StartElement startParameter = EVENT_FACTORY.createStartElement(prefix,
                                namespaceURI, IUserLayoutManager.PARAMETER, parameterAttrs.iterator(), null);
                        transientEventBuffer.offer(startParameter);

                        final EndElement endParameter = EVENT_FACTORY.createEndElement(prefix, namespaceURI,
                                IUserLayoutManager.PARAMETER, null);
                        transientEventBuffer.offer(endParameter);
                    }

                    final EndElement endChannel = EVENT_FACTORY.createEndElement(prefix, namespaceURI,
                            IUserLayoutManager.CHANNEL, null);
                    transientEventBuffer.offer(endChannel);
                }
            }

            final EndElement endFolder = EVENT_FACTORY.createEndElement(prefix, namespaceURI,
                    IUserLayoutManager.FOLDER, null);
            transientEventBuffer.offer(endFolder);

            return transientEventBuffer;
        }
    }

    return null;
}

From source file:org.jasig.portal.portlet.registry.PortletWindowRegistryImpl.java

@Override
public Tuple<IPortletWindow, StartElement> getPortletWindow(HttpServletRequest request, StartElement element) {
    //Check if the layout node explicitly specifies the window id
    final Attribute windowIdAttribute = element.getAttributeByName(PORTLET_WINDOW_ID_ATTR_NAME);
    if (windowIdAttribute != null) {
        final String windowIdStr = windowIdAttribute.getValue();
        final IPortletWindowId portletWindowId = this.getPortletWindowId(request, windowIdStr);
        final IPortletWindow portletWindow = this.getPortletWindow(request, portletWindowId);
        return new Tuple<IPortletWindow, StartElement>(portletWindow, element);
    }/*ww w .j  av a 2  s. c o m*/

    //No explicit window id, look it up based on the layout node id
    final Attribute nodeIdAttribute = element.getAttributeByName(IUserLayoutManager.ID_ATTR_NAME);
    final String layoutNodeId = nodeIdAttribute.getValue();

    IPortletWindow portletWindow = this.getOrCreateDefaultPortletWindowByLayoutNodeId(request, layoutNodeId);
    if (portletWindow == null) {
        //No window for the layout node, return null
        return null;
    }

    final IPortalRequestInfo portalRequestInfo = this.urlSyntaxProvider.getPortalRequestInfo(request);
    if (portalRequestInfo.getUrlState() == UrlState.DETACHED) {
        //Handle detached portlets explicitly
        //TODO Can we ever have non-targeted portlets render in a detached request? If so should they all be stateless windows anyways? 
        final IPortletWindowId portletWindowId = portletWindow.getPortletWindowId();
        portletWindow = this.getOrCreateStatelessPortletWindow(request, portletWindowId);
    }

    element = this.addPortletWindowId(element, portletWindow.getPortletWindowId());

    return new Tuple<IPortletWindow, StartElement>(portletWindow, element);
}

From source file:org.jonross.coercion.v2.BinderBase.java

/**
 * For use by block handler code; return the text of the attribute with the
 * specified name; if no such attribute, return <code>null</code>.
 *//*from w w  w.  j  av a  2 s . co m*/

protected String attribute(String name) {
    if (!activeEvent.isStartElement())
        return "";

    StartElement start = activeEvent.asStartElement();
    return start.getAttributeByName(new QName(name)).getValue();
}

From source file:org.jonross.coercion.v2.BinderSpec.java

/**
 *///from   ww w .  j a  va 2  s .c  om

public void parse(Reader input) throws XMLStreamException {
    // Grab the first element in the binder file and verify that it's
    // our show; check for binder package name and class name.

    XMLEventReader reader = inputFactory.createXMLEventReader(input);
    StartElement start = nextStartElement(reader).asStartElement();

    if (!start.getName().equals(CO_UNMARSHALLER))
        throw new CoercionException(
                "Root element must be <" + CO_UNMARSHALLER.getLocalPart() + "> in namespace " + MY_NS);

    Attribute att = start.getAttributeByName(CO_PACKAGE_NAME);
    if (att != null)
        packageName = att.getValue();

    att = start.getAttributeByName(CO_CLASS_NAME);
    if (att != null)
        className = att.getValue();
    else
        throw new CoercionException("Root <unmarshaller> element is missing className attribute");

    root = parse(reader, start.asStartElement());
}

From source file:org.omegat.util.TMXReader2.java

private static String getAttributeValue(StartElement e, String attrName) {
    Attribute a = e.getAttributeByName(new QName(attrName));
    return a != null ? a.getValue() : null;
}

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   ww w .  j a v a2  s  .  c  om*/
    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"));
}

From source file:org.openhim.mediator.denormalization.EnrichRegistryStoredQueryActor.java

private String enrichStoredQueryXML(Identifier id, InputStream xml) throws XMLStreamException {
    XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(xml);
    StringWriter output = new StringWriter();
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);
    XMLEventFactory eventFactory = XMLEventFactory.newFactory();

    String curSlot = null;//from ww w.  j  a  v a2s . c  o m
    boolean patientIdSlot = false;

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (event.getEventType() == XMLEvent.START_ELEMENT) {
            StartElement elem = event.asStartElement();
            if ("Slot".equals(elem.getName().getLocalPart())) {
                curSlot = elem.getAttributeByName(new QName("name")).getValue();
            } else if ("Value".equals(elem.getName().getLocalPart())
                    && ParseRegistryStoredQueryActor.PATIENT_ID_SLOT_TYPE.equals(curSlot)) {
                patientIdSlot = true;
                writer.add(event);
            }
        } else if (event.getEventType() == XMLEvent.END_ELEMENT) {
            EndElement elem = event.asEndElement();
            if (patientIdSlot && "Value".equals(elem.getName().getLocalPart())) {
                XMLEvent ecidEvent = eventFactory.createCharacters("'" + id.toString() + "'");
                writer.add(ecidEvent);
                patientIdSlot = false;
            }
        }

        if (!patientIdSlot) {
            writer.add(event);
        }
    }

    writer.close();
    return output.toString();
}